KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ajaxtags > tags > AjaxToggleTag


1 /**
2  * Copyright 2005 Darren L. Spurgeon
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.ajaxtags.tags;
17
18 import java.io.IOException JavaDoc;
19
20 import javax.servlet.jsp.JspException JavaDoc;
21 import javax.servlet.jsp.JspWriter JavaDoc;
22 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
23
24 import org.apache.commons.lang.BooleanUtils;
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
27
28 /**
29  * Tag handler for the toggle (on/off, true/false) AJAX tag.
30  *
31  * @author Darren Spurgeon
32  * @version $Revision: 1.3 $ $Date: 2007/06/20 20:55:56 $ $Author: jenskapitza $
33  */

34 public class AjaxToggleTag extends TagSupport JavaDoc {
35
36     private String JavaDoc var;
37
38     private String JavaDoc attachTo;
39
40     private String JavaDoc baseUrl;
41
42     private String JavaDoc parameters;
43
44     private String JavaDoc source;
45
46     private String JavaDoc ratings;
47
48     private String JavaDoc defaultRating;
49
50     private String JavaDoc state;
51
52     private String JavaDoc onOff;
53
54     private String JavaDoc containerClass;
55
56     private String JavaDoc messageClass;
57
58     private String JavaDoc selectedClass;
59
60     private String JavaDoc selectedOverClass;
61
62     private String JavaDoc selectedLessClass;
63
64     private String JavaDoc overClass;
65
66     private String JavaDoc preFunction;
67
68     private String JavaDoc postFunction;
69
70     private String JavaDoc errorFunction;
71
72     private String JavaDoc parser;
73     
74     private String JavaDoc updateFunction;
75     public String JavaDoc getUpdateFunction() {
76         return updateFunction;
77     }
78     public void setUpdateFunction(String JavaDoc updateFunction) {
79         this.updateFunction = updateFunction;
80     }
81
82     public String JavaDoc getBaseUrl() {
83         return baseUrl;
84     }
85
86     public void setBaseUrl(String JavaDoc baseUrl) {
87         this.baseUrl = baseUrl;
88     }
89
90     public String JavaDoc getContainerClass() {
91         return containerClass;
92     }
93
94     public void setContainerClass(String JavaDoc containerClass) {
95         this.containerClass = containerClass;
96     }
97
98     public String JavaDoc getDefaultRating() {
99         return defaultRating;
100     }
101
102     public void setDefaultRating(String JavaDoc defaultRating) {
103         this.defaultRating = defaultRating;
104     }
105
106     public String JavaDoc getErrorFunction() {
107         return errorFunction;
108     }
109
110     public void setErrorFunction(String JavaDoc errorFunction) {
111         this.errorFunction = errorFunction;
112     }
113
114     public String JavaDoc getSource() {
115         return source;
116     }
117
118     public void setSource(String JavaDoc source) {
119         this.source = source;
120     }
121
122     public String JavaDoc getMessageClass() {
123         return messageClass;
124     }
125
126     public void setMessageClass(String JavaDoc messageClass) {
127         this.messageClass = messageClass;
128     }
129
130     public String JavaDoc getOnOff() {
131         return onOff;
132     }
133
134     public void setOnOff(String JavaDoc onOff) {
135         this.onOff = onOff;
136     }
137
138     public String JavaDoc getOverClass() {
139         return overClass;
140     }
141
142     public void setOverClass(String JavaDoc overClass) {
143         this.overClass = overClass;
144     }
145
146     public String JavaDoc getParameters() {
147         return parameters;
148     }
149
150     public void setParameters(String JavaDoc parameters) {
151         this.parameters = parameters;
152     }
153
154     public String JavaDoc getParser() {
155         return parser;
156     }
157
158     public void setParser(String JavaDoc parser) {
159         this.parser = parser;
160     }
161
162     public String JavaDoc getPostFunction() {
163         return postFunction;
164     }
165
166     public void setPostFunction(String JavaDoc postFunction) {
167         this.postFunction = postFunction;
168     }
169
170     public String JavaDoc getPreFunction() {
171         return preFunction;
172     }
173
174     public void setPreFunction(String JavaDoc preFunction) {
175         this.preFunction = preFunction;
176     }
177
178     public String JavaDoc getRatings() {
179         return ratings;
180     }
181
182     public void setRatings(String JavaDoc ratings) {
183         this.ratings = ratings;
184     }
185
186     public String JavaDoc getSelectedClass() {
187         return selectedClass;
188     }
189
190     public void setSelectedClass(String JavaDoc selectedClass) {
191         this.selectedClass = selectedClass;
192     }
193
194     public String JavaDoc getSelectedLessClass() {
195         return selectedLessClass;
196     }
197
198     public void setSelectedLessClass(String JavaDoc selectedLessClass) {
199         this.selectedLessClass = selectedLessClass;
200     }
201
202     public String JavaDoc getSelectedOverClass() {
203         return selectedOverClass;
204     }
205
206     public void setSelectedOverClass(String JavaDoc selectedOverClass) {
207         this.selectedOverClass = selectedOverClass;
208     }
209
210     public String JavaDoc getState() {
211         return state;
212     }
213
214     public void setState(String JavaDoc state) {
215         this.state = state;
216     }
217
218     public String JavaDoc getVar() {
219         return var;
220     }
221
222     public void setVar(String JavaDoc var) {
223         this.var = var;
224     }
225
226     public String JavaDoc getAttachTo() {
227         return attachTo;
228     }
229
230     public void setAttachTo(String JavaDoc attachTo) {
231         this.attachTo = attachTo;
232     }
233
234     @Override JavaDoc
235     public int doStartTag() throws JspException JavaDoc {
236         // Required Properties
237
this.baseUrl = (String JavaDoc) ExpressionEvaluatorManager.evaluate("baseUrl",
238                 this.baseUrl, String JavaDoc.class, this, super.pageContext);
239         this.source = (String JavaDoc) ExpressionEvaluatorManager.evaluate("source",
240                 this.source, String JavaDoc.class, this, super.pageContext);
241         this.ratings = (String JavaDoc) ExpressionEvaluatorManager.evaluate("ratings",
242                 this.ratings, String JavaDoc.class, this, super.pageContext);
243         this.containerClass = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
244                 "containerClass", this.containerClass, String JavaDoc.class, this,
245                 super.pageContext);
246         this.selectedClass = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
247                 "selectedClass", this.selectedClass, String JavaDoc.class, this,
248                 super.pageContext);
249         this.selectedOverClass = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
250                 "selectedOverClass", this.selectedOverClass, String JavaDoc.class,
251                 this, super.pageContext);
252         this.selectedLessClass = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
253                 "selectedLessClass", this.selectedLessClass, String JavaDoc.class,
254                 this, super.pageContext);
255         this.overClass = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
256                 "overClass", this.overClass, String JavaDoc.class, this,
257                 super.pageContext);
258
259         // Optional Properties
260
if (this.var != null) {
261             this.var = (String JavaDoc) ExpressionEvaluatorManager.evaluate("var",
262                     this.var, String JavaDoc.class, this, super.pageContext);
263         }
264         if (this.attachTo != null) {
265             this.attachTo = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
266                     "attachTo", this.attachTo, String JavaDoc.class, this,
267                     super.pageContext);
268         }
269         if (this.messageClass != null) {
270             this.messageClass = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
271                     "messageClass", this.messageClass, String JavaDoc.class, this,
272                     super.pageContext);
273         }
274         if (this.parameters != null) {
275             this.parameters = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
276                     "parameters", this.parameters, String JavaDoc.class, this,
277                     super.pageContext);
278         }
279         if (this.state != null) {
280             this.state = (String JavaDoc) ExpressionEvaluatorManager.evaluate("state",
281                     this.state, String JavaDoc.class, this, super.pageContext);
282         }
283         if (this.onOff != null) {
284             this.onOff = (String JavaDoc) ExpressionEvaluatorManager.evaluate("onOff",
285                     this.onOff, String JavaDoc.class, this, super.pageContext);
286         }
287         if (this.defaultRating != null) {
288             this.defaultRating = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
289                     "defaultRating", this.defaultRating, String JavaDoc.class, this,
290                     super.pageContext);
291         }
292         if (this.preFunction != null) {
293             this.preFunction = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
294                     "preFunction", this.preFunction, String JavaDoc.class, this,
295                     super.pageContext);
296         }
297         if (this.postFunction != null) {
298             this.postFunction = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
299                     "postFunction", this.postFunction, String JavaDoc.class, this,
300                     super.pageContext);
301         }
302         if (this.errorFunction != null) {
303             this.errorFunction = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
304                     "errorFunction", this.errorFunction, String JavaDoc.class, this,
305                     super.pageContext);
306         }
307
308         return SKIP_BODY;
309     }
310
311     @Override JavaDoc
312     public int doEndTag() throws JspException JavaDoc {
313         OptionsBuilder options = new OptionsBuilder();
314         options.add("source", this.source, true).add("ratings", this.ratings,
315                 true).add("containerClass", this.containerClass, true).add(
316                 "selectedClass", this.selectedClass, true).add(
317                 "selectedOverClass", this.selectedOverClass, true).add(
318                 "selectedLessClass", this.selectedLessClass, true).add(
319                 "overClass", this.overClass, true);
320
321         if (this.messageClass != null)
322             options.add("messageClass", this.messageClass, true);
323         if (this.parameters != null)
324             options.add("parameters", this.parameters, true);
325         if (this.state != null)
326             options.add("state", this.state, true);
327         if (this.onOff != null)
328             options.add("onOff", this.onOff, true);
329         if (this.defaultRating != null)
330             options.add("defaultRating", this.defaultRating, true);
331         if (this.preFunction != null)
332             options.add("preFunction", this.preFunction, false);
333         if (this.postFunction != null)
334             options.add("postFunction", this.postFunction, false);
335         if (this.errorFunction != null)
336             options.add("errorFunction", this.errorFunction, false);
337         if (this.parser != null)
338             options.add("parser", this.parser, false);
339         if (this.updateFunction != null)
340             options.add("updateFunction", this.updateFunction, false);
341         
342         StringBuffer JavaDoc script = new StringBuffer JavaDoc();
343         // write opening div
344
script.append("<div id=\"").append(this.source).append("\" class=\"")
345                 .append(this.containerClass);
346         if (this.onOff != null && "true".equalsIgnoreCase(this.onOff)) {
347             script.append(" onoff");
348         }
349         script.append("\">");
350         // write links
351
if (BooleanUtils.toBoolean(this.onOff)) {
352             if (StringUtils.isNotBlank(this.defaultRating)
353                     && StringUtils.isNotBlank(this.ratings)
354                     && this.defaultRating.equalsIgnoreCase(this.ratings
355                             .split(",")[0])) {
356                 script.append("<a HREF=\"javascript:void(0)\" title=\"")
357                         .append(this.ratings.split(",")[0]).append(
358                                 "\" class=\"").append(this.selectedClass)
359                         .append("\"></a>");
360             } else {
361                 script.append("<a HREF=\"javascript:void(0)\" title=\"")
362                         .append(this.ratings.split(",")[1]).append("\"></a>");
363             }
364         } else {
365             boolean ratingMatch = false;
366             String JavaDoc[] ratingValues = this.ratings.split(",");
367             for (int i = 0; i < ratingValues.length; i++) {
368                 String JavaDoc val = ratingValues[i];
369                 if (StringUtils.isBlank(this.defaultRating)
370                         || ratingMatch == true) {
371                     script.append("<a HREF=\"javascript:void(0)\" title=\"")
372                             .append(val).append("\"></a>");
373                 } else if (ratingMatch == false
374                         || this.defaultRating.equalsIgnoreCase(val)) {
375                     script.append("<a HREF=\"javascript:void(0)\" title=\"")
376                             .append(val).append("\" class=\"").append(
377                                     this.selectedClass).append("\"></a>");
378                     if (this.defaultRating.equalsIgnoreCase(val))
379                         ratingMatch = true;
380                 }
381             }
382         }
383         script.append("</div>\n");
384
385         // write script
386
script.append("<script type=\"text/javascript\">\n");
387
388         if (StringUtils.isNotEmpty(this.attachTo)) {
389             script.append(this.attachTo).append(".");
390             if (StringUtils.isNotEmpty(this.var)) {
391                 script.append(this.var);
392             } else {
393                 script.append("aj_").append(this.source);
394             }
395         } else if (StringUtils.isNotEmpty(this.var)) {
396             script.append("var ").append(this.var);
397         } else {
398             script.append("var aj_").append(this.source);
399         }
400
401         script.append(" = new AjaxJspTag.Toggle(\n").append("\"").append(
402                 this.baseUrl).append("\", {\n").append(options.toString())
403                 .append("});\n").append("</script>\n\n");
404
405         JspWriter JavaDoc writer = pageContext.getOut();
406         try {
407             writer.println(script);
408         } catch (IOException JavaDoc e) {
409             throw new JspException JavaDoc(e.getMessage());
410         }
411         return EVAL_PAGE;
412     }
413
414     @Override JavaDoc
415     public void release() {
416         this.var = null;
417         this.attachTo = null;
418         this.baseUrl = null;
419         this.parameters = null;
420         this.source = null;
421         this.ratings = null;
422         this.defaultRating = null;
423         this.state = null;
424         this.onOff = null;
425         this.containerClass = null;
426         this.messageClass = null;
427         this.selectedClass = null;
428         this.selectedOverClass = null;
429         this.selectedLessClass = null;
430         this.overClass = null;
431         this.preFunction = null;
432         this.postFunction = null;
433         this.errorFunction = null;
434         this.parser = null;
435         super.release();
436     }
437 }
438
Popular Tags