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: * AbstractStyleSheet.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.style;
030:
031: import org.jfree.report.util.InstanceID;
032:
033: /**
034: * Creation-Date: 22.04.2007, 16:48:33
035: *
036: * @author Thomas Morgner
037: */
038: public abstract class AbstractStyleSheet implements StyleSheet {
039:
040: /**
041: * The instance id of this ElementStyleSheet. This id is shared among all clones.
042: */
043: private InstanceID id;
044:
045: protected AbstractStyleSheet() {
046: this .id = new InstanceID();
047: }
048:
049: /**
050: * Returns the value of a style. If the style is not found in this style-sheet, the code looks in the parent
051: * style-sheets. If the style is not found in any of the parent style-sheets, then <code>null</code> is returned.
052: *
053: * @param key the style key.
054: * @return the value.
055: */
056: public Object getStyleProperty(final StyleKey key) {
057: return getStyleProperty(key, null);
058: }
059:
060: /**
061: * Returns a boolean style (defaults to false if the style is not found).
062: *
063: * @param key the style key.
064: * @return <code>true</code> or <code>false</code>.
065: */
066: public boolean getBooleanStyleProperty(final StyleKey key) {
067: final Boolean b = (Boolean) getStyleProperty(key, null);
068: if (b == null) {
069: return false;
070: }
071: return b.booleanValue();
072: }
073:
074: /**
075: * Returns a boolean style.
076: *
077: * @param key the style key.
078: * @param defaultValue the default value.
079: * @return true or false.
080: */
081: public boolean getBooleanStyleProperty(final StyleKey key,
082: final boolean defaultValue) {
083: final Boolean b = (Boolean) getStyleProperty(key, null);
084: if (b == null) {
085: return defaultValue;
086: }
087: return b.booleanValue();
088: }
089:
090: /**
091: * Returns an integer style.
092: *
093: * @param key the style key.
094: * @param def the default value.
095: * @return the style value.
096: */
097: public int getIntStyleProperty(final StyleKey key, final int def) {
098: final Number i = (Number) getStyleProperty(key, null);
099: if (i == null) {
100: return def;
101: }
102: return i.intValue();
103: }
104:
105: /**
106: * Returns an double style.
107: *
108: * @param key the style key.
109: * @param def the default value.
110: * @return the style value.
111: */
112: public double getDoubleStyleProperty(final StyleKey key,
113: final double def) {
114: final Number i = (Number) getStyleProperty(key, null);
115: if (i == null) {
116: return def;
117: }
118: return i.doubleValue();
119: }
120:
121: /**
122: * Returns the font for this style-sheet.
123: *
124: * @return the font.
125: */
126: public FontDefinition getFontDefinitionProperty() {
127: final String name = (String) getStyleProperty(TextStyleKeys.FONT);
128: final int size = getIntStyleProperty(TextStyleKeys.FONTSIZE, -1);
129: final boolean bold = getBooleanStyleProperty(TextStyleKeys.BOLD);
130: final boolean italic = getBooleanStyleProperty(TextStyleKeys.ITALIC);
131: final boolean underlined = getBooleanStyleProperty(TextStyleKeys.UNDERLINED);
132: final boolean strike = getBooleanStyleProperty(TextStyleKeys.STRIKETHROUGH);
133: final boolean embed = getBooleanStyleProperty(TextStyleKeys.EMBEDDED_FONT);
134: final String encoding = (String) getStyleProperty(TextStyleKeys.FONTENCODING);
135:
136: return new FontDefinition(name, size, bold, italic, underlined,
137: strike, encoding, embed);
138: }
139:
140: /**
141: * Returns the ID of the stylesheet. The ID does identify an element stylesheet an all all cloned instances of that
142: * stylesheet.
143: *
144: * @return the ID of this stylesheet.
145: */
146: public InstanceID getId() {
147: return id;
148: }
149:
150: public Object[] toArray(final StyleKey[] keys) {
151: final Object[] data = new Object[keys.length];
152: for (int i = 0; i < keys.length; i++) {
153: final StyleKey key = keys[i];
154: final int identifier = key.getIdentifier();
155: data[identifier] = getStyleProperty(key, null);
156: }
157: return data;
158: }
159:
160: public long getChangeTracker() {
161: return 0;
162: }
163: }
|