KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > collection > CollectionStyleTag


1 package fr.improve.struts.taglib.layout.collection;
2
3 import javax.servlet.jsp.JspException JavaDoc;
4
5 import org.apache.struts.taglib.html.Constants;
6
7 import fr.improve.struts.taglib.layout.util.LayoutUtils;
8 /**
9  * Allow to fix the style class of the inner collectionItem tags.
10  * @author: JeanNoel Ribette
11  */

12 public class CollectionStyleTag extends javax.servlet.jsp.tagext.TagSupport JavaDoc {
13     protected String JavaDoc name = Constants.BEAN_KEY;
14     protected String JavaDoc property;
15     protected String JavaDoc value;
16     protected String JavaDoc valueName;
17     protected String JavaDoc valueProperty;
18     protected String JavaDoc matchStyleClass;
19     protected String JavaDoc nonMatchStyleClass;
20     protected String JavaDoc matchStyle;
21     protected String JavaDoc nonMatchStyle;
22
23     protected String JavaDoc style;
24     protected String JavaDoc styleClass;
25
26     protected BaseCollectionTag collectionTag;
27     
28     protected boolean notNull = false;
29     protected String JavaDoc greaterThan;
30     protected String JavaDoc lessThan;
31
32 public int doEndTag() {
33     if (styleClass!=null) {
34         collectionTag.setTempStyleClass(null);
35         styleClass = null;
36     }
37     if (style!=null) {
38         collectionTag.removeTempStyle();
39         style = null;
40     }
41     collectionTag = null;
42     return EVAL_PAGE;
43 }
44 public int doStartTag() throws JspException JavaDoc {
45     try {
46         // get the collection
47
collectionTag = (BaseCollectionTag) findAncestorWithClass(this, BaseCollectionTag.class);
48         if (collectionTag==null) throw new JspException JavaDoc("Invalid use of collectionStyle tag");
49         if (collectionTag.first) return EVAL_BODY_INCLUDE;
50
51         // calculate the styleClass
52
if (checkCondition()) {
53             styleClass = matchStyleClass;
54             style = matchStyle;
55         } else {
56             styleClass = nonMatchStyleClass;
57             style = nonMatchStyle;
58         }
59
60         // set the styleClass for the inner item.
61
if (styleClass!=null) {
62             collectionTag.setTempStyleClass(styleClass);
63         }
64         if (style!=null) {
65             collectionTag.addTempStyle(style);
66         }
67         
68     } catch (ClassCastException JavaDoc e) {
69         throw new JspException JavaDoc("Invalid use of collectionStyle tag");
70     }
71         
72     return EVAL_BODY_INCLUDE;
73 }
74
75     protected boolean checkCondition() throws JspException JavaDoc {
76         Object JavaDoc lc_value = pageContext.findAttribute(name);
77         if (lc_value!=null) {
78             lc_value = LayoutUtils.getProperty(lc_value, property);
79         }
80         
81         if (greaterThan!=null) {
82             return checkGreaterThan(lc_value);
83         }else if (lessThan!=null) {
84             return checkLessThan(lc_value);
85         } else if (notNull) {
86             return lc_value!=null;
87         } else {
88         
89             Object JavaDoc lc_valueToCheck = null;
90             if (valueName!=null) {
91                 lc_valueToCheck = pageContext.findAttribute(valueName);
92                 if (lc_valueToCheck!=null) lc_valueToCheck = LayoutUtils.getProperty(lc_valueToCheck, valueProperty);
93             } else {
94                 lc_valueToCheck = value;
95             }
96             
97             if (lc_valueToCheck!=null && lc_value!=null && lc_valueToCheck.toString().equals(lc_value.toString())) {
98                 return true;
99             } else {
100                 return false;
101             }
102         }
103     }
104
105     private boolean checkLessThan(Object JavaDoc in_value) {
106         return checkGreaterThan(lessThan, in_value);
107     }
108
109     private boolean checkGreaterThan(Object JavaDoc in_value) {
110         return checkGreaterThan(in_value, greaterThan);
111     }
112     
113     private boolean checkGreaterThan(Object JavaDoc in_value, Object JavaDoc greaterThan) {
114         if (in_value==null || greaterThan==null) {
115             return false;
116         }
117         
118         Number JavaDoc n1;
119         Number JavaDoc n2;
120         if (in_value instanceof Number JavaDoc) {
121             n1 = (Number JavaDoc) in_value;
122         } else {
123             n1 = new Float JavaDoc(in_value.toString());
124         }
125         if (greaterThan instanceof Number JavaDoc) {
126             n2 = (Number JavaDoc) greaterThan;
127         } else {
128             n2 = new Float JavaDoc(greaterThan.toString());
129         }
130         return n1.doubleValue() >= n2.doubleValue();
131     }
132 /**
133  * Insert the method's description here.
134  * Creation date: (09/10/2001 16:24:12)
135  * @return java.lang.String
136  */

137 public java.lang.String JavaDoc getMatchStyleClass() {
138     return matchStyleClass;
139 }
140 /**
141  * Insert the method's description here.
142  * Creation date: (09/10/2001 16:14:52)
143  * @return java.lang.String
144  */

145 public java.lang.String JavaDoc getName() {
146     return name;
147 }
148 /**
149  * Insert the method's description here.
150  * Creation date: (09/10/2001 16:24:12)
151  * @return java.lang.String
152  */

153 public java.lang.String JavaDoc getNonMatchStyleClass() {
154     return nonMatchStyleClass;
155 }
156 /**
157  * Insert the method's description here.
158  * Creation date: (09/10/2001 16:14:52)
159  * @return java.lang.String
160  */

161 public java.lang.String JavaDoc getProperty() {
162     return property;
163 }
164 /**
165  * Insert the method's description here.
166  * Creation date: (09/10/2001 16:14:52)
167  * @return java.lang.String
168  */

169 public java.lang.String JavaDoc getValue() {
170     return value;
171 }
172 /**
173  * Insert the method's description here.
174  * Creation date: (16/10/2001 12:58:47)
175  * @return java.lang.String
176  */

177 public java.lang.String JavaDoc getValueName() {
178     return valueName;
179 }
180 /**
181  * Insert the method's description here.
182  * Creation date: (16/10/2001 12:58:48)
183  * @return java.lang.String
184  */

185 public java.lang.String JavaDoc getValueProperty() {
186     return valueProperty;
187 }
188     public void release() {
189         super.release();
190         name = Constants.BEAN_KEY;
191         property = null;
192         value = null;
193         valueName = null;
194         valueProperty = null;
195         matchStyleClass = null;
196         nonMatchStyleClass = null;
197         matchStyle = null;
198         nonMatchStyle = null;
199         collectionTag = null;
200         
201         greaterThan = null;
202         lessThan = null;
203     }
204 /**
205  * Insert the method's description here.
206  * Creation date: (09/10/2001 16:24:12)
207  * @param newMatchStyleClass java.lang.String
208  */

209 public void setMatchStyleClass(java.lang.String JavaDoc newMatchStyleClass) {
210     matchStyleClass = newMatchStyleClass;
211 }
212 /**
213  * Insert the method's description here.
214  * Creation date: (09/10/2001 16:14:52)
215  * @param newName java.lang.String
216  */

217 public void setName(java.lang.String JavaDoc newName) {
218     name = newName;
219 }
220 /**
221  * Insert the method's description here.
222  * Creation date: (09/10/2001 16:24:12)
223  * @param newNonMatchStyleClass java.lang.String
224  */

225 public void setNonMatchStyleClass(java.lang.String JavaDoc newNonMatchStyleClass) {
226     nonMatchStyleClass = newNonMatchStyleClass;
227 }
228 /**
229  * Insert the method's description here.
230  * Creation date: (09/10/2001 16:14:52)
231  * @param newProperty java.lang.String
232  */

233 public void setProperty(java.lang.String JavaDoc newProperty) {
234     property = newProperty;
235 }
236 /**
237  * Insert the method's description here.
238  * Creation date: (09/10/2001 16:14:52)
239  * @param newValue java.lang.String
240  */

241 public void setValue(java.lang.String JavaDoc newValue) {
242     value = newValue;
243 }
244 /**
245  * Insert the method's description here.
246  * Creation date: (16/10/2001 12:58:48)
247  * @param newValueName java.lang.String
248  */

249 public void setValueName(java.lang.String JavaDoc newValueName) {
250     valueName = newValueName;
251 }
252 /**
253  * Insert the method's description here.
254  * Creation date: (16/10/2001 12:58:48)
255  * @param newValueProperty java.lang.String
256  */

257 public void setValueProperty(java.lang.String JavaDoc newValueProperty) {
258     valueProperty = newValueProperty;
259 }
260     /**
261      * Sets the matchStyle.
262      * @param matchStyle The matchStyle to set
263      */

264     public void setMatchStyle(String JavaDoc matchStyle) {
265         this.matchStyle = matchStyle;
266     }
267
268     /**
269      * Sets the nonMatchStyle.
270      * @param nonMatchStyle The nonMatchStyle to set
271      */

272     public void setNonMatchStyle(String JavaDoc nonMatchStyle) {
273         this.nonMatchStyle = nonMatchStyle;
274     }
275     
276     public void setPresent(boolean in_bool) {
277         notNull = in_bool;
278     }
279     
280     public void setGreaterThan(String JavaDoc in_greater) {
281         greaterThan = in_greater;
282     }
283     
284     public void setLessThan(String JavaDoc in_less){
285         lessThan = in_less;
286     }
287
288 }
289
Popular Tags