001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028:
029: /*
030: * Contributors:
031: * John Bindel - jbindel@users.sourceforge.net
032: */
033:
034: package net.sf.jasperreports.engine;
035:
036: import java.io.Serializable;
037: import java.util.ArrayList;
038: import java.util.Collection;
039: import java.util.HashMap;
040: import java.util.Iterator;
041: import java.util.List;
042: import java.util.Map;
043:
044: /**
045: * An instance of this class represents a page-oriented document
046: * that can be viewed, printed or exported to other formats.
047: * <p>
048: * When filling report designs with data, the engine produces instances
049: * of this class and these can be transferred over the network,
050: * stored in a serialized form on disk or exported to various
051: * other formats like PDF, HTML, XLS, CSV or XML.
052: *
053: * @author Teodor Danciu (teodord@users.sourceforge.net)
054: * @version $Id: JasperPrint.java 1818 2007-08-22 13:46:00Z teodord $
055: */
056: public class JasperPrint implements Serializable, JRPropertiesHolder {
057:
058: /**
059: * A small class for implementing just the style provider functionality.
060: */
061: private static class DefaultStyleProvider implements
062: JRDefaultStyleProvider, Serializable {
063: private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
064:
065: private JRReportFont defaultFont;
066: private JRStyle defaultStyle;
067:
068: DefaultStyleProvider(JRReportFont font, JRStyle style) {
069: this .defaultFont = font;
070: this .defaultStyle = style;
071: }
072:
073: public JRReportFont getDefaultFont() {
074: return defaultFont;
075: }
076:
077: void setDefaultFont(JRReportFont font) {
078: this .defaultFont = font;
079: }
080:
081: public JRStyle getDefaultStyle() {
082: return defaultStyle;
083: }
084:
085: void setDefaultStyle(JRStyle style) {
086: this .defaultStyle = style;
087: }
088: }
089:
090: /**
091: *
092: */
093: private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
094:
095: /**
096: *
097: */
098: private String name = null;
099: private int pageWidth = 0;
100: private int pageHeight = 0;
101: private byte orientation = JRReport.ORIENTATION_PORTRAIT;
102:
103: private Map fontsMap = new HashMap();
104: private List fontsList = new ArrayList();
105: private Map stylesMap = new HashMap();
106: private List stylesList = new ArrayList();
107:
108: private List pages = new ArrayList();
109:
110: private transient Map anchorIndexes = null;
111: private DefaultStyleProvider defaultStyleProvider = null;
112:
113: private String formatFactoryClass;
114: private String localeCode;
115: private String timeZoneId;
116:
117: private JRPropertiesMap propertiesMap;
118:
119: /**
120: * Creates a new empty document.
121: */
122: public JasperPrint() {
123: defaultStyleProvider = new DefaultStyleProvider(null, null);
124:
125: propertiesMap = new JRPropertiesMap();
126: }
127:
128: /**
129: * @return Returns the name of the document
130: */
131: public String getName() {
132: return name;
133: }
134:
135: /**
136: * Sets the name of the document.
137: *
138: * @param name name of the document
139: */
140: public void setName(String name) {
141: this .name = name;
142: }
143:
144: /**
145: * @return Returns the page width
146: */
147: public int getPageWidth() {
148: return pageWidth;
149: }
150:
151: /**
152: * Sets the page width.
153: *
154: * @param pageWidth page width
155: */
156: public void setPageWidth(int pageWidth) {
157: this .pageWidth = pageWidth;
158: }
159:
160: /**
161: * @return Returns the page height.
162: */
163: public int getPageHeight() {
164: return pageHeight;
165: }
166:
167: /**
168: * Sets the page height.
169: *
170: * @param pageHeight page height
171: */
172: public void setPageHeight(int pageHeight) {
173: this .pageHeight = pageHeight;
174: }
175:
176: /**
177: * Returns the page orientation.
178: * @see JRReport ORIENTATION_PORTRAIT,
179: * @see JRReport ORIENTATION_LANDSCAPE
180: */
181: public byte getOrientation() {
182: return orientation;
183: }
184:
185: /**
186: * Sets the page orientation.
187: * @see JRReport ORIENTATION_PORTRAIT,
188: * @see JRReport ORIENTATION_LANDSCAPE
189: */
190: public void setOrientation(byte orientation) {
191: this .orientation = orientation;
192: }
193:
194: /**
195: *
196: */
197: public JRPropertiesMap getPropertiesMap() {
198: return propertiesMap;
199: }
200:
201: /**
202: *
203: */
204: public String[] getPropertyNames() {
205: return propertiesMap.getPropertyNames();
206: }
207:
208: /**
209: *
210: */
211: public String getProperty(String propName) {
212: return propertiesMap.getProperty(propName);
213: }
214:
215: /**
216: *
217: */
218: public void setProperty(String propName, String value) {
219: propertiesMap.setProperty(propName, value);
220: }
221:
222: /**
223: *
224: */
225: public void removeProperty(String propName) {
226: propertiesMap.removeProperty(propName);
227: }
228:
229: /**
230: * Returns the default report font.
231: */
232: public JRReportFont getDefaultFont() {
233: return defaultStyleProvider.getDefaultFont();
234: }
235:
236: /**
237: * Sets the default report font.
238: */
239: public void setDefaultFont(JRReportFont font) {
240: defaultStyleProvider.setDefaultFont(font);
241: }
242:
243: /**
244: * When we want to virtualize pages, we want a font provider that
245: * is <i>not</i> the print object itself.
246: */
247: public JRDefaultFontProvider getDefaultFontProvider() {
248: return defaultStyleProvider;
249: }
250:
251: /**
252: * Gets an array of report fonts.
253: * @deprecated
254: */
255: public JRReportFont[] getFonts() {
256: JRReportFont[] fontsArray = new JRReportFont[fontsList.size()];
257:
258: fontsList.toArray(fontsArray);
259:
260: return fontsArray;
261: }
262:
263: /**
264: * Gets a list of report fonts.
265: * @deprecated
266: */
267: public List getFontsList() {
268: return fontsList;
269: }
270:
271: /**
272: * Gets a map of report fonts.
273: * @deprecated
274: */
275: public Map getFontsMap() {
276: return fontsMap;
277: }
278:
279: /**
280: * Adds a new font to the report fonts.
281: * @deprecated
282: */
283: public synchronized void addFont(JRReportFont reportFont)
284: throws JRException {
285: addFont(reportFont, false);
286: }
287:
288: /**
289: * Adds a new font to the report fonts.
290: * @deprecated
291: */
292: public synchronized void addFont(JRReportFont reportFont,
293: boolean isIgnoreDuplicate) throws JRException {
294: if (fontsMap.containsKey(reportFont.getName())) {
295: if (!isIgnoreDuplicate) {
296: throw new JRException(
297: "Duplicate declaration of report font : "
298: + reportFont.getName());
299: }
300: } else {
301: fontsList.add(reportFont);
302: fontsMap.put(reportFont.getName(), reportFont);
303:
304: if (reportFont.isDefault()) {
305: setDefaultFont(reportFont);
306: }
307: }
308: }
309:
310: /**
311: * @deprecated
312: */
313: public synchronized JRReportFont removeFont(String fontName) {
314: return removeFont((JRReportFont) fontsMap.get(fontName));
315: }
316:
317: /**
318: * @deprecated
319: */
320: public synchronized JRReportFont removeFont(JRReportFont reportFont) {
321: if (reportFont != null) {
322: if (reportFont.isDefault()) {
323: setDefaultFont(null);
324: }
325:
326: fontsList.remove(reportFont);
327: fontsMap.remove(reportFont.getName());
328: }
329:
330: return reportFont;
331: }
332:
333: /**
334: * Returns the default report style.
335: */
336: public JRStyle getDefaultStyle() {
337: return defaultStyleProvider.getDefaultStyle();
338: }
339:
340: /**
341: * Sets the default report style.
342: */
343: public synchronized void setDefaultStyle(JRStyle style) {
344: defaultStyleProvider.setDefaultStyle(style);
345: }
346:
347: /**
348: * When we want to virtualize pages, we want a style provider that
349: * is <i>not</i> the print object itself.
350: */
351: public JRDefaultStyleProvider getDefaultStyleProvider() {
352: return defaultStyleProvider;
353: }
354:
355: /**
356: * Gets an array of report styles.
357: */
358: public JRStyle[] getStyles() {
359: JRStyle[] stylesArray = new JRStyle[stylesList.size()];
360:
361: stylesList.toArray(stylesArray);
362:
363: return stylesArray;
364: }
365:
366: /**
367: * Gets a list of report styles.
368: */
369: public List getStylesList() {
370: return stylesList;
371: }
372:
373: /**
374: * Gets a map of report styles.
375: */
376: public Map getStylesMap() {
377: return stylesMap;
378: }
379:
380: /**
381: * Adds a new style to the report styles.
382: */
383: public synchronized void addStyle(JRStyle style) throws JRException {
384: addStyle(style, false);
385: }
386:
387: /**
388: * Adds a new style to the report styles.
389: */
390: public synchronized void addStyle(JRStyle style,
391: boolean isIgnoreDuplicate) throws JRException {
392: if (stylesMap.containsKey(style.getName())) {
393: if (!isIgnoreDuplicate) {
394: throw new JRException(
395: "Duplicate declaration of report style : "
396: + style.getName());
397: }
398: } else {
399: stylesList.add(style);
400: stylesMap.put(style.getName(), style);
401:
402: if (style.isDefault()) {
403: setDefaultStyle(style);
404: }
405: }
406: }
407:
408: /**
409: *
410: */
411: public synchronized JRStyle removeStyle(String styleName) {
412: return removeStyle((JRStyle) stylesMap.get(styleName));
413: }
414:
415: /**
416: *
417: */
418: public synchronized JRStyle removeStyle(JRStyle style) {
419: if (style != null) {
420: if (style.isDefault()) {
421: setDefaultStyle(null);
422: }
423:
424: stylesList.remove(style);
425: stylesMap.remove(style.getName());
426: }
427:
428: return style;
429: }
430:
431: /**
432: * Returns a list of all pages in the filled report.
433: */
434: public List getPages() {
435: return pages;
436: }
437:
438: /**
439: * Adds a new page to the document.
440: */
441: public synchronized void addPage(JRPrintPage page) {
442: anchorIndexes = null;
443: pages.add(page);
444: }
445:
446: /**
447: * Adds a new page to the document, placing it at the specified index.
448: */
449: public synchronized void addPage(int index, JRPrintPage page) {
450: anchorIndexes = null;
451: pages.add(index, page);
452: }
453:
454: /**
455: * Removes a page from the document.
456: */
457: public synchronized JRPrintPage removePage(int index) {
458: anchorIndexes = null;
459: return (JRPrintPage) pages.remove(index);
460: }
461:
462: /**
463: *
464: */
465: public synchronized Map getAnchorIndexes() {
466: if (anchorIndexes == null) {
467: anchorIndexes = new HashMap();
468:
469: int i = 0;
470: for (Iterator itp = pages.iterator(); itp.hasNext(); i++) {
471: JRPrintPage page = (JRPrintPage) itp.next();
472: Collection elements = page.getElements();
473: collectAnchors(elements, i, 0, 0);
474: }
475: }
476:
477: return anchorIndexes;
478: }
479:
480: protected void collectAnchors(Collection elements, int pageIndex,
481: int offsetX, int offsetY) {
482: if (elements != null && elements.size() > 0) {
483: JRPrintElement element = null;
484: for (Iterator it = elements.iterator(); it.hasNext();) {
485: element = (JRPrintElement) it.next();
486: if (element instanceof JRPrintAnchor) {
487: anchorIndexes.put(((JRPrintAnchor) element)
488: .getAnchorName(), new JRPrintAnchorIndex(
489: pageIndex, element, offsetX, offsetY));
490: }
491:
492: if (element instanceof JRPrintFrame) {
493: JRPrintFrame frame = (JRPrintFrame) element;
494: collectAnchors(frame.getElements(), pageIndex,
495: offsetX + frame.getX(), offsetY
496: + frame.getY());
497: }
498: }
499: }
500: }
501:
502: /**
503: * Returns the name of the class implementing the {@link net.sf.jasperreports.engine.util.FormatFactory FormatFactory}
504: * interface to use with this document.
505: */
506: public String getFormatFactoryClass() {
507: return formatFactoryClass;
508: }
509:
510: /**
511: * Sets the name of the class implementing the {@link net.sf.jasperreports.engine.util.FormatFactory FormatFactory}
512: * interface to use with this document.
513: */
514: public void setFormatFactoryClass(String formatFactoryClass) {
515: this .formatFactoryClass = formatFactoryClass;
516: }
517:
518: /**
519: * Returns the code of the default <code>java.util.Locale</code> to be used for the
520: * elements of this print object.
521: * <p>
522: * When filling a report, the value of the {@link JRParameter#REPORT_LOCALE REPORT_LOCALE} parameter
523: * (or the default locale if the parameter has no explicit value)
524: * is saved using this attribute. Some elements (e.g. elements rendered by a subreport)
525: * in the print object can override this default locale.
526: * </p>
527: *
528: * @return the code of the default <code>java.util.Locale</code> for this object
529: * @see JRPrintText#getLocaleCode()
530: */
531: public String getLocaleCode() {
532: return localeCode;
533: }
534:
535: /**
536: * Sets the the code of the default <code>java.util.Locale</code> to be used for this object.
537: *
538: * @param localeCode the locale code, using the {@link java.util.Locale#toString() java.util.Locale.toString()}
539: * convention.
540: * @see #getLocaleCode()
541: * @see java.util.Locale#toString()
542: */
543: public void setLocaleCode(String localeCode) {
544: this .localeCode = localeCode;
545: }
546:
547: /**
548: * Returns the {@link java.util.TimeZone#getID() ID} of the default <code>java.util.TimeZone</code>
549: * to be used for the elements of this print object.
550: * <p>
551: * When filling a report, the value of the {@link JRParameter#REPORT_TIME_ZONE REPORT_TIME_ZONE} parameter
552: * (or the default time zine if the parameter has no explicit value)
553: * is saved using this attribute. Some elements (e.g. elements rendered by a subreport)
554: * in the print object can override this default time zone.
555: * </p>
556: *
557: * @return the ID of the default <code>java.util.TimeZone</code> for this object
558: * @see JRPrintText#getTimeZoneId()
559: */
560: public String getTimeZoneId() {
561: return timeZoneId;
562: }
563:
564: /**
565: * Sets the the {@link java.util.TimeZone#getID() ID} of the default <code>java.util.TimeZone</code>
566: * to be used for this object.
567: *
568: * @param timeZoneId the time zone ID
569: * @see #getTimeZoneId()
570: * @see java.util.TimeZone#getID()
571: */
572: public void setTimeZoneId(String timeZoneId) {
573: this.timeZoneId = timeZoneId;
574: }
575:
576: }
|