KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > input > Select


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
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.apache.taglibs.input;
17
18 import java.util.Collection JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.servlet.ServletRequest JavaDoc;
25 import javax.servlet.jsp.JspException JavaDoc;
26 import javax.servlet.jsp.JspTagException JavaDoc;
27 import javax.servlet.jsp.JspWriter JavaDoc;
28 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
29
30 /**
31  *
32  * This class implements the <input:select> tag, which presents a
33  * <select> form element.
34  *
35  * @version 0.90
36  * @author Shawn Bayern
37  * @author Lance Lavandowska
38  */

39
40 public class Select extends TagSupport JavaDoc {
41
42     private String JavaDoc name; // name of the select element
43

44     private String JavaDoc dVal; // default value if none is found
45

46     private String JavaDoc[] dValArray; // our multiple default values
47

48     private Map JavaDoc attributes; // attributes of the <select> element
49

50     private Map JavaDoc options; // what are our options? :)
51

52     private String JavaDoc attributesText; // attributes of the <input> element as text
53

54     private String JavaDoc beanId; // bean id to get default values from
55

56     private boolean multiple; // select multiple
57

58     private String JavaDoc size; // select size
59

60     private List JavaDoc optionLabels; // a list of option labels
61

62     private List JavaDoc optionValues; // a list of option values
63

64     private HashMap JavaDoc chosen; // chosen options (created in doStartTag)
65

66     public void release() {
67         super.release();
68         name = null;
69         dVal = null;
70         dValArray = null;
71         attributes = null;
72         options = null;
73         attributesText = null;
74         beanId = null;
75         multiple = false;
76         size = null;
77         optionLabels = null;
78         optionValues = null;
79         chosen = null;
80     }
81
82     public int doStartTag() throws JspException JavaDoc {
83         try {
84             // sanity check
85
if (name == null || name.equals(""))
86                 throw new JspTagException JavaDoc("invalid null or empty 'name'");
87
88             // Store beanId in a local variable as we change it
89
String JavaDoc beanId = this.beanId;
90
91             // Get default beanId
92
if (beanId == null) {
93                 beanId = Util.defaultFormBeanId(this);
94             } else if (beanId.length() == 0) {
95                 // An empty beanId means, do not use any bean - not even default
96
beanId = null;
97             }
98
99             // get what we need from the page
100
ServletRequest JavaDoc req = pageContext.getRequest();
101             JspWriter JavaDoc out = pageContext.getOut();
102
103             // start building up the tag
104
out.print("<select name=\"" + Util.quote(name) + "\" ");
105
106             // include any attributes we've got here
107
Util.printAttributes(out, attributes);
108             if (attributesText != null) {
109                 out.print(attributesText + " ");
110             }
111
112             if (multiple) {
113                 out.print("multiple=\"multiple\" ");
114             }
115             if (size != null) {
116                 out.print("size=\"" + Util.quote(size) + "\" ");
117             }
118
119             // end the starting tag
120
out.println(">");
121
122             /*
123              * Print out our options, selecting one or more if appropriate. If
124              * there are multiple selections but the page doesn't call for a
125              * <select> that accepts them, ignore the selections. This is
126              * preferable to throwing a JspException because the (end) user can
127              * control input, and we don't want the user causing exceptions in
128              * our application.
129              */

130
131             String JavaDoc[] selected;
132
133             // Use selected from the bean, if available
134
String JavaDoc[] beanValues = (beanId != null ? Util.beanPropertyValues(
135                     pageContext.findAttribute(beanId), name) : null);
136             if (beanValues != null) {
137                 selected = beanValues;
138             } else {
139                 // get the current selection from the request
140
selected = req.getParameterValues(name);
141                 if (selected == null) {
142                     // Use defaults
143
if (dValArray != null && dVal != null) {
144                         selected = new String JavaDoc[dValArray.length + 1];
145                         selected[0] = dVal;
146                         System.arraycopy(dValArray, 0, selected, 1,
147                                 dValArray.length);
148                     } else if (dValArray != null) {
149                         selected = dValArray;
150                     } else if (dVal != null) {
151                         selected = new String JavaDoc[] { dVal };
152                     }
153                 }
154             }
155
156             if (selected != null
157                     && selected.length > 1
158                     && ((attributes == null || !attributes
159                             .containsKey("multiple")) && !multiple))
160                 selected = null;
161
162             // load up the selected values into a hash table for faster access
163
// and for option tags to access
164
// NB: the chosen Map may be modified by Option tags in this select
165
// tag
166
// so that only the first option is selected if the select is not a
167
// multiple select
168
chosen = new HashMap JavaDoc();
169             if (selected != null) {
170                 for (int i = 0; i < selected.length; i++) {
171                     if (selected[i] != null) {
172                         chosen.put(selected[i], null);
173                     }
174                 }
175             }
176
177             // actually print the <option> tags
178
if (optionLabels != null) {
179                 // Print out using the optionLabels
180
int n = optionLabels.size();
181                 for (int i = 0; i < n; i++) {
182                     Object JavaDoc oLabel = optionLabels.get(i);
183                     Object JavaDoc oVal = (options != null ? options.get(oLabel)
184                             : (optionValues != null ? optionValues.get(i)
185                                     : oLabel));
186
187                     outputOption(out, oLabel, oVal);
188                 }
189             } else if (options != null) {
190                 Iterator JavaDoc i = options.keySet().iterator();
191                 while (i.hasNext()) {
192                     Object JavaDoc oLabel = i.next();
193                     Object JavaDoc oVal = options.get(oLabel);
194
195                     outputOption(out, oLabel, oVal);
196                 }
197             }
198
199         } catch (Exception JavaDoc ex) {
200             throw new JspTagException JavaDoc(ex.getMessage());
201         }
202         return EVAL_BODY_INCLUDE;
203     }
204
205     private void outputOption(JspWriter JavaDoc out, Object JavaDoc oLabel, Object JavaDoc oVal)
206             throws java.io.IOException JavaDoc {
207         String JavaDoc label = oLabel.toString();
208         /*
209          * Convert the value to a String if it is not already.
210          */

211         String JavaDoc value = (oVal != null ? oVal.toString() : null);
212
213         /* Output the option tag */
214         out.print("<option");
215         
216         /* Output the value if there is one specified separate from the label */
217         if ( value != null ) {
218             out.print(" value=\"" + Util.quote(value) + "\"");
219         }
220         
221         /* If there is no value specified then use the label as the value,
222          * this is for checking if this option is selected based on value.
223          */

224         if (value == null)
225             value = label; // use label if value is null
226

227         /*
228          * This may look confusing: we match the VALUE of this option pair with
229          * the KEY of the 'chosen' Map (We want to match <option>s on values,
230          * not keys.)
231          */

232         if (chosen.containsKey(value)) {
233             if (!multiple) {
234                 chosen.remove(value);
235             }
236             out.print(" selected=\"selected\"");
237         }
238         out.print(">");
239         out.print(Util.quote(label));
240         out.println("</option>");
241     }
242
243     public int doEndTag() throws JspException JavaDoc {
244         try {
245             JspWriter JavaDoc out = pageContext.getOut();
246             out.print("</select>");
247         } catch (Exception JavaDoc ex) {
248             throw new JspTagException JavaDoc(ex.getMessage());
249         }
250         return EVAL_PAGE;
251     }
252
253     public void setName(String JavaDoc x) {
254         name = x;
255     }
256
257     public void setAttributes(Map JavaDoc x) {
258         attributes = x;
259     }
260
261     public void setAttributesText(String JavaDoc x) {
262         attributesText = x;
263     }
264
265     public void setBean(String JavaDoc x) {
266         beanId = x;
267     }
268
269     public void setMultiple(boolean x) {
270         multiple = x;
271     }
272
273     public void setSize(String JavaDoc x) {
274         size = x;
275     }
276
277     public void setDefault(String JavaDoc x) {
278         dVal = x;
279     }
280
281     public void setDefaults(String JavaDoc[] x) {
282         dValArray = x;
283     }
284
285     public void setDefaults(Map JavaDoc x) {
286         dValArray = new String JavaDoc[x.size()];
287         Iterator JavaDoc it = x.keySet().iterator();
288         int i = 0;
289         while (it.hasNext()) {
290             dValArray[i++] = it.next().toString();
291         }
292     }
293
294     public void setDefaults(Collection JavaDoc c) {
295         dValArray = new String JavaDoc[c.size()];
296         Iterator JavaDoc it = c.iterator();
297         int i = 0;
298         while (it.hasNext()) {
299             dValArray[i++] = it.next().toString();
300         }
301     }
302
303     public void setOptions(Map JavaDoc x) {
304         options = x;
305     }
306
307     public void setOptionLabels(List JavaDoc x) {
308         optionLabels = x;
309     }
310
311     public void setOptionValues(List JavaDoc x) {
312         optionValues = x;
313     }
314
315     public HashMap JavaDoc getChosen() {
316         return chosen;
317     }
318
319     /**
320      * Getter for property name.
321      *
322      * @return Value of property name.
323      */

324     public String JavaDoc getName() {
325         return name;
326     }
327
328     /**
329      * Getter for property default.
330      *
331      * @return Value of property default.
332      */

333     public String JavaDoc getDefault() {
334         return dVal;
335     }
336
337     /**
338      * Getter for property bean.
339      *
340      * @return Value of property bean.
341      */

342     public String JavaDoc getBean() {
343         return beanId;
344     }
345
346     /**
347      * Getter for property attributesText.
348      *
349      * @return Value of property attributesText.
350      */

351     public String JavaDoc getAttributesText() {
352         return attributesText;
353     }
354
355     /**
356      * Getter for property attributes.
357      *
358      * @return Value of property attributes.
359      */

360     public Map JavaDoc getAttributes() {
361         return attributes;
362     }
363
364     /**
365      * Getter for property defaults.
366      *
367      * @return Value of property defaults.
368      */

369     public String JavaDoc[] getDefaults() {
370         return dValArray;
371     }
372
373     /**
374      * Getter for property multiple.
375      *
376      * @return Value of property multiple.
377      */

378     public boolean isMultiple() {
379         return multiple;
380     }
381
382     /**
383      * Getter for property optionLabels.
384      *
385      * @return Value of property optionLabels.
386      */

387     public List JavaDoc getOptionLabels() {
388         return optionLabels;
389     }
390
391     /**
392      * Getter for property optionValues.
393      *
394      * @return Value of property optionValues.
395      */

396     public List JavaDoc getOptionValues() {
397         return optionValues;
398     }
399
400     /**
401      * Getter for property options.
402      *
403      * @return Value of property options.
404      */

405     public Map JavaDoc getOptions() {
406         return options;
407     }
408
409     /**
410      * Getter for property size.
411      *
412      * @return Value of property size.
413      */

414     public String JavaDoc getSize() {
415         return size;
416     }
417
418 }
Popular Tags