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 * StringFieldTemplate.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 org.jfree.report.Element;
34 import org.jfree.report.filter.DataRowDataSource;
35 import org.jfree.report.filter.FormatSpecification;
36 import org.jfree.report.filter.RawDataSource;
37 import org.jfree.report.filter.StringFilter;
38 import org.jfree.report.function.ExpressionRuntime;
39
40 /**
41 * A string field template.
42 *
43 * @author Thomas Morgner
44 */
45 public class StringFieldTemplate extends AbstractTemplate
46 implements RawDataSource
47 {
48 /**
49 * The data-row data source.
50 */
51 private DataRowDataSource dataRowDataSource;
52
53 /**
54 * A string filter.
55 */
56 private StringFilter stringFilter;
57
58 /**
59 * Creates a new string field template.
60 */
61 public StringFieldTemplate ()
62 {
63 dataRowDataSource = new DataRowDataSource();
64 stringFilter = new StringFilter();
65 stringFilter.setDataSource(dataRowDataSource);
66 }
67
68 /**
69 * Returns the field name.
70 *
71 * @return The field name.
72 */
73 public String getField ()
74 {
75 return dataRowDataSource.getDataSourceColumnName();
76 }
77
78 /**
79 * Sets the field name.
80 *
81 * @param field the field name.
82 */
83 public void setField (final String field)
84 {
85 dataRowDataSource.setDataSourceColumnName(field);
86 }
87
88 /**
89 * Returns the formula used to compute the value of the data source.
90 *
91 * @return the formula.
92 */
93 public String getFormula()
94 {
95 return dataRowDataSource.getFormula();
96 }
97
98 /**
99 * Defines the formula used to compute the value of this data source.
100 *
101 * @param formula the formula for the data source.
102 */
103 public void setFormula(final String formula)
104 {
105 dataRowDataSource.setFormula(formula);
106 }
107
108 /**
109 * Returns the value displayed by the field when the data source value is
110 * <code>null</code>.
111 *
112 * @return A value to represent <code>null</code>.
113 */
114 public String getNullValue ()
115 {
116 return stringFilter.getNullValue();
117 }
118
119 /**
120 * Sets the value displayed by the field when the data source value is
121 * <code>null</code>.
122 *
123 * @param nullValue the value that represents <code>null</code>.
124 */
125 public void setNullValue (final String nullValue)
126 {
127 stringFilter.setNullValue(nullValue);
128 }
129
130 /**
131 * Returns the current value for the data source.
132 *
133 * @param runtime the expression runtime that is used to evaluate formulas and expressions when computing the value of
134 * this filter.
135 * @param element
136 * @return the value.
137 */
138 public Object getValue(final ExpressionRuntime runtime, final Element element)
139 {
140 return stringFilter.getValue(runtime, element);
141 }
142
143 /**
144 * Clones the template.
145 *
146 * @return the clone.
147 *
148 * @throws CloneNotSupportedException this should never happen.
149 */
150 public Object clone ()
151 throws CloneNotSupportedException
152 {
153 final StringFieldTemplate template = (StringFieldTemplate) super.clone();
154 template.stringFilter = (StringFilter) stringFilter.clone();
155 template.dataRowDataSource = (DataRowDataSource) template.stringFilter.getDataSource();
156 return template;
157 }
158
159 /**
160 * Returns the datarow data source used in this template.
161 *
162 * @return the datarow data source.
163 */
164 protected DataRowDataSource getDataRowDataSource ()
165 {
166 return dataRowDataSource;
167 }
168
169 public Object getRawValue(final ExpressionRuntime runtime, final Element element)
170 {
171 return stringFilter.getRawValue(runtime, element);
172 }
173
174 public FormatSpecification getFormatString(final ExpressionRuntime runtime, final Element element, FormatSpecification formatSpecification)
175 {
176 if (formatSpecification == null)
177 {
178 formatSpecification = new FormatSpecification();
179 }
180 formatSpecification.redefine(FormatSpecification.TYPE_UNDEFINED, null);
181 return formatSpecification;
182 }
183 }