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
|
package com.vaadin.tests.components.table;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Date;
import java.util.Locale;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.data.util.converter.Converter;
import com.vaadin.data.util.converter.StringToDoubleConverter;
import com.vaadin.tests.components.TestBase;
import com.vaadin.tests.data.bean.Address;
import com.vaadin.tests.data.bean.Country;
import com.vaadin.tests.data.bean.Person;
import com.vaadin.tests.data.bean.Sex;
import com.vaadin.tests.util.Log;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Table;
public class DoublesInTable extends TestBase {
BeanItemContainer<Person> personBeanItemContainer = new BeanItemContainer<Person>(
Person.class);
private Table table;
private Log log = new Log(5);
private ComboBox localeSelect;
private CheckBox useCustomConverters;
private CheckBox editMode;
@Override
protected void setup() {
editMode = new CheckBox("Edit mode");
editMode.setImmediate(true);
editMode.addListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
table.setEditable(editMode.getValue());
}
});
useCustomConverters = new CheckBox("Use custom converters");
useCustomConverters.setImmediate(true);
useCustomConverters.addListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
recreateTable();
}
});
localeSelect = createLocaleSelect();
personBeanItemContainer = createContainer(100);
addComponent(log);
addComponent(localeSelect);
addComponent(useCustomConverters);
addComponent(editMode);
recreateTable();
}
private ComboBox createLocaleSelect() {
ComboBox cb = new ComboBox();
cb.setNullSelectionAllowed(false);
for (Locale l : Locale.getAvailableLocales()) {
cb.addItem(l);
}
cb.setImmediate(true);
cb.setValue(Locale.US);
cb.addListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
recreateTable();
}
});
return cb;
}
protected void recreateTable() {
Table newTable = createTable(useCustomConverters.getValue(),
(Locale) localeSelect.getValue());
newTable.setEditable(editMode.getValue());
if (table == null) {
addComponent(newTable);
} else {
replaceComponent(table, newTable);
}
table = newTable;
}
private static BeanItemContainer<Person> createContainer(int nr) {
BeanItemContainer<Person> bic = new BeanItemContainer<Person>(
Person.class);
for (int i = 1; i <= nr; i++) {
Person p = new Person();
p.setFirstName("First " + i);
p.setLastName("Last " + i);
p.setAge(i);
p.setDeceased((i % 5 - 2) == 0);
p.setEmail("person" + i + "@mail.com");
p.setRent(new BigDecimal(i * 1250.25));
p.setSalary(3000 + i);
p.setSex((i % 4) == 0 ? Sex.MALE : Sex.FEMALE);
p.setBirthDate(new Date(2011 - 1900 - p.getAge(), 11 - 1, 24));
if (i % 42 == 0) {
p.setSex(Sex.UNKNOWN);
}
String city = "City " + (i / 10);
Country country = Country.FINLAND;
Address address = new Address("Street " + i, 12345 + i * 2, city,
country);
p.setAddress(address);
bic.addBean(p);
}
return bic;
}
protected Table createTable(boolean useCustomConverters, Locale locale) {
Table t = new Table("Persons");
t.setLocale(locale);
t.setContainerDataSource(personBeanItemContainer);
t.setSortDisabled(false);
if (useCustomConverters) {
addConverters(t);
}
t.setSelectable(true);
t.setImmediate(true);
t.addListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
log.log("Value is now: " + event.getProperty().getValue());
}
});
return t;
}
private void addConverters(Table t) {
t.setConverter("sex", new Converter<String, Sex>() {
@Override
public Sex convertToModel(String value,
Class<? extends Sex> targetType, Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException {
// not used in this test - Table only converts to presentation
return null;
}
@Override
public String convertToPresentation(Sex value,
Class<? extends String> targetType, Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException {
if (value == null) {
value = Sex.UNKNOWN;
}
return value.getStringRepresentation();
}
@Override
public Class<Sex> getModelType() {
return Sex.class;
}
@Override
public Class<String> getPresentationType() {
return String.class;
}
});
t.setConverter("deceased", new Converter<String, Boolean>() {
@Override
public Boolean convertToModel(String value,
Class<? extends Boolean> targetType, Locale locale) {
// not used in this test - Table only converts from source to
// target
return null;
}
@Override
public String convertToPresentation(Boolean value,
Class<? extends String> targetType, Locale locale) {
if (value == null || value) {
return "YES, DEAD!";
} else {
return "-";
}
}
@Override
public Class<Boolean> getModelType() {
return Boolean.class;
}
@Override
public Class<String> getPresentationType() {
return String.class;
}
});
t.setConverter("age", new Converter<String, Integer>() {
@Override
public Integer convertToModel(String value,
Class<? extends Integer> targetType, Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException {
// not used in this test - Table only converts from source to
// target
return null;
}
@Override
public String convertToPresentation(Integer value,
Class<? extends String> targetType, Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException {
if (value == null) {
return null;
}
if (value < 3) {
return value + " (baby)";
} else if (value < 7) {
return value + " (kid)";
} else if (value < 18) {
return value + " (young)";
} else {
return value + "";
}
}
@Override
public Class<Integer> getModelType() {
return Integer.class;
}
@Override
public Class<String> getPresentationType() {
return String.class;
}
});
t.setConverter("address", new Converter<String, Address>() {
@Override
public Address convertToModel(String value,
Class<? extends Address> targetType, Locale locale)
throws ConversionException {
// not used in this test - Table only converts to presentation
return null;
}
@Override
public String convertToPresentation(Address value,
Class<? extends String> targetType, Locale locale)
throws ConversionException {
return value.getStreetAddress() + ", " + value.getCity() + " ("
+ value.getCountry() + ")";
}
@Override
public Class<Address> getModelType() {
return Address.class;
}
@Override
public Class<String> getPresentationType() {
return String.class;
}
});
t.setConverter("rent", new StringToDoubleConverter() {
@Override
protected NumberFormat getFormat(Locale locale) {
return NumberFormat.getCurrencyInstance(locale);
// DecimalFormat df = new DecimalFormat();
// df.setDecimalSeparatorAlwaysShown(true);
// df.setGroupingUsed(true);
// df.setMinimumFractionDigits(2);
// df.setMaximumFractionDigits(2);
// return df;
}
});
}
@Override
protected String getDescription() {
// TODO Auto-generated method stub
return null;
}
@Override
protected Integer getTicketNumber() {
// TODO Auto-generated method stub
return null;
}
}
|