KickJava   Java API By Example, From Geeks To Geeks.

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


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 // java imports
21

22 import org.apache.beehive.netui.tags.IHtmlAccessable;
23 import org.apache.beehive.netui.tags.rendering.*;
24 import org.apache.beehive.netui.util.Bundle;
25
26 import javax.servlet.ServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.jsp.JspException JavaDoc;
29 import javax.servlet.jsp.tagext.Tag JavaDoc;
30
31 // external imports
32

33 /**
34  * A checkbox whose state is determined by its enclosing CheckBoxGroup.
35  * @jsptagref.tagdescription Renders a single HTML checkbox within a group of checkboxes.
36  *
37  * The <netui:checkBoxOption> tags must have a parent
38  * {@link CheckBoxGroup} tag (which
39  * determines the data binding for the <netui:checkBoxOption> tags).
40  * @example In this sample, a set of <checkBoxOption> tags are submitted
41  * to the <code>processData</code> action method.
42  *
43  * <pre> &lt;netui:form action="processData">
44  * &lt;netui:checkBoxGroup dataSource="actionForm.data" defaultValue="${actionForm.defaultChoices}">
45  * &lt;netui:checkBoxOption value="wantSpecialCDOffers">Do you want to be notified of special CD offers?&lt;/netui:checkBoxOption>&lt;br>
46  * &lt;netui:checkBoxOption value="wantSpecialDVDOffers">Do you want to be notified of special DVD offers?&lt;/netui:checkBoxOption>&lt;br>
47  * &lt;/netui:checkBoxGroup>
48  * &lt;netui:button value="Submit" type="submit"/>
49  * &lt;/netui:form></pre>
50  * @netui:tag name="checkBoxOption" description="A checkbox whose state is determined by its enclosing CheckBoxGroup."
51  */

