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: package net.sf.jasperreports.engine.fill;
029:
030: import java.util.HashMap;
031: import java.util.Locale;
032: import java.util.Map;
033: import java.util.TimeZone;
034:
035: import net.sf.jasperreports.engine.JRException;
036: import net.sf.jasperreports.engine.JRPrintImage;
037: import net.sf.jasperreports.engine.JRPrintPage;
038: import net.sf.jasperreports.engine.JRTemplate;
039: import net.sf.jasperreports.engine.JasperReport;
040: import net.sf.jasperreports.engine.query.JRQueryExecuter;
041: import net.sf.jasperreports.engine.util.FormatFactory;
042:
043: /**
044: * Context class shared by all the fillers involved in a report (master and subfillers).
045: * <p>
046: * The context is created by the master filler and inherited by the subfillers.
047: *
048: * @author Lucian Chirita (lucianc@users.sourceforge.net)
049: * @version $Id: JRFillContext.java 1759 2007-06-20 16:47:34Z lucianc $
050: * @see net.sf.jasperreports.engine.fill.JRBaseFiller
051: */
052: public class JRFillContext {
053: private Map loadedImages;
054: private Map loadedSubreports;
055: private Map loadedTemplates;
056: private boolean usingVirtualizer = false;
057: private boolean perPageBoundElements = false;
058: private JRPrintPage printPage = null;
059: private boolean ignorePagination = false;
060: private JRQueryExecuter queryExecuter;
061:
062: private JRVirtualizationContext virtualizationContext;
063:
064: private FormatFactory masterFormatFactory;
065: private Locale masterLocale;
066: private TimeZone masterTimeZone;
067:
068: /**
069: * Constructs a fill context.
070: */
071: public JRFillContext() {
072: loadedImages = new HashMap();
073: loadedSubreports = new HashMap();
074: loadedTemplates = new HashMap();
075: }
076:
077: /**
078: * Checks whether an image given by source has already been loaded and cached.
079: *
080: * @param source the source of the image
081: * @return whether the image has been cached
082: * @see #getLoadedImage(Object)
083: * @see #registerLoadedImage(Object, JRPrintImage)
084: */
085: public boolean hasLoadedImage(Object source) {
086: return loadedImages.containsKey(source);
087: }
088:
089: /**
090: * Gets a cached image.
091: *
092: * @param source the source of the image
093: * @return the cached image
094: * @see #registerLoadedImage(Object, JRPrintImage)
095: */
096: public JRPrintImage getLoadedImage(Object source) {
097: return (JRPrintImage) loadedImages.get(source);
098: }
099:
100: /**
101: * Registers an image loaded from a source.
102: * <p>
103: * The image is cached for further use.
104: *
105: * @param source the source that was used to load the image
106: * @param image the loaded image
107: * @see #getLoadedImage(Object)
108: */
109: public void registerLoadedImage(Object source, JRPrintImage image) {
110: loadedImages.put(source, image);
111: if (usingVirtualizer) {
112: virtualizationContext.cacheRenderer(image);
113: }
114: }
115:
116: /**
117: * Checks whether a subreport given by source has already been loaded and cached.
118: *
119: * @param source the source of the subreport
120: * @return whether the subreport has been cached
121: * @see #getLoadedSubreport(Object)
122: * @see #registerLoadedSubreport(Object, JasperReport)
123: */
124: public boolean hasLoadedSubreport(Object source) {
125: return loadedSubreports.containsKey(source);
126: }
127:
128: /**
129: * Gets a cached subreport.
130: *
131: * @param source the source of the subreport
132: * @return the cached subreport
133: * @see #registerLoadedSubreport(Object, JasperReport)
134: */
135: public JasperReport getLoadedSubreport(Object source) {
136: return (JasperReport) loadedSubreports.get(source);
137: }
138:
139: /**
140: * Registers a subreport loaded from a source.
141: * <p>
142: * The subreport is cached for further use.
143: *
144: * @param source the source that was used to load the subreport
145: * @param subreport the loaded subreport
146: * @see #getLoadedSubreport(Object)
147: */
148: public void registerLoadedSubreport(Object source,
149: JasperReport subreport) {
150: loadedSubreports.put(source, subreport);
151: }
152:
153: /**
154: * Sets the flag indicating whether a virtualizer is used by the filling process.
155: *
156: * @param usingVirtualizer whether virtualization is used
157: * @see #isUsingVirtualizer()
158: */
159: public void setUsingVirtualizer(boolean usingVirtualizer) {
160: this .usingVirtualizer = usingVirtualizer;
161: if (usingVirtualizer && virtualizationContext == null) {
162: virtualizationContext = new JRVirtualizationContext();
163: }
164: }
165:
166: /**
167: * Decides whether virtualization is used by the filling process.
168: *
169: * @return <code>true</code> iff a virtualizer is used
170: * @see #setUsingVirtualizer(boolean)
171: * @see net.sf.jasperreports.engine.JRParameter#REPORT_VIRTUALIZER
172: */
173: public boolean isUsingVirtualizer() {
174: return usingVirtualizer;
175: }
176:
177: /**
178: * Sets the flag indicating whether fillers should keep per page bound
179: * element maps.
180: *
181: * @param perPageBoundElements the value of the flag
182: * @see #isPerPageBoundElements()
183: */
184: public void setPerPageBoundElements(boolean perPageBoundElements) {
185: this .perPageBoundElements = perPageBoundElements;
186: }
187:
188: /**
189: * Decides whether fillers should keep per page bound element maps.
190: *
191: * @return <code>true</code> iff fillers should keep per page bound element maps
192: * @see #setPerPageBoundElements(boolean)
193: */
194: public boolean isPerPageBoundElements() {
195: return perPageBoundElements;
196: }
197:
198: /**
199: * Sets the current master print page.
200: *
201: * @param page the master print page
202: * @see #getPrintPage()
203: */
204: public void setPrintPage(JRPrintPage page) {
205: printPage = page;
206: }
207:
208: /**
209: * Returns the current master print page.
210: *
211: * @return the current master print page
212: * @see #setPrintPage(JRPrintPage)
213: */
214: public JRPrintPage getPrintPage() {
215: return printPage;
216: }
217:
218: /**
219: * Sets the flag that decides whether pagination should be ignored during filling.
220: *
221: * @param ignorePagination
222: * @see #isIgnorePagination()
223: */
224: public void setIgnorePagination(boolean ignorePagination) {
225: this .ignorePagination = ignorePagination;
226: }
227:
228: /**
229: * Decides whether the filling should ignore pagination.
230: *
231: * @return whether the filling should ignore pagination
232: * @see #setIgnorePagination(boolean)
233: * @see net.sf.jasperreports.engine.JRParameter#IS_IGNORE_PAGINATION
234: */
235: public boolean isIgnorePagination() {
236: return ignorePagination;
237: }
238:
239: /**
240: * Sets the running query executer.
241: * <p>
242: * This method is called before firing the query.
243: *
244: * @param queryExecuter the running query executer
245: */
246: public synchronized void setRunningQueryExecuter(
247: JRQueryExecuter queryExecuter) {
248: this .queryExecuter = queryExecuter;
249: }
250:
251: /**
252: * Clears the running query executer.
253: * <p>
254: * This method is called after the query has ended.
255: *
256: */
257: public synchronized void clearRunningQueryExecuter() {
258: this .queryExecuter = null;
259: }
260:
261: /**
262: * Cancels the running query.
263: *
264: * @return <code>true</code> iff there is a running query and it has been cancelled.
265: * @throws JRException
266: */
267: public synchronized boolean cancelRunningQuery() throws JRException {
268: if (queryExecuter != null) {
269: return queryExecuter.cancelQuery();
270: }
271:
272: return false;
273: }
274:
275: /**
276: * Ensures that the master page is available when virtualization is used.
277: */
278: public void ensureMasterPageAvailable() {
279: if (usingVirtualizer) {
280: printPage.getElements();
281: }
282: }
283:
284: /**
285: * Returns the virtualization context.
286: *
287: * @return the virtualization context
288: */
289: public JRVirtualizationContext getVirtualizationContext() {
290: return virtualizationContext;
291: }
292:
293: public FormatFactory getMasterFormatFactory() {
294: return masterFormatFactory;
295: }
296:
297: public void setMasterFormatFactory(FormatFactory masterFormatFactory) {
298: this .masterFormatFactory = masterFormatFactory;
299: }
300:
301: public Locale getMasterLocale() {
302: return masterLocale;
303: }
304:
305: public void setMasterLocale(Locale masterLocale) {
306: this .masterLocale = masterLocale;
307: }
308:
309: public TimeZone getMasterTimeZone() {
310: return masterTimeZone;
311: }
312:
313: public void setMasterTimeZone(TimeZone masterTimeZone) {
314: this .masterTimeZone = masterTimeZone;
315: }
316:
317: /**
318: * Checks whether a template given by source has already been loaded and cached.
319: *
320: * @param source the source of the template
321: * @return whether the template has been cached
322: * @see #getLoadedTemplate(Object)
323: * @see #registerLoadedTemplate(Object, JRTemplate)
324: */
325: public boolean hasLoadedTemplate(Object source) {
326: return loadedTemplates.containsKey(source);
327: }
328:
329: /**
330: * Gets a cached template.
331: *
332: * @param source the source of the templage
333: * @return the cached templage
334: * @see #registerLoadedTemplate(Object, JRTemplate)
335: */
336: public JRTemplate getLoadedTemplate(Object source) {
337: return (JRTemplate) loadedTemplates.get(source);
338: }
339:
340: /**
341: * Registers a template loaded from a source.
342: * <p>
343: * The template is cached for further use.
344: *
345: * @param source the source that was used to load the template
346: * @param template the loaded templage
347: * @see #getLoadedTemplate(Object)
348: */
349: public void registerLoadedTemplate(Object source,
350: JRTemplate template) {
351: loadedTemplates.put(source, template);
352: }
353: }
|