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: * PaintComponentFunction.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.BorderLayout;
032: import java.awt.Frame;
033: import java.io.IOException;
034: import java.io.ObjectInputStream;
035:
036: import org.jfree.report.JFreeReportBoot;
037: import org.jfree.report.event.LayoutEvent;
038: import org.jfree.report.event.LayoutListener;
039: import org.jfree.util.Log;
040:
041: /**
042: * Paints a AWT or Swing Component, fitting the component into the element bounds. The component must be contained in
043: * the dataRow.
044: * <p/>
045: * In an headless environment this function wont work and will always return null.
046: *
047: * @author Thomas Morgner
048: * @deprecated Use the new Component-Element instead. It uses drawables for this job, and therefore the result looks
049: * much better. This method does no longer work, as it depended on implementation details that are no longer
050: * in use.
051: */
052: public class PaintComponentFunction extends AbstractFunction implements
053: LayoutListener {
054: /**
055: * Supplies a valid AWT-peer for the draw operation.
056: */
057: private transient Frame peerSupply;
058: /**
059: * The name of the report element that should receive the image.
060: */
061: private String element;
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: public PaintComponentFunction() {
077: if (isHeadless() == false) {
078: peerSupply = new Frame();
079: peerSupply.setLayout(new BorderLayout());
080: }
081: this .scale = 1;
082: }
083:
084: /**
085: * Returns the element used by the function. <P> The element name corresponds to a element in the report. The element
086: * name must be unique, as the first occurence of the element is used.
087: *
088: * @return The field name.
089: */
090: public String getElement() {
091: return element;
092: }
093:
094: /**
095: * Sets the element name for the function. <P> The element name corresponds to a element in the report. The element
096: * name must be unique, as the first occurence of the element is used.
097: *
098: * @param field the field name (null not permitted).
099: */
100: public void setElement(final String field) {
101: if (field == null) {
102: throw new NullPointerException();
103: }
104: this .element = field;
105: }
106:
107: /**
108: * Returns the field used by the function. The field name corresponds to a column name in the report's data-row.
109: *
110: * @return The field name.
111: */
112: public String getField() {
113: return field;
114: }
115:
116: /**
117: * Sets the field name for the function. The field name corresponds to a column name in the report's data-row.
118: *
119: * @param field the field name.
120: */
121: public void setField(final String field) {
122: this .field = field;
123: }
124:
125: /**
126: * Tests, whether the report generation is executed in an headless environment.
127: *
128: * @return true, if this is an headless environment, false otherwise.
129: */
130: protected static boolean isHeadless() {
131: return "true".equals(JFreeReportBoot.getInstance()
132: .getGlobalConfig().getConfigProperty(
133: "java.awt.headless", "false"));
134: }
135:
136: /**
137: * Receives notification that the band layouting has completed. <P> The event carries the current report state.
138: *
139: * @param event The event.
140: */
141: public void layoutComplete(final LayoutEvent event) {
142: if (isHeadless()) {
143: return;
144: }
145: Log
146: .error("The PaintComponentFunction is no longer valid. Use a component-field instead.");
147: }
148:
149: /**
150: * Return the current expression value. <P> The value depends (obviously) on the expression implementation.
151: *
152: * @return the value of the function.
153: */
154: public Object getValue() {
155: return null;
156: }
157:
158: /**
159: * Define a scale factor for the created image. Using a higher scale factor will produce better results. A scale
160: * factor of 2 will double the resolution. A scale factor of 1 will create 72 dpi images.
161: *
162: * @param scale the scale factor.
163: */
164: public void setScale(final float scale) {
165: this .scale = scale;
166: }
167:
168: /**
169: * Gets the scale factor for the created image. Using a higher scale factor will produce better results. A scale
170: * factor of 2 will double the resolution. A scale factor of 1 will create 72 dpi images.
171: *
172: * @return the scale factor.
173: */
174: public float getScale() {
175: return scale;
176: }
177:
178: /**
179: * Return a completly separated copy of this function. The copy does no longer share any changeable objects with the
180: * original function.
181: *
182: * @return a copy of this function.
183: */
184: public Expression getInstance() {
185: final PaintComponentFunction pc = (PaintComponentFunction) super
186: .getInstance();
187: if (isHeadless() == false) {
188: pc.peerSupply = new Frame();
189: pc.peerSupply.setLayout(new BorderLayout());
190: }
191: return pc;
192: }
193:
194: /**
195: * Helper method for serialization.
196: *
197: * @param in the input stream from where to read the serialized object.
198: * @throws IOException when reading the stream fails.
199: * @throws ClassNotFoundException if a class definition for a serialized object could not be found.
200: */
201: private void readObject(final ObjectInputStream in)
202: throws IOException, ClassNotFoundException {
203: in.defaultReadObject();
204: if (isHeadless() == false) {
205: peerSupply = new Frame();
206: peerSupply.setLayout(new BorderLayout());
207: }
208: }
209:
210: /**
211: * Not used.
212: * @param event not used.
213: */
214: public void outputComplete(final LayoutEvent event) {
215: }
216: }
|