KickJava   Java API By Example, From Geeks To Geeks.

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


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.ByRef;
23 import org.apache.beehive.netui.tags.HtmlUtils;
24 import org.apache.beehive.netui.tags.IHtmlAccessable;
25 import org.apache.beehive.netui.tags.rendering.*;
26 import org.apache.beehive.netui.util.Bundle;
27
28 import javax.servlet.ServletRequest JavaDoc;
29 import javax.servlet.jsp.JspException JavaDoc;
30 import java.util.ArrayList JavaDoc;
31
32 /**
33  * Renders a databound TextBox with the given attributes.
34  * @jsptagref.tagdescription Renders an HTML <input type="text"> tag.
35  * @example In this sample, a <netui:textBox> reads from and writes to the Form Bean's firstname property.
36  * If the submitted text is NULL, the default value is specified by the Page Flow's firstName property.
37  * <pre> &lt;netui:textBox dataSource="actionForm.firstName"
38  * defaultValue="${pageFlow.defaultFirstName}"
39  * size="20" /></pre>
40  * @netui:tag name="textBox" description="Renders a databound TextBox with the given attributes."
41  */

42 public class TextBox extends HtmlDefaultableDataSourceTag
43         implements IFormattable, IHtmlAccessable
44 {
45     private InputTextTag.State _state = new InputTextTag.State();
46
47     private ArrayList JavaDoc _formatters;
48     private boolean _password = false;
49     private boolean _formatErrors = false;
50
51     /**
52      * Return the name of the Tag.
53      */

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

64     protected AbstractHtmlState getState()
65     {
66         return _state;
67     }
68
69     /**
70      * Base support for the attribute tag. This is overridden to prevent setting the <code>type</code>,
71      * and <code>value</code> attributes.
72      * @param name The name of the attribute. This value may not be null or the empty string.
73      * @param value The value of the attribute. This may contain an expression.
74      * @param facet The name of a facet to which the attribute will be applied. This is optional.
75      * @throws JspException A JspException may be thrown if there is an error setting the attribute.
76      */

77     public void setAttribute(String JavaDoc name, String JavaDoc value, String JavaDoc facet)
78             throws JspException JavaDoc
79     {
80         if (name != null) {
81             if (name.equals(TYPE) || name.equals(VALUE)) {
82                 String JavaDoc s = Bundle.getString("Tags_AttributeMayNotBeSet", new Object JavaDoc[]{name});
83                 registerTagError(s, null);
84             }
85             else {
86                 if (name.equals(DISABLED)) {
87                     _state.disabled = new Boolean JavaDoc(value).booleanValue();
88                     return;
89                 }
90                 else if (name.equals(READONLY)) {
91                     _state.readonly = new Boolean JavaDoc(value).booleanValue();
92                     return;
93                 }
94                 else if (name.equals(MAXLENGTH)) {
95                     _state.maxlength = Integer.parseInt(value);
96                     return;
97                 }
98                 else if (name.equals(SIZE)) {
99                     _state.size = Integer.parseInt(value);
100                     return;
101                 }
102             }
103         }
104
105         super.setAttribute(name, value, facet);
106     }
107
108     //******************************** ATTRIBUTES *******************************
109

110     /**
111      * Set the maximum length (in characters) of the TextBox.
112      * @param maxlength the max length
113      * @jsptagref.attributedescription Integer. The maximum number of character that can be entered in the rendered &lt;input> element.
114      * @jsptagref.databindable Read Only
115      * @jsptagref.attributesyntaxvalue <i>integer_maxLength</i>
116      * @netui:attribute required="false" rtexprvalue="true" type="int"
117      * description="The maximum number of character that can be entered in the rendered <input> element."
118      */

119     public void setMaxlength(int maxlength)
120     {
121         _state.maxlength = maxlength;
122     }
123
124     /**
125      * Set the password state (true means this is a password field).
126      * @param password the password state
127      * @jsptagref.attributedescription Boolean. Determines whether the password characters that the user enters into the &lt;input> element will be disguised in the browser.
128      * @jsptagref.databindable false
129      * @jsptagref.attributesyntaxvalue <i>boolean_password</i>
130      * @netui:attribute required="false" rtexprvalue="true" type="boolean"
131      * description="Determines whether the password characters that the user enters into the &lt;input> element will be disguised in the browser."
132      */

133     public void setPassword(boolean password)
134     {
135         _password = password;
136     }
137
138     /**
139      * Get the password state.
140      * @return <code>true</code> if the text box is used for passwords. <code>false</code> otherwise.
141      */

142     protected boolean getPassword()
143     {
144         return _password;
145     }
146
147     /**
148      * Set if this TextBox is read-only.
149      * @param readonly the read-only state
150      * @jsptagref.attributedescription Boolean. Determines if the rendered &lt;input> element is read-only.
151      * @jsptagref.databindable false
152      * @jsptagref.attributesyntaxvalue <i>boolean_readOnly</i>
153      * @netui:attribute required="false" rtexprvalue="true" type="boolean"
154      * description="Determines if the rendered &lt;input> element is read-only"
155      */

156     public void setReadonly(boolean readonly)
157     {
158         _state.readonly = readonly;
159     }
160
161     /**
162      * Set the size (in characters) of the TextBox.
163      * @param size the size
164      * @jsptagref.attributedescription Integer. The number of characters visible in the &lt;input> element.
165      * @jsptagref.databindable Read Only
166      * @jsptagref.attributesyntaxvalue <i>integer_size</i>
167      * @netui:attribute required="false" rtexprvalue="true" type="int"
168      * description="The number of characters visible in the <input> element."
169      */

170     public void setSize(int size)
171     {
172         _state.size = size;
173     }
174
175     /**
176      * Sets the accessKey attribute value. This should key value of the
177      * keyboard navigation key. It is recommended not to use the following
178      * values because there are often used by browsers <code>A, C, E, F, G,
179      * H, V, left arrow, and right arrow</code>.
180      * @param accessKey the accessKey value.
181      * @jsptagref.attributedescription The keyboard navigation key for the element.
182      * The following values are not recommended because they
183      * are often used by browsers: <code>A, C, E, F, G,
184      * H, V, left arrow, and right arrow</code>
185      * @jsptagref.databindable false
186      * @jsptagref.attributesyntaxvalue <i>string_accessKey</i>
187      * @netui:attribute required="false" rtexprvalue="true" type="char"
188      * description="The keyboard navigation key for the element.
189      * The following values are not recommended because they
190      * are often used by browsers: A, C, E, F, G,
191      * H, V, left arrow, and right arrow"
192      */

193     public void setAccessKey(char accessKey)
194     {
195         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ACCESSKEY, Character.toString(accessKey));
196     }
197
198     /**
199      * Sets the alt attribute value.
200      * @param alt the alt value.
201      * @jsptagref.attributedescription The alt attribute of the element.
202      * @jsptagref.databindable Read Only
203      * @jsptagref.attributesyntaxvalue <i>string_alt</i>
204      * @netui:attribute required="false" rtexprvalue="true"
205      * description="The alt attribute of the element."
206      */

