KickJava   Java API By Example, From Geeks To Geeks.

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


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.StringUtils;
25 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
26
27 /**
28  * Tag handler for the autocomplete AJAX tag.
29  *
30  * @author Darren Spurgeon
31  * @version $Revision: 1.2 $ $Date: 2007/06/20 20:55:56 $ $Author: jenskapitza $
32  */

33 public class AjaxAutocompleteTag extends TagSupport JavaDoc {
34
35   /**
36      * serialVersionUID
37      */

38     private static final long serialVersionUID = -6332721548834673822L;
39
40 private String JavaDoc var;
41
42   private String JavaDoc attachTo;
43
44   private String JavaDoc baseUrl;
45
46   private String JavaDoc source;
47
48   private String JavaDoc target;
49
50   private String JavaDoc parameters;
51
52   private String JavaDoc minimumCharacters;
53
54   private String JavaDoc appendSeparator;
55
56   private String JavaDoc className;
57
58   private String JavaDoc preFunction;
59
60   private String JavaDoc postFunction;
61
62   private String JavaDoc errorFunction;
63
64   private String JavaDoc parser;
65
66   private String JavaDoc indicator;
67
68   public String JavaDoc getAppendSeparator() {
69     return appendSeparator;
70   }
71
72   public void setAppendSeparator(String JavaDoc appendSeparator) {
73     this.appendSeparator = appendSeparator;
74   }
75
76   public String JavaDoc getBaseUrl() {
77     return baseUrl;
78   }
79
80   public void setBaseUrl(String JavaDoc baseUrl) {
81     this.baseUrl = baseUrl;
82   }
83
84   public String JavaDoc getClassName() {
85     return className;
86   }
87
88   public void setClassName(String JavaDoc className) {
89     this.className = className;
90   }
91
92   public String JavaDoc getErrorFunction() {
93     return errorFunction;
94   }
95
96   public void setErrorFunction(String JavaDoc errorFunction) {
97     this.errorFunction = errorFunction;
98   }
99
100   public String JavaDoc getIndicator() {
101     return indicator;
102   }
103
104   public void setIndicator(String JavaDoc indicator) {
105     this.indicator = indicator;
106   }
107
108   public String JavaDoc getMinimumCharacters() {
109     return minimumCharacters;
110   }
111
112   public void setMinimumCharacters(String JavaDoc minimumCharacters) {
113     this.minimumCharacters = minimumCharacters;
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   @Override JavaDoc
181   public int doStartTag() throws JspException JavaDoc {
182     // Required Properties
183
this.baseUrl = (String JavaDoc) ExpressionEvaluatorManager.evaluate("baseUrl", this.baseUrl,
184         String JavaDoc.class, this, super.pageContext);
185     this.source = (String JavaDoc) ExpressionEvaluatorManager.evaluate("source", this.source, String JavaDoc.class,
186         this, super.pageContext);
187     this.target = (String JavaDoc) ExpressionEvaluatorManager.evaluate("target", this.target, String JavaDoc.class,
188         this, super.pageContext);
189     this.className = (String JavaDoc) ExpressionEvaluatorManager.evaluate("className", this.className,
190         String JavaDoc.class, this, super.pageContext);
191
192     // Optional Properties
193
if (this.var != null) {
194       this.var = (String JavaDoc) ExpressionEvaluatorManager.evaluate("var", this.var, String JavaDoc.class, this,
195           super.pageContext);
196     }
197     if (this.attachTo != null) {
198       this.attachTo = (String JavaDoc) ExpressionEvaluatorManager.evaluate("attachTo", this.attachTo,
199           String JavaDoc.class, this, super.pageContext);
200     }
201     if (this.parameters != null) {
202       this.parameters = (String JavaDoc) ExpressionEvaluatorManager.evaluate("parameters", this.parameters,
203           String JavaDoc.class, this, super.pageContext);
204     }
205     if (this.minimumCharacters != null) {
206       this.minimumCharacters = (String JavaDoc) ExpressionEvaluatorManager.evaluate("minimumCharacters",
207           this.minimumCharacters, String JavaDoc.class, this, super.pageContext);
208     }
209     if (this.indicator != null) {
210       this.indicator = (String JavaDoc) ExpressionEvaluatorManager.evaluate("indicator",
211           this.indicator, String JavaDoc.class, this, super.pageContext);
212     }
213     if (this.preFunction != null) {
214       this.preFunction = (String JavaDoc) ExpressionEvaluatorManager.evaluate("preFunction",
215           this.preFunction, String JavaDoc.class, this, super.pageContext);
216     }
217     if (this.postFunction != null) {
218       this.postFunction = (String JavaDoc) ExpressionEvaluatorManager.evaluate("postFunction",
219           this.postFunction, String JavaDoc.class, this, super.pageContext);
220     }
221     if (this.errorFunction != null) {
222       this.errorFunction = (String JavaDoc) ExpressionEvaluatorManager.evaluate("errorFunction",
223           this.errorFunction, String JavaDoc.class, this, super.pageContext);
224     }
225     return SKIP_BODY;
226   }
227
228   @Override JavaDoc
229   public int doEndTag() throws JspException JavaDoc {
230     OptionsBuilder options = new OptionsBuilder();
231     options.add("source", this.source, true).add("target", this.target, true).add("className",
232         this.className, true);
233     if (this.parameters != null)
234       options.add("parameters", this.parameters, true);
235     if (this.indicator != null)
236       options.add("indicator", this.indicator, true);
237     if (this.minimumCharacters != null)
238       options.add("minimumCharacters", this.minimumCharacters, true);
239     if (this.appendSeparator != null)
240       options.add("appendSeparator", this.appendSeparator, true);
241     if (this.preFunction != null)
242       options.add("preFunction", this.preFunction, false);
243     if (this.postFunction != null)
244       options.add("postFunction", this.postFunction, false);
245     if (this.errorFunction != null)
246       options.add("errorFunction", this.errorFunction, false);
247     if (this.parser != null)
248       options.add("parser", this.parser, false);
249
250     StringBuffer JavaDoc script = new StringBuffer JavaDoc();
251     script.append("<script type=\"text/javascript\">\n");
252     if (StringUtils.isNotEmpty(this.var)) {
253       if (StringUtils.isNotEmpty(this.attachTo)) {
254         script.append(this.attachTo).append(".").append(this.var);
255       } else {
256         script.append("var ").append(this.var);
257       }
258       script.append(" = ");
259     }
260     script
261         .append("new AjaxJspTag.Autocomplete(\n")
262         .append("\"")
263         .append(this.baseUrl)
264         .append("\", {\n")
265         .append(options.toString())
266         .append("});\n")
267         .append("</script>\n\n");
268
269     JspWriter JavaDoc writer = pageContext.getOut();
270     try {
271       writer.println(script);
272     } catch (IOException JavaDoc e) {
273       throw new JspException JavaDoc(e.getMessage());
274     }
275     return EVAL_PAGE;
276   }
277
278   @Override JavaDoc
279   public void release() {
280     this.var = null;
281     this.attachTo = null;
282     this.baseUrl = null;
283     this.className = null;
284     this.preFunction = null;
285     this.errorFunction = null;
286     this.minimumCharacters = null;
287     this.appendSeparator = null;
288     this.parameters = null;
289     this.postFunction = null;
290     this.source = null;
291     this.target = null;
292     this.parser = null;
293     this.indicator = null;
294     super.release();
295   }
296
297 }
298
Popular Tags