KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > tag > sort > SortTag


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

18 package net.sf.uitags.tag.sort;
19
20 import javax.servlet.jsp.JspException JavaDoc;
21
22 import net.sf.uitags.tag.AbstractUiTag;
23 import net.sf.uitags.tagutil.TaglibProperties;
24 import net.sf.uitags.tagutil.validation.RuntimeValidationException;
25 import net.sf.uitags.tagutil.validation.RuntimeValidator;
26 import net.sf.uitags.util.Template;
27
28
29 /**
30  * Renders a Javascript button which sorts options in a select box.
31  *
32  * @see TaglibProperties
33  * @author hgani
34  * @version $Id$
35  */

36 public class SortTag extends AbstractUiTag {
37
38   ///////////////////////////////
39
////////// Constants //////////
40
///////////////////////////////
41

42   /**
43    * Serial Version UID.
44    */

45   private static final long serialVersionUID = 100L;
46
47
48
49   ///////////////////////////////////////////////
50
////////// Property keys (constants) //////////
51
///////////////////////////////////////////////
52

53   private static final String JavaDoc PROP_COMPARATOR = "sort.comparator";
54
55
56
57   ////////////////////////////
58
////////// Fields //////////
59
////////////////////////////
60

61   /**
62    * The "type" tag attribute
63    */

64   private ShortcutType shortcutType;
65   /**
66    * The "applyTo" tag attribute
67    */

68   private String JavaDoc applyTo;
69   /**
70    * The "applyToName" tag attribute
71    */

72   private String JavaDoc applyToName;
73   /**
74    * The "injectTo" tag attribute
75    */

76   private String JavaDoc injectTo;
77   /**
78    * The "injectToName" tag attribute
79    */

80   private String JavaDoc injectToName;
81   /**
82    * The "comparator" tag attribute
83    */

84   private String JavaDoc comparator;
85
86
87
88   //////////////////////////////////
89
////////// Constructors //////////
90
//////////////////////////////////
91

92   /**
93    * Default constructor.
94    */

95   public SortTag() {
96     super();
97   }
98
99
100
101   ///////////////////////////////////////////
102
////////// Tag attribute setters //////////
103
///////////////////////////////////////////
104

105   /**
106    * Tag attribute setter.
107    *
108    * @param val value of the tag attribute
109    */

110   public void setType(String JavaDoc val) {
111     this.shortcutType = ShortcutType.findByName(val);
112   }
113
114   /**
115    * Tag attribute setter.
116    *
117    * @param val value of the tag attribute
118    */

119   public void setApplyTo(String JavaDoc val) {
120     this.applyTo = val;
121   }
122
123   /**
124    * Tag attribute setter.
125    *
126    * @param val value of the tag attribute
127    */

128   public void setApplyToName(String JavaDoc val) {
129     this.applyToName = val;
130   }
131
132   /**
133    * Tag attribute setter.
134    *
135    * @param val value of the tag attribute
136    */

137   public void setInjectTo(String JavaDoc val) {
138     this.injectTo = val;
139   }
140
141   /**
142    * Tag attribute setter.
143    *
144    * @param val value of the tag attribute
145    */

146   public void setInjectToName(String JavaDoc val) {
147     this.injectToName = val;
148   }
149
150   /**
151    * Tag attribute setter.
152    *
153    * @param val value of the tag attribute
154    */

155   public void setComparator(String JavaDoc val) {
156     this.comparator = val;
157   }
158
159
160
161   ///////////////////////////////
162
////////// Tag logic //////////
163
///////////////////////////////
164

165   /**
166    * Instructs web container to evaluate the tag's body.
167    *
168    * @see javax.servlet.jsp.tagext.Tag#doStartTag()
169    * @return <code>EVAL_BODY_INCLUDE</code>
170    * @throws JspException to communicate error
171    */

172   public int doStartTag() throws JspException JavaDoc {
173     return EVAL_BODY_INCLUDE;
174   }
175
176   /**
177    * Prints out HTML code to construct the button.
178    *
179    * @return <code>EVAL_PAGE</code>
180    * @throws JspException to communicate error
181    */

182   public int doEndTag() throws JspException JavaDoc {
183     RuntimeValidator.assertAttributeExclusive(
184         "applyTo", this.applyTo, "applyToName", this.applyToName);
185     RuntimeValidator.assertAttributeExclusive(
186         "injectTo", this.injectTo, "injectToName", this.injectToName);
187
188     RuntimeValidator.assertEitherSpecified(
189         "applyTo", this.applyTo, "applyToName", this.applyToName);
190     RuntimeValidator.assertEitherSpecified(
191         "injectTo", this.injectTo, "injectToName", this.injectToName);
192
193     TaglibProperties props = TaglibProperties.getInstance();
194     props.setRuntimeProperty(PROP_COMPARATOR, this.comparator);
195
196     Template tpl = Template.forName(Template.SORT);
197     tpl.map("callback", this.shortcutType.getCallbackName());
198     tpl.map("applyTo", this.applyTo);
199     tpl.map("applyToName", this.applyToName);
200     tpl.map("injectTo", this.injectTo);
201     tpl.map("injectToName", this.injectToName);
202     tpl.map("comparator", props.get(PROP_COMPARATOR));
203     println(tpl.evalToString());
204
205     return EVAL_PAGE;
206   }
207
208
209
210   ///////////////////////////////////
211
////////// Inner classes //////////
212
///////////////////////////////////
213

214   /**
215    * The type of the shortcut, which is either "ascending" or "descending".
216    *
217    * @author hgani
218    */

219   private abstract static class ShortcutType {
220     /// Constants ///
221

222     /**
223      * The "ascending" type
224      */

225     private static final ShortcutType ASCENDING = new ShortcutType("ascending") {
226       String JavaDoc getCallbackName() {
227         return "createAscendingSuite";
228       }
229     };
230
231     /**
232      * The "descending" type
233      */

234     private static final ShortcutType DESCENDING = new ShortcutType("descending") {
235       String JavaDoc getCallbackName() {
236         return "createDescendingSuite";
237       }
238     };
239
240     /// Fields ///
241

242     /**
243      * Name of the shortcut type
244      */

245     private final String JavaDoc name;
246
247     /// Constructors ///
248

249     /**
250      * Non-instantiable by client.
251      *
252      * @param name the name of the shortcut type
253      */

254     private ShortcutType(String JavaDoc name) {
255       this.name = name;
256     }
257
258     /// Methods ///
259

260     /**
261      * Returns the Javascript function name for callback.
262      *
263      * @return the name
264      */

265     abstract String JavaDoc getCallbackName();
266
267     /**
268      * Returns the shortcut type given the name.
269      *
270      * @param name the name of the shortcut type to return
271      * @return the shortcut type to return
272      * @throws RuntimeValidationException if supplied shortcut name is invalid
273      */

274     static ShortcutType findByName(String JavaDoc name) {
275       if (name.equals(ASCENDING.name)) {
276         return ASCENDING;
277       }
278       else if (name.equals(DESCENDING.name)) {
279         return DESCENDING;
280       }
281
282       // assert is not used as the "name" value comes from JSP, which can be
283
// anything
284
throw new RuntimeValidationException("Invalid type: '" + name + "'.");
285     }
286   }
287 }
Popular Tags