207     public void setAlt(String JavaDoc alt)
208     {
209         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ALT, alt);
210     }
211
212     /**
213      * Sets the tabIndex of the rendered html tag.
214      * @param tabindex the tab index.
215      * @jsptagref.attributedescription The tabIndex of the rendered HTML tag. This attribute determines the position of the
216      * tag in the sequence of page elements that the user may advance through by pressing the TAB key.
217      * @jsptagref.databindable false
218      * @jsptagref.attributesyntaxvalue <i>string_tabIndex</i>
219      * @netui:attribute required="false" rtexprvalue="true" type="int"
220      * description="The tabIndex of the rendered HTML tag. This attribute determines the position of the
221      * tag in the sequence of page elements that the user may advance through by pressing the TAB key."
222      */

223     public void setTabindex(int tabindex)
224     {
225         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, TABINDEX, Integer.toString(tabindex));
226     }
227
228     //*************************************** TAG METHODS *****************************************
229

230     /**
231      * Prepare the TextBox's formatters.
232      * @throws JspException if a JSP exception has occurred
233      */

234     public int doStartTag() throws JspException JavaDoc
235     {
236         return EVAL_BODY_BUFFERED;
237     }
238
239     /**
240      * Render the TextBox.
241      * @throws JspException if a JSP exception has occurred
242      */

