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.io.Serializable;
031: import java.util.HashMap;
032: import java.util.Map;
033:
034: import org.apache.commons.collections.ReferenceMap;
035: import org.apache.commons.logging.Log;
036: import org.apache.commons.logging.LogFactory;
037:
038: import net.sf.jasperreports.engine.JRConstants;
039: import net.sf.jasperreports.engine.JRPrintImage;
040: import net.sf.jasperreports.engine.JRRenderable;
041: import net.sf.jasperreports.engine.JasperPrint;
042:
043: /**
044: * Context used to store data shared by virtualized objects resulted from a report fill process.
045: *
046: * @author Lucian Chirita (lucianc@users.sourceforge.net)
047: * @version $Id: JRVirtualizationContext.java 1254 2006-05-15 08:57:30Z lucianc $
048: */
049: public class JRVirtualizationContext implements Serializable {
050: private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
051:
052: private static final Log log = LogFactory
053: .getLog(JRVirtualizationContext.class);
054:
055: private static final ReferenceMap contexts = new ReferenceMap(
056: ReferenceMap.WEAK, ReferenceMap.WEAK);
057:
058: private Map cachedRenderers;
059: private Map cachedTemplates;
060:
061: private boolean readOnly;
062:
063: /**
064: * Constructs a context.
065: */
066: public JRVirtualizationContext() {
067: cachedRenderers = new HashMap();
068: cachedTemplates = new HashMap();
069: }
070:
071: /**
072: * Caches an image renderer.
073: *
074: * @param image the image whose renderer should be cached
075: */
076: public void cacheRenderer(JRPrintImage image) {
077: JRRenderable renderer = image.getRenderer();
078: if (renderer != null) {
079: cachedRenderers.put(renderer.getId(), renderer);
080: }
081: }
082:
083: /**
084: * Retrieves a cached image renderer based on an ID.
085: *
086: * @param id the ID
087: * @return the cached image renderer for the ID
088: */
089: public JRRenderable getCachedRenderer(String id) {
090: return (JRRenderable) cachedRenderers.get(id);
091: }
092:
093: /**
094: * Determines whether a cached image renderer for a specified ID exists.
095: *
096: * @param id the ID
097: * @return <code>true</code> iff the context contains a cached renderer with the specified ID
098: */
099: public boolean hasCachedRenderer(String id) {
100: return cachedRenderers.containsKey(id);
101: }
102:
103: /**
104: * Determines whether a cached {@link JRTemplateElement template} with a specified ID exists.
105: *
106: * @param id the template ID
107: * @return <code>true</code> iff the context contains a cached template with the specified ID
108: */
109: public boolean hasCachedTemplate(String id) {
110: return cachedTemplates.containsKey(id);
111: }
112:
113: /**
114: * Caches an element template.
115: *
116: * @param template the template to cache
117: */
118: public void cacheTemplate(JRTemplateElement template) {
119: Object old = cachedTemplates.put(template.getId(), template);
120: if (old == null && log.isDebugEnabled()) {
121: log.debug("Cached template " + template + " having id "
122: + template.getId());
123: }
124: }
125:
126: /**
127: * Retrieves a cached template.
128: *
129: * @param templateId the template ID
130: * @return the cached template having the given ID
131: */
132: public JRTemplateElement getCachedTemplate(String templateId) {
133: return (JRTemplateElement) cachedTemplates.get(templateId);
134: }
135:
136: /**
137: * Determines whether this context has been marked as read-only.
138: *
139: * @return whether this context has been marked as read-only
140: * @see #setReadOnly(boolean)
141: */
142: public boolean isReadOnly() {
143: return readOnly;
144: }
145:
146: /**
147: * Sets the read-only flag for this context.
148: * <p>
149: * When in read-only mode, all the virtualizable objects belonging to this context
150: * are assumed final by the virtualizer and any change in a virtualizable object's data
151: * would be discarded on virtualization.
152: *
153: * @param readOnly the read-only flag
154: */
155: public void setReadOnly(boolean readOnly) {
156: this .readOnly = readOnly;
157: }
158:
159: /**
160: * Registers a virtualization context for {@link JasperPrint JasperPrint} object.
161: *
162: * @param context the virtualization context
163: * @param print the print object
164: */
165: public static void register(JRVirtualizationContext context,
166: JasperPrint print) {
167: synchronized (contexts) {
168: contexts.put(print, context);
169: }
170: }
171:
172: /**
173: * Returns the virtualization context registered for a print object.
174: * <p>
175: * When the engine fills a report using a virtualizer, it {@link #register(JRVirtualizationContext, JasperPrint) registers}
176: * the virtualization context with the generated {@link JasperPrint JasperPrint} object so that the caller
177: * would be able to retrieve the context based on the returned print object.
178: *
179: * @param print a print object
180: * @return the virtualization context registered for the print object, or <code>null</code> if no context
181: * has been registered
182: */
183: public static JRVirtualizationContext getRegistered(
184: JasperPrint print) {
185: synchronized (contexts) {
186: return (JRVirtualizationContext) contexts.get(print);
187: }
188: }
189: }
|