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: * ImageURLFieldElementFactory.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.elementfactory;
030:
031: import java.awt.geom.Rectangle2D;
032: import java.net.URL;
033:
034: import org.jfree.report.Element;
035: import org.jfree.report.ImageElement;
036: import org.jfree.report.filter.templates.ImageURLFieldTemplate;
037:
038: /**
039: * A factory to define ImageURLFieldElements. These elements expect an java.net.URL or an
040: * String as content and will display the image content of that URL in the report.
041: *
042: * @author Thomas Morgner
043: */
044: public class ImageURLFieldElementFactory extends ImageElementFactory {
045: /**
046: * The fieldname of the datarow from where to read the content.
047: */
048: private String fieldname;
049: /**
050: * The value-formula that computes the value for this element.
051: */
052: private String formula;
053:
054: /** The base URL is used to resolve relative URLs. */
055: private URL baseURL;
056:
057: /**
058: * DefaultConstructor.
059: */
060: public ImageURLFieldElementFactory() {
061: }
062:
063: /**
064: * Returns the field name from where to read the content of the element.
065: *
066: * @return the field name.
067: */
068: public String getFieldname() {
069: return fieldname;
070: }
071:
072: /**
073: * Defines the field name from where to read the content of the element. The field name
074: * is the name of a datarow column.
075: *
076: * @param fieldname the field name.
077: */
078: public void setFieldname(final String fieldname) {
079: this .fieldname = fieldname;
080: }
081:
082: /**
083: * Returns the formula that should be used to compute the value of the field.
084: * The formula must be valid according to the OpenFormula
085: * specifications.
086: *
087: * @return the formula as string.
088: */
089: public String getFormula() {
090: return formula;
091: }
092:
093: /**
094: * Assigns a formula to the element to compute the value for this element.
095: * If a formula is defined, it will override the 'field' property.
096: *
097: * @param formula the formula as a string.
098: */
099: public void setFormula(final String formula) {
100: this .formula = formula;
101: }
102:
103: /**
104: * Returns the base url. The BaseURL is used to resolve relative URLs found in the
105: * datasource.
106: *
107: * @return the base url.
108: */
109: public URL getBaseURL() {
110: return baseURL;
111: }
112:
113: /**
114: * Defines a BaseURL for the new element. The BaseURL is used to resolve relative URLs found in the
115: * datasource.
116: *
117: * @param baseURL the base URL.
118: */
119: public void setBaseURL(final URL baseURL) {
120: this .baseURL = baseURL;
121: }
122:
123: /**
124: * Creates the image URL field element based on the defined properties.
125: *
126: * @return the created element.
127: *
128: * @see org.jfree.report.elementfactory.ElementFactory#createElement()
129: */
130: public Element createElement() {
131: final ImageURLFieldTemplate fieldTemplate = new ImageURLFieldTemplate();
132: if (getFormula() != null) {
133: fieldTemplate.setFormula(getFormula());
134: } else {
135: fieldTemplate.setField(getFieldname());
136: }
137: fieldTemplate.setBaseURL(getBaseURL());
138: final ImageElement element = new ImageElement();
139: applyElementName(element);
140: applyStyle(element.getStyle());
141: element.setDataSource(fieldTemplate);
142:
143: return element;
144: }
145:
146: /**
147: * Creates a new ImageElement, which is fed from an URL stored in the datasource.
148: *
149: * @param name the name of the new element
150: * @param bounds the bounds of the new element
151: * @param field the name of the column/function/expression that returns the URL for the
152: * image.
153: * @return a report element for displaying an image based on a URL.
154: *
155: * @throws NullPointerException if bounds, name or source are null
156: * @throws IllegalArgumentException if the given alignment is invalid
157: */
158: public static ImageElement createImageURLElement(final String name,
159: final Rectangle2D bounds, final String field) {
160: return createImageURLElement(name, bounds, field, true);
161: }
162:
163: /**
164: * Creates a new ImageElement, which is fed from an URL stored in the datasource.
165: *
166: * @param name the name of the new element.
167: * @param bounds the bounds of the new element.
168: * @param field the name of the column/function/expression that returns the URL for the
169: * image.
170: * @param scale scale the image?
171: * @return a report element for displaying an image based on a URL.
172: *
173: * @throws NullPointerException if bounds, name or source are null
174: * @throws IllegalArgumentException if the given alignment is invalid
175: */
176: public static ImageElement createImageURLElement(final String name,
177: final Rectangle2D bounds, final String field,
178: final boolean scale) {
179: return createImageURLElement(name, bounds, field, scale, false);
180: }
181:
182: /**
183: * Creates a new ImageElement, which is fed from an URL stored in the datasource.
184: *
185: * @param name the name of the new element
186: * @param bounds the bounds of the new element
187: * @param field the name of the column/function/expression that returns the
188: * URL for the image.
189: * @param scale true if the content should be scaled to fit.
190: * @param keepAspectRatio preserve the aspect ratio.
191: * @return a report element for displaying an image based on a URL.
192: *
193: * @throws NullPointerException if bounds, name or source are null
194: * @throws IllegalArgumentException if the given alignment is invalid
195: */
196: public static ImageElement createImageURLElement(final String name,
197: final Rectangle2D bounds, final String field,
198: final boolean scale, final boolean keepAspectRatio) {
199: final ImageURLFieldElementFactory factory = new ImageURLFieldElementFactory();
200: factory.setName(name);
201: factory.setX(new Float(bounds.getX()));
202: factory.setY(new Float(bounds.getY()));
203: factory.setMinimumWidth(new Float(bounds.getWidth()));
204: factory.setMinimumHeight(new Float(bounds.getHeight()));
205: factory.setScale(ElementFactory.getBooleanValue(scale));
206: factory.setKeepAspectRatio(ElementFactory
207: .getBooleanValue(keepAspectRatio));
208: factory.setFieldname(field);
209: return (ImageElement) factory.createElement();
210: }
211: }
|