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
|
package com.vaadin.tests.widgetset.client.grid;
import java.util.ArrayList;
import java.util.List;
import com.google.gwt.user.client.ui.Composite;
import com.vaadin.client.ui.grid.Cell;
import com.vaadin.client.ui.grid.ColumnConfiguration;
import com.vaadin.client.ui.grid.Escalator;
import com.vaadin.client.ui.grid.EscalatorUpdater;
import com.vaadin.client.ui.grid.Row;
import com.vaadin.client.ui.grid.RowContainer;
import com.vaadin.shared.ui.grid.ScrollDestination;
public class VTestGrid extends Composite {
private static class Data {
private int columnCounter = 0;
private int rowCounter = 0;
private final List<Integer> columns = new ArrayList<Integer>();
private final List<Integer> rows = new ArrayList<Integer>();
@SuppressWarnings("boxing")
public void insertRows(final int offset, final int amount) {
final List<Integer> newRows = new ArrayList<Integer>();
for (int i = 0; i < amount; i++) {
newRows.add(rowCounter++);
}
rows.addAll(offset, newRows);
}
@SuppressWarnings("boxing")
public void insertColumns(final int offset, final int amount) {
final List<Integer> newColumns = new ArrayList<Integer>();
for (int i = 0; i < amount; i++) {
newColumns.add(columnCounter++);
}
columns.addAll(offset, newColumns);
}
public EscalatorUpdater createHeaderUpdater() {
return new EscalatorUpdater() {
@Override
public void updateCells(final Row row,
final Iterable<Cell> cellsToUpdate) {
for (final Cell cell : cellsToUpdate) {
if (cell.getColumn() % 3 == 0) {
cell.setColSpan(2);
}
final Integer columnName = columns
.get(cell.getColumn());
cell.getElement().setInnerText("Header " + columnName);
}
}
};
}
public EscalatorUpdater createFooterUpdater() {
return new EscalatorUpdater() {
@Override
public void updateCells(final Row row,
final Iterable<Cell> cellsToUpdate) {
for (final Cell cell : cellsToUpdate) {
if (cell.getColumn() % 3 == 1) {
cell.setColSpan(2);
}
final Integer columnName = columns
.get(cell.getColumn());
cell.getElement().setInnerText("Footer " + columnName);
}
}
};
}
public EscalatorUpdater createBodyUpdater() {
return new EscalatorUpdater() {
private int i = 0;
public void renderCell(final Cell cell) {
final Integer columnName = columns.get(cell.getColumn());
final Integer rowName = rows.get(cell.getRow());
final String cellInfo = columnName + "," + rowName + " ("
+ i + ")";
if (cell.getColumn() > 0) {
cell.getElement().setInnerText("Cell: " + cellInfo);
} else {
cell.getElement().setInnerText(
"Row " + cell.getRow() + ": " + cellInfo);
}
if (cell.getColumn() % 3 == cell.getRow() % 3) {
cell.setColSpan(3);
}
final double c = i * .1;
final int r = (int) ((Math.cos(c) + 1) * 128);
final int g = (int) ((Math.cos(c / Math.PI) + 1) * 128);
final int b = (int) ((Math.cos(c / (Math.PI * 2)) + 1) * 128);
cell.getElement()
.getStyle()
.setBackgroundColor(
"rgb(" + r + "," + g + "," + b + ")");
if ((r * .8 + g * 1.3 + b * .9) / 3 < 127) {
cell.getElement().getStyle().setColor("white");
} else {
cell.getElement().getStyle().clearColor();
}
i++;
}
@Override
public void updateCells(final Row row,
final Iterable<Cell> cellsToUpdate) {
for (final Cell cell : cellsToUpdate) {
renderCell(cell);
}
}
};
}
public void removeRows(final int offset, final int amount) {
for (int i = 0; i < amount; i++) {
rows.remove(offset);
}
}
public void removeColumns(final int offset, final int amount) {
for (int i = 0; i < amount; i++) {
columns.remove(offset);
}
}
}
private final Escalator escalator = new Escalator();
private final Data data = new Data();
public VTestGrid() {
initWidget(escalator);
final RowContainer header = escalator.getHeader();
header.setEscalatorUpdater(data.createHeaderUpdater());
header.insertRows(0, 1);
final RowContainer footer = escalator.getFooter();
footer.setEscalatorUpdater(data.createFooterUpdater());
footer.insertRows(0, 1);
escalator.getBody().setEscalatorUpdater(data.createBodyUpdater());
insertRows(0, 100);
insertColumns(0, 10);
setWidth(TestGridState.DEFAULT_WIDTH);
setHeight(TestGridState.DEFAULT_HEIGHT);
}
public void insertRows(final int offset, final int number) {
data.insertRows(offset, number);
escalator.getBody().insertRows(offset, number);
}
public void insertColumns(final int offset, final int number) {
data.insertColumns(offset, number);
escalator.getColumnConfiguration().insertColumns(offset, number);
}
public ColumnConfiguration getColumnConfiguration() {
return escalator.getColumnConfiguration();
}
public void scrollToRow(final int index,
final ScrollDestination destination, final int padding) {
escalator.scrollToRow(index, destination, padding);
}
public void scrollToColumn(final int index,
final ScrollDestination destination, final int padding) {
escalator.scrollToColumn(index, destination, padding);
}
public void removeRows(final int offset, final int amount) {
data.removeRows(offset, amount);
escalator.getBody().removeRows(offset, amount);
}
public void removeColumns(final int offset, final int amount) {
data.removeColumns(offset, amount);
escalator.getColumnConfiguration().removeColumns(offset, amount);
}
@Override
public void setWidth(String width) {
escalator.setWidth(width);
}
@Override
public void setHeight(String height) {
escalator.setHeight(height);
}
public RowContainer getHeader() {
return escalator.getHeader();
}
public RowContainer getBody() {
return escalator.getBody();
}
public RowContainer getFooter() {
return escalator.getFooter();
}
public void calculateColumnWidths() {
escalator.calculateColumnWidths();
}
}
|