KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > tag > shift > ShiftTag


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.shift;
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 shifts (upwards or downwards)
31  * a group options in a select box.
32  *
33  * @see TaglibProperties
34  * @author hgani
35  * @version $Id$
36  */

37 public class ShiftTag extends AbstractUiTag {
38
39   ///////////////////////////////
40
////////// Constants //////////
41
///////////////////////////////
42

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

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

54   /**
55    * The "type" tag attribute
56    */

57   private ShortcutType shortcutType;
58   /**
59    * The "applyTo" tag attribute
60    */

61   private String JavaDoc applyTo;
62   /**
63    * The "applyToName" tag attribute
64    */

65   private String JavaDoc applyToName;
66   /**
67    * The "injectTo" tag attribute
68    */

69   private String JavaDoc injectTo;
70   /**
71    * The "injectToName" tag attribute
72    */

73   private String JavaDoc injectToName;
74
75
76
77   //////////////////////////////////
78
////////// Constructors //////////
79
//////////////////////////////////
80

81   /**
82    * Default constructor.
83    */

84   public ShiftTag() {
85     super();
86   }
87
88
89
90   ///////////////////////////////////////////
91
////////// Tag attribute setters //////////
92
///////////////////////////////////////////
93

94   /**
95    * Tag attribute setter.
96    *
97    * @param val value of the tag attribute
98    */

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

108   public void setApplyTo(String JavaDoc val) {
109     this.applyTo = val;
110   }
111
112   /**
113    * Tag attribute setter.
114    *
115    * @param val value of the tag attribute
116    */

117   public void setApplyToName(String JavaDoc val) {
118     this.applyToName = val;
119   }
120
121   /**
122    * Tag attribute setter.
123    *
124    * @param val value of the tag attribute
125    */

126   public void setInjectTo(String JavaDoc val) {
127     this.injectTo = val;
128   }
129
130   /**
131    * Tag attribute setter.
132    *
133    * @param val value of the tag attribute
134    */

135   public void setInjectToName(String JavaDoc val) {
136     this.injectToName = val;
137   }
138
139
140
141   ///////////////////////////////
142
////////// Tag logic //////////
143
///////////////////////////////
144

145   /**
146    * Instructs web container to evaluate the tag's body.
147    *
148    * @see javax.servlet.jsp.tagext.Tag#doStartTag()
149    * @return <code>EVAL_BODY_INCLUDE</code>
150    * @throws JspException to communicate error
151    */

152   public int doStartTag() throws JspException JavaDoc {
153     return EVAL_BODY_INCLUDE;
154   }
155
156   /**
157    * Prints out HTML code to construct the button.
158    *
159    * @return <code>EVAL_PAGE</code>
160    * @throws JspException to communicate error
161    */

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

190   /**
191    * The type of the shortcut, which is either "up", "down",
192    * "first", or "last".
193    *
194    * @author hgani
195    */

196   private abstract static class ShortcutType {
197     /// Constants ///
198

199     /**
200      * The "up" type
201      */

202     private static final ShortcutType UP = new ShortcutType("up") {
203       String JavaDoc getCallbackName() {
204         return "createUpSuite";
205       }
206     };
207
208     /**
209      * The "down" type
210      */

211     private static final ShortcutType DOWN = new ShortcutType("down") {
212       String JavaDoc getCallbackName() {
213         return "createDownSuite";
214       }
215     };
216
217     /**
218      * The "first" type
219      */

220     private static final ShortcutType FIRST = new ShortcutType("first") {
221       String JavaDoc getCallbackName() {
222         return "createFirstSuite";
223       }
224     };
225
226     /**
227      * The "last" type
228      */

229     private static final ShortcutType LAST = new ShortcutType("last") {
230       String JavaDoc getCallbackName() {
231         return "createLastSuite";
232       }
233     };
234
235     /// Fields ///
236

237     /**
238      * Name of the shortcut type
239      */

240     private final String JavaDoc name;
241
242     /// Constructors ///
243

244     /**
245      * Non-instantiable by client.
246      *
247      * @param name the name of the shortcut type
248      */

249     private ShortcutType(String JavaDoc name) {
250       this.name = name;
251     }
252
253     /// Methods ///
254

255     /**
256      * Returns the Javascript function name for callback.
257      *
258      * @return the name
259      */

260     abstract String JavaDoc getCallbackName();
261
262     /**
263      * Returns the shortcut type given the name.
264      *
265      * @param name the name of the shortcut type to return
266      * @return the shortcut type to return
267      * @throws RuntimeValidationException if supplied shortcut name is invalid
268      */

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