KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
19
20 import javax.servlet.ServletRequest JavaDoc;
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.JspTagException JavaDoc;
23 import javax.servlet.jsp.JspWriter JavaDoc;
24 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
25
26 /**
27  *
28  * This class implements the <input:radio> tag, which presents an
29  * <input type="radio" ... /> form element.
30  *
31  * @version 0.90
32  * @author Shawn Bayern
33  * @author Lance Lavandowska
34  * @author Karl von Randow
35  */

36
37 public class Radio extends TagSupport JavaDoc {
38
39     private String JavaDoc name; // name of the radio-button group
40

41     private String JavaDoc value; // value of this particular button
42

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

45     private Map JavaDoc attributes; // attributes of the <input> element
46

47     private String JavaDoc attributesText; // attributes of the <input> element as text
48

49     private String JavaDoc beanId; // bean id to get default values from
50

51     public void release() {
52         super.release();
53         name = null;
54         dVal = null;
55         attributes = null;
56         attributesText = null;
57         beanId = null;
58     }
59
60     public int doStartTag() throws JspException JavaDoc {
61         try {
62             // sanity check (but value CAN be empty)
63
if (name == null || name.equals("") || value == null)
64                 throw new JspTagException JavaDoc(
65                         "invalid null or empty 'name' or 'value'");
66
67             // Store beanId in a local variable as we change it
68
String JavaDoc beanId = this.beanId;
69
70             // Get default beanId
71
if (beanId == null) {
72                 beanId = Util.defaultFormBeanId(this);
73             } else if (beanId.length() == 0) {
74                 // An empty beanId means, do not use any bean - not even default
75
beanId = null;
76             }
77
78             // get what we need from the page
79
ServletRequest JavaDoc req = pageContext.getRequest();
80             JspWriter JavaDoc out = pageContext.getOut();
81
82             // start building up the tag
83
out.print("<input type=\"radio\" ");
84             out.print("name=\"" + Util.quote(name) + "\" ");
85             out.print("value=\"" + Util.quote(value) + "\" ");
86
87             // include any attributes we've got here
88
Util.printAttributes(out, attributes);
89             if (attributesText != null) {
90                 out.print(attributesText + " ");
91             }
92
93             // check this button if it's the right one
94
String JavaDoc target;
95
96             // First check the bean value
97
String JavaDoc beanValue = (beanId != null ? Util.beanPropertyValue(
98                     pageContext.findAttribute(beanId), name) : null);
99             if (beanValue != null) {
100                 target = beanValue;
101             }
102             // If no bean value, check the request - if none, use default
103
else if (req.getParameter(name) == null) {
104                 target = dVal;
105             } else {
106                 target = req.getParameter(name);
107             }
108
109             if (target != null && target.equals(value))
110                 out.print("checked=\"checked\" ");
111
112             // end the tag
113
out.print("/>");
114
115         } catch (Exception JavaDoc ex) {
116             throw new JspTagException JavaDoc(ex.getMessage());
117         }
118         return SKIP_BODY;
119     }
120
121     public void setName(String JavaDoc x) {
122         name = x;
123     }
124
125     public void setValue(String JavaDoc x) {
126         value = x;
127     }
128
129     public void setAttributes(Map JavaDoc x) {
130         attributes = x;
131     }
132
133     public void setAttributesText(String JavaDoc x) {
134         attributesText = x;
135     }
136
137     public void setBean(String JavaDoc x) {
138         beanId = x;
139     }
140
141     public void setDefault(String JavaDoc x) {
142         dVal = x;
143     }
144
145     /**
146      * Getter for property name.
147      *
148      * @return Value of property name.
149      */

150     public String JavaDoc getName() {
151         return name;
152     }
153
154     /**
155      * Getter for property default.
156      *
157      * @return Value of property default.
158      */

159     public String JavaDoc getDefault() {
160         return dVal;
161     }
162
163     /**
164      * Getter for property bean.
165      *
166      * @return Value of property bean.
167      */

168     public String JavaDoc getBean() {
169         return beanId;
170     }
171
172     /**
173      * Getter for property attributesText.
174      *
175      * @return Value of property attributesText.
176      */

177     public String JavaDoc getAttributesText() {
178         return attributesText;
179     }
180
181     /**
182      * Getter for property attributes.
183      *
184      * @return Value of property attributes.
185      */

186     public Map JavaDoc getAttributes() {
187         return attributes;
188     }
189 }
Popular Tags