KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > JasperPrint


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28
29
30 /*
31  * Contributors:
32  * John Bindel - jbindel@users.sourceforge.net
33  */

34
35 package net.sf.jasperreports.engine;
36
37 import java.io.Serializable JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.Collection JavaDoc;
40 import java.util.HashMap JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.Map JavaDoc;
44
45
46 /**
47  * An instance of this class represents a page-oriented document
48  * that can be viewed, printed or exported to other formats.
49  * <p>
50  * When filling report designs with data, the engine produces instances
51  * of this class and these can be transferred over the network,
52  * stored in a serialized form on disk or exported to various
53  * other formats like PDF, HTML, XLS, CSV or XML.
54  *
55  * @author Teodor Danciu (teodord@users.sourceforge.net)
56  * @version $Id: JasperPrint.java 1485 2006-11-14 20:23:17 +0200 (Tue, 14 Nov 2006) teodord $
57  */

58 public class JasperPrint implements Serializable JavaDoc
59 {
60     
61     /**
62      * A small class for implementing just the style provider functionality.
63      */

64     private static class DefaultStyleProvider implements JRDefaultStyleProvider, Serializable JavaDoc
65     {
66         private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
67         
68         private JRReportFont defaultFont;
69         private JRStyle defaultStyle;
70
71         DefaultStyleProvider(JRReportFont font, JRStyle style)
72         {
73             this.defaultFont = font;
74             this.defaultStyle = style;
75         }
76
77         public JRReportFont getDefaultFont()
78         {
79             return defaultFont;
80         }
81
82         void setDefaultFont(JRReportFont font)
83         {
84             this.defaultFont = font;
85         }
86
87         public JRStyle getDefaultStyle()
88         {
89             return defaultStyle;
90         }
91
92         void setDefaultStyle(JRStyle style)
93         {
94             this.defaultStyle = style;
95         }
96     }
97
98
99     /**
100      *
101      */

102     private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
103
104     /**
105      *
106      */

107     private String JavaDoc name = null;
108     private int pageWidth = 0;
109     private int pageHeight = 0;
110     private byte orientation = JRReport.ORIENTATION_PORTRAIT;
111
112     private Map JavaDoc fontsMap = new HashMap JavaDoc();
113     private List JavaDoc fontsList = new ArrayList JavaDoc();
114     private Map JavaDoc stylesMap = new HashMap JavaDoc();
115     private List JavaDoc stylesList = new ArrayList JavaDoc();
116
117     private List JavaDoc pages = new ArrayList JavaDoc();
118
119     private transient Map JavaDoc anchorIndexes = null;
120     private DefaultStyleProvider defaultStyleProvider = null;
121     
122     private String JavaDoc formatFactoryClass;
123     private String JavaDoc localeCode;
124     private String JavaDoc timeZoneId;
125
126
127     /**
128      * Creates a new empty document.
129      */

130     public JasperPrint()
131     {
132         defaultStyleProvider = new DefaultStyleProvider(null, null);
133     }
134
135     /**
136      * @return Returns the name of the document
137      */

138     public String JavaDoc getName()
139     {
140         return name;
141     }
142         
143     /**
144      * Sets the name of the document.
145      *
146      * @param name name of the document
147      */

148     public void setName(String JavaDoc name)
149     {
150         this.name = name;
151     }
152
153     /**
154      * @return Returns the page width
155      */

156     public int getPageWidth()
157     {
158         return pageWidth;
159     }
160         
161     /**
162      * Sets the page width.
163      *
164      * @param pageWidth page width
165      */

166     public void setPageWidth(int pageWidth)
167     {
168         this.pageWidth = pageWidth;
169     }
170
171     /**
172      * @return Returns the page height.
173      */

174     public int getPageHeight()
175     {
176         return pageHeight;
177     }
178         
179     /**
180      * Sets the page height.
181      *
182      * @param pageHeight page height
183      */

184     public void setPageHeight(int pageHeight)
185     {
186         this.pageHeight = pageHeight;
187     }
188
189
190     /**
191      * Returns the page orientation.
192      * @see JRReport ORIENTATION_PORTRAIT,
193      * @see JRReport ORIENTATION_LANDSCAPE
194      */

195     public byte getOrientation()
196     {
197         return orientation;
198     }
199         
200     /**
201      * Sets the page orientation.
202      * @see JRReport ORIENTATION_PORTRAIT,
203      * @see JRReport ORIENTATION_LANDSCAPE
204      */

205     public void setOrientation(byte orientation)
206     {
207         this.orientation = orientation;
208     }
209
210     /**
211      * Returns the default report font.
212      */

213     public JRReportFont getDefaultFont()
214     {
215         return defaultStyleProvider.getDefaultFont();
216     }
217
218     /**
219      * Sets the default report font.
220      */

221     public void setDefaultFont(JRReportFont font)
222     {
223         defaultStyleProvider.setDefaultFont(font);
224     }
225
226     /**
227      * When we want to virtualize pages, we want a font provider that
228      * is <i>not</i> the print object itself.
229      */

230     public JRDefaultFontProvider getDefaultFontProvider()
231     {
232         return defaultStyleProvider;
233     }
234         
235     /**
236      * Gets an array of report fonts.
237      * @deprecated
238      */

239     public JRReportFont[] getFonts()
240     {
241         JRReportFont[] fontsArray = new JRReportFont[fontsList.size()];
242         
243         fontsList.toArray(fontsArray);
244
245         return fontsArray;
246     }
247
248     /**
249      * Gets a list of report fonts.
250      * @deprecated
251      */

252     public List JavaDoc getFontsList()
253     {
254         return fontsList;
255     }
256
257     /**
258      * Gets a map of report fonts.
259      * @deprecated
260      */

261     public Map JavaDoc getFontsMap()
262     {
263         return fontsMap;
264     }
265
266     /**
267      * Adds a new font to the report fonts.
268      * @deprecated
269      */

270     public synchronized void addFont(JRReportFont reportFont) throws JRException
271     {
272         addFont(reportFont, false);
273     }
274
275     /**
276      * Adds a new font to the report fonts.
277      * @deprecated
278      */

279     public synchronized void addFont(JRReportFont reportFont, boolean isIgnoreDuplicate) throws JRException
280     {
281         if (fontsMap.containsKey(reportFont.getName()))
282         {
283             if (!isIgnoreDuplicate)
284             {
285                 throw new JRException("Duplicate declaration of report font : " + reportFont.getName());
286             }
287         }
288         else
289         {
290             fontsList.add(reportFont);
291             fontsMap.put(reportFont.getName(), reportFont);
292             
293             if (reportFont.isDefault())
294             {
295                 setDefaultFont(reportFont);
296             }
297         }
298     }
299
300     /**
301      * @deprecated
302      */

303     public synchronized JRReportFont removeFont(String JavaDoc fontName)
304     {
305         return removeFont(
306             (JRReportFont)fontsMap.get(fontName)
307             );
308     }
309
310     /**
311      * @deprecated
312      */

313     public synchronized JRReportFont removeFont(JRReportFont reportFont)
314     {
315         if (reportFont != null)
316         {
317             if (reportFont.isDefault())
318             {
319                 setDefaultFont(null);
320             }
321
322             fontsList.remove(reportFont);
323             fontsMap.remove(reportFont.getName());
324         }
325         
326         return reportFont;
327     }
328
329     /**
330      * Returns the default report style.
331      */

332     public JRStyle getDefaultStyle()
333     {
334         return defaultStyleProvider.getDefaultStyle();
335     }
336
337     /**
338      * Sets the default report style.
339      */

340     public synchronized void setDefaultStyle(JRStyle style)
341     {
342         defaultStyleProvider.setDefaultStyle(style);
343     }
344
345     /**
346      * When we want to virtualize pages, we want a style provider that
347      * is <i>not</i> the print object itself.
348      */

349     public JRDefaultStyleProvider getDefaultStyleProvider()
350     {
351         return defaultStyleProvider;
352     }
353         
354     /**
355      * Gets an array of report styles.
356      */

357     public JRStyle[] getStyles()
358     {
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 JavaDoc getStylesList()
370     {
371         return stylesList;
372     }
373
374     /**
375      * Gets a map of report styles.
376      */

377     public Map JavaDoc getStylesMap()
378     {
379         return stylesMap;
380     }
381
382     /**
383      * Adds a new style to the report styles.
384      */

385     public synchronized void addStyle(JRStyle style) throws JRException
386     {
387         addStyle(style, false);
388     }
389
390     /**
391      * Adds a new style to the report styles.
392      */

393     public synchronized void addStyle(JRStyle style, boolean isIgnoreDuplicate) throws JRException
394     {
395         if (stylesMap.containsKey(style.getName()))
396         {
397             if (!isIgnoreDuplicate)
398             {
399                 throw new JRException("Duplicate declaration of report style : " + style.getName());
400             }
401         }
402         else
403         {
404             stylesList.add(style);
405             stylesMap.put(style.getName(), style);
406             
407             if (style.isDefault())
408             {
409                 setDefaultStyle(style);
410             }
411         }
412     }
413
414     /**
415      *
416      */

417     public synchronized JRStyle removeStyle(String JavaDoc styleName)
418     {
419         return removeStyle(
420             (JRStyle)stylesMap.get(styleName)
421             );
422     }
423
424     /**
425      *
426      */

427     public synchronized JRStyle removeStyle(JRStyle style)
428     {
429         if (style != null)
430         {
431             if (style.isDefault())
432             {
433                 setDefaultStyle(null);
434             }
435
436             stylesList.remove(style);
437             stylesMap.remove(style.getName());
438         }
439         
440         return style;
441     }
442
443     /**
444      * Returns a list of all pages in the filled report.
445      */

446     public List JavaDoc getPages()
447     {
448         return pages;
449     }
450         
451     /**
452      * Adds a new page to the document.
453      */

454     public synchronized void addPage(JRPrintPage page)
455     {
456         anchorIndexes = null;
457         pages.add(page);
458     }
459
460     /**
461      * Adds a new page to the document, placing it at the specified index.
462      */

463     public synchronized void addPage(int index, JRPrintPage page)
464     {
465         anchorIndexes = null;
466         pages.add(index, page);
467     }
468
469     /**
470      * Removes a page from the document.
471      */

472     public synchronized JRPrintPage removePage(int index)
473     {
474         anchorIndexes = null;
475         return (JRPrintPage)pages.remove(index);
476     }
477
478     /**
479      *
480      */

481     public synchronized Map JavaDoc getAnchorIndexes()
482     {
483         if (anchorIndexes == null)
484         {
485             anchorIndexes = new HashMap JavaDoc();
486             
487             int i = 0;
488             for(Iterator JavaDoc itp = pages.iterator(); itp.hasNext(); i++)
489             {
490                 JRPrintPage page = (JRPrintPage)itp.next();
491                 Collection JavaDoc elements = page.getElements();
492                 collectAnchors(elements, i, 0, 0);
493             }
494         }
495         
496         return anchorIndexes;
497     }
498
499     protected void collectAnchors(Collection JavaDoc elements, int pageIndex, int offsetX, int offsetY)
500     {
501         if (elements != null && elements.size() > 0)
502         {
503             JRPrintElement element = null;
504             for(Iterator JavaDoc it = elements.iterator(); it.hasNext();)
505             {
506                 element = (JRPrintElement)it.next();
507                 if (element instanceof JRPrintAnchor)
508                 {
509                     anchorIndexes.put(
510                         ((JRPrintAnchor)element).getAnchorName(),
511                         new JRPrintAnchorIndex(pageIndex, element, offsetX, offsetY)
512                         );
513                 }
514                 
515                 if (element instanceof JRPrintFrame)
516                 {
517                     JRPrintFrame frame = (JRPrintFrame) element;
518                     collectAnchors(frame.getElements(), pageIndex, offsetX + frame.getX(), offsetY + frame.getY());
519                 }
520             }
521         }
522     }
523
524
525     /**
526      * Returns the name of the class implementing the {@link net.sf.jasperreports.engine.util.FormatFactory FormatFactory}
527      * interface to use with this document.
528      */

529     public String JavaDoc getFormatFactoryClass()
530     {
531         return formatFactoryClass;
532     }
533
534
535     /**
536      * Sets the name of the class implementing the {@link net.sf.jasperreports.engine.util.FormatFactory FormatFactory}
537      * interface to use with this document.
538      */

539     public void setFormatFactoryClass(String JavaDoc formatFactoryClass)
540     {
541         this.formatFactoryClass = formatFactoryClass;
542     }
543
544
545     /**
546      * Returns the code of the default <code>java.util.Locale</code> to be used for the
547      * elements of this print object.
548      * <p>
549      * When filling a report, the value of the {@link JRParameter#REPORT_LOCALE REPORT_LOCALE} parameter
550      * (or the default locale if the parameter has no explicit value)
551      * is saved using this attribute. Some elements (e.g. elements rendered by a subreport)
552      * in the print object can override this default locale.
553      * </p>
554      *
555      * @return the code of the default <code>java.util.Locale</code> for this object
556      * @see JRPrintText#getLocaleCode()
557      */

558     public String JavaDoc getLocaleCode()
559     {
560         return localeCode;
561     }
562
563
564     /**
565      * Sets the the code of the default <code>java.util.Locale</code> to be used for this object.
566      *
567      * @param localeCode the locale code, using the {@link java.util.Locale#toString() java.util.Locale.toString()}
568      * convention.
569      * @see #getLocaleCode()
570      * @see java.util.Locale#toString()
571      */

572     public void setLocaleCode(String JavaDoc localeCode)
573     {
574         this.localeCode = localeCode;
575     }
576
577
578     /**
579      * Returns the {@link java.util.TimeZone#getID() ID} of the default <code>java.util.TimeZone</code>
580      * to be used for the elements of this print object.
581      * <p>
582      * When filling a report, the value of the {@link JRParameter#REPORT_TIME_ZONE REPORT_TIME_ZONE} parameter
583      * (or the default time zine if the parameter has no explicit value)
584      * is saved using this attribute. Some elements (e.g. elements rendered by a subreport)
585      * in the print object can override this default time zone.
586      * </p>
587      *
588      * @return the ID of the default <code>java.util.TimeZone</code> for this object
589      * @see JRPrintText#getTimeZoneId()
590      */

591     public String JavaDoc getTimeZoneId()
592     {
593         return timeZoneId;
594     }
595
596
597     /**
598      * Sets the the {@link java.util.TimeZone#getID() ID} of the default <code>java.util.TimeZone</code>
599      * to be used for this object.
600      *
601      * @param timeZoneId the time zone ID
602      * @see #getTimeZoneId()
603      * @see java.util.TimeZone#getID()
604      */

605     public void setTimeZoneId(String JavaDoc timeZoneId)
606     {
607         this.timeZoneId = timeZoneId;
608     }
609         
610
611 }
612
Popular Tags