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: * TextFieldElementFactory.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.geom.Rectangle2D;
033:
034: import org.jfree.report.Element;
035: import org.jfree.report.ElementAlignment;
036: import org.jfree.report.TextElement;
037: import org.jfree.report.filter.templates.StringFieldTemplate;
038: import org.jfree.report.style.ElementStyleSheet;
039: import org.jfree.report.style.FontDefinition;
040:
041: /**
042: * A factory to define text fields. Text fields read their content from the dataRow and
043: * try to print it as plain text (using toString() if required).
044: *
045: * @author Thomas Morgner
046: */
047: public class TextFieldElementFactory extends TextElementFactory {
048: /**
049: * The fieldname of the datarow from where to read the content.
050: */
051: private String fieldname;
052:
053: /**
054: * The nullstring of the text element if the value in the datasource was null.
055: */
056: private String nullString;
057:
058: /**
059: * The value-formula that computes the value for this element.
060: */
061: private String formula;
062:
063: /**
064: * DefaultConstructor.
065: */
066: public TextFieldElementFactory() {
067: }
068:
069: /**
070: * Returns the field name from where to read the content of the element.
071: *
072: * @return the field name.
073: */
074: public String getFieldname() {
075: return fieldname;
076: }
077:
078: /**
079: * Defines the field name from where to read the content of the element. The field name
080: * is the name of a datarow column.
081: *
082: * @param fieldname the field name.
083: */
084: public void setFieldname(final String fieldname) {
085: this .fieldname = fieldname;
086: }
087:
088: /**
089: * Returns the formula that should be used to compute the value of the field.
090: * The formula must be valid according to the OpenFormula
091: * specifications.
092: *
093: * @return the formula as string.
094: */
095: public String getFormula() {
096: return formula;
097: }
098:
099: /**
100: * Assigns a formula to the element to compute the value for this element.
101: * If a formula is defined, it will override the 'field' property.
102: *
103: * @param formula the formula as a string.
104: */
105: public void setFormula(final String formula) {
106: this .formula = formula;
107: }
108:
109: /**
110: * Returns the null string for the text element. The null string is used when no content
111: * is found for that element.
112: *
113: * @return the null string.
114: */
115: public String getNullString() {
116: return nullString;
117: }
118:
119: /**
120: * Defines the null string for the text element. The null string is used when no content
121: * is found for that element. The nullstring itself can be null.
122: *
123: * @param nullString the null string.
124: */
125: public void setNullString(final String nullString) {
126: this .nullString = nullString;
127: }
128:
129: /**
130: * Creates the text field element.
131: *
132: * @return the generated text field element
133: *
134: * @throws IllegalStateException if the fieldname is null.
135: * @see org.jfree.report.elementfactory.ElementFactory#createElement()
136: */
137: public Element createElement() {
138: final StringFieldTemplate template = new StringFieldTemplate();
139: if (getFormula() != null) {
140: template.setFormula(getFormula());
141: } else {
142: template.setField(getFieldname());
143: }
144:
145: if (getNullString() != null) {
146: template.setNullValue(getNullString());
147: }
148:
149: final TextElement element = new TextElement();
150: applyElementName(element);
151: element.setDataSource(template);
152: final ElementStyleSheet style = element.getStyle();
153: applyStyle(style);
154: return element;
155: }
156:
157: /**
158: * Creates a new TextElement without any additional filtering.
159: *
160: * @param name the name of the new element
161: * @param bounds the bounds of the new element
162: * @param paint the text color of this text element
163: * @param alignment the horizontal text alignment.
164: * @param font the font for this element
165: * @param nullString the text used when the value of this element is null
166: * @param field the field in the datamodel to retrieve values from
167: * @return a report element for displaying <code>String</code> objects.
168: *
169: * @throws NullPointerException if bounds, name or function are null
170: * @throws IllegalArgumentException if the given alignment is invalid
171: */
172: public static TextElement createStringElement(final String name,
173: final Rectangle2D bounds, final Color paint,
174: final ElementAlignment alignment,
175: final FontDefinition font, final String nullString,
176: final String field) {
177: return createStringElement(name, bounds, paint, alignment,
178: ElementAlignment.TOP, font, nullString, field);
179: }
180:
181: /**
182: * Creates a new TextElement without any additional filtering.
183: *
184: * @param name the name of the new element
185: * @param bounds the bounds of the new element
186: * @param paint the text color of this text element
187: * @param alignment the horizontal text alignment.
188: * @param valign the vertical alignment.
189: * @param font the font for this element
190: * @param nullString the text used when the value of this element is null
191: * @param field the field in the datamodel to retrieve values from
192: * @return a report element for displaying <code>String</code> objects.
193: *
194: * @throws NullPointerException if bounds, name or function are null
195: * @throws IllegalArgumentException if the given alignment is invalid
196: */
197: public static TextElement createStringElement(final String name,
198: final Rectangle2D bounds, final Color paint,
199: final ElementAlignment alignment,
200: final ElementAlignment valign, final FontDefinition font,
201: final String nullString, final String field) {
202: final TextFieldElementFactory factory = new TextFieldElementFactory();
203: factory.setX(new Float(bounds.getX()));
204: factory.setY(new Float(bounds.getY()));
205: factory.setMinimumWidth(new Float(bounds.getWidth()));
206: factory.setMinimumHeight(new Float(bounds.getHeight()));
207: factory.setName(name);
208: factory.setColor(paint);
209: factory.setHorizontalAlignment(alignment);
210: factory.setVerticalAlignment(valign);
211:
212: if (font != null) {
213: factory.setFontName(font.getFontName());
214: factory.setFontSize(new Integer(font.getFontSize()));
215: factory.setBold(ElementFactory.getBooleanValue(font
216: .isBold()));
217: factory.setItalic(ElementFactory.getBooleanValue(font
218: .isItalic()));
219: factory.setEncoding(font.getFontEncoding(null));
220: factory.setUnderline(ElementFactory.getBooleanValue(font
221: .isUnderline()));
222: factory.setStrikethrough(ElementFactory
223: .getBooleanValue(font.isStrikeThrough()));
224: factory.setEmbedFont(ElementFactory.getBooleanValue(font
225: .isEmbeddedFont()));
226: }
227: factory.setFieldname(field);
228: factory.setNullString(nullString);
229: return (TextElement) factory.createElement();
230: }
231:
232: }
|