KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > widget > TextboxWidget


1 /*
2  * Copyright (c) 1998-2005 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Sam
27  */

28
29 package com.caucho.widget;
30
31 import java.io.IOException;
32
33 public class TextboxWidget
34   extends XulWidget
35 {
36   private static final int NULL_INTEGER = Integer.MIN_VALUE;
37   private static final String NULL_VALUE = "";
38
39   private VarDefinition valueDef = new XulVarDefinition(this, "value", String.class);
40
41   private EditProperty _editProperty = new EditProperty(this);
42   private DisabledProperty _disabledProperty = new DisabledProperty(this);
43   private ReadonlyProperty _readonlyProperty = new ReadonlyProperty(this);
44
45   private boolean _isMultiline;
46
47   private int _tabindex = NULL_INTEGER;
48
49   private int _cols = NULL_INTEGER;
50   private int _rows = NULL_INTEGER;
51
52   private int _size = NULL_INTEGER;
53
54   private int _maxlength = NULL_INTEGER;
55
56   public TextboxWidget()
57   {
58   }
59
60   public TextboxWidget(String id)
61   {
62     setId(id);
63   }
64
65   public TextboxWidget(String id, String initialValue)
66   {
67     setId(id);
68     setValue(initialValue);
69   }
70
71   public void setValue(String value)
72   {
73     valueDef.setValue(value);
74   }
75
76   public String getValue()
77   {
78     return valueDef.getValue();
79   }
80
81   /**
82    * Default false.
83    */

84   public void setMultiline(boolean isMultiline)
85   {
86     _isMultiline = isMultiline;
87   }
88
89   public boolean isMultiline()
90   {
91     return _isMultiline;
92   }
93
94   /**
95    * This value determines the width of the control that is displayed,
96    * the default is to use the value of <i>cols</i>.
97    *
98    * If both <i>cols</i> and <i>size</i> are set, then <i>size</i> is used
99    * for a singleline control, <i>cols</i> is used for a multiline control.
100    *
101    * If no <i>size</i> or <i>cols</i> has been set, the typical result is that
102    * the browser chooses a width.
103    */

104   public void setSize(int size)
105   {
106     _size = size;
107   }
108
109   public int getSize()
110   {
111     return _size;
112   }
113
114   /**
115    * This value determines the width of the control that is displayed,
116    * the default is to use the value of <i>size</i>.
117    *
118    * If both <i>cols</i> and <i>size</i> are set, then <i>size</i> is used
119    * for a singleline control, <i>cols</i> is used for a multiline control.
120    *
121    * If no <i>cols</i> or <i>size</i> has been set, the typical result is that
122    * the browser chooses a width.
123    */

124   public void setCols(int cols)
125   {
126     _cols = cols;
127   }
128
129   public int getCols()
130   {
131     return _cols;
132   }
133
134   /**
135    * This value determines the height of the control that is displayed,
136    * the default is to specify no <i>rows</i>, typically resulting in the browser
137    * choosing a height.
138    *
139    * This value applies to only a multi line textbox, it is ignored for single line.
140    */

141   public void setRows(int rows)
142   {
143     _rows = rows;
144   }
145
146   public int getRows()
147   {
148     return _rows;
149   }
150
151   /**
152    * The maximum number of characters that the textbox allows to be entered.
153    * For HTML, this will typically only have an effect for single-line textboxes.
154    */

155   public void setMaxlength(int maxlength)
156   {
157     _maxlength = maxlength;
158   }
159
160   public int getMaxlength()
161   {
162     return _maxlength;
163   }
164
165   public void setTabindex(int tabindex)
166   {
167     _tabindex = tabindex;
168   }
169
170   public int getTabindex()
171   {
172     return _tabindex;
173   }
174
175   public void setEdit(boolean isEdit)
176   {
177     _editProperty.setValue(isEdit);
178   }
179
180   public boolean isEdit()
181   {
182     return _editProperty.getValue();
183   }
184
185   public void setDisabled(boolean isDisabled)
186   {
187     _disabledProperty.setValue(isDisabled);
188   }
189
190   public boolean isDisabled()
191   {
192     return _disabledProperty.getValue();
193   }
194
195   public void setReadonly(boolean isReadonly)
196   {
197     _readonlyProperty.setValue(isReadonly);
198   }
199
200   public boolean isReadonly()
201   {
202     return _readonlyProperty.getValue();
203   }
204
205   public void init(WidgetInit init)
206     throws WidgetException
207   {
208     super.init(init);
209
210     _editProperty.init(init);
211     _disabledProperty.init(init);
212     _readonlyProperty.init(init);
213
214     if (valueDef.getValue() == null)
215       valueDef.setValue(NULL_VALUE);
216
217     init.addVarDefinition(valueDef);
218   }
219
220   /**
221    * A null value becomes the empty string.
222    */

223   public void setValue(VarContext context, String value)
224   {
225     if (value == null)
226       value = NULL_VALUE;
227
228     context.setVar(this, valueDef, value);
229   }
230
231   /**
232    * A null value is never returned, LabelWidget treats null as an empty
233    * string.
234    */

235   public String getValue(VarContext context)
236   {
237     return context.getVar(this, valueDef);
238   }
239
240   public void setEdit(VarContext context, boolean isEdit)
241   {
242     _editProperty.setValue(context, isEdit);
243   }
244
245   public boolean isEdit(VarContext context)
246   {
247     return _editProperty.getValue(context);
248   }
249
250   public void setDisabled(VarContext context, boolean isDisabled)
251   {
252     _disabledProperty.setValue(context, isDisabled);
253   }
254
255   public boolean isDisabled(VarContext context)
256   {
257     return _disabledProperty.getValue(context);
258   }
259
260   public void setReadonly(VarContext context, boolean isReadonly)
261   {
262     _readonlyProperty.setValue(context, isReadonly);
263   }
264
265   public boolean isReadonly(VarContext context)
266   {
267     return _readonlyProperty.getValue(context);
268   }
269
270   public void response(WidgetResponse response)
271     throws WidgetException, IOException
272   {
273     WidgetWriter out = response.getWriter();
274
275     String id = getId();
276     String cssClass = getCssClass(response);
277
278     String value = getValue(response);
279
280     boolean isEdit = isEdit(response);
281     boolean isReadonly = isReadonly(response);
282     boolean isDisabled = isDisabled(response);
283
284     out.startElement("span");
285     out.writeAttribute("id", id);
286
287     String readonlyAttribute = isReadonly ? "readonly" : null;
288     String disabledAttribute = isDisabled ? "disabled" : null;
289
290     out.writeAttribute("class", cssClass, readonlyAttribute, disabledAttribute);
291
292     if (isEdit) {
293       int tabindex = getTabindex();
294       int cols = getCols();
295       int rows = getRows();
296       int maxlength = getMaxlength();
297       int size = getSize();
298
299       if (isMultiline()) {
300         out.startElement("textarea");
301
302         out.writeAttribute("name", id);
303
304         if (tabindex != NULL_INTEGER)
305           out.writeAttribute("tabindex", getCols());
306
307         if (isReadonly)
308           out.writeAttribute("readonly", true);
309
310         if (isDisabled)
311           out.writeAttribute("disabled", true);
312
313         if (cols != NULL_INTEGER)
314           out.writeAttribute("cols", getCols());
315         else if (size != NULL_INTEGER)
316           out.writeAttribute("cols", size);
317
318         if (rows != NULL_INTEGER)
319           out.writeAttribute("rows", rows);
320
321         if (value != null)
322           out.writeText(value);
323
324         out.endElement("textarea");
325       }
326       else {
327         out.startElement("input");
328         out.writeAttribute("name", id);
329
330         if (tabindex != NULL_INTEGER)
331           out.writeAttribute("tabindex", getCols());
332
333         if (isReadonly)
334           out.writeAttribute("readonly", true);
335
336         if (isDisabled)
337           out.writeAttribute("disabled", true);
338
339         if (maxlength != NULL_INTEGER)
340           out.writeAttribute("maxlength", maxlength);
341
342         if (size != NULL_INTEGER)
343           out.writeAttribute("size", size);
344         else if (cols != NULL_INTEGER)
345           out.writeAttribute("size", cols);
346
347         if (value != null)
348           out.writeAttribute("value", value);
349
350         out.endElement("input");
351       }
352     }
353     else {
354       out.writeText(value);
355     }
356
357     out.endElement("span");
358   }
359 }
Popular Tags