243     public int doEndTag() throws JspException JavaDoc
244     {
245         ServletRequest JavaDoc req = pageContext.getRequest();
246
247         // Create an appropriate "input" element based on our parameters
248
if (_password) {
249             _state.type = INPUT_PASSWORD;
250         }
251         else {
252             _state.type = INPUT_TEXT;
253         }
254
255
256         // Create the state for the input tag
257
ByRef ref = new ByRef();
258         nameHtmlControl(_state, ref);
259
260         _state.disabled = isDisabled();
261
262         // Create the text value of the TextBox
263
Object JavaDoc textObject = evaluateDataSource();
264         if ((textObject == null) || (textObject.toString().length() == 0)) {
265             textObject = _defaultValue;
266         }
267
268         // Get the text value for the textbox, the result
269
String JavaDoc text = null;
270         if (textObject != null) {
271             text = formatText(textObject);
272             InternalStringBuilder sb = new InternalStringBuilder(text.length() + 16);
273             StringBuilderRenderAppender sbAppend = new StringBuilderRenderAppender(sb);
274             HtmlUtils.filter(text, sbAppend);
275             text = sb.toString();
276         }
277         _state.value = text;
278
279         // if there were format errors then report them
280
if (_formatErrors) {
281             if (bodyContent != null) {
282                 String JavaDoc value = bodyContent.getString().trim();
283                 bodyContent.clearBody();
284                 write(value);
285             }
286         }
287
288         if (hasErrors())
289             return reportAndExit(EVAL_PAGE);
290
291         // create the input tag.
292
WriteRenderAppender writer = new WriteRenderAppender(pageContext);
293         TagRenderingBase br = TagRenderingBase.Factory.getRendering(TagRenderingBase.INPUT_TEXT_TAG, req);
294         assert(br != null);
295
296         br.doStartTag(writer, _state);
297         br.doEndTag(writer);
298
299         // write any script that needs to be written
300
if (!ref.isNull())
301             write((String JavaDoc) ref.getRef());
302
303         localRelease();
304         return EVAL_PAGE;
305     }
306
307     /**
308      * Release any acquired resources.
309      */

310     protected void localRelease()
311     {
312         super.localRelease();
313
314         _state.clear();
315
316         _formatters = null;
317         _password = false;
318         _formatErrors = false;
319     }
320
321     /**
322      * Adds a FormatTag.Formatter to the TextBox's set of formatters
323      * @param formatter a FormatTag.Formatter added by a child FormatTag.
324      */

325     public void addFormatter(FormatTag.Formatter formatter)
326     {
327         if (_formatters == null)
328             _formatters = new ArrayList JavaDoc();
329
330         _formatters.add(formatter);
331     }
332
333     /**
334      * Indicate that a formatter has reported an error so the formatter should output it's
335      * body text.
336      */

337     public void formatterHasError()
338     {
339         _formatErrors = true;
340     }
341
342     /**
343      */

344     private String JavaDoc formatText(Object JavaDoc text)
345             throws JspException JavaDoc
346     {
347         assert(text != null) : "parameter 'text' must not be null";
348
349         if (_formatters == null)
350             return text.toString();
351
352         for (int i = 0; i < _formatters.size(); i++) {
353             FormatTag.Formatter currentFormatter = (FormatTag.Formatter) _formatters.get(i);
354             try {
355                 text = currentFormatter.format(text);
356             }
357             catch (JspException JavaDoc e) {
358                 registerTagError(e.getMessage(), null);
359             }
360         }
361         return text.toString();
362     }
363 }
364
Popular Tags