KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: MultiboxTag.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 2001-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 import javax.servlet.jsp.PageContext JavaDoc;
25
26 import org.apache.commons.beanutils.BeanUtils;
27 import org.apache.struts.Globals;
28 import org.apache.struts.taglib.TagUtils;
29 import org.apache.struts.util.MessageResources;
30
31 /**
32  * Tag for input fields of type "checkbox". This differs from CheckboxTag
33  * because it assumes that the underlying property is an array getter (of any
34  * supported primitive type, or String), and the checkbox is initialized to
35  * "checked" if the value listed for the "value" attribute is present in the
36  * values returned by the property getter.
37  *
38  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
39  */

40
41 public class MultiboxTag extends BaseHandlerTag {
42
43     // ----------------------------------------------------- Instance Variables
44

45     /**
46      * The constant String value to be returned when this checkbox is
47      * selected and the form is submitted.
48      */

49     protected String JavaDoc constant = null;
50
51     /**
52      * The message resources for this package.
53      */

54     protected static MessageResources messages =
55         MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
56
57     /**
58      * The name of the bean containing our underlying property.
59      */

60     protected String JavaDoc name = Constants.BEAN_KEY;
61
62     public String JavaDoc getName() {
63         return (this.name);
64     }
65
66     public void setName(String JavaDoc name) {
67         this.name = name;
68     }
69
70     /**
71      * The property name for this field.
72      */

73     protected String JavaDoc property = null;
74
75     /**
76      * The value which will mark this checkbox as "checked" if present
77      * in the array returned by our property getter.
78      */

79     protected String JavaDoc value = null;
80
81     // ------------------------------------------------------------- Properties
82

83     /**
84      * Return the property name.
85      */

86     public String JavaDoc getProperty() {
87
88         return (this.property);
89
90     }
91
92     /**
93      * Set the property name.
94      *
95      * @param property The new property name
96      */

97     public void setProperty(String JavaDoc property) {
98
99         this.property = property;
100
101     }
102
103     /**
104      * Return the server value.
105      */

106     public String JavaDoc getValue() {
107
108         return (this.value);
109
110     }
111
112     /**
113      * Set the server value.
114      *
115      * @param value The new server value
116      */

117     public void setValue(String JavaDoc value) {
118
119         this.value = value;
120
121     }
122
123     // --------------------------------------------------------- Public Methods
124

125     /**
126      * Process the beginning of this tag.
127      *
128      * @exception JspException if a JSP exception has occurred
129      */

130     public int doStartTag() throws JspException JavaDoc {
131
132         // Defer processing until the end of this tag is encountered
133
this.constant = null;
134         return (EVAL_BODY_TAG);
135
136     }
137
138     /**
139      * Save the body contents of this tag as the constant that we will
140      * be returning.
141      *
142      * @exception JspException if a JSP exception has occurred
143      */

144     public int doAfterBody() throws JspException JavaDoc {
145
146         if (bodyContent != null) {
147             this.constant = bodyContent.getString().trim();
148         }
149             
150         if ("".equals(this.constant)) {
151             this.constant = null;
152         }
153             
154         return SKIP_BODY;
155     }
156
157     /**
158      * Render an input element for this tag.
159      *
160      * @exception JspException if a JSP exception has occurred
161      */

162     public int doEndTag() throws JspException JavaDoc {
163
164         // Create an appropriate "input" element based on our parameters
165
StringBuffer JavaDoc results = new StringBuffer JavaDoc("<input type=\"checkbox\"");
166
167         prepareAttribute(results, "name", prepareName());
168         prepareAttribute(results, "accesskey", getAccesskey());
169         prepareAttribute(results, "tabindex", getTabindex());
170         String JavaDoc value = prepareValue(results);
171         prepareChecked(results, value);
172         results.append(prepareEventHandlers());
173         results.append(prepareStyles());
174         prepareOtherAttributes(results);
175         results.append(getElementClose());
176
177         TagUtils.getInstance().write(pageContext, results.toString());
178
179         return EVAL_PAGE;
180     }
181
182
183     /**
184      * Prepare the name element
185      * @return The element name.
186      */

187     protected String JavaDoc prepareName() throws JspException JavaDoc {
188
189         return property;
190
191     }
192
193     /**
194      * Render the value element
195      * @param results The StringBuffer that output will be appended to.
196      */

197     protected String JavaDoc prepareValue(StringBuffer JavaDoc results) throws JspException JavaDoc {
198
199         String JavaDoc value = (this.value == null) ? this.constant : this.value;
200         if (value == null) {
201             JspException JavaDoc e = new JspException JavaDoc(messages.getMessage("multiboxTag.value"));
202             pageContext.setAttribute(Globals.EXCEPTION_KEY, e, PageContext.REQUEST_SCOPE);
203             throw e;
204         }
205
206         prepareAttribute(results, "value", TagUtils.getInstance().filter(value));
207
208         return value;
209
210     }
211
212     /**
213      * Render the checked element
214      * @param results The StringBuffer that output will be appended to.
215      */

216     protected void prepareChecked(StringBuffer JavaDoc results, String JavaDoc value) throws JspException JavaDoc {
217
218         Object JavaDoc bean = TagUtils.getInstance().lookup(pageContext, name, null);
219         String JavaDoc values[] = null;
220         
221         if (bean == null) {
222             throw new JspException JavaDoc(messages.getMessage("getter.bean", name));
223         }
224             
225         try {
226             values = BeanUtils.getArrayProperty(bean, property);
227             if (values == null) {
228                 values = new String JavaDoc[0];
229             }
230                 
231         } catch (IllegalAccessException JavaDoc e) {
232             throw new JspException JavaDoc(messages.getMessage("getter.access", property, name));
233         } catch (InvocationTargetException JavaDoc e) {
234             Throwable JavaDoc t = e.getTargetException();
235             throw new JspException JavaDoc(messages.getMessage("getter.result", property, t.toString()));
236         } catch (NoSuchMethodException JavaDoc e) {
237             throw new JspException JavaDoc(messages.getMessage("getter.method", property, name));
238         }
239         
240         for (int i = 0; i < values.length; i++) {
241             if (value.equals(values[i])) {
242                 results.append(" checked=\"checked\"");
243                 break;
244             }
245         }
246
247     }
248
249
250     /**
251      * Release any acquired resources.
252      */

253     public void release() {
254
255         super.release();
256         constant = null;
257         name = Constants.BEAN_KEY;
258         property = null;
259         value = null;
260
261     }
262
263 }
264
Popular Tags