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
|
package com.vaadin.tests.components.treetable;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.vaadin.data.Container;
import com.vaadin.data.Container.Ordered;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.data.util.ContainerHierarchicalWrapper;
import com.vaadin.tests.components.TestBase;
import com.vaadin.tests.util.Log;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.NativeButton;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.Table;
import com.vaadin.ui.Table.ColumnGenerator;
import com.vaadin.ui.Tree.CollapseEvent;
import com.vaadin.ui.Tree.CollapseListener;
import com.vaadin.ui.Tree.ExpandEvent;
import com.vaadin.ui.Tree.ExpandListener;
import com.vaadin.ui.TreeTable;
public class TreeTableCacheOnPartialUpdates extends TestBase {
private Log log = new Log(5);
public class TestBean implements Serializable {
private String col1;
private String col2;
public TestBean() {
col1 = "";
col2 = "";
}
public TestBean(String col1, String col2) {
this.col1 = col1;
this.col2 = col2;
}
public String getCol1() {
return col1;
}
public void setCol1(String col1) {
this.col1 = col1;
}
public String getCol2() {
return col2;
}
public void setCol2(String col2) {
this.col2 = col2;
}
@Override
public String toString() {
return "TestBean [col1=" + col1 + ", col2=" + col2 + "]";
}
}
public class Col3ColumnGenerator implements ColumnGenerator {
@Override
public Component generateCell(final com.vaadin.ui.Table source,
final Object itemId, Object columnId) {
TestBean tb = (TestBean) itemId;
String identifier = "Item " + itemId + "/" + columnId;
Button btnCol3 = new NativeButton(identifier);
btnCol3.setId("cacheTestButton-" + tb.getCol1() + "-"
+ tb.getCol2());
btnCol3.addClickListener(new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
log.log("Button " + event.getButton().getCaption()
+ " clicked. Row index: "
+ indexOfId(source, itemId));
}
});
return btnCol3;
}
}
public class Col4ColumnGenerator implements ColumnGenerator {
@Override
public Component generateCell(final com.vaadin.ui.Table source,
final Object itemId, Object columnId) {
TestBean tb = (TestBean) itemId;
String identifier = "Expand/Collapse";
Button btnCol4 = new NativeButton(identifier);
btnCol4.setId("cacheTestButtonToggle-" + tb.getCol1() + "-"
+ tb.getCol2());
btnCol4.addClickListener(new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
treeTable.setCollapsed(itemId,
!treeTable.isCollapsed(itemId));
}
});
return btnCol4;
}
}
protected int indexOfId(Table source, Object itemId) {
Container.Ordered c = (Ordered) source.getContainerDataSource();
if (c instanceof Container.Indexed) {
return ((Container.Indexed) source).indexOfId(itemId);
} else {
ArrayList<Object> list = new ArrayList<Object>(source.getItemIds());
return list.indexOf(itemId);
}
}
private TreeTable treeTable;
private BeanItemContainer<TestBean> testBeanContainer;
private static String[] columnHeaders = new String[] { "Col1", "Col2",
"Col3", "Col4" };
private static Object[] visibleColumns = new Object[] { "col1", "col2",
"col3", "col4" };
@Override
public void setup() {
// Force row height to be the same in all browsers so scrolling based on
// pixels works as expected
Button b = new Button("Show first");
addComponent(b);
b.addClickListener(new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
log.log("First visible item id is: "
+ treeTable.getCurrentPageFirstItemId());
}
});
NativeSelect cacheRateSelect = new NativeSelect("Cache rate");
cacheRateSelect.setImmediate(true);
cacheRateSelect.setNullSelectionAllowed(false);
cacheRateSelect.addItem(new Integer(0));
cacheRateSelect.addItem(new Integer(1));
cacheRateSelect.addItem(new Integer(2));
cacheRateSelect.setValue(2);
cacheRateSelect.addValueChangeListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
treeTable
.setCacheRate((Integer) event.getProperty().getValue());
}
});
addComponent(cacheRateSelect);
treeTable = new TreeTable();
treeTable.addStyleName("table-equal-rowheight");
testBeanContainer = new BeanItemContainer<TestBean>(TestBean.class);
Map<String, Integer> hasChildren = new HashMap<String, Integer>();
hasChildren.put("1", 5);
hasChildren.put("3", 10);
hasChildren.put("5", 20);
hasChildren.put("6", 7);
hasChildren.put("7", 1);
hasChildren.put("40", 20);
hasChildren.put("99", 20);
treeTable.setContainerDataSource(createContainer(100, hasChildren));
treeTable.addGeneratedColumn("col3", new Col3ColumnGenerator());
treeTable.addGeneratedColumn("col4", new Col4ColumnGenerator());
treeTable.addExpandListener(new ExpandListener() {
@Override
public void nodeExpand(ExpandEvent event) {
logExpandCollapse(event.getItemId(), "expanded");
}
});
treeTable.addCollapseListener(new CollapseListener() {
@Override
public void nodeCollapse(CollapseEvent event) {
logExpandCollapse(event.getItemId(), "collapsed");
}
});
treeTable.setVisibleColumns(visibleColumns);
treeTable.setColumnHeaders(columnHeaders);
treeTable.setColumnWidth("col1", 150);
treeTable.setColumnWidth("col2", 50);
treeTable.setHeight("430px");
addComponent(log);
addComponent(treeTable);
}
protected void logExpandCollapse(Object itemId, String operation) {
String identifier = "Item " + itemId;
log.log("Row " + identifier + " " + operation + ". Row index: "
+ indexOfId(treeTable, itemId));
}
private Container createContainer(int items,
Map<String, Integer> hasChildren) {
ContainerHierarchicalWrapper container;
container = new ContainerHierarchicalWrapper(testBeanContainer);
populate(container, items, hasChildren);
return container;
}
private void populate(Container.Hierarchical container, int items,
Map<String, Integer> children) {
for (int i = 0; i < items; i++) {
String row = String.valueOf(i + 1);
String rowText = stringForInt(i);
TestBean itemId = new TestBean(row, rowText);
container.addItem(itemId);
if (children.containsKey(row)) {
itemId.setCol1(itemId.getCol1() + " (children)");
for (int j = 0; j < children.get(row); j++) {
TestBean childItemId = new TestBean(row + "." + (j + 1),
rowText + "." + stringForInt(j));
container.addItem(childItemId);
container.setParent(childItemId, itemId);
}
}
}
}
private String stringForInt(int i) {
int charsAllowed = 26; // a-z
if (i >= charsAllowed) {
return stringForInt(i / charsAllowed - 1)
+ stringForInt(i % charsAllowed);
} else {
return String.valueOf((char) (i + 'A'));
}
}
@Override
protected String getDescription() {
// TODO Auto-generated method stub
return null;
}
@Override
protected Integer getTicketNumber() {
// TODO Auto-generated method stub
return null;
}
}
|