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: * DateFieldElementFactory.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.DateFormat;
034: import java.text.SimpleDateFormat;
035:
036: import org.jfree.report.Element;
037: import org.jfree.report.ElementAlignment;
038: import org.jfree.report.TextElement;
039: import org.jfree.report.filter.DataRowDataSource;
040: import org.jfree.report.filter.DataSource;
041: import org.jfree.report.filter.DateFormatFilter;
042: import org.jfree.report.filter.templates.DateFieldTemplate;
043: import org.jfree.report.style.ElementStyleKeys;
044: import org.jfree.report.style.FontDefinition;
045:
046: /**
047: * The date format factory can be used to create date/time text elements. These text
048: * elements have special abilities to format date/time values.
049: * <p/>
050: * Once the desired properties are set, the factory can be reused to create similiar text
051: * elements.
052: *
053: * @author Thomas Morgner
054: */
055: public class DateFieldElementFactory extends TextFieldElementFactory {
056: /**
057: * The date format instance used to format date values in the text element.
058: */
059: private DateFormat format;
060:
061: /** The cell format for the excel export. */
062: private String excelCellFormat;
063:
064: /**
065: * Creates a new date field element factory.
066: */
067: public DateFieldElementFactory() {
068: }
069:
070: /**
071: * Returns the excel export cell format.
072: *
073: * @return the excel cell format.
074: */
075: public String getExcelCellFormat() {
076: return excelCellFormat;
077: }
078:
079: /**
080: * Defines a special cell format that should be used when exporting the report
081: * into Excel workbooks.
082: *
083: * @param excelCellFormat the excel cell format
084: */
085: public void setExcelCellFormat(final String excelCellFormat) {
086: this .excelCellFormat = excelCellFormat;
087: }
088:
089: /**
090: * Returns the date format used for all generated text elements. The date format is
091: * shared among all generated elements.
092: *
093: * @return the date format used in this factory.
094: */
095: public DateFormat getFormat() {
096: return format;
097: }
098:
099: /**
100: * Defines the date format used for all generated text elements. The date format is
101: * shared among all generated elements.
102: *
103: * @param format the date format used in this factory.
104: */
105: public void setFormat(final DateFormat format) {
106: this .format = format;
107: }
108:
109: /**
110: * Returns the format string of the used date format. This method will return null, if
111: * the current date format is no instance of SimpleDateFormat.
112: *
113: * @return the formatstring of the date format instance.
114: */
115: public String getFormatString() {
116: if (getFormat() instanceof SimpleDateFormat) {
117: final SimpleDateFormat decFormat = (SimpleDateFormat) getFormat();
118: return decFormat.toPattern();
119: }
120: return null;
121: }
122:
123: /**
124: * Defines the format string of the used date format. This method will replace the date
125: * format instance of this factory. If the format string is null, the default format
126: * string of the current locale is used.
127: *
128: * @param formatString the formatstring of the date format instance.
129: */
130: public void setFormatString(final String formatString) {
131: if (formatString == null) {
132: setFormat(null);
133: } else {
134: setFormat(new SimpleDateFormat(formatString));
135: }
136: }
137:
138: /**
139: * Creates the date text element based on the defined settings. Undefined properties
140: * will not be set in the generated element.
141: *
142: * @return the generated date text element
143: *
144: * @see org.jfree.report.elementfactory.ElementFactory#createElement()
145: */
146: public Element createElement() {
147: final DataSource ds;
148: if (format instanceof SimpleDateFormat) {
149: final DateFieldTemplate template = new DateFieldTemplate();
150: template.setDateFormat((SimpleDateFormat) format);
151: if (getFormula() != null) {
152: template.setFormula(getFormula());
153: } else {
154: template.setField(getFieldname());
155: }
156:
157: if (getNullString() != null) {
158: template.setNullValue(getNullString());
159: }
160: ds = template;
161: } else {
162: final DateFormatFilter dataSource = new DateFormatFilter();
163: if (format != null) {
164: dataSource.setFormatter(format);
165: }
166:
167: final DataRowDataSource dds = new DataRowDataSource();
168: if (getFormula() != null) {
169: dds.setFormula(getFormula());
170: } else {
171: dds.setDataSourceColumnName(getFieldname());
172: }
173:
174: dataSource.setDataSource(dds);
175: if (getNullString() != null) {
176: dataSource.setNullValue(getNullString());
177: }
178: ds = dataSource;
179: }
180:
181: final TextElement element = new TextElement();
182: element.setDataSource(ds);
183: applyElementName(element);
184: applyStyle(element.getStyle());
185: element.getStyle().setStyleProperty(
186: ElementStyleKeys.EXCEL_DATA_FORMAT_STRING,
187: getExcelCellFormat());
188:
189: return element;
190: }
191:
192: /**
193: * Creates a new {@link TextElement} containing a date filter structure.
194: *
195: * @param name the name of the new element
196: * @param bounds the bounds of the new element
197: * @param paint the text color of this text element
198: * @param alignment the horizontal text alignment.
199: * @param font the font for this element
200: * @param nullString the text used when the value of this element is <code>null</code>
201: * @param format the SimpleDateFormat-formatstring used to format the date
202: * @param field the fieldname to retrieve values from
203: * @return a report element for displaying a java.util.Date value.
204: *
205: * @throws NullPointerException if bounds, format or field are <code>null</code>
206: * @throws IllegalArgumentException if the given alignment is invalid
207: */
208: public static TextElement createDateElement(final String name,
209: final Rectangle2D bounds, final Color paint,
210: final ElementAlignment alignment,
211: final FontDefinition font, final String nullString,
212: final String format, final String field) {
213: return createDateElement(name, bounds, paint, alignment, null,
214: font, nullString, format, field);
215: }
216:
217: /**
218: * Creates a new {@link TextElement} containing a date filter structure.
219: *
220: * @param name the name of the new element
221: * @param bounds the bounds of the new element
222: * @param paint the text color of this text element
223: * @param alignment the horizontal text alignment
224: * @param valign the vertical text alignment
225: * @param font the font for this element
226: * @param nullString the text used when the value of this element is <code>null</code>
227: * @param format the SimpleDateFormat-formatstring used to format the date
228: * @param field the fieldname to retrieve values from
229: * @return a report element for displaying a java.util.Date value.
230: *
231: * @throws NullPointerException if bounds, format or field are <code>null</code>
232: * @throws IllegalArgumentException if the given alignment is invalid
233: */
234: public static TextElement createDateElement(final String name,
235: final Rectangle2D bounds, final Color paint,
236: final ElementAlignment alignment,
237: final ElementAlignment valign, final FontDefinition font,
238: final String nullString, final String format,
239: final String field) {
240: final DateFieldElementFactory factory = new DateFieldElementFactory();
241: factory.setX(new Float(bounds.getX()));
242: factory.setY(new Float(bounds.getY()));
243: factory.setMinimumWidth(new Float(bounds.getWidth()));
244: factory.setMinimumHeight(new Float(bounds.getHeight()));
245: factory.setName(name);
246: factory.setColor(paint);
247: factory.setHorizontalAlignment(alignment);
248: factory.setVerticalAlignment(valign);
249:
250: if (font != null) {
251: factory.setFontName(font.getFontName());
252: factory.setFontSize(new Integer(font.getFontSize()));
253: factory.setBold(ElementFactory.getBooleanValue(font
254: .isBold()));
255: factory.setItalic(ElementFactory.getBooleanValue(font
256: .isItalic()));
257: factory.setEncoding(font.getFontEncoding(font
258: .getFontEncoding(null)));
259: factory.setUnderline(ElementFactory.getBooleanValue(font
260: .isUnderline()));
261: factory.setStrikethrough(ElementFactory
262: .getBooleanValue(font.isStrikeThrough()));
263: factory.setEmbedFont(ElementFactory.getBooleanValue(font
264: .isEmbeddedFont()));
265: }
266: factory.setNullString(nullString);
267: factory.setFormatString(format);
268: factory.setFieldname(field);
269: return (TextElement) factory.createElement();
270: }
271:
272: /**
273: * Creates a new {@link TextElement} containing a date filter structure.
274: *
275: * @param name the name of the new element
276: * @param bounds the bounds of the new element
277: * @param paint the text color of this text element
278: * @param alignment the horizontal text alignment
279: * @param font the font for this element
280: * @param nullString the text used when the value of this element is <code>null</code>
281: * @param format the SimpleDateFormat used to format the date
282: * @param field the fieldname to retrieve values from
283: * @return a report element for displaying a java.util.Date value.
284: *
285: * @throws NullPointerException if bounds, name, format or field are
286: * <code>null</code>
287: * @throws IllegalArgumentException if the given alignment is invalid
288: */
289: public static TextElement createDateElement(final String name,
290: final Rectangle2D bounds, final Color paint,
291: final ElementAlignment alignment,
292: final FontDefinition font, final String nullString,
293: final DateFormat format, final String field) {
294: return createDateElement(name, bounds, paint, alignment, null,
295: font, nullString, format, field);
296: }
297:
298: /**
299: * Creates a new TextElement containing a date filter structure.
300: *
301: * @param name the name of the new element
302: * @param bounds the bounds of the new element
303: * @param paint the text color of this text element
304: * @param alignment the horizontal text alignment.
305: * @param valign the vertical text alignment
306: * @param font the font for this element
307: * @param nullString the text used when the value of this element is null
308: * @param format the SimpleDateFormat used to format the date
309: * @param field the fieldname to retrieve values from
310: * @return a report element for displaying a java.util.Date value.
311: *
312: * @throws NullPointerException if bounds, name, format or field are null
313: * @throws IllegalArgumentException if the given alignment is invalid
314: */
315: public static TextElement createDateElement(final String name,
316: final Rectangle2D bounds, final Color paint,
317: final ElementAlignment alignment,
318: final ElementAlignment valign, final FontDefinition font,
319: final String nullString, final DateFormat format,
320: final String field) {
321: final DateFieldElementFactory factory = new DateFieldElementFactory();
322: factory.setX(new Float(bounds.getX()));
323: factory.setY(new Float(bounds.getY()));
324: factory.setMinimumWidth(new Float(bounds.getWidth()));
325: factory.setMinimumHeight(new Float(bounds.getHeight()));
326: factory.setName(name);
327: factory.setColor(paint);
328: factory.setHorizontalAlignment(alignment);
329: factory.setVerticalAlignment(valign);
330:
331: if (font != null) {
332: factory.setFontName(font.getFontName());
333: factory.setFontSize(new Integer(font.getFontSize()));
334: factory.setBold(ElementFactory.getBooleanValue(font
335: .isBold()));
336: factory.setItalic(ElementFactory.getBooleanValue(font
337: .isItalic()));
338: factory.setEncoding(font.getFontEncoding(font
339: .getFontEncoding(null)));
340: factory.setUnderline(ElementFactory.getBooleanValue(font
341: .isUnderline()));
342: factory.setStrikethrough(ElementFactory
343: .getBooleanValue(font.isStrikeThrough()));
344: factory.setEmbedFont(ElementFactory.getBooleanValue(font
345: .isEmbeddedFont()));
346: }
347: factory.setNullString(nullString);
348: factory.setFormat(format);
349: factory.setFieldname(field);
350: return (TextElement) factory.createElement();
351: }
352:
353: }
|