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: * ShapeFieldElementFactory.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.Color;
032: import java.awt.Stroke;
033: import java.awt.geom.Rectangle2D;
034:
035: import org.jfree.report.Element;
036: import org.jfree.report.ShapeElement;
037: import org.jfree.report.filter.templates.ShapeFieldTemplate;
038:
039: /**
040: * A factory to define Shape field elements.
041: *
042: * @author Thomas Morgner
043: */
044: public class ShapeFieldElementFactory extends ShapeElementFactory {
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: /**
055: * DefaultConstructor.
056: */
057: public ShapeFieldElementFactory() {
058: }
059:
060: /**
061: * Returns the field name from where to read the content of the element.
062: *
063: * @return the field name.
064: */
065: public String getFieldname() {
066: return fieldname;
067: }
068:
069: /**
070: * Defines the field name from where to read the content of the element. The field name
071: * is the name of a datarow column.
072: *
073: * @param fieldname the field name.
074: */
075: public void setFieldname(final String fieldname) {
076: this .fieldname = fieldname;
077: }
078:
079: /**
080: * Returns the formula that should be used to compute the value of the field.
081: * The formula must be valid according to the OpenFormula
082: * specifications.
083: *
084: * @return the formula as string.
085: */
086: public String getFormula() {
087: return formula;
088: }
089:
090: /**
091: * Assigns a formula to the element to compute the value for this element.
092: * If a formula is defined, it will override the 'field' property.
093: *
094: * @param formula the formula as a string.
095: */
096: public void setFormula(final String formula) {
097: this .formula = formula;
098: }
099:
100: /**
101: * Creates the shape field element.
102: *
103: * @return the generated shape field element
104: *
105: * @throws IllegalStateException if the fieldname is not defined.
106: * @see org.jfree.report.elementfactory.ElementFactory#createElement()
107: */
108: public Element createElement() {
109: final ShapeElement e = new ShapeElement();
110: applyStyle(e.getStyle());
111: applyElementName(e);
112:
113: final ShapeFieldTemplate template = new ShapeFieldTemplate();
114: if (getFormula() != null) {
115: template.setFormula(getFormula());
116: } else {
117: template.setField(getFieldname());
118: }
119: e.setDataSource(template);
120: return e;
121: }
122:
123: /**
124: * Creates a new ShapeElement.
125: *
126: * @param name the name of the new element.
127: * @param bounds the bounds.
128: * @param paint the line color of this element.
129: * @param stroke the stroke of this shape. For pdf use, restrict to
130: * BasicStrokes.
131: * @param fieldname the fieldname from where to get the shape.
132: * @param shouldDraw draw the shape?
133: * @param shouldFill fill the shape?
134: * @param shouldScale scale the shape?
135: * @param keepAspectRatio preserve the aspect ratio?
136: * @return a report element for drawing a line.
137: *
138: * @throws NullPointerException if bounds, name or shape are null
139: * @throws IllegalArgumentException if the given alignment is invalid
140: */
141: public static ShapeElement createShapeElement(final String name,
142: final Rectangle2D bounds, final Color paint,
143: final Stroke stroke, final String fieldname,
144: final boolean shouldDraw, final boolean shouldFill,
145: final boolean shouldScale, final boolean keepAspectRatio) {
146: final ShapeFieldElementFactory factory = new ShapeFieldElementFactory();
147: factory.setName(name);
148: factory.setX(new Float(bounds.getX()));
149: factory.setY(new Float(bounds.getY()));
150: factory.setMinimumWidth(new Float(bounds.getWidth()));
151: factory.setMinimumHeight(new Float(bounds.getHeight()));
152: factory.setColor(paint);
153: factory.setKeepAspectRatio(ElementFactory
154: .getBooleanValue(keepAspectRatio));
155: factory.setFieldname(fieldname);
156: factory.setStroke(stroke);
157: factory.setShouldDraw(ElementFactory
158: .getBooleanValue(shouldDraw));
159: factory.setShouldFill(ElementFactory
160: .getBooleanValue(shouldFill));
161: factory.setScale(ElementFactory.getBooleanValue(shouldScale));
162: return (ShapeElement) factory.createElement();
163: }
164:
165: }
|