KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: RadioTag.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 javax.servlet.jsp.JspException JavaDoc;
22
23 import org.apache.struts.taglib.TagUtils;
24 import org.apache.struts.util.MessageResources;
25
26 /**
27  * Tag for input fields of type "radio".
28  *
29  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
30  */

31 public class RadioTag extends BaseHandlerTag {
32
33
34     // ----------------------------------------------------- Instance Variables
35

36
37     /**
38      * The message resources for this package.
39      */

40     protected static MessageResources messages =
41      MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
42
43
44     /**
45      * The name of the bean containing our underlying property.
46      */

47     protected String JavaDoc name = Constants.BEAN_KEY;
48
49     public String JavaDoc getName() {
50         return (this.name);
51     }
52
53     public void setName(String JavaDoc name) {
54         this.name = name;
55     }
56
57
58     /**
59      * The property name for this field.
60      */

61     protected String JavaDoc property = null;
62
63
64     /**
65      * The body content of this tag (if any).
66      */

67     protected String JavaDoc text = null;
68
69
70     /**
71      * The server value for this option.
72      */

73     protected String JavaDoc value = null;
74
75
76     /**
77      * Name of the bean (in some scope) that will return the
78      * value of the radio tag.
79      * <p>
80      * If an iterator is used to render a series of radio tags,
81      * this field may be used to specify the name of the bean
82      * exposed by the iterator. In this case, the value attribute is
83      * used as the name of a property on the <code>idName</code> bean
84      * that returns the value of the radio tag in this iteration.
85      */

86     protected String JavaDoc idName = null;
87
88
89     // ------------------------------------------------------------- Properties
90

91
92     /**
93      * Return the property name.
94      */

95     public String JavaDoc getProperty() {
96
97         return (this.property);
98
99     }
100
101     /**
102      * Set the property name.
103      *
104      * @param property The new property name
105      */

106     public void setProperty(String JavaDoc property) {
107
108         this.property = property;
109
110     }
111
112     /**
113      * Return the server value.
114      */

115     public String JavaDoc getValue() {
116
117         return (this.value);
118
119     }
120
121     /**
122      * Set the server value.
123      *
124      * @param value The new server value
125      */

126     public void setValue(String JavaDoc value) {
127
128         this.value = value;
129
130     }
131
132     /**
133      * Return the idName.
134      * @since Struts 1.1
135      */

136     public String JavaDoc getIdName() {
137
138         return (this.idName);
139
140     }
141
142     /**
143      * Set the idName.
144      * @since Struts 1.1
145      *
146      * @param idName The new idName
147      */

148     public void setIdName(String JavaDoc idName) {
149
150         this.idName=idName;
151
152     }
153
154
155     // --------------------------------------------------------- Public Methods
156

157
158     /**
159      * Generate the required input tag.
160      * [Indexed property since Struts 1.1]
161      *
162      * @exception JspException if a JSP exception has occurred
163      */

164     public int doStartTag() throws JspException JavaDoc {
165
166         String JavaDoc radioTag = renderRadioElement(serverValue(), currentValue());
167
168         TagUtils.getInstance().write(pageContext, radioTag);
169
170         this.text = null;
171         return (EVAL_BODY_TAG);
172     }
173
174     /**
175      * Return the String to be used in the radio tag's <code>value</code> attribute
176      * that gets sent to the server on form submission.
177      * @throws JspException
178      */

179     private String JavaDoc serverValue() throws JspException JavaDoc {
180
181         // Not using indexed radio buttons
182
if (this.idName == null) {
183             return this.value;
184         }
185         
186         String JavaDoc serverValue = this.lookupProperty(this.idName, this.value);
187         
188         return (serverValue == null) ? "" : serverValue;
189     }
190
191     /**
192      * Acquire the current value of the bean specified by the <code>name</code>
193      * attribute and the property specified by the <code>property</code> attribute.
194      * This radio button with this value will be checked.
195      * @throws JspException
196      */

197     private String JavaDoc currentValue() throws JspException JavaDoc {
198         String JavaDoc current = this.lookupProperty(this.name, this.property);
199         
200         return (current == null) ? "" : current;
201     }
202     
203     /**
204      * Renders an HTML &lt;input type="radio"&gt; element.
205      * @param serverValue The data to be used in the tag's <code>value</code>
206      * attribute and sent to the server when the form is submitted.
207      * @param checkedValue If the serverValue equals this value the radio button
208      * will be checked.
209      * @return A radio input element.
210      * @throws JspException
211      * @since Struts 1.1
212      */

213     protected String JavaDoc renderRadioElement(String JavaDoc serverValue, String JavaDoc checkedValue)
214         throws JspException JavaDoc {
215             
216         StringBuffer JavaDoc results = new StringBuffer JavaDoc("<input type=\"radio\"");
217
218         prepareAttribute(results, "name", prepareName());
219         prepareAttribute(results, "accesskey", getAccesskey());
220         prepareAttribute(results, "tabindex", getTabindex());
221         prepareAttribute(results, "value", serverValue);
222         if (serverValue.equals(checkedValue)) {
223             results.append(" checked=\"checked\"");
224         }
225         results.append(prepareEventHandlers());
226         results.append(prepareStyles());
227         prepareOtherAttributes(results);
228         results.append(getElementClose());
229         return results.toString();
230     }
231
232     /**
233      * Save the associated label from the body content.
234      *
235      * @exception JspException if a JSP exception has occurred
236      */

237     public int doAfterBody() throws JspException JavaDoc {
238
239         if (this.bodyContent != null) {
240             String JavaDoc value = this.bodyContent.getString().trim();
241             if (value.length() > 0) {
242                 this.text = value;
243             }
244         }
245         
246         return (SKIP_BODY);
247     }
248
249     /**
250      * Optionally render the associated label from the body content.
251      *
252      * @exception JspException if a JSP exception has occurred
253      */

254     public int doEndTag() throws JspException JavaDoc {
255
256         // Render any description for this radio button
257
if (this.text != null) {
258             TagUtils.getInstance().write(pageContext, text);
259         }
260         
261         return (EVAL_PAGE);
262
263     }
264
265
266     /**
267      * Prepare the name element
268      * @return The element name.
269      */

270     protected String JavaDoc prepareName() throws JspException JavaDoc {
271
272         if (property == null) {
273             return null;
274         }
275
276         // * @since Struts 1.1
277
if(indexed) {
278             StringBuffer JavaDoc results = new StringBuffer JavaDoc();
279             prepareIndex(results, name);
280             results.append(property);
281             return results.toString();
282         }
283
284         return property;
285
286     }
287
288     /**
289      * Release any acquired resources.
290      */

291     public void release() {
292
293         super.release();
294         idName = null;
295         name = Constants.BEAN_KEY;
296         property = null;
297         text = null;
298         value = null;
299
300     }
301
302 }
303
Popular Tags