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: * MessageFieldElementFactory.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: import java.text.MessageFormat;
034:
035: import org.jfree.report.Element;
036: import org.jfree.report.ElementAlignment;
037: import org.jfree.report.TextElement;
038: import org.jfree.report.filter.templates.MessageFieldTemplate;
039: import org.jfree.report.style.FontDefinition;
040:
041: /**
042: * The message format factory can be used to create formatted text elements using the
043: * format defined for {@link MessageFormat}. These text elements have special abilities to
044: * format numeric values and dates based on the MessageFormat string.
045: * <p/>
046: * Once the desired properties are set, the factory can be reused to create similiar text
047: * elements.
048: *
049: * @author Jörg Schaible
050: */
051: public class MessageFieldElementFactory extends TextElementFactory {
052: /**
053: * The message format instance used to format the text element.
054: */
055: private String formatString;
056: /**
057: * The nullstring of the text element if the value in the datasource was null.
058: */
059: private String nullString;
060:
061: /**
062: * Creates a new message field element factory.
063: */
064: public MessageFieldElementFactory() {
065: }
066:
067: /**
068: * Returns the format string of the used message format.
069: *
070: * @return the formatstring of the number format instance.
071: */
072: public String getFormatString() {
073: return formatString;
074: }
075:
076: /**
077: * Defines the format string of the used message format. The format string should
078: * contain a format for the element 0. This method will replace the message format
079: * instance of this factory.
080: *
081: * @param formatString the formatstring of the message format instance.
082: */
083: public void setFormatString(final String formatString) {
084: this .formatString = formatString;
085: }
086:
087: /**
088: * Returns the null string for the text element. The null string is used when no content
089: * is found for that element.
090: *
091: * @return the null string.
092: */
093: public String getNullString() {
094: return nullString;
095: }
096:
097: /**
098: * Defines the null string for the text element. The null string is used when no content
099: * is found for that element. The nullstring itself can be null.
100: *
101: * @param nullString the null string.
102: */
103: public void setNullString(final String nullString) {
104: this .nullString = nullString;
105: }
106:
107: /**
108: * Creates the message text element based on the defined settings. Undefined properties
109: * will not be set in the generated element.
110: *
111: * @return the generated numberic text element
112: *
113: * @see org.jfree.report.elementfactory.ElementFactory#createElement()
114: */
115: public Element createElement() {
116: final MessageFieldTemplate messageFieldTemplate = new MessageFieldTemplate();
117: messageFieldTemplate.setFormat(getFormatString());
118: messageFieldTemplate.setNullValue(getNullString());
119:
120: final TextElement element = new TextElement();
121: applyElementName(element);
122: element.setDataSource(messageFieldTemplate);
123: applyStyle(element.getStyle());
124:
125: return element;
126: }
127:
128: /**
129: * Creates a new TextElement containing a message filter structure.
130: *
131: * @param name the name of the new element
132: * @param bounds the bounds of the new element
133: * @param paint the text color of this text element
134: * @param alignment the horizontal text alignment.
135: * @param font the font for this element
136: * @param nullString the text used when the value of this element is null
137: * @param format the format string used in this message element
138: * @return a report element for displaying <code>Number</code> objects.
139: *
140: * @throws NullPointerException if bounds, name or function are null
141: * @throws IllegalArgumentException if the given alignment is invalid
142: */
143: public static TextElement createMessageElement(final String name,
144: final Rectangle2D bounds, final Color paint,
145: final ElementAlignment alignment,
146: final FontDefinition font, final String nullString,
147: final String format) {
148: return createMessageElement(name, bounds, paint, alignment,
149: ElementAlignment.TOP, font, nullString, format);
150: }
151:
152: /**
153: * Creates a new TextElement containing a message filter structure.
154: *
155: * @param name the name of the new element.
156: * @param bounds the bounds of the new element.
157: * @param color the text color of this text element.
158: * @param alignment the horizontal text alignment.
159: * @param valign the vertical alignment.
160: * @param font the font for this element.
161: * @param nullString the text used when the value of this element is null.
162: * @param formatString the MessageFormat used in this number element.
163: * @return a report element for displaying <code>Number</code> objects.
164: *
165: * @throws NullPointerException if bounds, name or function are null
166: * @throws IllegalArgumentException if the given alignment is invalid
167: */
168: public static TextElement createMessageElement(final String name,
169: final Rectangle2D bounds, final Color color,
170: final ElementAlignment alignment,
171: final ElementAlignment valign, final FontDefinition font,
172: final String nullString, final String formatString) {
173:
174: final MessageFieldElementFactory factory = new MessageFieldElementFactory();
175: factory.setX(new Float(bounds.getX()));
176: factory.setY(new Float(bounds.getY()));
177: factory.setMinimumWidth(new Float(bounds.getWidth()));
178: factory.setMinimumHeight(new Float(bounds.getHeight()));
179: factory.setName(name);
180: factory.setColor(color);
181: factory.setHorizontalAlignment(alignment);
182: factory.setVerticalAlignment(valign);
183:
184: if (font != null) {
185: factory.setFontName(font.getFontName());
186: factory.setFontSize(new Integer(font.getFontSize()));
187: factory.setBold(ElementFactory.getBooleanValue(font
188: .isBold()));
189: factory.setItalic(ElementFactory.getBooleanValue(font
190: .isItalic()));
191: factory.setEncoding(font.getFontEncoding(null));
192: factory.setUnderline(ElementFactory.getBooleanValue(font
193: .isUnderline()));
194: factory.setStrikethrough(ElementFactory
195: .getBooleanValue(font.isStrikeThrough()));
196: factory.setEmbedFont(ElementFactory.getBooleanValue(font
197: .isEmbeddedFont()));
198: }
199: factory.setNullString(nullString);
200: factory.setFormatString(formatString);
201: return (TextElement) factory.createElement();
202: }
203: }
|