1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
/*
* Copyright 2000-2013 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.client.ui;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.TextBoxBase;
import com.vaadin.client.ApplicationConnection;
import com.vaadin.client.BrowserInfo;
import com.vaadin.client.Util;
import com.vaadin.shared.EventId;
import com.vaadin.shared.ui.textfield.TextFieldConstants;
/**
* This class represents a basic text input field with one row.
*
* @author Vaadin Ltd.
*
*/
public class VTextField extends TextBoxBase implements Field, ChangeHandler,
FocusHandler, BlurHandler, KeyDownHandler {
/**
* The input node CSS classname.
*/
public static final String CLASSNAME = "v-textfield";
/**
* This CSS classname is added to the input node on hover.
*/
public static final String CLASSNAME_FOCUS = "focus";
/** For internal use only. May be removed or replaced in the future. */
public String paintableId;
/** For internal use only. May be removed or replaced in the future. */
public ApplicationConnection client;
/** For internal use only. May be removed or replaced in the future. */
public String valueBeforeEdit = null;
/**
* Set to false if a text change event has been sent since the last value
* change event. This means that {@link #valueBeforeEdit} should not be
* trusted when determining whether a text change even should be sent.
*/
private boolean valueBeforeEditIsSynced = true;
private boolean immediate = false;
private int maxLength = -1;
private static final String CLASSNAME_PROMPT = "prompt";
private static final String TEXTCHANGE_MODE_TIMEOUT = "TIMEOUT";
private String inputPrompt = null;
private boolean prompting = false;
private int lastCursorPos = -1;
public VTextField() {
this(DOM.createInputText());
}
protected VTextField(Element node) {
super(node);
setStyleName(CLASSNAME);
addChangeHandler(this);
if (BrowserInfo.get().isIE()) {
// IE does not send change events when pressing enter in a text
// input so we handle it using a key listener instead
addKeyDownHandler(this);
}
addFocusHandler(this);
addBlurHandler(this);
}
/**
* For internal use only. May be removed or replaced in the future.
* <p>
* TODO When GWT adds ONCUT, add it there and remove workaround. See
* http://code.google.com/p/google-web-toolkit/issues/detail?id=4030
* <p>
* Also note that the cut/paste are not totally crossbrowsers compatible.
* E.g. in Opera mac works via context menu, but on via File->Paste/Cut.
* Opera might need the polling method for 100% working textchanceevents.
* Eager polling for a change is bit dum and heavy operation, so I guess we
* should first try to survive without.
*/
public static final int TEXTCHANGE_EVENTS = Event.ONPASTE | Event.KEYEVENTS
| Event.ONMOUSEUP;
@Override
public void onBrowserEvent(Event event) {
super.onBrowserEvent(event);
if (listenTextChangeEvents
&& (event.getTypeInt() & TEXTCHANGE_EVENTS) == event
.getTypeInt()) {
deferTextChangeEvent();
}
}
/*
* TODO optimize this so that only changes are sent + make the value change
* event just a flag that moves the current text to value
*/
private String lastTextChangeString = null;
private String getLastCommunicatedString() {
return lastTextChangeString;
}
private void communicateTextValueToServer() {
String text = getText();
if (prompting) {
// Input prompt visible, text is actually ""
text = "";
}
if (!text.equals(getLastCommunicatedString())) {
if (valueBeforeEditIsSynced && text.equals(valueBeforeEdit)) {
/*
* Value change for the current text has been enqueued since the
* last text change event was sent, but we can't know that it
* has been sent to the server. Ensure that all pending changes
* are sent now. Sending a value change without a text change
* will simulate a TextChangeEvent on the server.
*/
client.sendPendingVariableChanges();
} else {
// Default case - just send an immediate text change message
client.updateVariable(paintableId,
TextFieldConstants.VAR_CUR_TEXT, text, true);
// Shouldn't investigate valueBeforeEdit to avoid duplicate text
// change events as the states are not in sync any more
valueBeforeEditIsSynced = false;
}
lastTextChangeString = text;
}
}
private Timer textChangeEventTrigger = new Timer() {
@Override
public void run() {
if (isAttached()) {
updateCursorPosition();
communicateTextValueToServer();
scheduled = false;
}
}
};
private boolean scheduled = false;
/** For internal use only. May be removed or replaced in the future. */
public boolean listenTextChangeEvents;
/** For internal use only. May be removed or replaced in the future. */
public String textChangeEventMode;
public int textChangeEventTimeout;
private void deferTextChangeEvent() {
if (textChangeEventMode.equals(TEXTCHANGE_MODE_TIMEOUT) && scheduled) {
return;
} else {
textChangeEventTrigger.cancel();
}
textChangeEventTrigger.schedule(getTextChangeEventTimeout());
scheduled = true;
}
private int getTextChangeEventTimeout() {
return textChangeEventTimeout;
}
@Override
public void setReadOnly(boolean readOnly) {
boolean wasReadOnly = isReadOnly();
if (readOnly) {
setTabIndex(-1);
} else if (wasReadOnly && !readOnly && getTabIndex() == -1) {
/*
* Need to manually set tab index to 0 since server will not send
* the tab index if it is 0.
*/
setTabIndex(0);
}
super.setReadOnly(readOnly);
}
/** For internal use only. May be removed or replaced in the future. */
public void updateFieldContent(final String text) {
setPrompting(inputPrompt != null && focusedTextField != this
&& (text.equals("")));
String fieldValue;
if (prompting) {
fieldValue = isReadOnly() ? "" : inputPrompt;
addStyleDependentName(CLASSNAME_PROMPT);
} else {
fieldValue = text;
removeStyleDependentName(CLASSNAME_PROMPT);
}
setText(fieldValue);
lastTextChangeString = valueBeforeEdit = text;
valueBeforeEditIsSynced = true;
}
protected void onCut() {
if (listenTextChangeEvents) {
deferTextChangeEvent();
}
}
/** For internal use only. May be removed or replaced in the future. */
public native void attachCutEventListener(Element el)
/*-{
var me = this;
el.oncut = $entry(function() {
me.@com.vaadin.client.ui.VTextField::onCut()();
});
}-*/;
protected native void detachCutEventListener(Element el)
/*-{
el.oncut = null;
}-*/;
@Override
protected void onDetach() {
super.onDetach();
detachCutEventListener(getElement());
if (focusedTextField == this) {
focusedTextField = null;
}
}
@Override
protected void onAttach() {
super.onAttach();
if (listenTextChangeEvents) {
detachCutEventListener(getElement());
}
}
/** For internal use only. May be removed or replaced in the future. */
public void setMaxLength(int newMaxLength) {
if (newMaxLength == maxLength) {
return;
}
maxLength = newMaxLength;
updateMaxLength(maxLength);
}
/**
* This method is responsible for updating the DOM or otherwise ensuring
* that the given max length is enforced. Called when the max length for the
* field has changed.
*
* @param maxLength
* The new max length
*/
protected void updateMaxLength(int maxLength) {
if (maxLength >= 0) {
getElement().setPropertyInt("maxLength", maxLength);
} else {
getElement().removeAttribute("maxLength");
}
setMaxLengthToElement(maxLength);
}
protected void setMaxLengthToElement(int newMaxLength) {
if (newMaxLength >= 0) {
getElement().setPropertyInt("maxLength", newMaxLength);
} else {
getElement().removeAttribute("maxLength");
}
}
public int getMaxLength() {
return maxLength;
}
@Override
public void onChange(ChangeEvent event) {
valueChange(false);
}
/**
* Called when the field value might have changed and/or the field was
* blurred. These are combined so the blur event is sent in the same batch
* as a possible value change event (these are often connected).
*
* @param blurred
* true if the field was blurred
*/
public void valueChange(boolean blurred) {
if (client != null && paintableId != null) {
boolean sendBlurEvent = false;
boolean sendValueChange = false;
if (blurred && client.hasEventListeners(this, EventId.BLUR)) {
sendBlurEvent = true;
client.updateVariable(paintableId, EventId.BLUR, "", false);
}
String newText = getText();
if (!prompting && newText != null
&& !newText.equals(valueBeforeEdit)) {
sendValueChange = immediate;
client.updateVariable(paintableId, "text", newText, false);
valueBeforeEdit = newText;
valueBeforeEditIsSynced = true;
}
/*
* also send cursor position, no public api yet but for easier
* extension
*/
updateCursorPosition();
if (sendBlurEvent || sendValueChange) {
/*
* Avoid sending text change event as we will simulate it on the
* server side before value change events.
*/
textChangeEventTrigger.cancel();
scheduled = false;
client.sendPendingVariableChanges();
}
}
}
/**
* Updates the cursor position variable if it has changed since the last
* update.
*
* @return true iff the value was updated
*/
protected boolean updateCursorPosition() {
if (Util.isAttachedAndDisplayed(this)) {
int cursorPos = getCursorPos();
if (lastCursorPos != cursorPos) {
client.updateVariable(paintableId,
TextFieldConstants.VAR_CURSOR, cursorPos, false);
lastCursorPos = cursorPos;
return true;
}
}
return false;
}
private static VTextField focusedTextField;
public static void flushChangesFromFocusedTextField() {
if (focusedTextField != null) {
focusedTextField.onChange(null);
}
}
@Override
public void onFocus(FocusEvent event) {
addStyleDependentName(CLASSNAME_FOCUS);
if (prompting) {
setText("");
removeStyleDependentName(CLASSNAME_PROMPT);
setPrompting(false);
}
focusedTextField = this;
if (client != null && client.hasEventListeners(this, EventId.FOCUS)) {
client.updateVariable(paintableId, EventId.FOCUS, "", true);
}
}
@Override
public void onBlur(BlurEvent event) {
// this is called twice on Chrome when e.g. changing tab while prompting
// field focused - do not change settings on the second time
if (focusedTextField != this) {
return;
}
removeStyleDependentName(CLASSNAME_FOCUS);
focusedTextField = null;
String text = getText();
setPrompting(inputPrompt != null && (text == null || "".equals(text)));
if (prompting) {
setText(isReadOnly() ? "" : inputPrompt);
addStyleDependentName(CLASSNAME_PROMPT);
}
valueChange(true);
}
private void setPrompting(boolean prompting) {
this.prompting = prompting;
}
public void setColumns(int columns) {
if (columns <= 0) {
return;
}
setWidth(columns + "em");
}
@Override
public void onKeyDown(KeyDownEvent event) {
if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
valueChange(false);
}
}
public void setImmediate(boolean immediate) {
this.immediate = immediate;
}
public void setInputPrompt(String inputPrompt) {
this.inputPrompt = inputPrompt;
}
}
|