1 /**
2 * =========================================================
3 * Pentaho-Reporting-Classic : a free Java reporting library
4 * =========================================================
5 *
6 * Project Info: http://reporting.pentaho.org/
7 *
8 * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
9 *
10 * This library is free software; you can redistribute it and/or modify it under the terms
11 * of the GNU Lesser General Public License as published by the Free Software Foundation;
12 * either version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License along with this
19 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
21 *
22 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
23 * in the United States and other countries.]
24 *
25 * ------------
26 * NumberFieldTemplate.java
27 * ------------
28 * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
29 */
30
31 package org.jfree.report.filter.templates;
32
33 import java.text.DecimalFormat;
34
35 import org.jfree.report.Element;
36 import org.jfree.report.filter.DataRowDataSource;
37 import org.jfree.report.filter.DecimalFormatFilter;
38 import org.jfree.report.filter.FormatSpecification;
39 import org.jfree.report.filter.RawDataSource;
40 import org.jfree.report.filter.StringFilter;
41 import org.jfree.report.function.ExpressionRuntime;
42
43 /**
44 * A number field template.
45 *
46 * @author Thomas Morgner
47 */
48 public class NumberFieldTemplate extends AbstractTemplate
49 implements RawDataSource
50 {
51 /**
52 * A decimal format filter.
53 */
54 private DecimalFormatFilter decimalFormatFilter;
55
56 /**
57 * A data-row accessor.
58 */
59 private DataRowDataSource dataRowDataSource;
60
61 /**
62 * A string filter.
63 */
64 private StringFilter stringFilter;
65
66 /**
67 * Creates a new number field template.
68 */
69 public NumberFieldTemplate ()
70 {
71 dataRowDataSource = new DataRowDataSource();
72 decimalFormatFilter = new DecimalFormatFilter();
73 decimalFormatFilter.setDataSource(dataRowDataSource);
74 stringFilter = new StringFilter();
75 stringFilter.setDataSource(decimalFormatFilter);
76 }
77
78 /**
79 * Returns the number formatter.
80 *
81 * @return The number formatter.
82 */
83 public DecimalFormat getDecimalFormat ()
84 {
85 return (DecimalFormat) decimalFormatFilter.getFormatter();
86 }
87
88 /**
89 * Sets the number formatter.
90 *
91 * @param decimalFormat the number formatter.
92 */
93 public void setDecimalFormat (final DecimalFormat decimalFormat)
94 {
95 decimalFormatFilter.setFormatter(decimalFormat);
96 }
97
98 /**
99 * Returns the format string.
100 *
101 * @return The format string.
102 */
103 public String getFormat ()
104 {
105 return decimalFormatFilter.getFormatString();
106 }
107
108 /**
109 * Sets the format string.
110 *
111 * @param format the format string.
112 */
113 public void setFormat (final String format)
114 {
115 decimalFormatFilter.setFormatString(format);
116 }
117
118 /**
119 * Returns the field name.
120 *
121 * @return The field name.
122 */
123 public String getField ()
124 {
125 return dataRowDataSource.getDataSourceColumnName();
126 }
127
128 /**
129 * Sets the field name.
130 *
131 * @param field the field name.
132 */
133 public void setField (final String field)
134 {
135 dataRowDataSource.setDataSourceColumnName(field);
136 }
137
138 /**
139 * Returns the formula used to compute the value of the data source.
140 *
141 * @return the formula.
142 */
143 public String getFormula()
144 {
145 return dataRowDataSource.getFormula();
146 }
147
148 /**
149 * Defines the formula used to compute the value of this data source.
150 *
151 * @param formula the formula for the data source.
152 */
153 public void setFormula(final String formula)
154 {
155 dataRowDataSource.setFormula(formula);
156 }
157
158 /**
159 * Returns the string that represents a <code>null</code> value.
160 *
161 * @return A string.
162 */
163 public String getNullValue ()
164 {
165 return stringFilter.getNullValue();
166 }
167
168 /**
169 * Sets the string that represents a <code>null</code> value.
170 *
171 * @param nullValue the string that represents a <code>null</code> value.
172 */
173 public void setNullValue (final String nullValue)
174 {
175 stringFilter.setNullValue(nullValue);
176 }
177
178 /**
179 * Returns the current value for the data source.
180 *
181 * @param runtime the expression runtime that is used to evaluate formulas and expressions when computing the value of
182 * this filter.
183 * @param element
184 * @return the value.
185 */
186 public Object getValue(final ExpressionRuntime runtime, final Element element)
187 {
188 return stringFilter.getValue(runtime, element);
189 }
190
191 /**
192 * Clones the template.
193 *
194 * @return the clone.
195 *
196 * @throws CloneNotSupportedException this should never happen.
197 */
198 public Object clone ()
199 throws CloneNotSupportedException
200 {
201 final NumberFieldTemplate template = (NumberFieldTemplate) super.clone();
202 template.stringFilter = (StringFilter) stringFilter.clone();
203 template.decimalFormatFilter = (DecimalFormatFilter) template.stringFilter.getDataSource();
204 template.dataRowDataSource = (DataRowDataSource) template.decimalFormatFilter.getDataSource();
205 return template;
206 }
207
208 /**
209 * Returns the datarow data source used in this template.
210 *
211 * @return the datarow data source.
212 */
213 protected DataRowDataSource getDataRowDataSource ()
214 {
215 return dataRowDataSource;
216 }
217
218 public Object getRawValue(final ExpressionRuntime runtime, final Element element)
219 {
220 return decimalFormatFilter.getRawValue(runtime, element);
221 }
222
223 public FormatSpecification getFormatString(final ExpressionRuntime runtime, final Element element, final FormatSpecification formatSpecification)
224 {
225 return decimalFormatFilter.getFormatString(runtime, element, formatSpecification);
226 }
227 }