KickJava   Java API By Example, From Geeks To Geeks.

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


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

17 package org.ajaxtags.tags;
18
19 import java.io.IOException JavaDoc;
20
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.JspWriter JavaDoc;
23 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
24
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
27
28 /**
29  * Tag handler for the select AJAX tag.
30  *
31  * @author Darren Spurgeon
32  * @author Jens Kapitza
33  * @version $Revision: 1.3 $ $Date: 2007/07/08 17:52:30 $ $Author: jenskapitza $
34  *
35  */

36 public class AjaxSelectTag extends TagSupport JavaDoc {
37
38     private String JavaDoc var;
39
40     private String JavaDoc attachTo;
41     private String JavaDoc emptyOptionValue;
42     private String JavaDoc emptyOptionName;
43
44     public void setEmptyOptionName(String JavaDoc emptyOptionName) {
45         this.emptyOptionName = emptyOptionName;
46     }
47
48     public String JavaDoc getEmptyOptionName() {
49         return emptyOptionName;
50     }
51
52     public void setEmptyOptionValue(String JavaDoc emptyOptionValue) {
53         this.emptyOptionValue = emptyOptionValue;
54     }
55
56     public String JavaDoc getEmptyOptionValue() {
57         return emptyOptionValue;
58     }
59
60     private boolean doPost;
61
62     public void setDoPost(boolean doPost) {
63         this.doPost = doPost;
64     }
65
66     public boolean getDoPost() {
67         return this.doPost;
68     }
69
70     private String JavaDoc baseUrl;
71
72     private String JavaDoc source;
73
74     private String JavaDoc target;
75
76     private String JavaDoc parameters;
77
78     private String JavaDoc eventType;
79
80     private String JavaDoc executeOnLoad;
81
82     private String JavaDoc defaultOptions;
83
84     private String JavaDoc postFunction;
85
86     private String JavaDoc preFunction;
87
88     private String JavaDoc errorFunction;
89
90     private String JavaDoc parser;
91
92     public String JavaDoc getBaseUrl() {
93         return baseUrl;
94     }
95
96     public void setBaseUrl(String JavaDoc baseUrl) {
97         this.baseUrl = baseUrl;
98     }
99
100     public String JavaDoc getErrorFunction() {
101         return errorFunction;
102     }
103
104     public void setErrorFunction(String JavaDoc errorFunction) {
105         this.errorFunction = errorFunction;
106     }
107
108     public String JavaDoc getEventType() {
109         return eventType;
110     }
111
112     public void setEventType(String JavaDoc eventType) {
113         this.eventType = eventType;
114     }
115
116     public String JavaDoc getVar() {
117         return var;
118     }
119
120     public void setVar(String JavaDoc var) {
121         this.var = var;
122     }
123
124     public String JavaDoc getAttachTo() {
125         return attachTo;
126     }
127
128     public void setAttachTo(String JavaDoc attachTo) {
129         this.attachTo = attachTo;
130     }
131
132     public String JavaDoc getParameters() {
133         return parameters;
134     }
135
136     public void setParameters(String JavaDoc parameters) {
137         this.parameters = parameters;
138     }
139
140     public String JavaDoc getParser() {
141         return parser;
142     }
143
144     public void setParser(String JavaDoc parser) {
145         this.parser = parser;
146     }
147
148     public String JavaDoc getPreFunction() {
149         return preFunction;
150     }
151
152     public void setPreFunction(String JavaDoc preFunction) {
153         this.preFunction = preFunction;
154     }
155
156     public String JavaDoc getPostFunction() {
157         return postFunction;
158     }
159
160     public void setPostFunction(String JavaDoc postFunction) {
161         this.postFunction = postFunction;
162     }
163
164     public String JavaDoc getSource() {
165         return source;
166     }
167
168     public void setSource(String JavaDoc source) {
169         this.source = source;
170     }
171
172     public String JavaDoc getTarget() {
173         return target;
174     }
175
176     public void setTarget(String JavaDoc target) {
177         this.target = target;
178     }
179
180     public String JavaDoc getExecuteOnLoad() {
181         return executeOnLoad;
182     }
183
184     public void setExecuteOnLoad(String JavaDoc executeOnLoad) {
185         this.executeOnLoad = executeOnLoad;
186     }
187
188     public String JavaDoc getDefaultOptions() {
189         return defaultOptions;
190     }
191
192     public void setDefaultOptions(String JavaDoc defaultOptions) {
193         this.defaultOptions = defaultOptions;
194     }
195
196     @Override JavaDoc
197     public int doStartTag() throws JspException JavaDoc {
198         // Required Properties
199
this.baseUrl = (String JavaDoc) ExpressionEvaluatorManager.evaluate("baseUrl",
200                 this.baseUrl, String JavaDoc.class, this, super.pageContext);
201         this.source = (String JavaDoc) ExpressionEvaluatorManager.evaluate("source",
202                 this.source, String JavaDoc.class, this, super.pageContext);
203         this.target = (String JavaDoc) ExpressionEvaluatorManager.evaluate("target",
204                 this.target, String JavaDoc.class, this, super.pageContext);
205         this.parameters = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
206                 "parameters", this.parameters, String JavaDoc.class, this,
207                 super.pageContext);
208
209         if (this.emptyOptionValue != null) {
210             this.emptyOptionValue = (String JavaDoc) ExpressionEvaluatorManager
211                     .evaluate("emptyOptionValue", this.emptyOptionValue,
212                             String JavaDoc.class, this, super.pageContext);
213         }
214         if (this.emptyOptionName != null) {
215             this.emptyOptionName = (String JavaDoc) ExpressionEvaluatorManager
216                     .evaluate("emptyOptionName", this.emptyOptionName,
217                             String JavaDoc.class, this, super.pageContext);
218         }
219
220         // Optional Properties
221
if (this.var != null) {
222             this.var = (String JavaDoc) ExpressionEvaluatorManager.evaluate("var",
223                     this.var, String JavaDoc.class, this, super.pageContext);
224         }
225         if (this.attachTo != null) {
226             this.attachTo = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
227                     "attachTo", this.attachTo, String JavaDoc.class, this,
228                     super.pageContext);
229         }
230         if (this.eventType != null) {
231             this.eventType = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
232                     "eventType", this.eventType, String JavaDoc.class, this,
233                     super.pageContext);
234         }
235         if (this.defaultOptions != null) {
236             this.defaultOptions = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
237                     "defaultOptions", this.defaultOptions, String JavaDoc.class, this,
238                     super.pageContext);
239         }
240         if (this.preFunction != null) {
241             this.preFunction = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
242                     "preFunction", this.preFunction, String JavaDoc.class, this,
243                     super.pageContext);
244         }
245         if (this.postFunction != null) {
246             this.postFunction = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
247                     "postFunction", this.postFunction, String JavaDoc.class, this,
248                     super.pageContext);
249         }
250         if (this.errorFunction != null) {
251             this.errorFunction = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
252                     "errorFunction", this.errorFunction, String JavaDoc.class, this,
253                     super.pageContext);
254         }
255         return SKIP_BODY;
256     }
257
258     @Override JavaDoc
259     public int doEndTag() throws JspException JavaDoc {
260         OptionsBuilder options = new OptionsBuilder();
261         options.add("source", this.source, true).add("target", this.target,
262                 true).add("parameters", this.parameters, true);
263         if (this.executeOnLoad != null)
264             options.add("executeOnLoad", this.executeOnLoad, true);
265         if (this.eventType != null)
266             options.add("eventType", this.eventType, true);
267         if (this.defaultOptions != null)
268             options.add("defaultOptions", this.defaultOptions, true);
269         if (this.preFunction != null)
270             options.add("preFunction", this.preFunction, false);
271         if (this.postFunction != null)
272             options.add("postFunction", this.postFunction, false);
273         if (this.errorFunction != null)
274             options.add("errorFunction", this.errorFunction, false);
275         if (this.parser != null)
276             options.add("parser", this.parser, false);
277         if (this.doPost)
278             options.add("doPost", String.valueOf(this.doPost), false);
279
280         if (this.emptyOptionValue != null)
281             options.add("emptyOptionValue", emptyOptionValue, true);
282         if (this.emptyOptionName != null)
283             options.add("emptyOptionName", emptyOptionName, true);
284
285         StringBuffer JavaDoc script = new StringBuffer JavaDoc();
286         script.append("<script type=\"text/javascript\">\n");
287         if (StringUtils.isNotEmpty(this.var)) {
288             if (StringUtils.isNotEmpty(this.attachTo)) {
289                 script.append(this.attachTo).append(".").append(this.var);
290             } else {
291                 script.append("var ").append(this.var);
292             }
293             script.append(" = ");
294         }
295         script.append("new AjaxJspTag.Select(\n");
296         script.append("\"");
297         script.append(this.baseUrl);
298         script.append("\", {\n");
299         script.append(options.toString());
300         script.append("});\n");
301         script.append("</script>\n\n");
302
303         JspWriter JavaDoc writer = pageContext.getOut();
304         try {
305             writer.println(script);
306         } catch (IOException JavaDoc e) {
307             throw new JspException JavaDoc(e.getMessage());
308         }
309         return EVAL_PAGE;
310     }
311
312     @Override JavaDoc
313     public void release() {
314         this.var = null;
315         this.attachTo = null;
316         this.baseUrl = null;
317         this.source = null;
318         this.target = null;
319         this.parameters = null;
320         this.eventType = null;
321         this.executeOnLoad = null;
322         this.defaultOptions = null;
323         this.preFunction = null;
324         this.postFunction = null;
325         this.errorFunction = null;
326         this.parser = null;
327         super.release();
328     }
329
330 }
331
Popular Tags