blob: 363e9d448da5ba8100906e987180461cb0e4c202 (
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
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
|
package com.vaadin.tests.components.textfield;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.event.FieldEvents.BlurEvent;
import com.vaadin.event.FieldEvents.BlurListener;
import com.vaadin.event.FieldEvents.FocusEvent;
import com.vaadin.event.FieldEvents.FocusListener;
import com.vaadin.event.FieldEvents.TextChangeEvent;
import com.vaadin.event.FieldEvents.TextChangeListener;
import com.vaadin.tests.components.TestBase;
import com.vaadin.ui.AbstractTextField.TextChangeEventMode;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
public class TextChangeEvents2 extends TestBase {
@Override
protected void setup() {
{
final TextField tf = new TextField("Debug");
getLayout().addComponent(tf);
tf.addListener(new TextChangeListener() {
@Override
public void textChange(TextChangeEvent event) {
System.err.println(tf.getCaption() + " textChange");
}
});
tf.addListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
System.err.println(tf.getCaption() + " valueChange");
}
});
tf.addListener(new BlurListener() {
@Override
public void blur(BlurEvent event) {
System.err.println(tf.getCaption() + " blur");
}
});
tf.addListener(new FocusListener() {
@Override
public void focus(FocusEvent event) {
System.err.println(tf.getCaption() + " focus");
}
});
}
{
final TextField tf = new TextField("Label");
getLayout().addComponent(tf);
final Label l = new Label();
getLayout().addComponent(l);
tf.addListener(new TextChangeListener() {
@Override
public void textChange(TextChangeEvent event) {
l.setValue(event.getText());
}
});
tf.addListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
System.err.println(tf.getCaption() + " valueChange");
}
});
}
{
final TextField tf = new TextField("Slow label");
tf.setTextChangeTimeout(2000);
tf.setImmediate(true);
getLayout().addComponent(tf);
final Label l = new Label();
getLayout().addComponent(l);
tf.addListener(new TextChangeListener() {
@Override
public void textChange(TextChangeEvent event) {
l.setValue(event.getText());
}
});
tf.addListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
System.err.println(tf.getCaption() + " valueChange");
}
});
}
{
final TextField tf = new TextField("Uppercase");
tf.setTextChangeTimeout(1);
getLayout().addComponent(tf);
final Label l = new Label();
getLayout().addComponent(l);
tf.addListener(new TextChangeListener() {
@Override
public void textChange(TextChangeEvent event) {
tf.setValue(event.getText().toUpperCase());
}
});
tf.addListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
System.err.println(tf.getCaption() + " valueChange");
}
});
}
{
final TextField[] tfs = new TextField[] { new TextField(),
new TextField(), new TextField(), new TextField() };
HorizontalLayout hl = new HorizontalLayout();
hl.setCaption("Blää");
getLayout().addComponent(hl);
for (TextField tf : tfs) {
tf.setColumns(4);
tf.setTextChangeEventMode(TextChangeEventMode.EAGER);
hl.addComponent(tf);
tf.addListener(new TextChangeListener() {
@Override
public void textChange(TextChangeEvent event) {
String txt = event.getText();
int len = txt.length();
if (len >= 4) {
int idx = 0;
while (tfs[idx] != event.getComponent()) {
idx++;
}
tfs[idx].setValue("");
tfs[idx].setValue(txt.substring(0, 4));
if (idx < tfs.length - 1) {
TextField next = tfs[idx + 1];
next.focus();
if (len > 4) {
next.setValue(txt.substring(4, len > 8 ? 8
: len));
} else {
next.selectAll();
}
}
}
}
});
tf.addListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
TextField tf = (TextField) event.getProperty();
String val = (String) tf.getValue();
if (val != null && val.length() > 4) {
tf.setValue(val.substring(0, 4));
}
}
});
}
}
}
@Override
protected String getDescription() {
return "Another set of simple use case/tests for TextChangeEvents";
}
@Override
protected Integer getTicketNumber() {
return 2387;
}
}
|