KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.internal.InternalStringBuilder;
21
22 import org.apache.beehive.netui.tags.rendering.AbstractHtmlState;
23 import org.apache.beehive.netui.tags.rendering.OptionTag;
24 import org.apache.beehive.netui.tags.rendering.TagRenderingBase;
25 import org.apache.beehive.netui.tags.rendering.WriteRenderAppender;
26 import org.apache.beehive.netui.util.Bundle;
27
28 import javax.servlet.ServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.jsp.JspException JavaDoc;
31 import javax.servlet.jsp.tagext.Tag JavaDoc;
32
33 /**
34  * An option whose state is determined by its enclosing SelectOption.
35  * @jsptagref.tagdescription Renders a single <option> tag.
36  *
37  * <p>The &lt;netui:selectOption> must have a parent {@link Select} tag.
38  *
39  * <p>To dynamically generate a set of &lt;option> tags, point the {@link Select}
40  * tag at a String[], {@link java.util.HashMap java.util.HashMap},
41  * or any object that implements {@link java.util.Map java.util.Map}.
42  * @example The following sample generates a set of &lt;option> tags.
43  * <pre> &lt;netui:form action="submit">
44  * &lt;netui:select dataSource="actionForm.selections" size="5">
45  * &lt;netui:selectOption value="red" />
46  * &lt;netui:selectOption value="blue" />
47  * &lt;netui:selectOption value="green" />
48  * &lt;netui:selectOption value="yellow" />
49  * &lt;netui:selectOption value="orange" />
50  * &lt;/netui:select>
51  * &lt;netui:button type="submit" value="Submit"/>
52  * &lt;/netui:form></pre>
53  * @netui:tag name="selectOption" description="An option whose state is determined by its enclosing netui:selectOption."
54  */

