KickJava   Java API By Example, From Geeks To Geeks.

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


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.ByRef;
21 import org.apache.beehive.netui.tags.HtmlUtils;
22 import org.apache.beehive.netui.tags.rendering.AbstractHtmlState;
23 import org.apache.beehive.netui.tags.rendering.TagRenderingBase;
24 import org.apache.beehive.netui.tags.rendering.TextAreaTag;
25 import org.apache.beehive.netui.tags.rendering.WriteRenderAppender;
26
27 import javax.servlet.ServletRequest JavaDoc;
28 import javax.servlet.jsp.JspException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30
31 /**
32  * Renders a databound TextArea with the given attributes.
33  * @jsptagref.tagdescription Renders an HTML <textarea> tag.
34  *
35  * <pre> &lt;textarea/></pre>
36  * @example In this sample a text area reads from and writes to the Form Bean's
37  * description property. If the submitted value is NULL the default value is specified
38  * by the Page Flow's
39  * defaultDescription property.
40  * <pre> &lt;netui:textArea dataSource="actionForm.description"
41  * defaultValue="${pageFlow.defaultDescription}"
42  * cols="25" rows="3" /></pre>
43  * @netui:tag name="textArea" description="Renders a databound TextArea with the given attributes."
44  */

45 public class TextArea extends HtmlDefaultableDataSourceTag
46         implements IFormattable
47 {
48     private TextAreaTag.State _state = new TextAreaTag.State();
49
50     private ArrayList JavaDoc _formatters = null;
51     private boolean _formatErrors = false;
52
53     /**
54      * Return the name of the Tag.
55      */

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

66     protected AbstractHtmlState getState()
67     {
68         return _state;
69     }
70
71     /**
72      * Base support for the attribute tag.
73      * @param name The name of the attribute. This value may not be null or the empty string.
74      * @param value The value of the attribute. This may contain an expression.
75      * @param facet The name of a facet to which the attribute will be applied. This is optional.
76      * @throws JspException A JspException may be thrown if there is an error setting 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(DISABLED)) {
83                 _state.disabled = new Boolean JavaDoc(value).booleanValue();
84                 return;
85             }
86             else if (name.equals(READONLY)) {
87                 _state.readonly = new Boolean JavaDoc(value).booleanValue();
88                 return;
89             }
90             else if (name.equals(COLS)) {
91                 _state.cols = Integer.parseInt(value);
92                 return;
93             }
94             else if (name.equals(ROWS)) {
95                 _state.rows = Integer.parseInt(value);
96                 return;
97             }
98         }
99
100         super.setAttribute(name, value, facet);
101     }
102
103     /**
104      * Sets the number of columns in the TextArea.
105      * @param cols the number of columns
106      * @jsptagref.attributedescription Integer. The number of columns in the &lt;netui:textArea>.
107      * @jsptagref.databindable Read Only
108      * @jsptagref.attributesyntaxvalue <i>integer_columns</i>
109      * @netui:attribute required="false" rtexprvalue="true" type="int"
110      * description="The number of columns in the <netui:textArea>."
111      */

112     public void setCols(int cols)
113     {
114         _state.cols = cols;
115     }
116
117     /**
118      * Set if this TextArea is read-only.
119      * @param readonly the read-only state
120      * @jsptagref.attributedescription Boolean. Determines whether text can be entered in the &lt;netui:textArea> or not.
121      * @jsptagref.databindable false
122      * @jsptagref.attributesyntaxvalue <i>boolean_disabled</i>
123      * @netui:attribute required="false" rtexprvalue="true" type="boolean"
124      * description="Determines whether text can be entered in the <netui:textArea> or not."
125      */

126     public void setReadonly(boolean readonly)
127     {
128         _state.readonly = readonly;
129     }
130
131     /**
132      * Sets the number of rows in the TextArea.
133      * @param rows the number of rows
134      * @jsptagref.attributedescription The number of rows in the &lt;netui:textArea>
135      * @jsptagref.databindable Read Only
136      * @jsptagref.attributesyntaxvalue <i>integer_rows</i>
137      * @netui:attribute required="false" rtexprvalue="true" type="int"
138      * description="The number of rows in the <netui:textArea>"
139      */

140     public void setRows(int rows)
141     {
142         _state.rows = rows;
143     }
144
145     /**
146      * Prepare the TextArea's formatters.
147      * @throws JspException if a JSP exception has occurred
148      */

149     public int doStartTag() throws JspException JavaDoc
150     {
151         return EVAL_BODY_BUFFERED;
152     }
153
154     /**
155      * Render the TextArea.
156      * @throws JspException if a JSP exception has occurred
157      */

158     public int doEndTag() throws JspException JavaDoc
159     {
160         ServletRequest JavaDoc req = pageContext.getRequest();
161         Object JavaDoc textObject = null;
162
163         // Get the value of the data source. The object will not be null.
164
Object JavaDoc val = evaluateDataSource();
165         textObject = (val != null) ? val : "";
166         assert(textObject != null);
167
168         // setup the rest of the state.
169
ByRef ref = new ByRef();
170         nameHtmlControl(_state, ref);
171
172         _state.disabled = isDisabled();
173
174         // if there were expression errors report them
175
if (hasErrors())
176             return reportAndExit(EVAL_PAGE);
177
178         // if there were format errors then report them
179
if (_formatErrors) {
180             if (bodyContent != null) {
181                 String JavaDoc value = bodyContent.getString().trim();
182                 bodyContent.clearBody();
183                 write(value);
184             }
185         }
186
187         // create the input tag.
188
WriteRenderAppender writer = new WriteRenderAppender(pageContext);
189         TagRenderingBase br = TagRenderingBase.Factory.getRendering(TagRenderingBase.TEXT_AREA_TAG, req);
190         br.doStartTag(writer, _state);
191
192         // create the text value which will be found inside the textarea.
193
if ((textObject == null) || (textObject.toString().length() == 0)) {
194             textObject = _defaultValue;
195         }
196
197         String JavaDoc text = formatText(textObject);
198         if (text != null)
199             HtmlUtils.filter(text, writer);
200
201         //results.append(text);
202
br.doEndTag(writer);
203
204         // if there are errors report them...
205
if (hasErrors())
206             return reportAndExit(EVAL_PAGE);
207
208         // report any script
209
if (!ref.isNull())
210             write((String JavaDoc) ref.getRef());
211
212         localRelease();
213         return EVAL_PAGE;
214     }
215
216     /**
217      * Release any acquired resources.
218      */

219     protected void localRelease()
220     {
221         super.localRelease();
222
223         _state.clear();
224         _formatters = null;
225         _formatErrors = false;
226     }
227
228     /**
229      * Adds a FormatTag.Formatter to the TextArea's set of formatters
230      * @param formatter a FormatTag.Formatter added by a child FormatTag.
231      */

232     public void addFormatter(FormatTag.Formatter formatter)
233     {
234         if (_formatters == null)
235             _formatters = new ArrayList JavaDoc();
236
237         _formatters.add(formatter);
238     }
239
240     /**
241      * Indicate that a formatter has reported an error so the formatter should output it's
242      * body text.
243      */

244     public void formatterHasError()
245     {
246         _formatErrors = true;
247     }
248
249     /**
250      */

251     private String JavaDoc formatText(Object JavaDoc text)
252             throws JspException JavaDoc
253     {
254         if (text == null)
255             return null;
256         if (_formatters == null)
257             return text.toString();
258
259         for (int i = 0; i < _formatters.size(); i++) {
260             FormatTag.Formatter currentFormatter = (FormatTag.Formatter) _formatters.get(i);
261             try {
262                 text = currentFormatter.format(text);
263             }
264             catch (JspException JavaDoc e) {
265                 registerTagError(e.getMessage(), null);
266             }
267         }
268         return text.toString();
269     }
270
271     /* ==================================================================
272      *
273      * This tag's publically exposed HTML, CSS, and JavaScript attributes
274      *
275      * ==================================================================
276      */

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

296     public void setAccessKey(char accessKey)
297     {
298         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, "accesskey", Character.toString(accessKey));
299     }
300
301     /**
302      * Sets the tabIndex of the rendered html tag.
303      * @param tabindex the tab index.
304      * @jsptagref.attributedescription The tabIndex of the rendered HTML tag. This attribute determines the position of the
305      * tag in the sequence of page elements that the user may advance through by pressing the TAB key.
306      * @jsptagref.databindable false
307      * @jsptagref.attributesyntaxvalue <i>string_tabIndex</i>
308      * @netui:attribute required="false" rtexprvalue="true" type="int"
309      * description="The tabIndex of the rendered HTML tag. This attribute determines the position of the
310      * tag in the sequence of page elements that the user may advance through by pressing the TAB key."
311      */

312     public void setTabindex(int tabindex)
313     {
314         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, TABINDEX, Integer.toString(tabindex));
315     }
316 }
317
Popular Tags