KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > html > RadioButtonOption


1 /*
2  * Copyright 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  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.html;
19
20 import org.apache.beehive.netui.tags.IHtmlAccessable;
21 import org.apache.beehive.netui.tags.rendering.*;
22 import org.apache.beehive.netui.util.Bundle;
23
24 import javax.servlet.ServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.jsp.JspException JavaDoc;
27 import javax.servlet.jsp.tagext.Tag JavaDoc;
28
29 /**
30  * A radio button whose state is determined by its enclosing RadioButtonGroup.
31  * @jsptagref.tagdescription Generates a single radiobutton option in a group of options.
32  *
33  * <p>The &lt;radioButtonOption> tag must have a parent {@link RadioButtonGroup} tag.
34  * @example In this example, three radiobuttons are generated in the browser.
35  *
36  * <pre> &lt;netui:form action="processData">
37  * &lt;netui:radioButtonGroup dataSource="actionForm.selection">
38  * &lt;netui:radioButtonOption value="value1">Display Text 1&lt;/netui:radioButtonOption>&lt;br>
39  * &lt;netui:radioButtonOption value="value2">Display Text 2&lt;/netui:radioButtonOption>&lt;br>
40  * &lt;netui:radioButtonOption value="value3">Display Text 3&lt;/netui:radioButtonOption>&lt;br>
41  * &lt;/netui:radioButtonGroup>
42  * &lt;netui:button value="Submit" />
43  * &lt;/netui:form></pre>
44  * @netui:tag name="radioButtonOption" description="A radio button whose state is determined by its enclosing netui:RadioButtonGroup."
45  */

