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;
029:
030: import java.awt.Dimension;
031: import java.awt.Graphics2D;
032: import java.awt.Image;
033: import java.awt.geom.Dimension2D;
034: import java.awt.geom.Rectangle2D;
035: import java.io.File;
036: import java.io.InputStream;
037: import java.lang.ref.SoftReference;
038: import java.net.URL;
039: import java.net.URLStreamHandlerFactory;
040:
041: import net.sf.jasperreports.engine.util.JRImageLoader;
042: import net.sf.jasperreports.engine.util.JRLoader;
043: import net.sf.jasperreports.engine.util.JRResourcesUtil;
044: import net.sf.jasperreports.engine.util.JRTypeSniffer;
045:
046: /**
047: * @author Teodor Danciu (teodord@users.sourceforge.net)
048: * @version $Id: JRImageRenderer.java 1811 2007-08-13 19:52:07Z teodord $
049: */
050: public class JRImageRenderer extends JRAbstractRenderer {
051:
052: /**
053: *
054: */
055: private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
056:
057: /**
058: *
059: */
060: private byte[] imageData = null;
061: private String imageLocation = null;
062: private byte imageType = IMAGE_TYPE_UNKNOWN;
063:
064: /**
065: *
066: */
067: private transient SoftReference awtImageRef = null;
068:
069: /**
070: *
071: */
072: private JRImageRenderer(byte[] imageData) {
073: this .imageData = imageData;
074:
075: if (imageData != null) {
076: imageType = JRTypeSniffer.getImageType(imageData);
077: }
078:
079: }
080:
081: /**
082: *
083: */
084: private JRImageRenderer(String imageLocation) {
085: this .imageLocation = imageLocation;
086: }
087:
088: /**
089: * @deprecated replaced by
090: * {@link JRResourcesUtil#getThreadClassLoader() JRResourcesUtil.getThreadClassLoader()}
091: */
092: public static ClassLoader getClassLoader() {
093: return JRResourcesUtil.getThreadClassLoader();
094: }
095:
096: /**
097: * @deprecated replace by
098: * {@link JRResourcesUtil#setThreadClassLoader(ClassLoader) JRResourcesUtil.setThreadClassLoader(ClassLoader)}
099: */
100: public static void setClassLoader(ClassLoader classLoader) {
101: JRResourcesUtil.setThreadClassLoader(classLoader);
102: }
103:
104: /**
105: *
106: */
107: public static JRImageRenderer getInstance(byte[] imageData) {
108: return new JRImageRenderer(imageData);
109: }
110:
111: /**
112: *
113: */
114: public static JRRenderable getInstance(String imageLocation)
115: throws JRException {
116: return getInstance(imageLocation, JRImage.ON_ERROR_TYPE_ERROR,
117: true);
118: }
119:
120: /**
121: *
122: */
123: public static JRRenderable getInstance(String imageLocation,
124: byte onErrorType) throws JRException {
125: return getInstance(imageLocation, onErrorType, true);
126: }
127:
128: /**
129: *
130: */
131: public static JRRenderable getInstance(String imageLocation,
132: byte onErrorType, boolean isLazy) throws JRException {
133: return getInstance(imageLocation, onErrorType, isLazy, null,
134: null);
135: }
136:
137: /**
138: *
139: */
140: public static JRRenderable getInstance(String imageLocation,
141: byte onErrorType, boolean isLazy, ClassLoader classLoader,
142: URLStreamHandlerFactory urlHandlerFactory)
143: throws JRException {
144: if (imageLocation == null) {
145: return null;
146: }
147:
148: if (isLazy) {
149: return new JRImageRenderer(imageLocation);
150: }
151:
152: try {
153: byte[] data = JRLoader.loadBytesFromLocation(imageLocation,
154: classLoader, urlHandlerFactory);
155: return new JRImageRenderer(data);
156: } catch (JRException e) {
157: return getOnErrorRenderer(onErrorType, e);
158: }
159: }
160:
161: /**
162: *
163: */
164: public static JRRenderable getInstance(Image img, byte onErrorType)
165: throws JRException {
166: return getInstance(img, JRRenderable.IMAGE_TYPE_JPEG,
167: onErrorType);
168: }
169:
170: /**
171: * Creates and returns an instance of the JRImageRenderer class after encoding the image object using an image
172: * encoder that supports the supplied image type.
173: *
174: * @param image the java.awt.Image object to wrap into a JRImageRenderer instance
175: * @param imageType the type of the image as specified by one of the constants defined in the JRRenderable interface
176: * @param onErrorType one of the error type constants defined in the JRImage interface
177: * @return the image renderer instance
178: */
179: public static JRRenderable getInstance(Image image, byte imageType,
180: byte onErrorType) throws JRException {
181: try {
182: return new JRImageRenderer(JRImageLoader
183: .loadImageDataFromAWTImage(image, imageType));
184: } catch (JRException e) {
185: return getOnErrorRenderer(onErrorType, e);
186: }
187: }
188:
189: /**
190: *
191: */
192: public static JRRenderable getInstance(InputStream is,
193: byte onErrorType) throws JRException {
194: try {
195: return new JRImageRenderer(JRLoader.loadBytes(is));
196: } catch (JRException e) {
197: return getOnErrorRenderer(onErrorType, e);
198: }
199: }
200:
201: /**
202: *
203: */
204: public static JRRenderable getInstance(URL url, byte onErrorType)
205: throws JRException {
206: try {
207: return new JRImageRenderer(JRLoader.loadBytes(url));
208: } catch (JRException e) {
209: return getOnErrorRenderer(onErrorType, e);
210: }
211: }
212:
213: /**
214: *
215: */
216: public static JRRenderable getInstance(File file, byte onErrorType)
217: throws JRException {
218: try {
219: return new JRImageRenderer(JRLoader.loadBytes(file));
220: } catch (JRException e) {
221: return getOnErrorRenderer(onErrorType, e);
222: }
223: }
224:
225: /**
226: *
227: */
228: public static JRRenderable getOnErrorRendererForDimension(
229: JRRenderable renderer, byte onErrorType) throws JRException {
230: try {
231: renderer.getDimension();
232: return renderer;
233: } catch (JRException e) {
234: return getOnErrorRenderer(onErrorType, e);
235: }
236: }
237:
238: /**
239: *
240: */
241: public static JRRenderable getOnErrorRendererForImageData(
242: JRRenderable renderer, byte onErrorType) throws JRException {
243: try {
244: renderer.getImageData();
245: return renderer;
246: } catch (JRException e) {
247: return getOnErrorRenderer(onErrorType, e);
248: }
249: }
250:
251: /**
252: *
253: */
254: public static JRImageRenderer getOnErrorRendererForImage(
255: JRImageRenderer renderer, byte onErrorType)
256: throws JRException {
257: try {
258: renderer.getImage();
259: return renderer;
260: } catch (JRException e) {
261: return getOnErrorRenderer(onErrorType, e);
262: }
263: }
264:
265: /**
266: *
267: */
268: private static JRImageRenderer getOnErrorRenderer(byte onErrorType,
269: JRException e) throws JRException {
270: JRImageRenderer renderer = null;
271:
272: switch (onErrorType) {
273: case JRImage.ON_ERROR_TYPE_ICON: {
274: renderer = new JRImageRenderer(
275: "net/sf/jasperreports/engine/images/noimage.GIF");
276: //FIXME cache these renderers
277: break;
278: }
279: case JRImage.ON_ERROR_TYPE_BLANK: {
280: break;
281: }
282: case JRImage.ON_ERROR_TYPE_ERROR:
283: default: {
284: throw e;
285: }
286: }
287:
288: return renderer;
289: }
290:
291: /**
292: *
293: */
294: public Image getImage() throws JRException {
295: if (awtImageRef == null || awtImageRef.get() == null) {
296: Image awtImage = JRImageLoader.loadImage(getImageData());
297: awtImageRef = new SoftReference(awtImage);
298: }
299: return (Image) awtImageRef.get();
300: }
301:
302: /**
303: *
304: */
305: public String getImageLocation() {
306: return imageLocation;
307: }
308:
309: /**
310: *
311: */
312: public byte getType() {
313: return TYPE_IMAGE;
314: }
315:
316: public byte getImageType() {
317: return imageType;
318: }
319:
320: /**
321: *
322: */
323: public Dimension2D getDimension() throws JRException {
324: Image img = getImage();
325: return new Dimension(img.getWidth(null), img.getHeight(null));
326: }
327:
328: /**
329: *
330: */
331: public byte[] getImageData() throws JRException {
332: if (imageData == null) {
333: imageData = JRLoader.loadBytesFromLocation(imageLocation);
334: }
335:
336: return imageData;
337: }
338:
339: /**
340: *
341: */
342: public void render(Graphics2D grx, Rectangle2D rectanle)
343: throws JRException {
344: Image img = getImage();
345:
346: grx.drawImage(img, (int) rectanle.getX(),
347: (int) rectanle.getY(), (int) rectanle.getWidth(),
348: (int) rectanle.getHeight(), null);
349: }
350:
351: }
|