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: * DateFieldTemplate.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.filter.templates;
030:
031: import java.text.SimpleDateFormat;
032:
033: import org.jfree.report.filter.DataRowDataSource;
034: import org.jfree.report.filter.RawDataSource;
035: import org.jfree.report.filter.SimpleDateFormatFilter;
036: import org.jfree.report.filter.StringFilter;
037: import org.jfree.report.filter.FormatSpecification;
038: import org.jfree.report.function.ExpressionRuntime;
039:
040: /**
041: * A date field template.
042: *
043: * @author Thomas Morgner
044: */
045: public class DateFieldTemplate extends AbstractTemplate implements
046: RawDataSource {
047: /**
048: * The date format filter.
049: */
050: private SimpleDateFormatFilter dateFilter;
051:
052: /**
053: * The data-row datasource.
054: */
055: private DataRowDataSource dataRowDataSource;
056:
057: /**
058: * A string filter.
059: */
060: private StringFilter stringFilter;
061:
062: /**
063: * Creates a new date field template.
064: */
065: public DateFieldTemplate() {
066: dataRowDataSource = new DataRowDataSource();
067: dateFilter = new SimpleDateFormatFilter();
068: dateFilter.setDataSource(dataRowDataSource);
069: stringFilter = new StringFilter();
070: stringFilter.setDataSource(dateFilter);
071: }
072:
073: /**
074: * Returns the date format string.
075: *
076: * @return The date format string.
077: */
078: public String getFormat() {
079: return getDateFilter().getFormatString();
080: }
081:
082: /**
083: * Sets the date format string.
084: *
085: * @param format the format string.
086: */
087: public void setFormat(final String format) {
088: getDateFilter().setFormatString(format);
089: }
090:
091: /**
092: * Returns the field name.
093: *
094: * @return The field name.
095: */
096: public String getField() {
097: return getDataRowDataSource().getDataSourceColumnName();
098: }
099:
100: /**
101: * Sets the field name.
102: *
103: * @param field the field name.
104: */
105: public void setField(final String field) {
106: getDataRowDataSource().setDataSourceColumnName(field);
107: }
108:
109: /**
110: * Returns the formula used to compute the value of the data source.
111: *
112: * @return the formula.
113: */
114: public String getFormula() {
115: return dataRowDataSource.getFormula();
116: }
117:
118: /**
119: * Defines the formula used to compute the value of this data source.
120: *
121: * @param formula the formula for the data source.
122: */
123: public void setFormula(final String formula) {
124: dataRowDataSource.setFormula(formula);
125: }
126:
127: /**
128: * Returns the string that represents <code>null</code> values.
129: *
130: * @return A string.
131: */
132: public String getNullValue() {
133: return getStringFilter().getNullValue();
134: }
135:
136: /**
137: * Sets the string that represents <code>null</code> values.
138: *
139: * @param nullValue the string that represents <code>null</code> values.
140: */
141: public void setNullValue(final String nullValue) {
142: getStringFilter().setNullValue(nullValue);
143: }
144:
145: /**
146: * Returns the date formatter.
147: *
148: * @return The date formatter.
149: */
150: public SimpleDateFormat getDateFormat() {
151: return (SimpleDateFormat) getDateFilter().getFormatter();
152: }
153:
154: /**
155: * Sets the date formatter.
156: *
157: * @param dateFormat the date formatter.
158: */
159: public void setDateFormat(final SimpleDateFormat dateFormat) {
160: getDateFilter().setFormatter(dateFormat);
161: }
162:
163: /**
164: * Returns the current value for the data source.
165: *
166: * @param runtime the expression runtime that is used to evaluate formulas and expressions when computing the value of
167: * this filter.
168: * @return the value.
169: */
170: public Object getValue(final ExpressionRuntime runtime) {
171: return getStringFilter().getValue(runtime);
172: }
173:
174: /**
175: * Clones this template.
176: *
177: * @return the clone.
178: *
179: * @throws CloneNotSupportedException this should never happen.
180: */
181: public Object clone() throws CloneNotSupportedException {
182: final DateFieldTemplate template = (DateFieldTemplate) super
183: .clone();
184: template.stringFilter = (StringFilter) stringFilter.clone();
185: template.dateFilter = (SimpleDateFormatFilter) template.stringFilter
186: .getDataSource();
187: template.dataRowDataSource = (DataRowDataSource) template.dateFilter
188: .getDataSource();
189: return template;
190: }
191:
192: /**
193: * Returns the date filter.
194: *
195: * @return The date filter.
196: */
197: protected SimpleDateFormatFilter getDateFilter() {
198: return dateFilter;
199: }
200:
201: /**
202: * Returns the data-row datasource.
203: *
204: * @return The data-row datasource.
205: */
206: protected DataRowDataSource getDataRowDataSource() {
207: return dataRowDataSource;
208: }
209:
210: /**
211: * Returns the string filter.
212: *
213: * @return The string filter.
214: */
215: protected StringFilter getStringFilter() {
216: return stringFilter;
217: }
218:
219: public Object getRawValue(final ExpressionRuntime runtime) {
220: return dateFilter.getRawValue(runtime);
221: }
222:
223: public FormatSpecification getFormatString(
224: final ExpressionRuntime runtime,
225: final FormatSpecification formatSpecification) {
226: return dateFilter.getFormatString(runtime, formatSpecification);
227: }
228: }
|