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: * StyleBuilder.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.output.table.html.helper;
030:
031: import java.util.ArrayList;
032: import java.util.Locale;
033: import java.text.NumberFormat;
034: import java.text.DecimalFormat;
035: import java.text.DecimalFormatSymbols;
036:
037: import org.jfree.util.StringUtils;
038: import org.jfree.report.layout.model.BorderEdge;
039: import org.jfree.report.style.BorderStyle;
040: import org.jfree.report.util.geom.StrictGeomUtility;
041: import org.jfree.report.modules.output.table.html.util.HtmlColors;
042:
043: public final class StyleBuilder {
044: private static class StyleCarrier {
045: private String key;
046: private String value;
047: private String unit;
048:
049: protected StyleCarrier(final String key, final String value,
050: final String unit) {
051: if (key == null) {
052: throw new NullPointerException();
053: }
054: this .key = key;
055: this .value = value;
056: this .unit = unit;
057: }
058:
059: public String getUnit() {
060: return unit;
061: }
062:
063: public String getKey() {
064: return key;
065: }
066:
067: public String getValue() {
068: return value;
069: }
070:
071: public boolean equals(final Object o) {
072: if (this == o) {
073: return true;
074: }
075: if (o == null || getClass() != o.getClass()) {
076: return false;
077: }
078:
079: final StyleCarrier that = (StyleCarrier) o;
080:
081: if (!key.equals(that.key)) {
082: return false;
083: }
084:
085: return true;
086: }
087:
088: public int hashCode() {
089: return key.hashCode();
090: }
091: }
092:
093: private static final String INDENT = " ";
094:
095: private ArrayList styles;
096: private String lineSeparator;
097: private StringBuffer buffer;
098:
099: private NumberFormat pointConverter;
100: private NumberFormat pointIntConverter;
101:
102: public StyleBuilder() {
103: this .lineSeparator = StringUtils.getLineSeparator();
104: this .styles = new ArrayList();
105: this .buffer = new StringBuffer(100);
106:
107: pointConverter = new DecimalFormat("0.####",
108: new DecimalFormatSymbols(Locale.US));
109: pointIntConverter = new DecimalFormat("0",
110: new DecimalFormatSymbols(Locale.US));
111:
112: }
113:
114: public void clear() {
115: this .styles.clear();
116: }
117:
118: public void append(final String key, final String value) {
119: final StyleCarrier newCarrier = new StyleCarrier(key, value,
120: null);
121: styles.remove(newCarrier);
122: styles.add(newCarrier);
123: }
124:
125: public void append(final String key, final String value,
126: final String unit) {
127: final StyleCarrier newCarrier = new StyleCarrier(key, value,
128: unit);
129: styles.remove(newCarrier);
130: styles.add(newCarrier);
131: }
132:
133: /**
134: * Appends the style to the list. If the replace value if <code>false</code>
135: * and the list already contains the key, it will not be replaced.
136: */
137: public void append(final String key, final String value,
138: final boolean replace) {
139: final StyleCarrier newCarrier = new StyleCarrier(key, value,
140: null);
141: if (replace || !styles.contains(newCarrier)) {
142: styles.remove(newCarrier);
143: styles.add(newCarrier);
144: }
145: }
146:
147: /**
148: * Appends the style to the list. If the replace value if <code>false</code>
149: * and the list already contains the key, it will not be replaced.
150: */
151: public void append(final String key, final String value,
152: final String unit, final boolean replace) {
153: final StyleCarrier newCarrier = new StyleCarrier(key, value,
154: unit);
155: if (replace || !styles.contains(newCarrier)) {
156: styles.remove(newCarrier);
157: styles.add(newCarrier);
158: }
159: }
160:
161: public String toString() {
162: return toString(true);
163: }
164:
165: public String toString(final boolean compact) {
166: buffer.delete(0, buffer.length());
167: for (int i = 0; i < styles.size(); i++) {
168: if (i > 0) {
169: buffer.append("; ");
170: }
171:
172: final StyleCarrier sc = (StyleCarrier) styles.get(i);
173: if (compact == false) {
174: if (i != 0) {
175: buffer.append(lineSeparator);
176: }
177: buffer.append(StyleBuilder.INDENT);
178: }
179:
180: buffer.append(sc.getKey());
181: buffer.append(": ");
182: buffer.append(sc.getValue());
183: final String unit = sc.getUnit();
184: if (unit != null) {
185: buffer.append(unit);
186: }
187: }
188: return buffer.toString();
189: }
190:
191: public String printEdgeAsCSS(final BorderEdge edge) {
192: final BorderStyle borderStyle = edge.getBorderStyle();
193: final long width = edge.getWidth();
194: if (BorderStyle.NONE.equals(borderStyle) || width <= 0) {
195: return "none";
196: }
197: return (pointConverter.format(StrictGeomUtility
198: .toExternalValue(width)))
199: + "pt "
200: + borderStyle.toString()
201: + ' '
202: + HtmlColors.getColorString(edge.getColor());
203: }
204:
205: public NumberFormat getPointConverter() {
206: return pointConverter;
207: }
208:
209: public NumberFormat getPointIntConverter() {
210: return pointIntConverter;
211: }
212:
213: }
|