001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * Style.java
028: *
029: * Created on 12 dicembre 2005, 16.35
030: *
031: */
032:
033: package it.businesslogic.ireport;
034:
035: import it.businesslogic.ireport.gui.sheet.ColorSelectorPanel;
036: import java.awt.Color;
037: import java.util.HashMap;
038: import java.util.Iterator;
039: import java.util.Vector;
040:
041: /**
042: *
043: * @author Administrator
044: */
045: public class Style implements BoxElement {
046:
047: public static final String ATTRIBUTE_name = "name";
048: public static final String ATTRIBUTE_isDefault = "isDefault";
049: public static final String ATTRIBUTE_mode = "mode";
050: public static final String ATTRIBUTE_forecolor = "forecolor";
051: public static final String ATTRIBUTE_backcolor = "backcolor";
052: public static final String ATTRIBUTE_style = "style";
053:
054: public static final String ATTRIBUTE_pen = "pen";
055: public static final String ATTRIBUTE_fill = "fill";
056:
057: public static final String ATTRIBUTE_radius = "radius";
058:
059: public static final String ATTRIBUTE_scaleImage = "scaleImage";
060: // these are inherited by both images and texts.
061: public static final String ATTRIBUTE_hAlign = "hAlign";
062: public static final String ATTRIBUTE_vAlign = "vAlign";
063:
064: public static final String ATTRIBUTE_border = "border";
065: public static final String ATTRIBUTE_borderColor = "borderColor";
066: public static final String ATTRIBUTE_padding = "padding";
067: public static final String ATTRIBUTE_topBorder = "topBorder";
068: public static final String ATTRIBUTE_topBorderColor = "topBorderColor";
069: public static final String ATTRIBUTE_topPadding = "topPadding";
070: public static final String ATTRIBUTE_leftBorder = "leftBorder";
071: public static final String ATTRIBUTE_leftBorderColor = "leftBorderColor";
072: public static final String ATTRIBUTE_leftPadding = "leftPadding";
073: public static final String ATTRIBUTE_bottomBorder = "bottomBorder";
074: public static final String ATTRIBUTE_bottomBorderColor = "bottomBorderColor";
075: public static final String ATTRIBUTE_bottomPadding = "bottomPadding";
076: public static final String ATTRIBUTE_rightBorder = "rightBorder";
077: public static final String ATTRIBUTE_rightBorderColor = "rightBorderColor";
078: public static final String ATTRIBUTE_rightPadding = "rightPadding";
079:
080: public static final String ATTRIBUTE_rotation = "rotation";
081: public static final String ATTRIBUTE_lineSpacing = "lineSpacing";
082: public static final String ATTRIBUTE_isStyledText = "isStyledText";
083: public static final String ATTRIBUTE_pattern = "pattern";
084: public static final String ATTRIBUTE_isBlankWhenNull = "isBlankWhenNull";
085: public static final String ATTRIBUTE_markup = "markup";
086:
087: public static final String ATTRIBUTE_fontName = "fontName";
088: public static final String ATTRIBUTE_isBold = "isBold";
089: public static final String ATTRIBUTE_isItalic = "isItalic";
090: public static final String ATTRIBUTE_isUnderline = "isUnderline";
091: public static final String ATTRIBUTE_isStrikeThrough = "isStrikeThrough";
092: public static final String ATTRIBUTE_fontSize = "fontSize";
093: public static final String ATTRIBUTE_pdfFontName = "pdfFontName";
094: public static final String ATTRIBUTE_pdfEncoding = "pdfEncoding";
095: public static final String ATTRIBUTE_isPdfEmbedded = "isPdfEmbedded";
096: public static final String ATTRIBUTE_condition = "condition";
097:
098: private Vector conditionedStyles = new Vector();
099:
100: public static final String[] JRXMLStyleAttributes = { "name",
101: "isDefault", "style", "mode", "forecolor", "backcolor",
102: "pen", "fill", "radius", "scaleImage", "hAlign", "vAlign",
103: "border", "borderColor", "padding", "topBorder",
104: "topBorderColor", "topPadding", "leftBorder",
105: "leftBorderColor", "leftPadding", "bottomBorder",
106: "bottomBorderColor", "bottomPadding", "rightBorder",
107: "rightBorderColor", "rightPadding", "rotation",
108: "lineSpacing", "isStyledText", "fontName", "fontSize",
109: "isBold", "isItalic", "isUnderline", "isStrikeThrough",
110: "pdfFontName", "pdfEncoding", "isPdfEmbedded", "pattern",
111: "isBlankWhenNull", "markup" };
112:
113: private String name = "";
114: private HashMap attributes = new HashMap();
115: private Style defaultStyle = null;
116: private Box box = null;
117: private Pen pen = null;
118:
119: /**
120: * Create an empty style
121: */
122: public Style() {
123: }
124:
125: /**
126: * Create a new style with the same values of s1
127: * isDefault is reset to false.
128: */
129: public Style(Style s1) {
130: Iterator iterator_style = s1.getAttributes().keySet()
131: .iterator();
132: while (iterator_style.hasNext()) {
133: Object key = iterator_style.next();
134: Object val = s1.getAttributes().get(key);
135: this .getAttributes().put(key, val);
136: }
137: this .getAttributes().put(this .ATTRIBUTE_isDefault, "false");
138:
139: getConditionedStyles().clear();
140: for (int i = 0; i < s1.getConditionedStyles().size(); ++i) {
141: Style c_s = (Style) s1.getConditionedStyles().elementAt(i);
142: getConditionedStyles()
143: .addElement(new ConditionedStyle(c_s));
144: }
145:
146: if (s1.getBox() != null) {
147: this .setBox(s1.getBox().cloneMe());
148: }
149:
150: if (s1.getPen() != null) {
151: this .setPen(s1.getPen().cloneMe());
152: }
153: }
154:
155: /**
156: * Copy all values of s1 in the current style. The subStyles are not duplicated!
157: * A cimply copy of conditionedStyles vector is performed
158: */
159: public void copyStyleFrom(Style s1) {
160: Iterator iterator_style = s1.getAttributes().keySet()
161: .iterator();
162: while (iterator_style.hasNext()) {
163: Object key = iterator_style.next();
164: Object val = s1.getAttributes().get(key);
165: this .getAttributes().put(key, val);
166: }
167: this .setConditionedStyles(s1.getConditionedStyles());
168: setBox(s1.getBox());
169: setPen(s1.getPen());
170: }
171:
172: public String getName() {
173: return (getAttributes().get(ATTRIBUTE_name) == null) ? null
174: : getAttributes().get(ATTRIBUTE_name) + "";
175: }
176:
177: public void setName(String name) {
178: getAttributes().put(ATTRIBUTE_name, name);
179: }
180:
181: public HashMap getAttributes() {
182: return attributes;
183: }
184:
185: public void setAttributes(HashMap attributes) {
186: this .attributes = attributes;
187: }
188:
189: /**
190: * This method return the attribute value.
191: */
192: public Object getAttribute(String attribute) {
193: if (getAttributes().containsKey(attribute))
194: return getAttributes().get(attribute);
195: //if ( attribute != ATTRIBUTE_parent && getParent() != null ) return getParent().getAttribute(attribute);
196: return null;
197: }
198:
199: /**
200: * This method return the attribute value.
201: */
202: public Object getAttribute(String attribute, boolean lookInParent) {
203:
204: return getAttributeSafe(attribute, lookInParent, null);
205: }
206:
207: /**
208: * This method return the attribute value.
209: */
210: private Object getAttributeSafe(String attribute,
211: boolean lookInParent, java.util.Collection coll) {
212: if (getAttributes().containsKey(attribute))
213: return getAttributes().get(attribute);
214:
215: if (lookInParent && attribute != ATTRIBUTE_style
216: && getParent() != null) {
217: if (coll == null)
218: coll = new java.util.ArrayList();
219: coll.add(this );
220: if (coll.contains(getParent()))
221: return null; //CYCLE!!!!! this check avoid infinite loop
222:
223: return getParent().getAttributeSafe(attribute, true, coll);
224: }
225: return null;
226: }
227:
228: public String getAttributeString(String attribute,
229: String defaultString) {
230:
231: return getAttributeString(attribute, defaultString, false);
232: }
233:
234: /**
235: * This method is the same as getAttribute(String attribute).
236: */
237: public String getAttributeString(String attribute,
238: String defaultString, boolean lookInParentToo) {
239: Object obj = this .getAttribute(attribute, true);
240: return ((obj != null) ? "" + obj : defaultString);
241: }
242:
243: public int getAttributeInteger(String attribute, int defaultInt) {
244: return getAttributeInteger(attribute, defaultInt, false);
245: }
246:
247: /**
248: * This method is the same as getAttribute(String attribute).
249: */
250: public int getAttributeInteger(String attribute, int defaultInt,
251: boolean lookInParentToo) {
252: Object obj = this .getAttribute(attribute, true);
253: if (obj == null)
254: return defaultInt;
255: if (obj instanceof Double)
256: return ((Double) obj).intValue();
257: if (obj instanceof Integer)
258: return ((Integer) obj).intValue();
259: try {
260:
261: return Integer.parseInt(obj + "");
262: } catch (Exception ex) {
263:
264: }
265: return defaultInt;
266: }
267:
268: /**
269: * This method is the same as getAttribute(String attribute, true).
270: */
271: public double getAttributeDouble(String attribute,
272: double defaultDouble) {
273: Object obj = this .getAttribute(attribute);
274: if (obj == null)
275: return defaultDouble;
276: if (obj instanceof Double)
277: return ((Double) obj).doubleValue();
278: if (obj instanceof Integer)
279: return ((Integer) obj).intValue() * 1.0;
280: return defaultDouble;
281: }
282:
283: public Color getAttributeColor(String attribute, Color defaultColor) {
284:
285: return getAttributeColor(attribute, defaultColor, false);
286: }
287:
288: /**
289: * This method is the same as getAttribute(String attribute, true).
290: */
291: public Color getAttributeColor(String attribute,
292: Color defaultColor, boolean lookInParentToo) {
293: Object obj = this .getAttribute(attribute);
294: if (obj == null)
295: return defaultColor;
296: if (obj instanceof Color)
297: return (Color) obj;
298: Color c = ColorSelectorPanel.parseColorString(obj + "");
299: if (c != null)
300: return c;
301:
302: return defaultColor;
303: }
304:
305: public boolean getAttributeBoolean(String attribute,
306: boolean defaultBoolean) {
307: return getAttributeBoolean(attribute, defaultBoolean, false);
308: }
309:
310: /**
311: * This method is the same as getAttribute(String attribute, true).
312: */
313: public boolean getAttributeBoolean(String attribute,
314: boolean defaultBoolean, boolean lookInParentToo) {
315: Object obj = this .getAttribute(attribute, lookInParentToo);
316: if (obj == null)
317: return defaultBoolean;
318: if (obj instanceof Boolean)
319: return ((Boolean) obj).booleanValue();
320: try {
321: return Boolean.valueOf(obj + "").booleanValue();
322: } catch (Exception ex) {
323: ex.printStackTrace();
324: }
325: return defaultBoolean;
326: }
327:
328: public Style getParent() {
329: Object obj = this .getAttributes().get(ATTRIBUTE_style);
330: if (obj == null || !(obj instanceof Style)) {
331: //this.getAttributes().remove(ATTRIBUTE_style);
332: return null;
333: }
334:
335: return (Style) obj;
336: }
337:
338: public String toString() {
339: return this .getName() + "";
340: }
341:
342: public Vector getConditionedStyles() {
343: return conditionedStyles;
344: }
345:
346: public void setConditionedStyles(Vector conditionedStyles) {
347: this .conditionedStyles = conditionedStyles;
348: }
349:
350: public static boolean isDefaultValue(Object value,
351: String attributeName, Style elementStyle, Style defaultStyle) {
352:
353: if (elementStyle == null && defaultStyle == null) {
354: return false;
355: }
356:
357: Style referenceStyle = (elementStyle == null) ? defaultStyle
358: : elementStyle;
359:
360: Object defaultValue = referenceStyle.getAttribute(
361: attributeName, true);
362:
363: if (defaultValue == null)
364: return false;
365:
366: if (attributeName.endsWith("color") && value instanceof Color) {
367: Color color = it.businesslogic.ireport.gui.sheet.ColorSelectorPanel
368: .parseColorString("" + defaultValue);
369: return color.equals((Color) value);
370: }
371:
372: return defaultValue.equals(value);
373: }
374:
375: public Box getBox() {
376: return box;
377: }
378:
379: public void setBox(Box box) {
380: this .box = box;
381: }
382:
383: public Pen getPen() {
384: return pen;
385: }
386:
387: public void setPen(Pen pen) {
388: this.pen = pen;
389: }
390: }
|