blob: b38dd3628423d7ebc8a5c0d9e219e88063009b22 (
plain)
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
|
package com.vaadin.tests.components.textfield;
import com.vaadin.event.FieldEvents.TextChangeEvent;
import com.vaadin.event.FieldEvents.TextChangeListener;
import com.vaadin.tests.components.TestBase;
import com.vaadin.tests.util.Log;
import com.vaadin.tests.util.TestUtils;
import com.vaadin.ui.AbstractTextField.TextChangeEventMode;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;
public class TextChangeEvents extends TestBase {
Log l = new Log(10);
@Override
protected void setup() {
TextField tf = new TextField("Default");
TextChangeListener inputEventListener = new TextChangeListener() {
@Override
public void textChange(TextChangeEvent event) {
l.log("Text change event for "
+ event.getComponent().getCaption()
+ ", text content currently:'" + event.getText()
+ "' Cursor at index:" + event.getCursorPosition());
}
};
tf.addListener(inputEventListener);
getLayout().addComponent(tf);
TextField eager = new TextField("Eager");
eager.addListener(inputEventListener);
eager.setTextChangeEventMode(TextChangeEventMode.EAGER);
getLayout().addComponent(eager);
TextField to = new TextField("Timeout 3s");
to.addListener(inputEventListener);
to.setTextChangeEventMode(TextChangeEventMode.TIMEOUT);
to.setTextChangeTimeout(3000);
getLayout().addComponent(to);
TextArea ta = new TextArea("Default text area");
ta.addListener(inputEventListener);
getLayout().addComponent(ta);
VaadinDeveloperNameField vd = new VaadinDeveloperNameField();
vd.addListener(inputEventListener);
getLayout().addComponent(vd);
getLayout().addComponent(l);
}
@Override
protected String getDescription() {
return "Simple TextChangeEvent test cases.";
}
@Override
protected Integer getTicketNumber() {
return null;
}
/**
* "Autosuggest"
*
* Known issue is timing if suggestion comes while typing more content. IMO
* we will not support this kind of features in default TextField, but
* hopefully make it easily extendable to perfect suggest feature. MT
* 2010-10
*
*/
private class VaadinDeveloperNameField extends TextField implements
TextChangeListener {
private String[] names = new String[] { "Matti Tahvonen",
"Marc Englund", "Joonas Lehtinen", "Jouni Koivuviita",
"Marko Grönroos", "Artur Signell" };
public VaadinDeveloperNameField() {
setCaption("Start typing 'old' Vaadin developers.");
addListener((TextChangeListener) this);
setStyleName("nomatch");
}
@Override
public void attach() {
super.attach();
TestUtils.injectCSS(getUI(), ".match { background:green ;} "
+ ".nomatch {background:red;}");
}
@Override
public void textChange(TextChangeEvent event) {
boolean atTheEndOfText = event.getText().length() == getCursorPosition();
String match = findMatch(event.getText());
if (match != null) {
setStyleName("match");
String curText = event.getText();
int matchlenght = curText.length();
// autocomplete if garret is at the end of the text
if (atTheEndOfText) {
suggest(match, matchlenght);
}
} else {
setStyleName("nomatch");
}
}
private void suggest(String match, int matchlenght) {
setValue(match);
setSelectionRange(matchlenght, match.length() - matchlenght);
}
private String findMatch(String currentTextContent) {
if (currentTextContent.length() > 0) {
for (int i = 0; i < names.length; i++) {
if (names[i].startsWith(currentTextContent)) {
return names[i];
}
}
}
return null;
}
}
}
|