KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > fill > JRVirtualizationContext


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 package net.sf.jasperreports.engine.fill;
29
30 import java.io.Serializable JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import org.apache.commons.collections.ReferenceMap;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 import net.sf.jasperreports.engine.JRConstants;
39 import net.sf.jasperreports.engine.JRPrintImage;
40 import net.sf.jasperreports.engine.JRRenderable;
41 import net.sf.jasperreports.engine.JasperPrint;
42
43 /**
44  * Context used to store data shared by virtualized objects resulted from a report fill process.
45  *
46  * @author Lucian Chirita (lucianc@users.sourceforge.net)
47  * @version $Id: JRVirtualizationContext.java 1254 2006-05-15 11:57:30 +0300 (Mon, 15 May 2006) lucianc $
48  */

49 public class JRVirtualizationContext implements Serializable JavaDoc
50 {
51     private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
52     
53     private static final Log log = LogFactory.getLog(JRVirtualizationContext.class);
54     
55     private static final ReferenceMap contexts = new ReferenceMap(ReferenceMap.WEAK, ReferenceMap.WEAK);
56     
57     private Map JavaDoc cachedRenderers;
58     private Map JavaDoc cachedTemplates;
59     
60     private boolean readOnly;
61     
62     /**
63      * Constructs a context.
64      */

65     public JRVirtualizationContext()
66     {
67         cachedRenderers = new HashMap JavaDoc();
68         cachedTemplates = new HashMap JavaDoc();
69     }
70
71     
72     /**
73      * Caches an image renderer.
74      *
75      * @param image the image whose renderer should be cached
76      */

77     public void cacheRenderer(JRPrintImage image)
78     {
79         JRRenderable renderer = image.getRenderer();
80         if (renderer != null)
81         {
82             cachedRenderers.put(renderer.getId(), renderer);
83         }
84     }
85
86     
87     /**
88      * Retrieves a cached image renderer based on an ID.
89      *
90      * @param id the ID
91      * @return the cached image renderer for the ID
92      */

93     public JRRenderable getCachedRenderer(String JavaDoc id)
94     {
95         return (JRRenderable) cachedRenderers.get(id);
96     }
97
98     
99     /**
100      * Determines whether a cached image renderer for a specified ID exists.
101      *
102      * @param id the ID
103      * @return <code>true</code> iff the context contains a cached renderer with the specified ID
104      */

105     public boolean hasCachedRenderer(String JavaDoc id)
106     {
107         return cachedRenderers.containsKey(id);
108     }
109
110     
111     /**
112      * Determines whether a cached {@link JRTemplateElement template} with a specified ID exists.
113      *
114      * @param id the template ID
115      * @return <code>true</code> iff the context contains a cached template with the specified ID
116      */

117     public boolean hasCachedTemplate(String JavaDoc id)
118     {
119         return cachedTemplates.containsKey(id);
120     }
121     
122     
123     /**
124      * Caches an element template.
125      *
126      * @param template the template to cache
127      */

128     public void cacheTemplate(JRTemplateElement template)
129     {
130         Object JavaDoc old = cachedTemplates.put(template.getId(), template);
131         if (old == null && log.isDebugEnabled())
132         {
133             log.debug("Cached template " + template + " having id " + template.getId());
134         }
135     }
136     
137     
138     /**
139      * Retrieves a cached template.
140      *
141      * @param templateId the template ID
142      * @return the cached template having the given ID
143      */

144     public JRTemplateElement getCachedTemplate(String JavaDoc templateId)
145     {
146         return (JRTemplateElement) cachedTemplates.get(templateId);
147     }
148
149
150     /**
151      * Determines whether this context has been marked as read-only.
152      *
153      * @return whether this context has been marked as read-only
154      * @see #setReadOnly(boolean)
155      */

156     public boolean isReadOnly()
157     {
158         return readOnly;
159     }
160
161
162     /**
163      * Sets the read-only flag for this context.
164      * <p>
165      * When in read-only mode, all the virtualizable objects belonging to this context
166      * are assumed final by the virtualizer and any change in a virtualizable object's data
167      * would be discarded on virtualization.
168      *
169      * @param readOnly the read-only flag
170      */

171     public void setReadOnly(boolean readOnly)
172     {
173         this.readOnly = readOnly;
174     }
175     
176     
177     /**
178      * Registers a virtualization context for {@link JasperPrint JasperPrint} object.
179      *
180      * @param context the virtualization context
181      * @param print the print object
182      */

183     public static void register(JRVirtualizationContext context, JasperPrint print)
184     {
185         synchronized (contexts)
186         {
187             contexts.put(print, context);
188         }
189     }
190
191     
192     /**
193      * Returns the virtualization context registered for a print object.
194      * <p>
195      * When the engine fills a report using a virtualizer, it {@link #register(JRVirtualizationContext, JasperPrint) registers}
196      * the virtualization context with the generated {@link JasperPrint JasperPrint} object so that the caller
197      * would be able to retrieve the context based on the returned print object.
198      *
199      * @param print a print object
200      * @return the virtualization context registered for the print object, or <code>null</code> if no context
201      * has been registered
202      */

203     public static JRVirtualizationContext getRegistered(JasperPrint print)
204     {
205         synchronized (contexts)
206         {
207             return (JRVirtualizationContext) contexts.get(print);
208         }
209     }
210 }
211
Popular Tags