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: * TextElement.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report;
030:
031: import org.jfree.report.filter.StringFilter;
032: import org.jfree.report.function.ExpressionRuntime;
033: import org.jfree.report.style.ElementStyleSheet;
034: import org.jfree.report.style.FontDefinition;
035: import org.jfree.report.style.TextStyleKeys;
036:
037: /**
038: * The base class for all elements that display text in a report band.
039: * <p/>
040: * All content is converted to String using the String.valueOf () method. To convert
041: * values in a more sophisicated way, add filters to this element. Known filters are for
042: * instance the <code>NumberFormatFilter</code> or the <code>SimpleDateFormatFilter</code>.
043: * <p/>
044: * For more information on filters have a look at the filter package {@link
045: * org.jfree.report.filter}
046: *
047: * @author David Gilbert
048: * @author Thomas Morgner
049: */
050: public class TextElement extends Element {
051: /**
052: * The content type string.
053: * @deprecated No longer used.
054: */
055: public static final String CONTENT_TYPE = "text/plain";
056:
057: /**
058: * The filter used to convert values (Objects) to strings.
059: */
060: private StringFilter stringfilter;
061:
062: /**
063: * Creates a new empty text element.
064: */
065: public TextElement() {
066: stringfilter = new StringFilter();
067: setNullString(null);
068: }
069:
070: /**
071: * Return the null-value representation for this element. This will never return null,
072: * although you may feed a null value into the set method of this property.
073: *
074: * @return the null value representation for this element.
075: *
076: * @see #setNullString(String)
077: */
078: public String getNullString() {
079: return stringfilter.getNullValue();
080: }
081:
082: /**
083: * Defines the null value representation for this element. If null is given, the value
084: * is set to a reasonable value (this implementation sets the value to the string "-").
085: *
086: * @param s the null string.
087: */
088: public void setNullString(final String s) {
089: final String nstring = (s == null) ? "-" : s;
090: stringfilter.setNullValue(nstring);
091: }
092:
093: /**
094: * Returns the value for this text element.
095: * <p/>
096: * Internally, a <code>StringFilter</code> is used to ensure that the final result is an
097: * instance of String (even though it is returned as an Object.
098: *
099: * @param runtime the expression runtime for evaluating inline expression.
100: * @return the value for the element.
101: */
102: public final Object getValue(final ExpressionRuntime runtime) {
103: stringfilter.setDataSource(getDataSource());
104: return stringfilter.getValue(runtime);
105: }
106:
107: /**
108: * Returns a string representation of this element, useful for debugging purposes.
109: *
110: * @return a string.
111: */
112: public String toString() {
113: final StringBuffer b = new StringBuffer();
114: b.append(this .getClass().getName());
115: b.append("={ name=");
116: b.append(getName());
117: b.append(", font=");
118: b.append(getStyle().getFontDefinitionProperty());
119: b.append('}');
120: return b.toString();
121: }
122:
123: /**
124: * Clones this element.
125: *
126: * @return a clone of this element.
127: *
128: * @throws CloneNotSupportedException this should never happen.
129: */
130: public Object clone() throws CloneNotSupportedException {
131: final TextElement te = (TextElement) super .clone();
132: te.stringfilter = (StringFilter) stringfilter.clone();
133: return te;
134: }
135:
136: /**
137: * Returns the content type, in this case '<code>text/plain</code>'.
138: *
139: * @return the content type.
140: * @deprecated No longer used.
141: */
142: public String getContentType() {
143: return TextElement.CONTENT_TYPE;
144: }
145:
146: /**
147: * Returns the name of the current font.
148: *
149: * @return the font name
150: */
151: public String getFontName() {
152: return (String) getStyle().getStyleProperty(
153: ElementStyleSheet.FONT);
154: }
155:
156: /**
157: * Defines the font name of the current font.
158: *
159: * @param fontName the font name
160: */
161: public void setFontName(final String fontName) {
162: getStyle().setStyleProperty(ElementStyleSheet.FONT, fontName);
163: }
164:
165: /**
166: * Returns the font size in points.
167: *
168: * @return the font size.
169: */
170: public int getFontSize() {
171: final Integer i = (Integer) getStyle().getStyleProperty(
172: TextStyleKeys.FONTSIZE);
173: // fontsize is never null.
174: return i.intValue();
175: }
176:
177: /**
178: * Defines the height of the font in points.
179: * <p/>
180: * Calling this function with either parameter will override any previously defined
181: * value for the layoutcachable attribute. The value can no longer be inherited from
182: * parent stylesheets.
183: *
184: * @param fontSize the font size in points.
185: */
186: public void setFontSize(final int fontSize) {
187: getStyle().setStyleProperty(TextStyleKeys.FONTSIZE,
188: new Integer(fontSize));
189: }
190:
191: /**
192: * Checks, whether the font should be displayed in bold style.
193: *
194: * @return true, if the font should be bold, false otherwise.
195: */
196: public boolean isBold() {
197: return getStyle().getBooleanStyleProperty(TextStyleKeys.BOLD);
198: }
199:
200: /**
201: * Defines, whether the font should be displayed in bold, false otherwise.
202: * <p/>
203: * Calling this function with either parameter will override any previously defined
204: * value for the layoutcachable attribute. The value can no longer be inherited from
205: * parent stylesheets.
206: *
207: * @param bold true, if the font should be displayed in bold, false otherwise
208: */
209: public void setBold(final boolean bold) {
210: getStyle().setBooleanStyleProperty(TextStyleKeys.BOLD, bold);
211: }
212:
213: /**
214: * Checks, whether the font should be displayed in italic style.
215: *
216: * @return true, if the font should be italic, false otherwise.
217: */
218: public boolean isItalic() {
219: return getStyle().getBooleanStyleProperty(TextStyleKeys.ITALIC);
220: }
221:
222: /**
223: * Defines, whether the font should be displayed in italics.
224: * <p/>
225: * Calling this function with either parameter will override any previously defined
226: * value for the layoutcachable attribute. The value can no longer be inherited from
227: * parent stylesheets.
228: *
229: * @param italic true, if the font should be in italic style, false otherwise.
230: */
231: public void setItalic(final boolean italic) {
232: getStyle()
233: .setBooleanStyleProperty(TextStyleKeys.ITALIC, italic);
234: }
235:
236: /**
237: * Returns whether the text should be displayed underlined.
238: *
239: * @return true, if the fond should be underlined, false otherwise.
240: */
241: public boolean isUnderline() {
242: return getStyle().getBooleanStyleProperty(
243: TextStyleKeys.UNDERLINED);
244: }
245:
246: /**
247: * Defines, whether the text should be displayed with the underline style applied.
248: * <p/>
249: * Calling this function with either parameter will override any previously defined
250: * value for the layoutcachable attribute. The value can no longer be inherited from
251: * parent stylesheets.
252: *
253: * @param underline true, if the text should be displayed underlined, false otherwise.
254: */
255: public void setUnderline(final boolean underline) {
256: getStyle().setBooleanStyleProperty(TextStyleKeys.UNDERLINED,
257: underline);
258: }
259:
260: /**
261: * Returns whether the text should have the strikethough style applied.
262: *
263: * @return true, if the font should be striked through, false otherwise.
264: */
265: public boolean isStrikethrough() {
266: return getStyle().getBooleanStyleProperty(
267: TextStyleKeys.STRIKETHROUGH);
268: }
269:
270: /**
271: * Defines, whether the text should be displayed striked through.
272: * <p/>
273: * Calling this function with either parameter will override any previously defined
274: * value for the layoutcachable attribute. The value can no longer be inherited from
275: * parent stylesheets.
276: *
277: * @param strikethrough whether to display the text with strikethrough style applied
278: */
279: public void setStrikethrough(final boolean strikethrough) {
280: getStyle().setBooleanStyleProperty(TextStyleKeys.STRIKETHROUGH,
281: strikethrough);
282: }
283:
284: /**
285: * Returns the font definition object assigned with this element. Never null.
286: *
287: * @return the font definition for this element.
288: */
289: public FontDefinition getFont() {
290: return getStyle().getFontDefinitionProperty();
291: }
292:
293: /**
294: * Defines all font properties by applying the values from the given font definition
295: * object.
296: * <p/>
297: * Calling this function with either parameter will override any previously defined
298: * value for the layoutcachable attribute. The value can no longer be inherited from
299: * parent stylesheets.
300: *
301: * @param font the font definition for this element.
302: */
303: public void setFont(final FontDefinition font) {
304: getStyle().setFontDefinitionProperty(font);
305:
306: }
307:
308: /**
309: * Returns the lineheight for the element. The lineheight can be used to extend the
310: * space between two text lines, the effective lineheight will be the maximum of this
311: * property and the font height.
312: *
313: * @return the defined line height.
314: */
315: public float getLineHeight() {
316: final Float i = (Float) getStyle().getStyleProperty(
317: TextStyleKeys.LINEHEIGHT);
318: if (i == null) {
319: return 0;
320: }
321: return i.floatValue();
322: }
323:
324: /**
325: * Defines the lineheight for the element. The lineheight can be used to extend the
326: * space between two text lines, the effective lineheight will be the maximum of this
327: * property and the font height.
328: * <p/>
329: * Calling this function with any parameter will override any previously defined value
330: * for the layoutcachable attribute. The value can no longer be inherited from parent
331: * stylesheets.
332: *
333: * @param lineHeight the defined line height.
334: */
335: public void setLineHeight(final float lineHeight) {
336: getStyle().setStyleProperty(TextStyleKeys.LINEHEIGHT,
337: new Float(lineHeight));
338: }
339:
340: /**
341: * Returns the reserved literal for this text element. This literal is appended,
342: * whenever the text from tne content does not fully fit into the element's bounds.
343: *
344: * @return the reserved literal.
345: */
346: public String getReservedLiteral() {
347: return (String) getStyle().getStyleProperty(
348: TextStyleKeys.RESERVED_LITERAL);
349: }
350:
351: /**
352: * Defines the reserved literal for this text element. This literal is appended,
353: * whenever the text from tne content does not fully fit into the element's bounds.
354: *
355: * @param reservedLiteral the reserved literal.
356: */
357: public void setReservedLiteral(final String reservedLiteral) {
358: getStyle().setStyleProperty(TextStyleKeys.RESERVED_LITERAL,
359: reservedLiteral);
360: }
361: }
|