KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > client > ui > TextBoxBase


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.user.client.ui;
17
18 import com.google.gwt.core.client.GWT;
19 import com.google.gwt.user.client.DOM;
20 import com.google.gwt.user.client.Element;
21 import com.google.gwt.user.client.Event;
22 import com.google.gwt.user.client.ui.impl.TextBoxImpl;
23
24 /**
25  * Abstract base class for all text entry widgets.
26  */

27 public class TextBoxBase extends FocusWidget implements SourcesKeyboardEvents,
28     SourcesChangeEvents, SourcesClickEvents, HasText, HasName {
29
30   /**
31    * Text alignment constant, used in
32    * {@link TextBoxBase#setTextAlignment(TextBoxBase.TextAlignConstant)}.
33    */

34   public static class TextAlignConstant {
35     private String JavaDoc textAlignString;
36
37     private TextAlignConstant(String JavaDoc textAlignString) {
38       this.textAlignString = textAlignString;
39     }
40
41     private String JavaDoc getTextAlignString() {
42       return textAlignString;
43     }
44   }
45
46   /**
47    * Center the text.
48    */

49   public static final TextAlignConstant ALIGN_CENTER = new TextAlignConstant(
50       "center");
51
52   /**
53    * Justify the text.
54    */

55   public static final TextAlignConstant ALIGN_JUSTIFY = new TextAlignConstant(
56       "justify");
57
58   /**
59    * Align the text to the left edge.
60    */

61   public static final TextAlignConstant ALIGN_LEFT = new TextAlignConstant(
62       "left");
63
64   /**
65    * Align the text to the right.
66    */

67   public static final TextAlignConstant ALIGN_RIGHT = new TextAlignConstant(
68       "right");
69
70   private static TextBoxImpl impl = (TextBoxImpl) GWT.create(TextBoxImpl.class);
71
72   private ChangeListenerCollection changeListeners;
73   private ClickListenerCollection clickListeners;
74   private Event currentEvent;
75   private KeyboardListenerCollection keyboardListeners;
76
77   /**
78    * Creates a text box that wraps the given browser element handle. This is
79    * only used by subclasses.
80    *
81    * @param elem the browser element to wrap
82    */

83   protected TextBoxBase(Element elem) {
84     super(elem);
85     sinkEvents(Event.ONCHANGE);
86   }
87
88   public void addChangeListener(ChangeListener listener) {
89     if (changeListeners == null) {
90       changeListeners = new ChangeListenerCollection();
91     }
92     changeListeners.add(listener);
93   }
94
95   public void addClickListener(ClickListener listener) {
96     if (clickListeners == null) {
97       clickListeners = new ClickListenerCollection();
98     }
99     clickListeners.add(listener);
100   }
101
102   public void addKeyboardListener(KeyboardListener listener) {
103     if (keyboardListeners == null) {
104       keyboardListeners = new KeyboardListenerCollection();
105     }
106     keyboardListeners.add(listener);
107   }
108
109   /**
110    * If a keyboard event is currently being handled on this text box, calling
111    * this method will suppress it. This allows listeners to easily filter
112    * keyboard input.
113    */

114   public void cancelKey() {
115     if (currentEvent != null) {
116       DOM.eventPreventDefault(currentEvent);
117     }
118   }
119
120   /**
121    * Gets the current position of the cursor (this also serves as the beginning
122    * of the text selection).
123    *
124    * @return the cursor's position
125    */

126   public int getCursorPos() {
127     return impl.getCursorPos(getElement());
128   }
129
130   public String JavaDoc getName() {
131     return DOM.getElementProperty(getElement(), "name");
132   }
133
134   /**
135    * Gets the text currently selected within this text box.
136    *
137    * @return the selected text, or an empty string if none is selected
138    */

139   public String JavaDoc getSelectedText() {
140     int start = getCursorPos(), length = getSelectionLength();
141     return getText().substring(start, start + length);
142   }
143
144   /**
145    * Gets the length of the current text selection.
146    *
147    * @return the text selection length
148    */

149   public int getSelectionLength() {
150     return impl.getSelectionLength(getElement());
151   }
152
153   public String JavaDoc getText() {
154     return DOM.getElementProperty(getElement(), "value");
155   }
156
157   /**
158    * Determines whether or not the widget is read-only.
159    *
160    * @return <code>true</code> if the widget is currently read-only,
161    * <code>false</code> if the widget is currently editable
162    */

163   public boolean isReadOnly() {
164     return DOM.getElementPropertyBoolean(getElement(), "readOnly");
165   }
166
167   public void onBrowserEvent(Event event) {
168     // Call the superclass' implementation first (because FocusWidget fires
169
// some events itself).
170
super.onBrowserEvent(event);
171
172     int type = DOM.eventGetType(event);
173     if ((keyboardListeners != null) && (type & Event.KEYEVENTS) != 0) {
174       // Fire the keyboard event. Hang on to the current event object so that
175
// cancelKey() and setKey() can be implemented.
176
currentEvent = event;
177       keyboardListeners.fireKeyboardEvent(this, event);
178       currentEvent = null;
179     } else if (type == Event.ONCLICK) {
180       // Fire the click event.
181
if (clickListeners != null) {
182         clickListeners.fireClick(this);
183       }
184     } else if (type == Event.ONCHANGE) {
185       // Fire the change event.
186
if (changeListeners != null) {
187         changeListeners.fireChange(this);
188       }
189     }
190   }
191
192   public void removeChangeListener(ChangeListener listener) {
193     if (changeListeners != null) {
194       changeListeners.remove(listener);
195     }
196   }
197
198   public void removeClickListener(ClickListener listener) {
199     if (clickListeners != null) {
200       clickListeners.remove(listener);
201     }
202   }
203
204   public void removeKeyboardListener(KeyboardListener listener) {
205     if (keyboardListeners != null) {
206       keyboardListeners.remove(listener);
207     }
208   }
209
210   /**
211    * Selects all of the text in the box.
212    */

213   public void selectAll() {
214     int length = getText().length();
215     if (length > 0) {
216       setSelectionRange(0, length);
217     }
218   }
219
220   /**
221    * Sets the cursor position.
222    *
223    * @param pos the new cursor position
224    */

225   public void setCursorPos(int pos) {
226     setSelectionRange(pos, 0);
227   }
228
229   /**
230    * If a keyboard event is currently being handled by the text box, this method
231    * replaces the unicode character or key code associated with it. This allows
232    * listeners to easily filter keyboard input.
233    *
234    * @param key the new key value
235    */

236   public void setKey(char key) {
237     if (currentEvent != null) {
238       DOM.eventSetKeyCode(currentEvent, key);
239     }
240   }
241
242   public void setName(String JavaDoc name) {
243     DOM.setElementProperty(getElement(), "name", name);
244   }
245
246   /**
247    * Turns read-only mode on or off.
248    *
249    * @param readOnly if <code>true</code>, the widget becomes read-only; if
250    * <code>false</code> the widget becomes editable
251    */

252   public void setReadOnly(boolean readOnly) {
253     DOM.setElementPropertyBoolean(getElement(), "readOnly", readOnly);
254     String JavaDoc readOnlyStyle = getStyleName() + "-readonly";
255     if (readOnly) {
256       addStyleName(readOnlyStyle);
257     } else {
258       removeStyleName(readOnlyStyle);
259     }
260   }
261
262   /**
263    * Sets the range of text to be selected.
264    *
265    * @param pos the position of the first character to be selected
266    * @param length the number of characters to be selected
267    */

268   public void setSelectionRange(int pos, int length) {
269     if (length < 0) {
270       throw new IndexOutOfBoundsException JavaDoc(
271           "Length must be a positive integer. Length: " + length);
272     }
273     if ((pos < 0) || (length + pos > getText().length())) {
274       throw new IndexOutOfBoundsException JavaDoc("From Index: " + pos + " To Index: "
275           + (pos + length) + " Text Length: " + getText().length());
276     }
277     impl.setSelectionRange(getElement(), pos, length);
278   }
279
280   public void setText(String JavaDoc text) {
281     DOM.setElementProperty(getElement(), "value", text != null ? text : "");
282   }
283
284   /**
285    * Sets the alignment of the text in the text box.
286    *
287    * @param align the text alignment (as specified by {@link #ALIGN_CENTER},
288    * {@link #ALIGN_JUSTIFY}, {@link #ALIGN_LEFT}, and
289    * {@link #ALIGN_RIGHT})
290    */

291   public void setTextAlignment(TextAlignConstant align) {
292     DOM.setStyleAttribute(getElement(), "textAlign", align.getTextAlignString());
293   }
294
295   protected TextBoxImpl getImpl() {
296     return impl;
297   }
298 }
299
Popular Tags