1 package org.vaadin.extension.gridscroll;
2
3 import java.lang.reflect.Method;
4
5 import org.vaadin.extension.gridscroll.shared.ColumnResizeCompensationMode;
6 import org.vaadin.extension.gridscroll.shared.GridScrollExtensionClientRPC;
7 import org.vaadin.extension.gridscroll.shared.GridScrollExtensionServerRPC;
8 import org.vaadin.extension.gridscroll.shared.GridScrollExtensionState;
9
10 import com.vaadin.event.ConnectorEventListener;
11 import com.vaadin.server.AbstractExtension;
12 import com.vaadin.shared.Registration;
13 import com.vaadin.ui.Grid;
14 import com.vaadin.ui.UI;
15 import com.vaadin.ui.Grid.Column;
16 import com.vaadin.util.ReflectTools;
17
18
19
20
21
22
23
24
25
26
27
28
29 @SuppressWarnings("serial")
30 public class GridScrollExtension<T> extends AbstractExtension {
31 private int lastXPosition;
32 private int lastYPosition;
33 private double[] columnWidths;
34 private Grid<T> grid;
35 private int lastWidth;
36 private int lastHeight;
37 private boolean restorePosition = true;
38
39
40
41
42
43
44 public interface GridScrolledListener<T> extends ConnectorEventListener {
45 Method GRID_SCROLLED_METHOD = ReflectTools.findMethod(
46 GridScrolledListener.class, "gridScrolled", GridScrolledEvent.class);
47 public void gridScrolled(GridScrolledEvent<T> event);
48 }
49
50
51
52
53
54
55 public interface GridRenderedListener<T> extends ConnectorEventListener {
56 Method GRID_RENDERED_METHOD = ReflectTools.findMethod(
57 GridRenderedListener.class, "gridRendered", GridRenderedEvent.class);
58 public void gridRendered(GridRenderedEvent<T> event);
59 }
60
61
62
63
64
65
66 public interface GridResizedListener<T> extends ConnectorEventListener {
67 Method GRID_RESIZED_METHOD = ReflectTools.findMethod(
68 GridResizedListener.class, "gridResized", GridResizedEvent.class);
69 public void gridResized(GridResizedEvent<T> event);
70 }
71
72
73
74
75
76
77 public interface GridColumnsResizedListener<T> extends ConnectorEventListener {
78 Method GRID_COLUMNS_RESIZED_METHOD = ReflectTools.findMethod(
79 GridColumnsResizedListener.class, "gridColumnsResized", GridColumnsResizedEvent.class);
80 public void gridColumnsResized(GridColumnsResizedEvent<T> event);
81 }
82
83
84
85
86
87
88 public GridScrollExtension(Grid<T> grid) {
89 this.extend(grid);
90 this.grid = grid;
91 registerRpc(new GridScrollExtensionServerRPC() {
92
93 @Override
94 public void ping() {
95 }
96
97 @Override
98 public void reportPosition(int Xposition, int Yposition) {
99 if (restorePosition && (Xposition == -1 || Yposition == -1)) {
100 restoreScrollPosition();
101 } else {
102 lastXPosition = Xposition;
103 lastYPosition = Yposition;
104 fireEvent(new GridScrolledEvent<T>(grid));
105 }
106 }
107
108 @Override
109 public void reportColumns(double[] widths, int column) {
110 columnWidths = widths;
111 fireEvent(new GridColumnsResizedEvent<T>(grid,column));
112
113
114 if (column != -1) {
115 UI ui = UI.getCurrent();
116 Thread t = new Thread(() -> {
117 try {
118 Thread.sleep(50);
119 } catch (InterruptedException e) {
120 }
121 ui.access(() -> {
122 grid.getColumns().get(column).setWidth(widths[column]);
123 });
124 });
125 t.start();
126 }
127 }
128
129 @Override
130 public void reportSize(int width, int height) {
131 lastWidth = width;
132 lastHeight = height;
133 fireEvent(new GridResizedEvent<T>(grid));
134 }
135
136 @Override
137 public void gridInitialColumnWidthsCalculated() {
138 fireEvent(new GridRenderedEvent<T>(grid));
139 }
140 });
141 }
142
143
144
145
146
147
148
149
150
151
152 public Registration addGridRenderedListener(GridRenderedListener<T> listener) {
153 return addListener(GridRenderedEvent.class, listener, GridRenderedListener.GRID_RENDERED_METHOD);
154 }
155
156
157
158
159
160
161
162
163
164
165
166 public Registration addGridResizedListener(GridResizedListener<T> listener) {
167 return addListener(GridResizedEvent.class, listener, GridResizedListener.GRID_RESIZED_METHOD);
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181 public Registration addGridColumnsResizedListener(GridColumnsResizedListener<T> listener) {
182 return addListener(GridColumnsResizedEvent.class, listener, GridColumnsResizedListener.GRID_COLUMNS_RESIZED_METHOD);
183 }
184
185
186
187
188
189
190
191
192
193 public Registration addGridScrolledListener(GridScrolledListener<T> listener) {
194 return addListener(GridScrolledEvent.class, listener, GridScrolledListener.GRID_SCROLLED_METHOD);
195 }
196
197
198
199
200
201
202
203
204
205
206 public void adjustGridWidth() {
207 getClientRPC().recalculateGridWidth();
208 }
209
210
211
212
213
214
215
216 public void adjustLastColumnWidth() {
217 getClientRPC().adjustLastColumn();
218 }
219
220
221
222
223
224
225
226
227
228
229 public double getColumnWidth(Column<?,?> column) {
230 double width = 0;
231 int i = 0;
232 if (columnWidths == null) return -1.0;
233 if (column.isHidden()) return -1.0;
234 for (Column<?, ?> col : grid.getColumns()) {
235 if (col == column) width = columnWidths[i];
236 if (!col.isHidden()) i++;
237 }
238 return width;
239 }
240
241
242
243
244
245
246
247
248
249
250 public double getColumnWidth(String columnId) {
251 double width = 0;
252 int i = 0;
253 if (columnWidths == null) return -1.0;
254 for (Column<?, ?> column : grid.getColumns()) {
255 if (column.getId().equals(columnId)) width = columnWidths[i];
256 i++;
257 }
258 return width;
259 }
260
261
262
263
264
265
266
267
268
269
270 public double getColumnWidth(int i) {
271 if (columnWidths == null) return -1.0;
272 if (i > grid.getColumns().size()) return -1.0;
273 return columnWidths[i];
274 }
275
276
277
278
279
280 public void restoreScrollPosition() {
281 setScrollPosition(lastXPosition, lastYPosition);
282 }
283
284
285
286
287
288
289 public int getLastXPosition() {
290 return lastXPosition;
291 }
292
293
294
295
296
297
298 public int getLastYPosition() {
299 return lastYPosition;
300 }
301
302
303
304
305
306
307
308
309 public int getWidth() {
310 return lastWidth;
311 }
312
313
314
315
316
317
318
319
320 public int getHeight() {
321 return lastHeight;
322 }
323
324
325
326
327
328
329
330 public void setScrollPosition(int Xposition, int Yposition) {
331 getClientRPC().setScrollPosition(Xposition, Yposition);
332 lastXPosition = Xposition;
333 lastYPosition = Yposition;
334 }
335
336 private GridScrollExtensionClientRPC getClientRPC() {
337 return getRpcProxy(GridScrollExtensionClientRPC.class);
338 }
339
340
341
342
343
344
345
346
347 @Deprecated
348 public void setAutoResizeWidth(boolean autoResizeWidth) {
349 if (autoResizeWidth) getState().compensationMode = ColumnResizeCompensationMode.RESIZE_GRID;
350 else getState().compensationMode = ColumnResizeCompensationMode.NONE;
351 }
352
353
354
355
356 @Deprecated
357 public void setColumnResizeComponesationMode(ColumnResizeCompensationMode mode) {
358 this.setColumnResizeCompensationMode(mode);
359 }
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377 public void setColumnResizeCompensationMode(ColumnResizeCompensationMode mode) {
378 getState().compensationMode = mode;
379 }
380
381
382
383
384 @Deprecated
385 public ColumnResizeCompensationMode getColumnResizeComponesationMode() {
386 return this.getColumnResizeCompensationMode();
387 }
388
389
390
391
392
393
394
395
396 public ColumnResizeCompensationMode getColumnResizeCompensationMode() {
397 return getState().compensationMode;
398 }
399
400 @Override
401 public GridScrollExtensionState getState() {
402 return (GridScrollExtensionState) super.getState();
403 }
404
405
406
407
408
409
410
411
412
413
414
415 public void setRestorePosition(boolean restorePosition) {
416 this.restorePosition = restorePosition;
417 }
418 }