1 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 27 public class TextBoxBase extends FocusWidget implements SourcesKeyboardEvents, 28 SourcesChangeEvents, SourcesClickEvents, HasText, HasName { 29 30 34 public static class TextAlignConstant { 35 private String textAlignString; 36 37 private TextAlignConstant(String textAlignString) { 38 this.textAlignString = textAlignString; 39 } 40 41 private String getTextAlignString() { 42 return textAlignString; 43 } 44 } 45 46 49 public static final TextAlignConstant ALIGN_CENTER = new TextAlignConstant( 50 "center"); 51 52 55 public static final TextAlignConstant ALIGN_JUSTIFY = new TextAlignConstant( 56 "justify"); 57 58 61 public static final TextAlignConstant ALIGN_LEFT = new TextAlignConstant( 62 "left"); 63 64 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 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 114 public void cancelKey() { 115 if (currentEvent != null) { 116 DOM.eventPreventDefault(currentEvent); 117 } 118 } 119 120 126 public int getCursorPos() { 127 return impl.getCursorPos(getElement()); 128 } 129 130 public String getName() { 131 return DOM.getElementProperty(getElement(), "name"); 132 } 133 134 139 public String getSelectedText() { 140 int start = getCursorPos(), length = getSelectionLength(); 141 return getText().substring(start, start + length); 142 } 143 144 149 public int getSelectionLength() { 150 return impl.getSelectionLength(getElement()); 151 } 152 153 public String getText() { 154 return DOM.getElementProperty(getElement(), "value"); 155 } 156 157 163 public boolean isReadOnly() { 164 return DOM.getElementPropertyBoolean(getElement(), "readOnly"); 165 } 166 167 public void onBrowserEvent(Event event) { 168 super.onBrowserEvent(event); 171 172 int type = DOM.eventGetType(event); 173 if ((keyboardListeners != null) && (type & Event.KEYEVENTS) != 0) { 174 currentEvent = event; 177 keyboardListeners.fireKeyboardEvent(this, event); 178 currentEvent = null; 179 } else if (type == Event.ONCLICK) { 180 if (clickListeners != null) { 182 clickListeners.fireClick(this); 183 } 184 } else if (type == Event.ONCHANGE) { 185 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 213 public void selectAll() { 214 int length = getText().length(); 215 if (length > 0) { 216 setSelectionRange(0, length); 217 } 218 } 219 220 225 public void setCursorPos(int pos) { 226 setSelectionRange(pos, 0); 227 } 228 229 236 public void setKey(char key) { 237 if (currentEvent != null) { 238 DOM.eventSetKeyCode(currentEvent, key); 239 } 240 } 241 242 public void setName(String name) { 243 DOM.setElementProperty(getElement(), "name", name); 244 } 245 246 252 public void setReadOnly(boolean readOnly) { 253 DOM.setElementPropertyBoolean(getElement(), "readOnly", readOnly); 254 String readOnlyStyle = getStyleName() + "-readonly"; 255 if (readOnly) { 256 addStyleName(readOnlyStyle); 257 } else { 258 removeStyleName(readOnlyStyle); 259 } 260 } 261 262 268 public void setSelectionRange(int pos, int length) { 269 if (length < 0) { 270 throw new IndexOutOfBoundsException ( 271 "Length must be a positive integer. Length: " + length); 272 } 273 if ((pos < 0) || (length + pos > getText().length())) { 274 throw new IndexOutOfBoundsException ("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 text) { 281 DOM.setElementProperty(getElement(), "value", text != null ? text : ""); 282 } 283 284 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 |