KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > html > SelectTag


1 /*
2  * $Id: SelectTag.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
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
19 package org.apache.struts.taglib.html;
20
21 import java.lang.reflect.InvocationTargetException JavaDoc;
22
23 import javax.servlet.jsp.JspException JavaDoc;
24
25 import org.apache.commons.beanutils.BeanUtils;
26 import org.apache.struts.taglib.TagUtils;
27 import org.apache.struts.util.MessageResources;
28
29 /**
30  * Custom tag that represents an HTML select element, associated with a
31  * bean property specified by our attributes. This tag must be nested
32  * inside a form tag.
33  *
34  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
35  */

36 public class SelectTag extends BaseHandlerTag {
37
38
39     // ----------------------------------------------------- Instance Variables
40

41
42     /**
43      * The actual values we will match against, calculated in doStartTag().
44      */

45     protected String JavaDoc match[] = null;
46
47
48     /**
49      * The message resources for this package.
50      */

51     protected static MessageResources messages =
52      MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
53
54
55     /**
56      * Should multiple selections be allowed. Any non-null value will
57      * trigger rendering this.
58      */

59     protected String JavaDoc multiple = null;
60
61     public String JavaDoc getMultiple() {
62         return (this.multiple);
63     }
64
65     public void setMultiple(String JavaDoc multiple) {
66         this.multiple = multiple;
67     }
68
69
70     /**
71      * The name of the bean containing our underlying property.
72      */

73     protected String JavaDoc name = Constants.BEAN_KEY;
74
75     public String JavaDoc getName() {
76         return (this.name);
77     }
78
79     public void setName(String JavaDoc name) {
80         this.name = name;
81     }
82
83
84     /**
85      * The property name we are associated with.
86      */

87     protected String JavaDoc property = null;
88
89
90     /**
91      * The saved body content of this tag.
92      */

93     protected String JavaDoc saveBody = null;
94
95
96     /**
97      * How many available options should be displayed when this element
98      * is rendered?
99      */

100     protected String JavaDoc size = null;
101
102     public String JavaDoc getSize() {
103         return (this.size);
104     }
105
106     public void setSize(String JavaDoc size) {
107         this.size = size;
108     }
109
110
111     /**
112      * The value to compare with for marking an option selected.
113      */

114     protected String JavaDoc value = null;
115
116
117     // ------------------------------------------------------------- Properties
118

119
120     /**
121      * Does the specified value match one of those we are looking for?
122      *
123      * @param value Value to be compared.
124      */

125     public boolean isMatched(String JavaDoc value) {
126         if ((this.match == null) || (value == null)) {
127             return false;
128         }
129         
130         for (int i = 0; i < this.match.length; i++) {
131             if (value.equals(this.match[i]))
132                 return true;
133         }
134         
135         return false;
136     }
137
138
139     /**
140      * Return the property name.
141      */

142     public String JavaDoc getProperty() {
143
144         return (this.property);
145
146     }
147
148
149     /**
150      * Set the property name.
151      *
152      * @param property The new property name
153      */

154     public void setProperty(String JavaDoc property) {
155
156         this.property = property;
157
158     }
159
160
161     /**
162      * Return the comparison value.
163      */

164     public String JavaDoc getValue() {
165
166         return (this.value);
167
168     }
169
170
171     /**
172      * Set the comparison value.
173      *
174      * @param value The new comparison value
175      */

176     public void setValue(String JavaDoc value) {
177
178         this.value = value;
179
180     }
181
182
183     // --------------------------------------------------------- Public Methods
184

185
186     /**
187      * Render the beginning of this select tag.
188      * <p>
189      * Support for indexed property since Struts 1.1
190      *
191      * @exception JspException if a JSP exception has occurred
192      */

193     public int doStartTag() throws JspException JavaDoc {
194
195         TagUtils.getInstance().write(pageContext, renderSelectStartElement());
196
197         // Store this tag itself as a page attribute
198
pageContext.setAttribute(Constants.SELECT_KEY, this);
199
200         this.calculateMatchValues();
201
202         return (EVAL_BODY_TAG);
203     }
204
205     /**
206      * Create an appropriate select start element based on our parameters.
207      * @throws JspException
208      * @since Struts 1.1
209      */

210     protected String JavaDoc renderSelectStartElement() throws JspException JavaDoc {
211         StringBuffer JavaDoc results = new StringBuffer JavaDoc("<select");
212         
213         prepareAttribute(results, "name", prepareName());
214         prepareAttribute(results, "accesskey", getAccesskey());
215         if (multiple != null) {
216             results.append(" multiple=\"multiple\"");
217         }
218         prepareAttribute(results, "size", getSize());
219         prepareAttribute(results, "tabindex", getTabindex());
220         results.append(prepareEventHandlers());
221         results.append(prepareStyles());
222         prepareOtherAttributes(results);
223         results.append(">");
224         
225         return results.toString();
226     }
227
228     /**
229      * Calculate the match values we will actually be using.
230      * @throws JspException
231      */

232     private void calculateMatchValues() throws JspException JavaDoc {
233         if (this.value != null) {
234             this.match = new String JavaDoc[1];
235             this.match[0] = this.value;
236             
237         } else {
238             Object JavaDoc bean = TagUtils.getInstance().lookup(pageContext, name, null);
239             if (bean == null) {
240                 JspException JavaDoc e =
241                     new JspException JavaDoc(messages.getMessage("getter.bean", name));
242                     
243                 TagUtils.getInstance().saveException(pageContext, e);
244                 throw e;
245             }
246
247             try {
248                 this.match = BeanUtils.getArrayProperty(bean, property);
249                 if (this.match == null) {
250                     this.match = new String JavaDoc[0];
251                 }
252
253             } catch (IllegalAccessException JavaDoc e) {
254                 TagUtils.getInstance().saveException(pageContext, e);
255                 throw new JspException JavaDoc(
256                     messages.getMessage("getter.access", property, name));
257
258             } catch (InvocationTargetException JavaDoc e) {
259                 Throwable JavaDoc t = e.getTargetException();
260                 TagUtils.getInstance().saveException(pageContext, t);
261                 throw new JspException JavaDoc(
262                     messages.getMessage("getter.result", property, t.toString()));
263
264             } catch (NoSuchMethodException JavaDoc e) {
265                 TagUtils.getInstance().saveException(pageContext, e);
266                 throw new JspException JavaDoc(
267                     messages.getMessage("getter.method", property, name));
268             }
269         }
270     }
271
272
273     /**
274      * Save any body content of this tag, which will generally be the
275      * option(s) representing the values displayed to the user.
276      *
277      * @exception JspException if a JSP exception has occurred
278      */

279     public int doAfterBody() throws JspException JavaDoc {
280
281         if (bodyContent != null) {
282             String JavaDoc value = bodyContent.getString();
283             if (value == null) {
284                 value = "";
285             }
286             
287             this.saveBody = value.trim();
288         }
289         
290         return (SKIP_BODY);
291     }
292
293
294     /**
295      * Render the end of this form.
296      *
297      * @exception JspException if a JSP exception has occurred
298      */

299     public int doEndTag() throws JspException JavaDoc {
300
301         // Remove the page scope attributes we created
302
pageContext.removeAttribute(Constants.SELECT_KEY);
303
304         // Render a tag representing the end of our current form
305
StringBuffer JavaDoc results = new StringBuffer JavaDoc();
306         if (saveBody != null) {
307             results.append(saveBody);
308             saveBody = null;
309         }
310         results.append("</select>");
311
312         TagUtils.getInstance().write(pageContext, results.toString());
313
314         return (EVAL_PAGE);
315     }
316
317     /**
318      * Prepare the name element
319      * @return The element name.
320      */

321     protected String JavaDoc prepareName() throws JspException JavaDoc {
322
323         if (property == null) {
324             return null;
325         }
326
327         // * @since Struts 1.1
328
if(indexed) {
329             StringBuffer JavaDoc results = new StringBuffer JavaDoc();
330             prepareIndex(results, name);
331             results.append(property);
332             return results.toString();
333         }
334
335         return property;
336
337     }
338
339     /**
340      * Release any acquired resources.
341      */

342     public void release() {
343
344         super.release();
345         match = null;
346         multiple = null;
347         name = Constants.BEAN_KEY;
348         property = null;
349         saveBody = null;
350         size = null;
351         value = null;
352
353     }
354
355 }
356
Popular Tags