52 public class CheckBoxOption extends HtmlFocusBaseTag
53         implements IHtmlAccessable
54 {
55     private InputBooleanTag.State _state = new InputBooleanTag.State();
56     private SpanTag.State _spanState = new SpanTag.State();
57     private String JavaDoc _text; // The message text to be displayed to the user for this tag (if any)
58
private Boolean JavaDoc _defaultValue;
59
60     /**
61      * Return the name of the Tag.
62      */

63     public String JavaDoc getTagName()
64     {
65         return "CheckBoxOption";
66     }
67
68     /**
69      * This method will return the state associated with the tag. This is used by this
70      * base class to access the individual state objects created by the tags.
71      * @return a subclass of the <code>AbstractHtmlState</code> class.
72      */

73     protected AbstractHtmlState getState()
74     {
75         return _state;
76     }
77
78     /**
79      * Base support for the attribute tag. This is overridden to prevent setting the <code>type</code>
80      * and <code>value</code> attribute.
81      * @param name The name of the attribute. This value may not be null or the empty string.
82      * @param value The value of the attribute. This may contain an expression.
83      * @param facet The name of a facet to which the attribute will be applied. This is optional.
84      * @throws JspException A JspException may be thrown if there is an error setting the attribute.
85      */

86     public void setAttribute(String JavaDoc name, String JavaDoc value, String JavaDoc facet)
87             throws JspException JavaDoc
88     {
89         if (name != null) {
90             if (name.equals(TYPE) || name.equals(VALUE) || name.equals(CHECKED)) {
91                 String JavaDoc s = Bundle.getString("Tags_AttributeMayNotBeSet", new Object JavaDoc[]{name});
92                 registerTagError(s, null);
93             }
94             else {
95                 _state.disabled = new Boolean JavaDoc(value).booleanValue();
96                 return;
97             }
98         }
99         super.setAttribute(name, value, facet);
100     }
101
102     /**
103      * Set the label style for each contained CheckBoxOption.
104      * The label style here will override a labelStyle at the CheckBoxGroup level.
105      * @param labelStyle the label style
106      * @jsptagref.attributedescription The style of the text displayed by the rendered checkbox.
107      * @jsptagref.databindable false
108      * @jsptagref.attributesyntaxvalue <i>string_label</i>
109      * @netui:attribute required="false" rtexprvalue="true"
110      * description="The style of the text displayed by the rendered checkbox."
111      */

112     public void setLabelStyle(String JavaDoc labelStyle)
113     {
114         _spanState.style = setNonEmptyValueAttribute(labelStyle);
115     }
116
117     /**
118      * Set the label style class for each contained CheckBoxOption.
119      * The label style class here will override a labelStyleClass at the CheckBoxGroup level.
120      * @param labelStyleClass the label style
121      * @jsptagref.attributedescription The class of the text displayed by the rendered checkbox.
122      * @jsptagref.databindable false
123      * @jsptagref.attributesyntaxvalue <i>string_class</i>
124      * @netui:attribute required="false" rtexprvalue="true"
125      * description="The class of the text displayed by the rendered checkbox."
126      */

127     public void setLabelStyleClass(String JavaDoc labelStyleClass)
128     {
129         _spanState.styleClass = setNonEmptyValueAttribute(labelStyleClass);
130     }
131
132     /**
133      * Set the value of this CheckBoxOption.
134      * @param value the CheckBoxOption value
135      * @jsptagref.attributedescription A String literal or a data binding expression. The value attribute determines the value submitted
136      * by the checkbox.
137      * @jsptagref.databindable true
138      * @jsptagref.attributesyntaxvalue <i>string_literal_or_expression_value</i>
139      * @netui:attribute required="false" rtexprvalue="true"
140      * description="A String literal or a data binding expression. The value attribute determines the value submitted
141      * by the checkbox."
142      */

143     public void setValue(Object JavaDoc value)
144             throws JspException JavaDoc
145     {
146         if (value != null)
147             _state.value = value.toString();
148         else
149             _state.value = null;
150     }
151
152     /**
153      * Sets the default value (can be an expression).
154      * @param defaultValue the default value
155      * @jsptagref.attributedescription The boolean value or expression to be used as the default value.
156      * @jsptagref.databindable true
157      * @jsptagref.attributesyntaxvalue <i>boolean_defaultValue</i>
158      * @netui:attribute required="false" rtexprvalue="true" type="boolean"
159      * description="Sets the default value."
160      */

161     public void setDefaultValue(boolean defaultValue)
162             throws JspException JavaDoc
163     {
164         _defaultValue = Boolean.valueOf(defaultValue);
165     }
166
167     /**
168      * Process the start of this tag.
169      * @throws JspException if a JSP exception has occurred
170      */

171     public int doStartTag() throws JspException JavaDoc
172     {
173         // verify that the parent is a CheckBoxGroup
174
Tag JavaDoc parentTag = findAncestorWithClass(this, CheckBoxGroup.class);
175         if (parentTag == null) {
176             String JavaDoc s = Bundle.getString("Tags_CheckBoxOptionNoCheckBoxGroup");
177             registerTagError(s, null);
178             return SKIP_BODY;
179         }
180
181         CheckBoxGroup parent = (CheckBoxGroup) parentTag;
182         if ((parent.getOptionsDataSource() != null && !parent.isRepeater())) {
183             String JavaDoc s = Bundle.getString("Tags_CheckBoxOptionParentHasOptionsDataSource");
184             parent.registerTagError(s, null);
185             return SKIP_BODY;
186         }
187
188         // Do nothing until doEndTag() is called
189
return EVAL_BODY_BUFFERED;
190     }
191
192     /**
193      * Process the body text of this tag (if any).
194      * @throws JspException if a JSP exception has occurred
195      */

196     public int doAfterBody() throws JspException JavaDoc
197     {
198         String JavaDoc text = bodyContent.getString();
199         if (text != null) {
200             bodyContent.clearBody();
201             text = text.trim();
202             if (text.length() > 0)
203                 _text = text;
204         }
205         return SKIP_BODY;
206     }
207
208     /**
209      * Process the end of this tag.
210      * @throws JspException if a JSP exception has occurred
211      */

212     public int doEndTag() throws JspException JavaDoc
213     {
214         if (hasErrors())
215             return reportAndExit(EVAL_PAGE);
216
217         // we verified that the parent was a CheckboxGroup in the doBeginTag()
218
ServletRequest JavaDoc req = pageContext.getRequest();
219         CheckBoxGroup parent = (CheckBoxGroup) findAncestorWithClass(this, CheckBoxGroup.class);
220         ConstantRendering cr = TagRenderingBase.Factory.getConstantRendering(req);
221         boolean repeat = parent.isRepeater();
222
223         // create the writer
224
WriteRenderAppender writer = new WriteRenderAppender(pageContext);
225         if (!repeat && parent.isVertical()) {
226             cr.TR_TD(writer);
227         }
228
229
230         _state.type = INPUT_CHECKBOX;
231         _state.name = parent.getQualifiedDataSourceName();
232         if (_state.id != null) {
233             String JavaDoc tagId = _state.id;
234             _state.id = getIdForTagId(_state.id);
235             /* This has been commented out because we can't set focus on checkbox options
236                because they don't have a unique name.
237             Form parentForm = getNearestForm();
238             if (parentForm != null)
239                 parentForm.addTagID(tagId, _state.name);
240                 */

241             String JavaDoc script = renderDefaultNameAndId((HttpServletRequest JavaDoc) req, _state, tagId, _state.name);
242             if (script != null) {
243                 write(script);
244             }
245         }
246
247         // Disabled on the option itself will override the parent setting this.
248
// We check to see if the option actuall had disabled set on it, if not then look
249
// at the parent.
250
if (parent.isMatched(_state.value, _defaultValue))
251             _state.checked = true;
252         _state.disabled = isDisabled();
253         if (!_state.disabled)
254             _state.disabled = parent.isDisabled();
255
256         TagRenderingBase br = TagRenderingBase.Factory.getRendering(TagRenderingBase.INPUT_BOOLEAN_TAG, req);
257         br.doStartTag(writer, _state);
258         br.doEndTag(writer);
259
260         if (repeat && !parent.isDisabled()) {
261             parent.createHiddenField(writer);
262         }
263
264         // if this is in a repeater, then we only output the <input tag>
265
if (repeat) {
266             //write(results.toString());
267
localRelease();
268             return EVAL_PAGE;
269         }
270
271         if (_spanState.style == null) {
272             _spanState.style = parent.getLabelStyle();
273         }
274         if (_spanState.styleClass == null) {
275             _spanState.styleClass = parent.getLabelStyleClass();
276         }
277
278         TagRenderingBase spanTag = TagRenderingBase.Factory.getRendering(TagRenderingBase.SPAN_TAG, req);
279         spanTag.doStartTag(writer, _spanState);
280         if (_text == null)
281             write(_state.value);
282         else
283             write(_text);
284         spanTag.doEndTag(writer);
285
286         if (parent.isVertical()) {
287             cr.end_TD_TR(writer);
288         }
289
290         // Continue evaluating this page
291
localRelease();
292         return EVAL_PAGE;
293
294     }
295
296     /**
297      * Release any acquired resources.
298      */

299     protected void localRelease()
300     {
301         super.localRelease();
302
303         _state.clear();
304         _spanState.clear();
305         _text = null;
306     }
307
308     /* ==================================================================
309      *
310      * This tag's publically exposed HTML, CSS, and JavaScript attributes
311      *
312      * ==================================================================
313      */

314
315     /**
316      * Sets the accessKey attribute value. This should key value of the
317      * keyboard navigation key. It is recommended not to use the following
318      * values because there are often used by browsers <code>A, C, E, F, G,
319      * H, V, left arrow, and right arrow</code>.
320      * @param accessKey the accessKey value.
321      * @jsptagref.attributedescription The keyboard navigation key for the element.
322      * The following values are not recommended because they
323      * are often used by browsers: <code>A, C, E, F, G,
324      * H, V, left arrow, and right arrow</code>
325      * @jsptagref.databindable false
326      * @jsptagref.attributesyntaxvalue <i>string_accessKey</i>
327      * @netui:attribute required="false" rtexprvalue="true" type="char"
328      * description="The keyboard navigation key for the element.
329      * The following values are not recommended because they
330      * are often used by browsers: A, C, E, F, G,
331      * H, V, left arrow, and right arrow"
332      */

333     public void setAccessKey(char accessKey)
334     {
335         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ACCESSKEY, Character.toString(accessKey));
336     }
337
338     /**
339      * Sets the alt attribute value.
340      * @param alt the alt value.
341      * @jsptagref.attributedescription The alt attribute of the element.
342      * @jsptagref.databindable Read Only
343      * @jsptagref.attributesyntaxvalue <i>string_alt</i>
344      * @netui:attribute required="false" rtexprvalue="true"
345      * description="The alt attribute of the element."
346      */

347     public void setAlt(String JavaDoc alt)
348     {
349         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ALT, alt);
350     }
351
352     /**
353      * Sets the tabIndex of the rendered html tag.
354      * @param tabindex the tab index.
355      * @jsptagref.attributedescription The tabIndex of the rendered HTML tag. This attribute determines the position of the
356      * tag in the sequence of page elements that the user may advance through by pressing the TAB key.
357      * @jsptagref.databindable false
358      * @jsptagref.attributesyntaxvalue <i>string_tabIndex</i>
359      * @netui:attribute required="false" rtexprvalue="true" type="int"
360      * description="The tabIndex of the rendered HTML tag. This attribute determines the position of the
361      * tag in the sequence of page elements that the user may advance through by pressing the TAB key."
362      */

363     public void setTabindex(int tabindex)
364     {
365         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, TABINDEX, Integer.toString(tabindex));
366     }
367 }
368
Popular Tags