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: * ComponentDrawableFilter.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.filter;
030:
031: import java.awt.Component;
032: import javax.swing.JFrame;
033:
034: import org.jfree.report.JFreeReportBoot;
035: import org.jfree.report.function.ExpressionRuntime;
036: import org.jfree.report.util.ComponentDrawable;
037: import org.jfree.util.Configuration;
038:
039: /**
040: * A filter that wraps AWT- and Swing-components into a Drawable implementation.
041: *
042: * @author Thomas Morgner
043: */
044: public class ComponentDrawableFilter implements DataFilter {
045: /** The datasource from where to read the urls. */
046: private DataSource source;
047: /** The Window-Peer used for the draw operation. */
048: private JFrame frame;
049:
050: /**
051: * Default constructor.
052: */
053: public ComponentDrawableFilter() {
054: }
055:
056: /**
057: * Returns the ComponentDrawable for the AWT-Component or null.
058: *
059: * @param runtime the expression runtime that is used to evaluate formulas and expressions when computing the value of
060: * this filter.
061: * @return the value.
062: */
063: public Object getValue(final ExpressionRuntime runtime) {
064: if (isHeadless()) {
065: return null;
066: }
067:
068: if (runtime == null) {
069: return null;
070: }
071:
072: final DataSource ds = getDataSource();
073: if (ds == null) {
074: return null;
075: }
076: final Object o = ds.getValue(runtime);
077: if (o == null) {
078: return null;
079: }
080:
081: if (o instanceof Component == false) {
082: return null;
083: }
084:
085: final Configuration config = runtime.getConfiguration();
086: final ComponentDrawable cd;
087: final String drawMode = config.getConfigProperty(
088: "org.jfree.report.ComponentDrawableMode", "shared");
089: if ("private".equals(drawMode)) {
090: cd = new ComponentDrawable();
091: } else if ("synchronized".equals(drawMode)) {
092: cd = new ComponentDrawable();
093: cd.setPaintSynchronized(true);
094: } else {
095: if (frame == null) {
096: frame = new JFrame();
097: }
098: cd = new ComponentDrawable(frame);
099: cd.setPaintSynchronized(true);
100: }
101:
102: final String allowOwnPeer = config
103: .getConfigProperty("org.jfree.report.AllowOwnPeerForComponentDrawable");
104: cd.setAllowOwnPeer("true".equals(allowOwnPeer));
105: cd.setComponent((Component) o);
106: return cd;
107: }
108:
109: /**
110: * A helper method that queries the configuration (and therefore also the system properties) for whether the
111: * system is in headless mode.
112: *
113: * @return true, if the system is headless, false otherwise.
114: */
115: protected static boolean isHeadless() {
116: final Configuration config = JFreeReportBoot.getInstance()
117: .getGlobalConfig();
118: return "true".equals(config.getConfigProperty(
119: "java.awt.headless", "false"));
120: }
121:
122: /**
123: * Creates a clone of this filter.
124: *
125: * @return the clone.
126: * @throws CloneNotSupportedException if an error occured.
127: */
128: public Object clone() throws CloneNotSupportedException {
129: final ComponentDrawableFilter il = (ComponentDrawableFilter) super
130: .clone();
131: if (source != null) {
132: il.source = (DataSource) source.clone();
133: }
134: return il;
135: }
136:
137: /**
138: * Returns the assigned DataSource for this Target.
139: *
140: * @return The datasource.
141: */
142: public DataSource getDataSource() {
143: return source;
144: }
145:
146: /**
147: * Assigns a DataSource for this Target.
148: *
149: * @param ds The data source.
150: */
151: public void setDataSource(final DataSource ds) {
152: this.source = ds;
153: }
154:
155: }
|