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
|
package com.vaadin.tests.components.table;
import com.vaadin.data.Container;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.tests.components.TestBase;
import com.vaadin.ui.BaseFieldFactory;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.Field;
import com.vaadin.ui.FieldFactory;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.AbstractSelect.NewItemHandler;
import com.vaadin.ui.Table.ColumnGenerator;
public class PropertyValueChange extends TestBase {
@Override
protected String getDescription() {
return "Property value change should only update absolutely "
+ "needed cells. Tables have common datasource. The first is "
+ "editable, second one has data in disabled fields, the lastone "
+ "is plain table that directly shows data. Use first table and "
+ "combobox/sync button to send changed values to server and evaluate "
+ "given uidl responses.";
}
@Override
protected Integer getTicketNumber() {
return 2823;
}
private IndexedContainer container;
// Also use column generator in test, to ensure it is possible to build
// columns that update automatically.
ColumnGenerator multiplier = new ColumnGenerator() {
private int getMultipliedValue(Property p) {
int i = ((Integer) p.getValue()).intValue();
return i * 3;
}
public Component generateCell(Table source, Object itemId,
Object columnId) {
final Label l = new Label();
final Property integer = source.getContainerProperty(itemId,
"integer");
l.setValue(getMultipliedValue(integer));
// we must hook value change listener to ensure updates in all use
// cases (eg. edit mode)
if (integer instanceof Property.ValueChangeNotifier) {
Property.ValueChangeNotifier notifier = (Property.ValueChangeNotifier) integer;
notifier.addListener(new ValueChangeListener() {
public void valueChange(ValueChangeEvent event) {
l.setValue(getMultipliedValue(integer));
}
});
}
return l;
}
};
FieldFactory ff = new MyFieldFactory();
@Override
public void setup() {
container = new IndexedContainer();
container.addContainerProperty("text", String.class, "sampletext");
container.addContainerProperty("integer", Integer.class, 5);
container.addItem();
container.addItem();
Table t1 = new Table(
"Editable table with bells and wistles. See description.");
t1.setDescription("Opening combobox should never fire table"
+ " refresh (for this table). Update from textfield "
+ "(integer) may be sent to server however. The readonly table"
+ " my refresh, but not this one.");
t1.setPageLength(0);
t1.setContainerDataSource(container);
t1.addGeneratedColumn("integer x 3", multiplier);
t1.setFieldFactory(ff);
t1.setEditable(true);
t1.setDebugId("editortable");
Table t2 = new Table(
"A clone of table1, but disabled. Properties are in components.");
t2
.setDescription("This table is in editable mode."
+ " Updates to common datasource should not affect redraw for this "
+ "table. Only the components inside table should get updated.");
t2.setFieldFactory(ff);
t2.setEditable(true);
t2.setEnabled(false);
t2.setContainerDataSource(container);
t2.addGeneratedColumn("integer x 3", multiplier);
t2.setPageLength(0);
t2.setDebugId("disabled table");
Table reader = new Table("Reader table");
reader
.setDescription("This table should be redrawn on container changes as container data is "
+ "displayed directly in cells.");
reader.setContainerDataSource(container);
reader.addGeneratedColumn("integer x 3", multiplier);
reader.setPageLength(0);
reader.setDebugId("reader table");
getLayout().addComponent(t1);
getLayout().addComponent(t2);
getLayout().addComponent(reader);
getLayout().addComponent(new Button("Sync!"));
}
}
class MyFieldFactory extends BaseFieldFactory {
IndexedContainer texts = new IndexedContainer();
public MyFieldFactory() {
texts.addItem("sampletext");
texts.addItem("foo");
texts.addItem("bar");
for (int i = 0; i < 100; i++) {
texts.addItem("foo" + 1);
}
}
@Override
public Field createField(Container container, Object itemId,
Object propertyId, Component uiContext) {
if (propertyId.equals("text")) {
// replace text fields with comboboxes
final ComboBox cb = new ComboBox() {
};
cb.setContainerDataSource(texts);
cb.setNewItemsAllowed(true);
cb.setNewItemHandler(new NewItemHandler() {
public void addNewItem(String newItemCaption) {
texts.addItem(newItemCaption);
cb.setValue(newItemCaption);
}
});
return cb;
}
return super.createField(container, itemId, propertyId, uiContext);
}
}
|