KickJava   Java API By Example, From Geeks To Geeks.

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


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;
29
30 import java.awt.Dimension JavaDoc;
31 import java.awt.Graphics2D JavaDoc;
32 import java.awt.Image JavaDoc;
33 import java.awt.geom.Dimension2D JavaDoc;
34 import java.awt.geom.Rectangle2D JavaDoc;
35 import java.io.File JavaDoc;
36 import java.io.InputStream JavaDoc;
37 import java.lang.ref.SoftReference JavaDoc;
38 import java.net.URL JavaDoc;
39 import java.net.URLStreamHandlerFactory JavaDoc;
40
41 import net.sf.jasperreports.engine.util.JRImageLoader;
42 import net.sf.jasperreports.engine.util.JRLoader;
43 import net.sf.jasperreports.engine.util.JRResourcesUtil;
44 import net.sf.jasperreports.engine.util.JRTypeSniffer;
45
46
47 /**
48  * @author Teodor Danciu (teodord@users.sourceforge.net)
49  * @version $Id: JRImageRenderer.java 1507 2006-11-27 17:12:17 +0200 (Mon, 27 Nov 2006) teodord $
50  */

51 public class JRImageRenderer extends JRAbstractRenderer
52 {
53
54     /**
55      *
56      */

57     private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
58
59     /**
60      *
61      */

62     private byte[] imageData = null;
63     private String JavaDoc imageLocation = null;
64     private byte onErrorType = JRImage.ON_ERROR_TYPE_ERROR;
65     private byte imageType = IMAGE_TYPE_UNKNOWN;
66
67     /**
68      *
69      */

70     private transient SoftReference JavaDoc awtImageRef = null;
71
72
73     /**
74      *
75      */

76     private JRImageRenderer(byte[] imageData, byte onErrorType)
77     {
78         this.imageData = imageData;
79         this.onErrorType = onErrorType;
80         
81         if(imageData != null)
82         {
83             imageType = JRTypeSniffer.getImageType(imageData);
84         }
85             
86     }
87
88
89     /**
90      *
91      */

92     private JRImageRenderer(String JavaDoc imageLocation, byte onErrorType)
93     {
94         this.imageLocation = imageLocation;
95         this.onErrorType = onErrorType;
96     }
97
98
99     /**
100      * @deprecated replaced by
101      * {@link JRResourcesUtil#getThreadClassLoader() JRResourcesUtil.getThreadClassLoader()}
102      */

103     public static ClassLoader JavaDoc getClassLoader()
104     {
105         return JRResourcesUtil.getThreadClassLoader();
106     }
107
108
109     /**
110      * @deprecated replace by
111      * {@link JRResourcesUtil#setThreadClassLoader(ClassLoader) JRResourcesUtil.setThreadClassLoader(ClassLoader)}
112      */

113     public static void setClassLoader(ClassLoader JavaDoc classLoader)
114     {
115         JRResourcesUtil.setThreadClassLoader(classLoader);
116     }
117
118
119     /**
120      *
121      */

122     public static JRImageRenderer getInstance(byte[] imageData)
123     {
124         return getInstance(imageData, JRImage.ON_ERROR_TYPE_ERROR);
125     }
126
127
128     /**
129      *
130      */

131     public static JRImageRenderer getInstance(byte[] imageData, byte onErrorType)
132     {
133 // if (imageData == null || imageData.length == 0)
134
// {
135
// return null;
136
// }
137
return new JRImageRenderer(imageData, onErrorType);
138     }
139
140
141     /**
142      *
143      */

144     public static JRRenderable getInstance(String JavaDoc imageLocation) throws JRException
145     {
146         return getInstance(imageLocation, JRImage.ON_ERROR_TYPE_ERROR, true);
147     }
148
149
150     /**
151      *
152      */

153     public static JRRenderable getInstance(String JavaDoc imageLocation, byte onErrorType) throws JRException
154     {
155         return getInstance(imageLocation, onErrorType, true);
156     }
157
158
159     /**
160      *
161      */

162     public static JRRenderable getInstance(String JavaDoc imageLocation, byte onErrorType, boolean isLazy) throws JRException
163     {
164         return getInstance(imageLocation, onErrorType, isLazy, null, null);
165     }
166
167     
168     public static JRRenderable getInstance(
169         String JavaDoc imageLocation,
170         byte onErrorType,
171         boolean isLazy,
172         ClassLoader JavaDoc classLoader
173         ) throws JRException
174     {
175         return getInstance(imageLocation, onErrorType, isLazy, classLoader);
176     }
177     
178     /**
179      *
180      */

181     public static JRRenderable getInstance(
182         String JavaDoc imageLocation,
183         byte onErrorType,
184         boolean isLazy,
185         ClassLoader JavaDoc classLoader,
186         URLStreamHandlerFactory JavaDoc urlHandlerFactory
187         ) throws JRException
188     {
189         if (imageLocation == null)
190         {
191             return null;
192         }
193
194         if (isLazy)
195         {
196             return new JRImageRenderer(imageLocation, onErrorType);
197         }
198
199         try
200         {
201             byte[] data = JRLoader.loadBytesFromLocation(imageLocation, classLoader, urlHandlerFactory);
202             return new JRImageRenderer(data, onErrorType);
203         }
204         catch (JRException e)
205         {
206             return getOnErrorRenderer(onErrorType, e);
207         }
208     }
209
210
211     /**
212      *
213      */

214     public static JRRenderable getInstance(Image JavaDoc img, byte onErrorType) throws JRException
215     {
216         return getInstance(img, JRRenderable.IMAGE_TYPE_JPEG, onErrorType);
217     }
218
219
220     /**
221      * Creates and returns an instance of the JRImageRenderer class after encoding the image object using an image
222      * encoder that supports the supplied image type.
223      *
224      * @param image the java.awt.Image object to wrap into a JRImageRenderer instance
225      * @param imageType the type of the image as specified by one of the constants defined in the JRRenderable interface
226      * @param onErrorType one of the error type constants defined in the JRImage interface
227      * @return the image renderer instance
228      */

229     public static JRRenderable getInstance(Image JavaDoc image, byte imageType, byte onErrorType) throws JRException
230     {
231         try
232         {
233             return new JRImageRenderer(JRImageLoader.loadImageDataFromAWTImage(image, imageType), onErrorType);
234         }
235         catch (JRException e)
236         {
237             return getOnErrorRenderer(onErrorType, e);
238         }
239     }
240
241
242     /**
243      *
244      */

245     public static JRRenderable getInstance(InputStream JavaDoc is, byte onErrorType) throws JRException
246     {
247         try
248         {
249             return new JRImageRenderer(JRLoader.loadBytes(is), onErrorType);
250         }
251         catch (JRException e)
252         {
253             return getOnErrorRenderer(onErrorType, e);
254         }
255     }
256
257
258     /**
259      *
260      */

261     public static JRRenderable getInstance(URL JavaDoc url, byte onErrorType) throws JRException
262     {
263         try
264         {
265             return new JRImageRenderer(JRLoader.loadBytes(url), onErrorType);
266         }
267         catch (JRException e)
268         {
269             return getOnErrorRenderer(onErrorType, e);
270         }
271     }
272
273
274     /**
275      *
276      */

277     public static JRRenderable getInstance(File JavaDoc file, byte onErrorType) throws JRException
278     {
279         try
280         {
281             return new JRImageRenderer(JRLoader.loadBytes(file), onErrorType);
282         }
283         catch (JRException e)
284         {
285             return getOnErrorRenderer(onErrorType, e);
286         }
287     }
288
289
290     /**
291      *
292      */

293     private static JRImageRenderer getOnErrorRenderer(byte onErrorType, JRException e) throws JRException
294     {
295         JRImageRenderer renderer = null;
296         
297         switch (onErrorType)
298         {
299             case JRImage.ON_ERROR_TYPE_ICON :
300             {
301                 renderer = new JRImageRenderer("net/sf/jasperreports/engine/images/noimage.GIF", JRImage.ON_ERROR_TYPE_ERROR);
302                 //FIXME cache these renderers
303
break;
304             }
305             case JRImage.ON_ERROR_TYPE_BLANK :
306             {
307                 renderer = new JRImageRenderer("net/sf/jasperreports/engine/images/pixel.GIF", JRImage.ON_ERROR_TYPE_ERROR);
308                 break;
309             }
310             case JRImage.ON_ERROR_TYPE_ERROR :
311             default :
312             {
313                 throw e;
314             }
315         }
316
317         return renderer;
318     }
319
320
321     /**
322      *
323      */

324     public Image JavaDoc getImage() throws JRException
325     {
326         Image JavaDoc awtImage = null;
327         if (awtImageRef == null || awtImageRef.get() == null) {
328             try
329             {
330                 awtImage = JRImageLoader.loadImage(getImageData());
331                 awtImageRef = new SoftReference JavaDoc(awtImage);
332             }
333             catch (JRException e)
334             {
335                 return getOnErrorRenderer(onErrorType, e).getImage();
336             }
337         }
338         return (Image JavaDoc) awtImageRef.get();
339     }
340
341
342     /**
343      *
344      */

345     public String JavaDoc getImageLocation()
346     {
347         return imageLocation;
348     }
349
350
351     /**
352      *
353      */

354     public byte getType()
355     {
356         return TYPE_IMAGE;
357     }
358     
359     
360     public byte getImageType() {
361         return imageType;
362     }
363     
364     
365
366     /**
367      *
368      */

369     public Dimension2D JavaDoc getDimension() throws JRException
370     {
371         Image JavaDoc img = getImage();
372         return new Dimension JavaDoc(img.getWidth(null), img.getHeight(null));
373     }
374
375
376     /**
377      *
378      */

379     public byte[] getImageData() throws JRException
380     {
381         if (imageData == null)
382         {
383             try
384             {
385                 imageData = JRLoader.loadBytesFromLocation(imageLocation);
386             }
387             catch (JRException e)
388             {
389                 imageData = getOnErrorRenderer(onErrorType, e).getImageData();
390             }
391         }
392
393         return imageData;
394     }
395
396
397     /**
398      *
399      */

400     public void render(Graphics2D JavaDoc grx, Rectangle2D JavaDoc rectanle) throws JRException
401     {
402         Image JavaDoc img = getImage();
403
404         grx.drawImage(
405             img,
406             (int)rectanle.getX(),
407             (int)rectanle.getY(),
408             (int)rectanle.getWidth(),
409             (int)rectanle.getHeight(),
410             null
411             );
412     }
413
414
415 }
416
Popular Tags