KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > Style


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * Style.java
28  *
29  * Created on 12 dicembre 2005, 16.35
30  *
31  */

32
33 package it.businesslogic.ireport;
34 import it.businesslogic.ireport.gui.sheet.ColorSelectorPanel;
35 import java.awt.Color JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Vector JavaDoc;
39 /**
40  *
41  * @author Administrator
42  */

43 public class Style {
44     
45         public static final String JavaDoc ATTRIBUTE_name = "name";
46     public static final String JavaDoc ATTRIBUTE_isDefault = "isDefault";
47     public static final String JavaDoc ATTRIBUTE_mode = "mode";
48     public static final String JavaDoc ATTRIBUTE_forecolor = "forecolor";
49     public static final String JavaDoc ATTRIBUTE_backcolor = "backcolor";
50     public static final String JavaDoc ATTRIBUTE_style = "style";
51
52     public static final String JavaDoc ATTRIBUTE_pen = "pen";
53     public static final String JavaDoc ATTRIBUTE_fill = "fill";
54
55     public static final String JavaDoc ATTRIBUTE_radius = "radius";
56
57     public static final String JavaDoc ATTRIBUTE_scaleImage = "scaleImage";
58     // these are inherited by both images and texts.
59
public static final String JavaDoc ATTRIBUTE_hAlign = "hAlign";
60     public static final String JavaDoc ATTRIBUTE_vAlign = "vAlign";
61
62     public static final String JavaDoc ATTRIBUTE_border = "border";
63     public static final String JavaDoc ATTRIBUTE_borderColor = "borderColor";
64     public static final String JavaDoc ATTRIBUTE_padding = "padding";
65     public static final String JavaDoc ATTRIBUTE_topBorder = "topBorder";
66     public static final String JavaDoc ATTRIBUTE_topBorderColor = "topBorderColor";
67     public static final String JavaDoc ATTRIBUTE_topPadding = "topPadding";
68     public static final String JavaDoc ATTRIBUTE_leftBorder = "leftBorder";
69     public static final String JavaDoc ATTRIBUTE_leftBorderColor = "leftBorderColor";
70     public static final String JavaDoc ATTRIBUTE_leftPadding = "leftPadding";
71     public static final String JavaDoc ATTRIBUTE_bottomBorder = "bottomBorder";
72     public static final String JavaDoc ATTRIBUTE_bottomBorderColor = "bottomBorderColor";
73     public static final String JavaDoc ATTRIBUTE_bottomPadding = "bottomPadding";
74     public static final String JavaDoc ATTRIBUTE_rightBorder = "rightBorder";
75     public static final String JavaDoc ATTRIBUTE_rightBorderColor = "rightBorderColor";
76     public static final String JavaDoc ATTRIBUTE_rightPadding = "rightPadding";
77
78     public static final String JavaDoc ATTRIBUTE_rotation = "rotation";
79     public static final String JavaDoc ATTRIBUTE_lineSpacing = "lineSpacing";
80     public static final String JavaDoc ATTRIBUTE_isStyledText = "isStyledText";
81     public static final String JavaDoc ATTRIBUTE_pattern = "pattern";
82     public static final String JavaDoc ATTRIBUTE_isBlankWhenNull = "isBlankWhenNull";
83
84     public static final String JavaDoc ATTRIBUTE_fontName = "fontName";
85     public static final String JavaDoc ATTRIBUTE_isBold = "isBold";
86     public static final String JavaDoc ATTRIBUTE_isItalic = "isItalic";
87     public static final String JavaDoc ATTRIBUTE_isUnderline = "isUnderline";
88     public static final String JavaDoc ATTRIBUTE_isStrikeThrough = "isStrikeThrough";
89     public static final String JavaDoc ATTRIBUTE_fontSize = "fontSize";
90     public static final String JavaDoc ATTRIBUTE_pdfFontName = "pdfFontName";
91     public static final String JavaDoc ATTRIBUTE_pdfEncoding = "pdfEncoding";
92     public static final String JavaDoc ATTRIBUTE_isPdfEmbedded = "isPdfEmbedded";
93         public static final String JavaDoc ATTRIBUTE_condition = "condition";
94         
95         private Vector JavaDoc conditionedStyles = new Vector JavaDoc();
96         
97         public static final String JavaDoc[] JRXMLStyleAttributes = {"name",
98                                                             "isDefault",
99                                                             "style",
100                                                             "mode",
101                                                             "forecolor",
102                                                             "backcolor",
103                                                             "pen",
104                                                             "fill",
105                                                             "radius",
106                                                             "scaleImage",
107                                                             "hAlign",
108                                                             "vAlign",
109                                                             "border",
110                                                             "borderColor",
111                                                             "padding",
112                                                             "topBorder",
113                                                             "topBorderColor",
114                                                             "topPadding",
115                                                             "leftBorder",
116                                                             "leftBorderColor",
117                                                             "leftPadding",
118                                                             "bottomBorder",
119                                                             "bottomBorderColor",
120                                                             "bottomPadding",
121                                                             "rightBorder",
122                                                             "rightBorderColor",
123                                                             "rightPadding",
124                                                             "rotation",
125                                                             "lineSpacing",
126                                                             "isStyledText",
127                                                             "fontName",
128                                                             "fontSize",
129                                                             "isBold",
130                                                             "isItalic",
131                                                             "isUnderline",
132                                                             "isStrikeThrough",
133                                                             "pdfFontName",
134                                                             "pdfEncoding",
135                                                             "isPdfEmbedded",
136                                                             "pattern",
137                                                             "isBlankWhenNull"};
138         
139     private String JavaDoc name = "";
140     private HashMap JavaDoc attributes = new HashMap JavaDoc();
141     private Style defaultStyle = null;
142
143     /**
144      * Create an empty style
145      */

146     public Style()
147     {}
148
149     /**
150      * Create a new style with the same values of s1
151      * isDefault is reset to false.
152      */

153     public Style(Style s1)
154     {
155         Iterator JavaDoc iterator_style = s1.getAttributes().keySet().iterator();
156         while (iterator_style.hasNext())
157         {
158             Object JavaDoc key = iterator_style.next();
159             Object JavaDoc val = s1.getAttributes().get(key);
160             this.getAttributes().put(key, val);
161         }
162         this.getAttributes().put( this.ATTRIBUTE_isDefault, "false");
163         
164         getConditionedStyles().clear();
165         for (int i=0; i<s1.getConditionedStyles().size(); ++i)
166         {
167             Style c_s = (Style)s1.getConditionedStyles().elementAt(i);
168             getConditionedStyles().addElement( new ConditionedStyle(c_s));
169         }
170     }
171
172     /**
173      * Copy all values of s1 in the current style. The subStyles are not duplicated!
174      * A cimply copy of conditionedStyles vector is performed
175      */

176     public void copyStyleFrom(Style s1)
177     {
178         Iterator JavaDoc iterator_style = s1.getAttributes().keySet().iterator();
179         while (iterator_style.hasNext())
180         {
181             Object JavaDoc key = iterator_style.next();
182             Object JavaDoc val = s1.getAttributes().get(key);
183             this.getAttributes().put(key, val);
184         }
185         this.setConditionedStyles( s1.getConditionedStyles());
186     }
187     
188     
189     public String JavaDoc getName() {
190         return (getAttributes().get(ATTRIBUTE_name) == null) ? null : getAttributes().get(ATTRIBUTE_name)+"";
191     }
192
193     public void setName(String JavaDoc name) {
194         getAttributes().put(ATTRIBUTE_name, name);
195     }
196
197     public HashMap JavaDoc getAttributes() {
198         return attributes;
199     }
200
201     public void setAttributes(HashMap JavaDoc attributes) {
202         this.attributes = attributes;
203     }
204     
205     /**
206      * This method return the attribute value.
207      */

208     public Object JavaDoc getAttribute(String JavaDoc attribute)
209     {
210         if ( getAttributes().containsKey(attribute)) return getAttributes().get(attribute);
211         //if ( attribute != ATTRIBUTE_parent && getParent() != null ) return getParent().getAttribute(attribute);
212
return null;
213     }
214     
215     /**
216      * This method return the attribute value.
217      */

218     public Object JavaDoc getAttribute(String JavaDoc attribute, boolean lookInParent)
219     {
220         
221         return getAttributeSafe(attribute, lookInParent, null);
222     }
223     
224     /**
225      * This method return the attribute value.
226      */

227     private Object JavaDoc getAttributeSafe(String JavaDoc attribute, boolean lookInParent,java.util.Collection JavaDoc coll)
228     {
229         if ( getAttributes().containsKey(attribute)) return getAttributes().get(attribute);
230         
231         if (lookInParent && attribute != ATTRIBUTE_style && getParent() != null )
232         {
233             if (coll == null) coll = new java.util.ArrayList JavaDoc();
234             coll.add(this);
235             if (coll.contains(getParent())) return null; //CYCLE!!!!! this check avoid infinite loop
236

237             return getParent().getAttributeSafe(attribute, true,coll);
238         }
239         return null;
240     }
241     
242     public String JavaDoc getAttributeString(String JavaDoc attribute, String JavaDoc defaultString)
243     {
244         
245         return getAttributeString(attribute, defaultString, false);
246     }
247     /**
248      * This method is the same as getAttribute(String attribute).
249      */

250     public String JavaDoc getAttributeString(String JavaDoc attribute, String JavaDoc defaultString, boolean lookInParentToo)
251     {
252         Object JavaDoc obj = this.getAttribute(attribute, true);
253         return ((obj != null) ? ""+obj : defaultString);
254     }
255     
256     
257     public int getAttributeInteger(String JavaDoc attribute, int defaultInt)
258     {
259         return getAttributeInteger(attribute, defaultInt, false);
260     }
261     
262     /**
263      * This method is the same as getAttribute(String attribute).
264      */

265     public int getAttributeInteger(String JavaDoc attribute, int defaultInt, boolean lookInParentToo)
266     {
267         Object JavaDoc obj = this.getAttribute(attribute, true);
268         if (obj == null) return defaultInt;
269         if (obj instanceof Double JavaDoc) return ((Double JavaDoc)obj).intValue();
270         if (obj instanceof Integer JavaDoc) return ((Integer JavaDoc)obj).intValue();
271         try {
272             
273             return Integer.parseInt(obj+"");
274         } catch (Exception JavaDoc ex)
275         {
276             
277         }
278         return defaultInt;
279     }
280     
281     /**
282      * This method is the same as getAttribute(String attribute, true).
283      */

284     public double getAttributeDouble(String JavaDoc attribute, double defaultDouble)
285     {
286         Object JavaDoc obj = this.getAttribute(attribute);
287         if (obj == null) return defaultDouble;
288         if (obj instanceof Double JavaDoc) return ((Double JavaDoc)obj).doubleValue();
289         if (obj instanceof Integer JavaDoc) return ((Integer JavaDoc)obj).intValue()*1.0;
290         return defaultDouble;
291     }
292     
293      public Color JavaDoc getAttributeColor(String JavaDoc attribute, Color JavaDoc defaultColor)
294     {
295          
296          return getAttributeColor(attribute, defaultColor, false);
297      }
298     /**
299      * This method is the same as getAttribute(String attribute, true).
300      */

301     public Color JavaDoc getAttributeColor(String JavaDoc attribute, Color JavaDoc defaultColor, boolean lookInParentToo)
302     {
303         Object JavaDoc obj = this.getAttribute(attribute);
304         if (obj == null) return defaultColor;
305         if (obj instanceof Color JavaDoc) return (Color JavaDoc)obj;
306         Color JavaDoc c = ColorSelectorPanel.parseColorString(obj+"");
307         if (c != null) return c;
308         
309         return defaultColor;
310     }
311     
312     public boolean getAttributeBoolean(String JavaDoc attribute, boolean defaultBoolean)
313     {
314         return getAttributeBoolean(attribute, defaultBoolean, false);
315     }
316     
317      /**
318      * This method is the same as getAttribute(String attribute, true).
319      */

320     public boolean getAttributeBoolean(String JavaDoc attribute, boolean defaultBoolean, boolean lookInParentToo)
321     {
322         Object JavaDoc obj = this.getAttribute(attribute, lookInParentToo);
323         if (obj == null) return defaultBoolean;
324         if (obj instanceof Boolean JavaDoc) return ((Boolean JavaDoc)obj).booleanValue();
325         try {
326              return Boolean.valueOf(obj+"").booleanValue();
327         } catch (Exception JavaDoc ex)
328         {
329             ex.printStackTrace();
330         }
331         return defaultBoolean;
332     }
333        
334     public Style getParent()
335     {
336         Object JavaDoc obj = this.getAttributes().get(ATTRIBUTE_style);
337         if (obj == null || !(obj instanceof Style))
338         {
339             this.getAttributes().remove(ATTRIBUTE_style);
340             return null;
341         }
342         
343         return (Style)obj;
344     }
345         
346     public String JavaDoc toString()
347     {
348         return this.getName()+"";
349     }
350
351     public Vector JavaDoc getConditionedStyles() {
352         return conditionedStyles;
353     }
354
355     public void setConditionedStyles(Vector JavaDoc conditionedStyles) {
356         this.conditionedStyles = conditionedStyles;
357     }
358     
359     
360     public static boolean isDefaultValue(Object JavaDoc value, String JavaDoc attributeName, Style elementStyle, Style defaultStyle)
361     {
362         
363         if (elementStyle == null && defaultStyle == null)
364         {
365             return false;
366         }
367         
368         Style referenceStyle = (elementStyle == null) ? defaultStyle : elementStyle;
369         
370         Object JavaDoc defaultValue = referenceStyle.getAttribute(attributeName, true);
371         
372         if (defaultValue == null) return false;
373         
374         if (attributeName.endsWith("color") && value instanceof Color JavaDoc)
375         {
376             Color JavaDoc color = it.businesslogic.ireport.gui.sheet.ColorSelectorPanel.parseColorString(""+defaultValue);
377             return color.equals((Color JavaDoc)value);
378         }
379         
380         return defaultValue.equals(value);
381     }
382 }
383
Popular Tags