aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/components/table/ExpandingContainer.java
blob: 829c29b95b0de75715b12e34a36f70011939446f (plain)
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package com.vaadin.tests.components.table;

import java.util.AbstractList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.logging.Logger;

import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.util.AbstractContainer;
import com.vaadin.data.util.BeanItem;
import com.vaadin.server.VaadinSession;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;

@SuppressWarnings("serial")
public class ExpandingContainer extends AbstractContainer implements
        Container.Ordered, Container.Indexed, Container.ItemSetChangeNotifier {

    public static final List<String> PROPERTY_IDS = Arrays.asList("id",
            "column1", "column2");

    private final Label sizeLabel;
    private final Logger log = Logger.getLogger(this.getClass().getName());

    private int currentSize = 300;

    private boolean loggingEnabled;

    public ExpandingContainer(Label sizeLabel) {
        this.sizeLabel = sizeLabel;
        updateLabel();
    }

    private void log(String message) {
        if (loggingEnabled) {
            log.info(message);
        }
    }

    // Expand container if we scroll past 85%
    public int checkExpand(int index) {
        log("checkExpand(" + index + ")");
        if (index >= currentSize * 0.85) {
            final int oldsize = currentSize;
            currentSize = (int) (oldsize * 1.3333);
            log("*** getSizeWithHint(" + index + "): went past 85% of size="
                    + oldsize + ", new size=" + currentSize);
            updateLabel();
        }
        return currentSize;
    };

    @Override
    public void fireItemSetChange() {
        super.fireItemSetChange();
    }

    private void updateLabel() {
        sizeLabel.setValue("Container size: " + currentSize);
    }

    public void triggerItemSetChange() {
        log("*** triggerItemSetChange(): scheduling item set change event");
        final VaadinSession session = VaadinSession.getCurrent();
        new Thread() {
            @Override
            public void run() {
                ExpandingContainer.this.invoke(session, new Runnable() {
                    @Override
                    public void run() {
                        log("*** Firing item set change event");
                        ExpandingContainer.this.fireItemSetChange();
                    }
                });
            }
        }.start();
    }

    private void invoke(VaadinSession session, Runnable action) {
        session.lock();
        VaadinSession previousSession = VaadinSession.getCurrent();
        VaadinSession.setCurrent(session);
        try {
            action.run();
        } finally {
            session.unlock();
            VaadinSession.setCurrent(previousSession);
        }
    }

    // Container

    @Override
    public BeanItem<MyBean> getItem(Object itemId) {
        if (!(itemId instanceof Integer)) {
            return null;
        }
        final int index = ((Integer) itemId).intValue();
        return new BeanItem<MyBean>(new MyBean(index));
    }

    @Override
    public Collection<Integer> getItemIds() {
        return new IntList(size());
    }

    @Override
    public List<String> getContainerPropertyIds() {
        return PROPERTY_IDS;
    }

    @Override
    @SuppressWarnings("rawtypes")
    public Property/* <?> */getContainerProperty(Object itemId,
            Object propertyId) {
        BeanItem<MyBean> item = getItem(itemId);
        return item != null ? item.getItemProperty(propertyId) : null;
    }

    @Override
    public Class<?> getType(Object propertyId) {
        return Component.class;
    }

    @Override
    public int size() {
        return currentSize;
    }

    @Override
    public boolean containsId(Object itemId) {
        if (!(itemId instanceof Integer)) {
            return false;
        }
        int index = ((Integer) itemId).intValue();
        checkExpand(index);
        return index >= 0 && index < currentSize;
    }

    /**
     * @throws UnsupportedOperationException
     *             always
     */
    @Override
    public Item addItem(Object itemId) {
        throw new UnsupportedOperationException();
    }

    /**
     * @throws UnsupportedOperationException
     *             always
     */
    @Override
    public Item addItem() {
        throw new UnsupportedOperationException();
    }

    /**
     * @throws UnsupportedOperationException
     *             always
     */
    @Override
    public boolean removeItem(Object itemId) {
        throw new UnsupportedOperationException();
    }

    /**
     * @throws UnsupportedOperationException
     *             always
     */
    @Override
    public boolean addContainerProperty(Object propertyId, Class<?> type,
            Object defaultValue) {
        throw new UnsupportedOperationException();
    }

    /**
     * @throws UnsupportedOperationException
     *             always
     */
    @Override
    public boolean removeContainerProperty(Object propertyId) {
        throw new UnsupportedOperationException();
    }

    /**
     * @throws UnsupportedOperationException
     *             always
     */
    @Override
    public boolean removeAllItems() {
        throw new UnsupportedOperationException();
    }

    // Container.Indexed

    /**
     * @throws UnsupportedOperationException
     *             always
     */
    @Override
    public Object addItemAt(int index) {
        throw new UnsupportedOperationException();
    }

    /**
     * @throws UnsupportedOperationException
     *             always
     */
    @Override
    public Item addItemAt(int index, Object newItemId) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Integer getIdByIndex(int index) {
        if (index < 0) {
            throw new IndexOutOfBoundsException("index < " + index);
        }
        final int size = currentSize;
        if (index >= size) {
            throw new IndexOutOfBoundsException("index=" + index + " but size="
                    + size);
        }
        checkExpand(index);
        return index;
    }

    @Override
    public List<Integer> getItemIds(int startIndex, int numberOfItems) {
        if (numberOfItems < 0) {
            throw new IllegalArgumentException("numberOfItems < 0");
        }
        final int size = currentSize;
        checkExpand(startIndex);
        if (startIndex < 0 || startIndex > size) {
            throw new IndexOutOfBoundsException("startIndex=" + startIndex
                    + " but size=" + size);
        }
        if (startIndex + numberOfItems > size) {
            numberOfItems = size - startIndex;
        }
        return new IntList(startIndex, numberOfItems);
    }

    @Override
    public int indexOfId(Object itemId) {
        if (!(itemId instanceof Integer)) {
            return -1;
        }
        final int index = ((Integer) itemId).intValue();
        checkExpand(index);
        if (index < 0 || index >= currentSize) {
            return -1;
        }
        return index;
    }

    // Container.Ordered

    @Override
    public Integer nextItemId(Object itemId) {
        if (!(itemId instanceof Integer)) {
            return null;
        }
        int index = ((Integer) itemId).intValue();
        checkExpand(index);
        if (index < 0 || index + 1 >= currentSize) {
            return null;
        }
        return index + 1;
    }

    @Override
    public Integer prevItemId(Object itemId) {
        if (!(itemId instanceof Integer)) {
            return null;
        }
        int index = ((Integer) itemId).intValue();
        checkExpand(index);
        if (index - 1 < 0 || index >= currentSize) {
            return null;
        }
        return index - 1;
    }

    @Override
    public Integer firstItemId() {
        return currentSize == 0 ? null : 0;
    }

    @Override
    public Integer lastItemId() {
        final int size = currentSize;
        return size == 0 ? null : size - 1;
    }

    @Override
    public boolean isFirstId(Object itemId) {
        if (!(itemId instanceof Integer)) {
            return false;
        }
        final int index = ((Integer) itemId).intValue();
        checkExpand(index);
        final int size = currentSize;
        return size > 0 && index == 0;
    }

    @Override
    public boolean isLastId(Object itemId) {
        if (!(itemId instanceof Integer)) {
            return false;
        }
        int index = ((Integer) itemId).intValue();
        checkExpand(index);
        int size = currentSize;
        return size > 0 && index == size - 1;
    }

    /**
     * @throws UnsupportedOperationException
     *             always
     */
    @Override
    public Item addItemAfter(Object previousItemId) {
        throw new UnsupportedOperationException();
    }

    /**
     * @throws UnsupportedOperationException
     *             always
     */
    @Override
    public Item addItemAfter(Object previousItemId, Object newItemId) {
        throw new UnsupportedOperationException();
    }

    // Container.ItemSetChangeNotifier

    @Override
    @SuppressWarnings("deprecation")
    public void addListener(Container.ItemSetChangeListener listener) {
        super.addListener(listener);
    }

    @Override
    public void addItemSetChangeListener(
            Container.ItemSetChangeListener listener) {
        super.addItemSetChangeListener(listener);
    }

    @Override
    @SuppressWarnings("deprecation")
    public void removeListener(Container.ItemSetChangeListener listener) {
        super.removeListener(listener);
    }

    @Override
    public void removeItemSetChangeListener(
            Container.ItemSetChangeListener listener) {
        super.removeItemSetChangeListener(listener);
    }

    // IntList

    private static class IntList extends AbstractList<Integer> {

        private final int min;
        private final int size;

        public IntList(int size) {
            this(0, size);
        }

        public IntList(int min, int size) {
            if (size < 0) {
                throw new IllegalArgumentException("size < 0");
            }
            this.min = min;
            this.size = size;
        }

        @Override
        public int size() {
            return size;
        }

        @Override
        public Integer get(int index) {
            if (index < 0 || index >= size) {
                throw new IndexOutOfBoundsException();
            }
            return min + index;
        }
    }

    // MyBean
    public class MyBean {

        private final int index;

        public MyBean(int index) {
            this.index = index;
        }

        public String getId() {
            return "ROW #" + index;
        }

        public String getColumn1() {
            return genText();
        }

        public String getColumn2() {
            return genText();
        }

        private String genText() {
            return "this is a line of text in row #" + index;
        }
    }

    public void logDetails(boolean enabled) {
        loggingEnabled = enabled;
    }
}