001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either 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, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * PaintDynamicComponentFunction.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.function;
030:
031: import java.awt.Color;
032: import java.awt.Component;
033: import java.awt.Dimension;
034: import java.awt.Graphics2D;
035: import java.awt.Image;
036: import java.awt.geom.AffineTransform;
037: import java.awt.geom.Rectangle2D;
038: import java.awt.image.BufferedImage;
039: import java.io.IOException;
040:
041: import org.jfree.report.DefaultImageReference;
042: import org.jfree.report.event.PageEventListener;
043: import org.jfree.report.event.ReportEvent;
044: import org.jfree.report.util.ComponentDrawable;
045: import org.jfree.report.util.ImageUtils;
046: import org.jfree.util.Configuration;
047: import org.jfree.util.Log;
048:
049: /**
050: * Paints a AWT or Swing Component. The component must be contained in the dataRow.
051: *
052: * @author Thomas Morgner
053: * @deprecated Use the new Component-Element instead. It uses drawables for this
054: * job, and therefore the result looks much better.
055: */
056: public class PaintDynamicComponentFunction extends AbstractFunction
057: implements PageEventListener {
058: /**
059: * the created image, cached for getValue().
060: */
061: private transient Image image;
062:
063: /**
064: * The field from where to read the AWT-Component.
065: */
066: private String field;
067:
068: /**
069: * The scale factor.
070: */
071: private float scale;
072:
073: /**
074: * DefaultConstructor.
075: *
076: * @throws IllegalStateException (HeadlessException) if no full AWT is available. This
077: * function needs a working layout manager.
078: */
079: public PaintDynamicComponentFunction() {
080: scale = 1;
081: }
082:
083: /**
084: * Returns the field used by the function. The field name corresponds to a column name in the report's data-row.
085: *
086: * @return The field name.
087: */
088: public String getField() {
089: return field;
090: }
091:
092: /**
093: * Sets the field name for the function. The field name corresponds to a column name in the report's data-row.
094: *
095: * @param field the field name.
096: */
097: public void setField(final String field) {
098: this .field = field;
099: }
100:
101: /**
102: * Receives notification that the report has started.
103: *
104: * @param event the event.
105: */
106: public void reportStarted(final ReportEvent event) {
107: image = null;
108: }
109:
110: /**
111: * Receives notification that report generation initializes the current run. <P> The
112: * event carries a ReportState.Started state. Use this to initialize the report.
113: *
114: * @param event The event.
115: */
116: public void reportInitialized(final ReportEvent event) {
117: image = null;
118: }
119:
120: /**
121: * Receives notification that the report has finished.
122: *
123: * @param event the event.
124: */
125: public void reportFinished(final ReportEvent event) {
126: image = null;
127: }
128:
129: /**
130: * Receives notification that a page has started.
131: *
132: * @param event the event.
133: */
134: public void pageStarted(final ReportEvent event) {
135: image = null;
136: }
137:
138: /**
139: * Receives notification that a page has ended.
140: *
141: * @param event the event.
142: */
143: public void pageFinished(final ReportEvent event) {
144: image = null;
145: }
146:
147: /**
148: * Receives notification that a group has started.
149: *
150: * @param event the event.
151: */
152: public void groupStarted(final ReportEvent event) {
153: image = null;
154: }
155:
156: /**
157: * Receives notification that a group has finished.
158: *
159: * @param event the event.
160: */
161: public void groupFinished(final ReportEvent event) {
162: image = null;
163: }
164:
165: /**
166: * Receives notification that a row of data is being processed.
167: *
168: * @param event the event.
169: */
170: public void itemsAdvanced(final ReportEvent event) {
171: image = null;
172: }
173:
174: /**
175: * Returns the device-resolution from the report-configuration.
176: *
177: * @return the resolution defined in the report-configuration.
178: */
179: private float getDeviceScale() {
180: final Configuration config = getReportConfiguration();
181: final String resolution = config
182: .getConfigProperty("org.jfree.report.layout.DeviceResolution");
183: if (resolution == null) {
184: return 1;
185: }
186: try {
187: return Float.parseFloat(resolution) / 72.0f;
188: } catch (NumberFormatException nfe) {
189: return 1;
190: }
191: }
192:
193: /**
194: * Creates the component.
195: *
196: * @return the created image or null, if no image could be created.
197: */
198: private Image createComponentImage() {
199: final Object o = getDataRow().get(getField());
200: if ((o instanceof Component) == false) {
201: return null;
202: }
203:
204: final float scale = getScale() * getDeviceScale();
205:
206: final ComponentDrawable drawable = new ComponentDrawable();
207: drawable.setComponent((Component) o);
208: drawable.setAllowOwnPeer(true);
209: drawable.setPaintSynchronized(true);
210: final Dimension dim = drawable.getSize();
211:
212: final int width = Math.max(1, (int) (scale * dim.width));
213: final int height = Math.max(1, (int) (scale * dim.height));
214:
215: final BufferedImage bi = ImageUtils.createTransparentImage(
216: width, height);
217: final Graphics2D graph = bi.createGraphics();
218: graph.setBackground(new Color(0, 0, 0, 0));
219: graph.setTransform(AffineTransform.getScaleInstance(scale,
220: scale));
221: drawable.draw(graph, new Rectangle2D.Float(0, 0, dim.width,
222: dim.height));
223: graph.dispose();
224:
225: return bi;
226: }
227:
228: /**
229: * Return the current expression value. <P> The value depends (obviously) on the
230: * expression implementation.
231: *
232: * @return the value of the function.
233: */
234: public Object getValue() {
235: if (image == null) {
236: image = createComponentImage();
237: }
238:
239: try {
240: final DefaultImageReference ref = new DefaultImageReference(
241: image);
242: ref.setScale(1.0f / getScale(), 1.0f / getScale());
243: return ref;
244: } catch (IOException e) {
245: Log
246: .warn("Unable to fully load a given image. (It should not happen here.)");
247: return null;
248: }
249: }
250:
251: /**
252: * Define a scale factor for the created image. Using a higher scale factor will produce
253: * better results. A scale factor of 2 will double the resolution. A scale factor of 1
254: * will create 72 dpi images.
255: *
256: * @param scale the scale factor.
257: */
258: public void setScale(final float scale) {
259: this .scale = scale;
260: }
261:
262: /**
263: * Gets the scale factor for the created image. Using a higher scale factor will produce
264: * better results. A scale factor of 2 will double the resolution. A scale factor of 1
265: * will create 72 dpi images.
266: *
267: * @return the scale factor.
268: */
269: public float getScale() {
270: return scale;
271: }
272: }
|