55 public class SelectOption extends HtmlBaseTag
56 {
57     private OptionTag.State _state = new OptionTag.State();
58
59     // this has it's own version of disabled in it...
60
private String JavaDoc _text; // The message text to be displayed to the user for this tag (if any)
61
private boolean _disabled; // Is this option disabled?
62
private String JavaDoc _value; // The server value for this option
63
private Select.RepeatingStages _repeatingType; // The type of the repeater.
64
private boolean _hasError = false; // Hack to avoid registering the same error in the SelectOption AND the Select.
65

66     /**
67      * Return the name of the Tag.
68      */

69     public String JavaDoc getTagName()
70     {
71         return "SelectOption";
72     }
73
74     /**
75      * Base support for the attribute tag. This is overridden to prevent setting the <code>value</code>
76      * attribute.
77      * @param name The name of the attribute
78      * @param value The value of the attribute
79      */

80     public void setAttribute(String JavaDoc name, String JavaDoc value, String JavaDoc facet)
81             throws JspException JavaDoc
82     {
83         if (name == null)
84             return;
85
86         if (name.equals(VALUE)) {
87             String JavaDoc s = Bundle.getString("Tags_AttributeMayNotBeSet", new Object JavaDoc[]{name});
88             registerTagError(s, null);
89         }
90         super.setAttribute(name, value, facet);
91     }
92
93     /**
94      * This method will return the state associated with the tag. This is used by this
95      * base class to access the individual state objects created by the tags.
96      * @return a subclass of the <code>AbstractHtmlState</code> class.
97      */

98     protected AbstractHtmlState getState()
99     {
100         return _state;
101     }
102
103     /**
104      * Gets if this option is disabled or not.
105      * @return the disabled state ("true" or "false")
106      */

107     public boolean getDisabled()
108     {
109         return _disabled;
110     }
111
112     /**
113      * Set if this option is disabled or not.
114      * @param disabled "true" or "false"
115      * @jsptagref.attributedescription Boolean value that determines whether the &lt;option> is disabled.
116      * @jsptagref.databindable false
117      * @jsptagref.attributesyntaxvalue <i>boolean_disabled</i>
118      * @netui:attribute required="false" rtexprvalue="true" type="boolean"
119      * description="Determines whether the <option> is disabled."
120      */

121     public void setDisabled(boolean disabled)
122     {
123         _disabled = disabled;
124     }
125
126     /**
127      * This method will a boolean indicating if the control is disabled or not. This will cause the
128      * disable attribute to be evaluated which may result in a runtime error or a JspException.
129      * @return <code>true</code> if the control is disabled.
130      * @throws JspException on an exception.
131      */

132     protected boolean isDisabled()
133             throws JspException JavaDoc
134     {
135         return _disabled;
136     }
137
138     /**
139      * If the selectOption is being used inside a repeating {@link Select}, this defines the Select
140      * attribute used to generate the option elements.
141      * @param repeatingType Value of "option", "dataSource", "default", (for optionsDataSource, dataSource,
142      * and defaultValue attributes respectively) or "null"
143      * @jsptagref.attributedescription If the selectOption tag is being used inside a repeating Select, this
144      * defines the Select attribute used to generate the option elements. Values are "option", "dataSource", "default",
145      * (for optionsDataSource, dataSource, and defaultValue attributes respectively) and "null".
146      * @jsptagref.databindable false
147      * @jsptagref.attributesyntaxvalue <i>string_repeatingType</i>
148      * @netui:attribute required="false" rtexprvalue="true"
149      * description="If the selectOption is being used inside a repeating Select, this defines the Select attribute used to generate the option elements."
150      */

151     public void setRepeatingType(String JavaDoc repeatingType)
152     {
153         _repeatingType = Select.RepeatingStages.parseString(repeatingType);
154     }
155
156     /**
157      * Set the value of this SelectOption.
158      * @param value the SelectOption value
159      * @jsptagref.attributedescription A literal or a data binding expression that determines the value submitted by the
160      * &lt;option> tag.
161      * @jsptagref.databindable Read Only
162      * @jsptagref.attributesyntaxvalue <i>string_or_expression_value</i>
163      * @netui:attribute required="true" rtexprvalue="true"
164      * description="A literal or a data binding expression that determines the value submitted by the
165      * <option> tag."
166      */

167     public void setValue(String JavaDoc value)
168             throws JspException JavaDoc
169     {
170         _value = value;
171     }
172
173     /**
174      * Process the start of this tag.
175      * @throws JspException if a JSP exception has occurred
176      */

177     public int doStartTag() throws JspException JavaDoc
178     {
179
180         Tag parentTag = getParent();
181         while (!(parentTag instanceof Select)) {
182             parentTag = parentTag.getParent();
183         }
184
185         if (parentTag == null) {
186             //throw error
187
String JavaDoc s = Bundle.getString("Tags_SelectOptionNoSelect");
188             registerTagError(s, null);
189             return SKIP_BODY;
190         }
191
192         Select parentSelect = (Select) parentTag;
193         boolean repeating = parentSelect.isRepeater();
194
195         // if we find an option inside a select and it's not a repeating select report the error
196
if ((parentSelect.getOptionsDataSource() != null) && !repeating) {
197             String JavaDoc s = Bundle.getString("Tags_SelectOptionParentHasOptionsDataSource");
198             _hasError = true;
199             parentSelect.registerTagError(s, null);
200             return SKIP_BODY;
201         }
202
203         // if we are an option inside a repeating select, we must specify the type of repeater we are.
204
if (repeating && _repeatingType == null) {
205             String JavaDoc s = Bundle.getString("Tags_SelectRepeatingOptionType");
206             _hasError = true;
207             parentSelect.registerTagError(s, null);
208             return SKIP_BODY;
209         }
210
211         if (repeating && !isRenderable(parentSelect)) {
212             return SKIP_BODY;
213         }
214
215         // Do nothing until doEndTag() is called
216
return EVAL_BODY_BUFFERED;
217     }
218
219     /**
220      * Process the body text of this tag (if any).
221      * @throws JspException if a JSP exception has occurred
222      */

223     public int doAfterBody() throws JspException JavaDoc
224     {
225         String JavaDoc text = bodyContent.getString();
226         if (text != null) {
227             bodyContent.clearBody();
228             text = text.trim();
229             if (text.length() > 0)
230                 _text = text;
231         }
232         return SKIP_BODY;
233     }
234
235     /**
236      * Process the end of this tag.
237      * @throws JspException if a JSP exception has occurred
238      */

239     public int doEndTag() throws JspException JavaDoc
240     {
241         String JavaDoc scriptId = null;
242
243         ServletRequest JavaDoc req = pageContext.getRequest();
244
245         if ((hasErrors()) || _hasError) {
246             localRelease();
247             return EVAL_PAGE;
248         }
249
250         // the parent was validated in the doStartTag call
251
Tag parentTag = getParent();
252         while (!(parentTag instanceof Select)) {
253             parentTag = parentTag.getParent();
254         }
255
256         assert (parentTag instanceof Select);
257         Select parentSelect = (Select) parentTag;
258         if (parentSelect.isRepeater()) {
259             if (!isRenderable(parentSelect))
260                 return EVAL_PAGE;
261         }
262
263
264         // Generate an HTML <option> element
265
//InternalStringBuilder results = new InternalStringBuilder(128);
266
_state.value = _value;
267
268         // we assume that tagId will over have override id if both
269
// are defined.
270
if (_state.id != null) {
271             scriptId = renderNameAndId((HttpServletRequest JavaDoc) req, _state, null);
272         }
273
274         _state.disabled = _disabled;
275
276         if (parentSelect.isMatched(_value))
277             _state.selected = true;
278
279         WriteRenderAppender writer = new WriteRenderAppender(pageContext);
280         TagRenderingBase br = TagRenderingBase.Factory.getRendering(TagRenderingBase.OPTION_TAG, req);
281         br.doStartTag(writer, _state);
282
283
284         if (_text == null)
285             write(parentSelect.formatText(_value));
286         else {
287             //@TODO: How should we report errors
288
write(parentSelect.formatText(_text));
289         }
290         br.doEndTag(writer);
291
292         parentSelect.addOptionToList(_value);
293
294         if (scriptId != null)
295             write(scriptId);
296
297         // Continue evaluating this page
298
localRelease();
299         return EVAL_PAGE;
300     }
301
302     /**
303      * Release any acquired resources.
304      */

305     protected void localRelease()
306     {
307         super.localRelease();
308
309         _state.clear();
310
311         _text = null;
312         _disabled = false;
313         _value = null;
314         _hasError = false;
315     }
316
317     private boolean isRenderable(Select sel)
318     {
319         assert(sel != null);
320
321         if (_repeatingType == null)
322             return true;
323
324         if (sel.isNullStage())
325             return _repeatingType == Select.RepeatingStages.NULL;
326         if (sel.isOptionStage() && _repeatingType == Select.RepeatingStages.OPTION)
327             return true;
328         if (sel.isDataSourceStage() && _repeatingType == Select.RepeatingStages.DATASOURCE)
329             return true;
330         if (sel.isDefaultStage() && _repeatingType == Select.RepeatingStages.DEFAULT)
331             return true;
332         return false;
333     }
334 }
335
Popular Tags