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: * NumberFieldTemplate.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.DecimalFormat;
032:
033: import org.jfree.report.filter.DataRowDataSource;
034: import org.jfree.report.filter.DecimalFormatFilter;
035: import org.jfree.report.filter.RawDataSource;
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 number field template.
042: *
043: * @author Thomas Morgner
044: */
045: public class NumberFieldTemplate extends AbstractTemplate implements
046: RawDataSource {
047: /**
048: * A decimal format filter.
049: */
050: private DecimalFormatFilter decimalFormatFilter;
051:
052: /**
053: * A data-row accessor.
054: */
055: private DataRowDataSource dataRowDataSource;
056:
057: /**
058: * A string filter.
059: */
060: private StringFilter stringFilter;
061:
062: /**
063: * Creates a new number field template.
064: */
065: public NumberFieldTemplate() {
066: dataRowDataSource = new DataRowDataSource();
067: decimalFormatFilter = new DecimalFormatFilter();
068: decimalFormatFilter.setDataSource(dataRowDataSource);
069: stringFilter = new StringFilter();
070: stringFilter.setDataSource(decimalFormatFilter);
071: }
072:
073: /**
074: * Returns the number formatter.
075: *
076: * @return The number formatter.
077: */
078: public DecimalFormat getDecimalFormat() {
079: return (DecimalFormat) decimalFormatFilter.getFormatter();
080: }
081:
082: /**
083: * Sets the number formatter.
084: *
085: * @param decimalFormat the number formatter.
086: */
087: public void setDecimalFormat(final DecimalFormat decimalFormat) {
088: decimalFormatFilter.setFormatter(decimalFormat);
089: }
090:
091: /**
092: * Returns the format string.
093: *
094: * @return The format string.
095: */
096: public String getFormat() {
097: return decimalFormatFilter.getFormatString();
098: }
099:
100: /**
101: * Sets the format string.
102: *
103: * @param format the format string.
104: */
105: public void setFormat(final String format) {
106: decimalFormatFilter.setFormatString(format);
107: }
108:
109: /**
110: * Returns the field name.
111: *
112: * @return The field name.
113: */
114: public String getField() {
115: return dataRowDataSource.getDataSourceColumnName();
116: }
117:
118: /**
119: * Sets the field name.
120: *
121: * @param field the field name.
122: */
123: public void setField(final String field) {
124: dataRowDataSource.setDataSourceColumnName(field);
125: }
126:
127: /**
128: * Returns the formula used to compute the value of the data source.
129: *
130: * @return the formula.
131: */
132: public String getFormula() {
133: return dataRowDataSource.getFormula();
134: }
135:
136: /**
137: * Defines the formula used to compute the value of this data source.
138: *
139: * @param formula the formula for the data source.
140: */
141: public void setFormula(final String formula) {
142: dataRowDataSource.setFormula(formula);
143: }
144:
145: /**
146: * Returns the string that represents a <code>null</code> value.
147: *
148: * @return A string.
149: */
150: public String getNullValue() {
151: return stringFilter.getNullValue();
152: }
153:
154: /**
155: * Sets the string that represents a <code>null</code> value.
156: *
157: * @param nullValue the string that represents a <code>null</code> value.
158: */
159: public void setNullValue(final String nullValue) {
160: stringFilter.setNullValue(nullValue);
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 stringFilter.getValue(runtime);
172: }
173:
174: /**
175: * Clones the template.
176: *
177: * @return the clone.
178: *
179: * @throws CloneNotSupportedException this should never happen.
180: */
181: public Object clone() throws CloneNotSupportedException {
182: final NumberFieldTemplate template = (NumberFieldTemplate) super
183: .clone();
184: template.stringFilter = (StringFilter) stringFilter.clone();
185: template.decimalFormatFilter = (DecimalFormatFilter) template.stringFilter
186: .getDataSource();
187: template.dataRowDataSource = (DataRowDataSource) template.decimalFormatFilter
188: .getDataSource();
189: return template;
190: }
191:
192: /**
193: * Returns the datarow data source used in this template.
194: *
195: * @return the datarow data source.
196: */
197: protected DataRowDataSource getDataRowDataSource() {
198: return dataRowDataSource;
199: }
200:
201: public Object getRawValue(final ExpressionRuntime runtime) {
202: return decimalFormatFilter.getRawValue(runtime);
203: }
204:
205: public FormatSpecification getFormatString(
206: final ExpressionRuntime runtime,
207: final FormatSpecification formatSpecification) {
208: return decimalFormatFilter.getFormatString(runtime,
209: formatSpecification);
210: }
211: }
|