KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > tag > select > SelectTag


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.select;
19
20 import javax.servlet.jsp.JspException JavaDoc;
21
22 import net.sf.uitags.tag.AbstractUiTag;
23 import net.sf.uitags.tagutil.validation.RuntimeValidationException;
24 import net.sf.uitags.tagutil.validation.RuntimeValidator;
25 import net.sf.uitags.util.Template;
26
27
28 /**
29  *
30  *
31  * @author jonni
32  * @author hgani
33  * @version $Id$
34  */

35 public class SelectTag extends AbstractUiTag {
36
37   ///////////////////////////////
38
////////// Constants //////////
39
///////////////////////////////
40

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

44   private static final long serialVersionUID = 100L;
45
46
47
48   ////////////////////////////
49
////////// Fields //////////
50
////////////////////////////
51

52   /**
53    * The "type" tag attribute
54    */

55   private ShortcutType shortcutType;
56   /**
57    * The "applyTo" tag attribute
58    */

59   private String JavaDoc applyTo;
60   /**
61    * The "applyToName" tag attribute
62    */

63   private String JavaDoc applyToName;
64   /**
65    * The "injectTo" tag attribute
66    */

67   private String JavaDoc injectTo;
68   /**
69    * The "injectToName" tag attribute
70    */

71   private String JavaDoc injectToName;
72
73
74   //////////////////////////////////
75
////////// Constructors //////////
76
//////////////////////////////////
77

78   /**
79    * Default constructor.
80    */

81   public SelectTag() {
82     super();
83   }
84
85
86   ///////////////////////////////////////////
87
////////// Tag attribute setters //////////
88
///////////////////////////////////////////
89

90   /**
91    * Tag attribute setter.
92    *
93    * @param val value of the tag attribute
94    */

95   public void setType(String JavaDoc val) {
96     this.shortcutType = ShortcutType.findByName(val);
97   }
98
99   /**
100    * Tag attribute setter.
101    *
102    * @param val value of the tag attribute
103    */

104   public void setApplyTo(String JavaDoc val) {
105     this.applyTo = val;
106   }
107
108   /**
109    * Tag attribute setter.
110    *
111    * @param val value of the tag attribute
112    */

113   public void setApplyToName(String JavaDoc val) {
114     this.applyToName = val;
115   }
116
117   /**
118    * Tag attribute setter.
119    *
120    * @param val value of the tag attribute
121    */

122   public void setInjectTo(String JavaDoc val) {
123     this.injectTo = val;
124   }
125
126   /**
127    * Tag attribute setter.
128    *
129    * @param val value of the tag attribute
130    */

131   public void setInjectToName(String JavaDoc val) {
132     this.injectToName = val;
133   }
134
135
136   ///////////////////////////////
137
////////// Tag logic //////////
138
///////////////////////////////
139

140   /**
141    * Instructs web container to evaluate the tag's body.
142    *
143    * @see javax.servlet.jsp.tagext.Tag#doStartTag()
144    * @return <code>EVAL_BODY_INCLUDE</code>
145    * @throws JspException to communicate error
146    */

147   public int doStartTag() throws JspException JavaDoc {
148     return EVAL_BODY_INCLUDE;
149   }
150
151   /**
152    * Prints out HTML code to construct the button.
153    *
154    * @return <code>EVAL_PAGE</code>
155    * @throws JspException to communicate error
156    */

157   public int doEndTag() throws JspException JavaDoc {
158     RuntimeValidator.assertAttributeExclusive(
159         "applyTo", this.applyTo, "applyToName", this.applyToName);
160     RuntimeValidator.assertAttributeExclusive(
161         "injectTo", this.injectTo, "injectToName", this.injectToName);
162
163     RuntimeValidator.assertEitherSpecified(
164         "applyTo", this.applyTo, "applyToName", this.applyToName);
165     RuntimeValidator.assertEitherSpecified(
166         "injectTo", this.injectTo, "injectToName", this.injectToName);
167
168     Template tpl = Template.forName(Template.SELECT);
169     tpl.map("callback", this.shortcutType.getCallbackName());
170     tpl.map("applyTo", this.applyTo);
171     tpl.map("applyToName", this.applyToName);
172     tpl.map("injectTo", this.injectTo);
173     tpl.map("injectToName", this.injectToName);
174     println(tpl.evalToString());
175
176     return EVAL_PAGE;
177   }
178
179
180   ///////////////////////////////////
181
////////// Inner classes //////////
182
///////////////////////////////////
183

184   /**
185    * The type of the shortcut, which is either "all", "inverse", or
186    * "range".
187    *
188    * @author jonni
189    */

190   private abstract static class ShortcutType {
191     /// Constants ///
192

193     /**
194      * The "all" type
195      */

196     private static final ShortcutType ALL = new ShortcutType("all") {
197       String JavaDoc getCallbackName() {
198         return "createAllSuite";
199       }
200     };
201
202     /**
203      * The "none" type
204      */

205     private static final ShortcutType NONE = new ShortcutType("none") {
206       String JavaDoc getCallbackName() {
207         return "createNoneSuite";
208       }
209     };
210
211     /**
212      * The "all-none" type
213      */

214     private static final ShortcutType ALL_NONE = new ShortcutType("all-none") {
215       String JavaDoc getCallbackName() {
216         return "createAllNoneSuite";
217       }
218     };
219
220     /**
221      * The "none-all" type
222      */

223     private static final ShortcutType NONE_ALL = new ShortcutType("none-all") {
224       String JavaDoc getCallbackName() {
225         return "createNoneAllSuite";
226       }
227     };
228
229     /**
230      * The "inverse" type
231      */

232     private static final ShortcutType INVERSE = new ShortcutType("inverse") {
233       String JavaDoc getCallbackName() {
234         return "createInverseSuite";
235       }
236     };
237
238     /**
239      * The "range" type
240      */

241     private static final ShortcutType RANGE = new ShortcutType("range") {
242       String JavaDoc getCallbackName() {
243         return "createRangeSuite";
244       }
245     };
246
247     /// Fields ///
248

249     /**
250      * Name of the shortcut type
251      */

252     private final String JavaDoc name;
253
254     /// Constructors ///
255

256     /**
257      * Non-instantiable by client.
258      *
259      * @param name the name of the shortcut type
260      */

261     private ShortcutType(String JavaDoc name) {
262       this.name = name;
263     }
264
265     /// Methods ///
266

267     /**
268      * Returns the Javascript function name for callback.
269      *
270      * @return the name
271      */

272     abstract String JavaDoc getCallbackName();
273
274     /**
275      * Returns the shortcut type given the name.
276      *
277      * @param name the name of the shortcut type to return
278      * @return the shortcut type to return
279      * @throws RuntimeValidationException if supplied shortcut name is invalid
280      */

281     static ShortcutType findByName(String JavaDoc name) {
282       if (name.equals(ALL_NONE.name)) {
283         return ALL_NONE;
284       }
285       else if (name.equals(NONE_ALL.name)) {
286         return NONE_ALL;
287       }
288       else if (name.equals(ALL.name)) {
289         return ALL;
290       }
291       else if (name.equals(NONE.name)) {
292         return NONE;
293       }
294       else if (name.equals(INVERSE.name)) {
295         return INVERSE;
296       }
297       else if (name.equals(RANGE.name)) {
298         return RANGE;
299       }
300
301       // assert is not used as the "name" value comes from JSP, which can be
302
// anything
303
throw new RuntimeValidationException("Invalid type: '" + name + "'.");
304     }
305   }
306 }
Popular Tags