46 public class RadioButtonOption
47         extends HtmlFocusBaseTag
48         implements IHtmlAccessable
49 {
50     private InputBooleanTag.State _state = new InputBooleanTag.State();
51     private SpanTag.State _spanState = new SpanTag.State();
52     private String JavaDoc _text;
53
54     /**
55      * Return the name of the Tag.
56      */

57     public String JavaDoc getTagName()
58     {
59         return "RadioButtonOption";
60     }
61
62     /**
63      * This method will return the state associated with the tag. This is used by this
64      * base class to access the individual state objects created by the tags.
65      * @return a subclass of the <code>AbstractHtmlState</code> class.
66      */

67     protected AbstractHtmlState getState()
68     {
69         return _state;
70     }
71
72     /**
73      * Base support for the attribute tag. This is overridden to prevent setting the <code>type</code>
74      * and <code>value</code> attribute.
75      * @param name The name of the attribute
76      * @param value The value of the attribute
77      */

78     public void setAttribute(String JavaDoc name, String JavaDoc value, String JavaDoc facet)
79             throws JspException JavaDoc
80     {
81         if (name != null) {
82             if (name.equals(TYPE) || name.equals(VALUE) || name.equals(CHECKED)) {
83                 String JavaDoc s = Bundle.getString("Tags_AttributeMayNotBeSet", new Object JavaDoc[]{name});
84                 registerTagError(s, null);
85             }
86             else {
87                 _state.disabled = new Boolean JavaDoc(value).booleanValue();
88                 return;
89             }
90         }
91         super.setAttribute(name, value, facet);
92     }
93
94     /**
95      * Set the label style for each contained RadioButtonOption.
96      * The label style here will override a labelStyle at the RadioButtonGroup level.
97      * @param labelStyle the label style
98      * @jsptagref.attributedescription The style of the label for each contained &lt;netui:radioButtonOption> tag.
99      * @jsptagref.databindable false
100      * @jsptagref.attributesyntaxvalue <i>string_labelStyle</i>
101      * @netui:attribute required="false" rtexprvalue="true"
102      * description="The style of the label for each contained <netui:radioButtonOption> tag."
103      */

104     public void setLabelStyle(String JavaDoc labelStyle)
105     {
106         _spanState.style = setNonEmptyValueAttribute(labelStyle);
107     }
108
109     /**
110      * Set the label style class for each contained RadioButtonOption.
111      * The label style class here will override a labelStyleClass at the RadioButtonGroup level.
112      * @param labelStyleClass the label style
113      * @jsptagref.attributedescription The class of the labels for each contained &lt;netui:radioButtonOption> tag.
114      * @jsptagref.databindable false
115      * @jsptagref.attributesyntaxvalue <i>string_class</i>
116      * @netui:attribute required="false" rtexprvalue="true"
117      * description="The class of the labels for each contained <netui:radioButtonOption> tag."
118      */

119     public void setLabelStyleClass(String JavaDoc labelStyleClass)
120     {
121         _spanState.styleClass = setNonEmptyValueAttribute(labelStyleClass);
122     }
123
124     /**
125      * Set the value of this RadioButtonOption.
126      * @param value the RadioButtonOption value
127      * @jsptagref.attributedescription A literal or data binding expression.
128      * @jsptagref.databindable Read Only
129      * @jsptagref.attributesyntaxvalue <i>string_or_expression_value</i>
130      * @netui:attribute required="true" rtexprvalue="true" type="java.lang.Object"
131      * description="The value of the option."
132      */

133     public void setValue(Object JavaDoc value)
134             throws JspException JavaDoc
135     {
136         if (value != null)
137             _state.value = value.toString();
138         else
139             _state.value = null;
140     }
141
142     /**
143      * Process the start of this tag.
144      * @throws JspException if a JSP exception has occurred
145      */

146     public int doStartTag() throws JspException JavaDoc
147     {
148
149         Tag JavaDoc parentTag = findAncestorWithClass(this, RadioButtonGroup.class);
150         if (parentTag == null) {
151             String JavaDoc s = Bundle.getString("Tags_RadioButtonOptionNoRadioButtonGroup");
152             registerTagError(s, null);
153             return SKIP_BODY;
154         }
155
156         RadioButtonGroup parent = (RadioButtonGroup) parentTag;
157         if ((parent.getOptionsDataSource() != null) && !parent.isRepeater()) {
158             String JavaDoc s = Bundle.getString("Tags_RadioOptionParentHasOptionsDataSource");
159             parent.registerTagError(s, null);
160             return SKIP_BODY;
161         }
162
163         return EVAL_BODY_BUFFERED;
164
165     }
166
167     /**
168      * Process the body text of this tag (if any).
169      * @throws JspException if a JSP exception has occurred
170      */

171     public int doAfterBody() throws JspException JavaDoc
172     {
173         String JavaDoc text = bodyContent.getString();
174         if (text != null) {
175             text = text.trim();
176             bodyContent.clearBody();
177             if (text.length() > 0)
178                 _text = text;
179         }
180         return SKIP_BODY;
181     }
182
183     /**
184      * Process the end of this tag.
185      * @throws JspException if a JSP exception has occurred
186      */

187     public int doEndTag() throws JspException JavaDoc
188     {
189         if (hasErrors())
190             return reportAndExit(EVAL_PAGE);
191
192         ServletRequest JavaDoc req = pageContext.getRequest();
193         ConstantRendering cr = TagRenderingBase.Factory.getConstantRendering(req);
194
195         // this was verified in doBeginTag
196
RadioButtonGroup parent = (RadioButtonGroup) findAncestorWithClass(this, RadioButtonGroup.class);
197         assert(parent != null);
198         boolean repeat = parent.isRepeater();
199
200         // Generate an HTML <input type='radio'> element
201
WriteRenderAppender writer = new WriteRenderAppender(pageContext);
202         if (parent.isVertical()) {
203             cr.TR_TD(writer);
204         }
205         _state.type = INPUT_RADIO;
206         _state.name = parent.getQualifiedDataSourceName();
207         if (_state.id != null) {
208             String JavaDoc tagId = _state.id;
209             _state.id = getIdForTagId(_state.id);
210             String JavaDoc script = renderDefaultNameAndId((HttpServletRequest JavaDoc) req, _state, tagId, _state.name);
211             if (script != null) {
212                 write(script);
213             }
214         }
215
216         // set the checked and disabled states
217
_state.checked = new Boolean JavaDoc(parent.isMatched(_state.value, null)).booleanValue();
218         _state.disabled = isDisabled();
219         if (!_state.disabled)
220             _state.disabled = parent.isDisabled();
221
222         TagRenderingBase br = TagRenderingBase.Factory.getRendering(TagRenderingBase.INPUT_BOOLEAN_TAG, req);
223         br.doStartTag(writer, _state);
224
225         // if this is in a repeater, then we only output the <input tag>
226
if (repeat) {
227             localRelease();
228             return EVAL_PAGE;
229         }
230
231         if (_spanState.style == null) {
232             _spanState.style = parent.getLabelStyle();
233         }
234         if (_spanState.styleClass == null) {
235             _spanState.styleClass = parent.getLabelStyleClass();
236         }
237
238         TagRenderingBase spanTag = TagRenderingBase.Factory.getRendering(TagRenderingBase.SPAN_TAG, req);
239         spanTag.doStartTag(writer, _spanState);
240         if (_text == null)
241             write(_state.value);
242         else
243             write(_text);
244         spanTag.doEndTag(writer);
245
246         if (parent.isVertical()) {
247             cr.end_TD_TR(writer);
248         }
249
250         // Continue evaluating this page
251
localRelease();
252         return EVAL_PAGE;
253
254     }
255
256     /**
257      * Release any acquired resources.
258      */

259     protected void localRelease()
260     {
261         super.localRelease();
262
263         _state.clear();
264         _spanState.clear();
265
266         _text = null;
267     }
268
269     /* ==================================================================
270      *
271      * This tag's publically exposed HTML, CSS, and JavaScript attributes
272      *
273      * ==================================================================
274      */

275     /**
276      * Sets the accessKey attribute value. This should key value of the
277      * keyboard navigation key. It is recommended not to use the following
278      * values because there are often used by browsers <code>A, C, E, F, G,
279      * H, V, left arrow, and right arrow</code>.
280      * @param accessKey the accessKey value.
281      * @jsptagref.attributedescription The keyboard navigation key for the element.
282      * The following values are not recommended because they
283      * are often used by browsers: <code>A, C, E, F, G,
284      * H, V, left arrow, and right arrow</code>
285      * @jsptagref.databindable false
286      * @jsptagref.attributesyntaxvalue <i>string_accessKey</i>
287      * @netui:attribute required="false" rtexprvalue="true" type="char"
288      * description=" The keyboard navigation key for the element.
289      * The following values are not recommended because they
290      * are often used by browsers: A, C, E, F, G,
291      * H, V, left arrow, and right arrow"
292      */

293     public void setAccessKey(char accessKey)
294     {
295         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ACCESSKEY, Character.toString(accessKey));
296     }
297
298     /**
299      * Sets the alt attribute value.
300      * @param alt the alt value.
301      * @jsptagref.attributedescription The alt attribute of the element.
302      * @jsptagref.databindable Read Only
303      * @jsptagref.attributesyntaxvalue <i>string_alt</i>
304      * @netui:attribute required="false" rtexprvalue="true"
305      * description="The alt attribute of the element."
306      */

307     public void setAlt(String JavaDoc alt)
308     {
309         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ALT, alt);
310     }
311
312     /**
313      * Sets the tabIndex of the rendered html tag.
314      * @param tabindex the tab index.
315      * @jsptagref.attributedescription The tabIndex of the rendered HTML tag. This attribute determines the position of the
316      * tag in the sequence of page elements that the user may advance through by pressing the TAB key.
317      * @jsptagref.databindable false
318      * @jsptagref.attributesyntaxvalue <i>string_tabIndex</i>
319      * @netui:attribute required="false" rtexprvalue="true" type="char"
320      * description="The tabIndex of the rendered HTML tag. This attribute determines the position of the
321      * tag in the sequence of page elements that the user may advance through by pressing the TAB key."
322      */

323     public void setTabindex(int tabindex)
324     {
325         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, TABINDEX, Integer.toString(tabindex));
326     }
327 }
328
Popular Tags