KickJava   Java API By Example, From Geeks To Geeks.

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


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 form field AJAX tag.
30  *
31  * @author Darren Spurgeon
32  * @author Jens Kapitza
33  * @version $Revision: 1.2 $ $Date: 2007/06/20 20:55:56 $ $Author: jenskapitza $
34  */

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