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: * ResourceMessageElementFactory.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.ResourceMessageTemplate;
038: import org.jfree.report.style.FontDefinition;
039:
040: /**
041: * A factory to define translateable LabelElements. LabelElements are considered immutable
042: * and should not be modified once they are created. The label expects plain text. The
043: * content of the label will be translated using an assigned resource bundle.
044: *
045: * @author Thomas Morgner
046: */
047: public class ResourceMessageElementFactory extends TextElementFactory {
048: /**
049: * The resource base from which to read the translations.
050: */
051: private String resourceBase;
052:
053: /**
054: * The nullstring of the text element if the translation was not found.
055: */
056: private String nullString;
057: /**
058: * The resource key which is used to retrieve the translation.
059: */
060: private String formatKey;
061:
062: /**
063: * DefaultConstructor.
064: */
065: public ResourceMessageElementFactory() {
066: }
067:
068: /**
069: * Returns the base name of the resource bundle used to translate the content later.
070: *
071: * @return the resource bundle name of the element.
072: */
073: public String getResourceBase() {
074: return resourceBase;
075: }
076:
077: /**
078: * Defines the base name of the resource bundle used to translate the content later.
079: *
080: * @param resourceBase the resource bundle name of the element.
081: */
082: public void setResourceBase(final String resourceBase) {
083: this .resourceBase = resourceBase;
084: }
085:
086: /**
087: * Returns the null string for the text element. The null string is used when no content
088: * is found for that element.
089: *
090: * @return the null string.
091: */
092: public String getNullString() {
093: return nullString;
094: }
095:
096: /**
097: * Defines the null string for the text element. The null string is used when no content
098: * is found for that element. The nullstring itself can be null.
099: *
100: * @param nullString the null string.
101: */
102: public void setNullString(final String nullString) {
103: this .nullString = nullString;
104: }
105:
106: /**
107: * Returns the resource key that contains the label text.
108: *
109: * @return the label resource bundle key.
110: */
111: public String getFormatKey() {
112: return formatKey;
113: }
114:
115: /**
116: * Defines the resource key, which will be used to read the translated label text.
117: *
118: * @param formatKey the resource bundle key.
119: */
120: public void setFormatKey(final String formatKey) {
121: this .formatKey = formatKey;
122: }
123:
124: /**
125: * Generates the element based on the defined properties.
126: *
127: * @return the generated element.
128: *
129: * @throws NullPointerException if the resource class name is null.
130: * @throws IllegalStateException if the resource key is not defined.
131: * @see ElementFactory#createElement()
132: */
133: public Element createElement() {
134: if (getFormatKey() == null) {
135: throw new IllegalStateException("ResourceKey is not set.");
136: }
137:
138: final ResourceMessageTemplate template = new ResourceMessageTemplate();
139: template.setResourceIdentifier(getResourceBase());
140: template.setFormatKey(getFormatKey());
141: template.setNullValue(getNullString());
142:
143: final TextElement element = new TextElement();
144: applyElementName(element);
145: element.setDataSource(template);
146: applyStyle(element.getStyle());
147: return element;
148: }
149:
150: /**
151: * Creates a ResourceElement. ResourceElements resolve their value using a
152: * <code>java.util.ResourceBundle</code>.
153: *
154: * @param name the name of the new element.
155: * @param bounds the bounds of the new element.
156: * @param paint the text color of this text element.
157: * @param alignment the horizontal alignment.
158: * @param valign the vertical alignment.
159: * @param font the font for this element.
160: * @param resourceKey the key which is used to query the resource bundle
161: * @param resourceBase the classname/basename of the assigned resource bundle
162: * @param nullValue the null string of the text element (can be null).
163: * @return the created ResourceElement
164: */
165: public static TextElement createResourceLabel(final String name,
166: final Rectangle2D bounds, final Color paint,
167: final ElementAlignment alignment,
168: final ElementAlignment valign, final FontDefinition font,
169: final String nullValue, final String resourceBase,
170: final String resourceKey) {
171: final ResourceMessageElementFactory factory = new ResourceMessageElementFactory();
172: factory.setX(new Float(bounds.getX()));
173: factory.setY(new Float(bounds.getY()));
174: factory.setMinimumWidth(new Float(bounds.getWidth()));
175: factory.setMinimumHeight(new Float(bounds.getHeight()));
176: factory.setName(name);
177: factory.setColor(paint);
178: factory.setHorizontalAlignment(alignment);
179: factory.setVerticalAlignment(valign);
180:
181: if (font != null) {
182: factory.setFontName(font.getFontName());
183: factory.setFontSize(new Integer(font.getFontSize()));
184: factory.setBold(new Boolean(font.isBold()));
185: factory.setItalic(new Boolean(font.isItalic()));
186: factory.setEncoding(font.getFontEncoding(null));
187: factory.setUnderline(new Boolean(font.isUnderline()));
188: factory
189: .setStrikethrough(new Boolean(font
190: .isStrikeThrough()));
191: factory.setEmbedFont(new Boolean(font.isEmbeddedFont()));
192: }
193: factory.setNullString(nullValue);
194: factory.setResourceBase(resourceBase);
195: factory.setFormatKey(resourceKey);
196: return (TextElement) factory.createElement();
197: }
198:
199: }
|