aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/components/TouchScrollables.java
blob: 963b4265133185dae6231da6c0480bce22edec24 (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
package com.vaadin.tests.components;

import java.util.Collection;

import com.vaadin.event.Action;
import com.vaadin.event.Action.Handler;
import com.vaadin.event.dd.DragAndDropEvent;
import com.vaadin.event.dd.DropHandler;
import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
import com.vaadin.event.dd.acceptcriteria.SourceIs;
import com.vaadin.shared.ui.dd.VerticalDropLocation;
import com.vaadin.tests.util.Person;
import com.vaadin.tests.util.PersonContainer;
import com.vaadin.tests.util.TestUtils;
import com.vaadin.ui.Accordion;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Label;
import com.vaadin.ui.Layout;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.v7.data.Item;
import com.vaadin.v7.data.util.IndexedContainer;
import com.vaadin.v7.event.DataBoundTransferable;
import com.vaadin.v7.ui.AbstractSelect.AbstractSelectTargetDetails;
import com.vaadin.v7.ui.Table;

public class TouchScrollables extends TestBase {
    java.util.Random r = new java.util.Random(1);

    private TabSheet testSelector = new TabSheet();

    @Override
    public void setup() {
        getLayout().addComponent(testSelector);
        testSelector.setHeight("500px");

        addTest(getPanelTest());
        addTest(getSimpleTableTest());
        addTest(getDDSortableTableTest());
        addTest(getTabSheetTest());
        addTest(getSplitPanelTest());
        addTest(getAccordionTest());
        addTest(getSubWindowTest());

        TestUtils.injectCSS(getLayout().getUI(),
                "body * {-webkit-user-select: none;} .v-table-row-drag-middle .v-table-cell-content {"
                        + "        background-color: inherit ; border-bottom: 1px solid cyan;"
                        + "}"
                        + ".v-table-row-drag-middle .v-table-cell-wrapper {"
                        + "        margin-bottom: -1px;" + "}" + ""

        );
    }

    private Component getPanelTest() {
        Layout cssLayout = new CssLayout();
        cssLayout.setCaption("Panel");

        final VerticalLayout pl = new VerticalLayout();
        pl.setMargin(true);
        final Panel p = new Panel(pl);
        p.setHeight("400px");
        Label l50 = null;
        for (int i = 0; i < 100; i++) {
            Label c = new Label("Label" + i);
            pl.addComponent(c);
            if (i == 50) {
                l50 = c;
            }
        }

        final Label l = l50;
        Button button = new Button("Scroll to label 50",
                event -> getLayout().getUI().scrollIntoView(l));
        cssLayout.addComponent(button);
        button = new Button("Scroll to 100px", event -> p.setScrollTop(100));
        cssLayout.addComponent(button);
        cssLayout.addComponent(p);
        return cssLayout;
    }

    private Component getTabSheetTest() {
        TabSheet ts = new TabSheet();
        ts.setCaption("Tabsheet");
        ts.setHeight("100%");
        ts.addTab(getBigComponent(), "Tab 1");
        ts.addTab(getBigComponent(), "Tab 2");
        return ts;
    }

    private Component getSplitPanelTest() {
        HorizontalSplitPanel sp = new HorizontalSplitPanel();
        sp.setCaption("Splitpanel");
        sp.addComponent(getBigComponent());
        sp.addComponent(getBigComponent());
        return sp;
    }

    private Component getSimpleTableTest() {
        CssLayout cssLayout = new CssLayout();
        final Table table = new Table();

        Button button = new Button("Toggle lazyloading");
        button.addClickListener(event -> {
            if (table.getCacheRate() == 100) {
                table.setCacheRate(2);
                table.setPageLength(15);
            } else {
                table.setCacheRate(100);
                table.setHeight("400px");
            }
        });
        cssLayout.addComponent(button);

        button = new Button("Toggle selectable");
        button.addClickListener(
                event -> table.setSelectable(!table.isSelectable()));
        cssLayout.addComponent(button);

        table.addContainerProperty("foo", String.class, "bar");
        table.setRowHeaderMode(Table.ROW_HEADER_MODE_INDEX);
        for (int i = 0; i < 1000; i++) {
            table.addItem();
        }
        cssLayout.addComponent(table);
        cssLayout.setCaption("Table");
        return cssLayout;
    }

    private Component getAccordionTest() {
        Accordion a = new Accordion();
        a.setCaption("Accordion");
        a.setHeight("100%");
        a.addTab(getBigComponent(), "Tab 1");
        a.addTab(getBigComponent(), "Tab 2");
        a.addTab(getBigComponent(), "Tab 3");
        return a;
    }

    private Component getSubWindowTest() {
        Button b = new Button("Open subwindow", event -> {
            VerticalLayout layout = new VerticalLayout();
            layout.setMargin(true);
            Window w = new Window("Subwindow", layout);
            w.center();
            w.setHeight("200px");
            layout.addComponent(getBigComponent());
            getMainWindow().addWindow(w);
        });
        return b;
    }

    private Component getDDSortableTableTest() {
        final Table table;
        table = new Table();
        table.setCaption("DD sortable table with context menus");
        // table.setWidth("100%");
        table.setPageLength(10);
        table.setRowHeaderMode(Table.ROW_HEADER_MODE_ID);
        table.setSelectable(true);
        table.setMultiSelect(true);

        table.addActionHandler(new Handler() {

            Action[] actions = { new Action("FOO"), new Action("BAR"),
                    new Action("CAR") };

            @Override
            public Action[] getActions(Object target, Object sender) {
                return actions;
            }

            @Override
            public void handleAction(Action action, Object sender,
                    Object target) {
                Notification.show(action.getCaption());

            }
        });

        populateTable(table);

        /*
         * Make table rows draggable
         */
        table.setDragMode(Table.TableDragMode.ROW);

        table.setDropHandler(new DropHandler() {
            // accept only drags from this table
            AcceptCriterion crit = new SourceIs(table);

            @Override
            public AcceptCriterion getAcceptCriterion() {
                return crit;
            }

            @Override
            public void drop(DragAndDropEvent dropEvent) {
                AbstractSelectTargetDetails dropTargetData = (AbstractSelectTargetDetails) dropEvent
                        .getTargetDetails();
                DataBoundTransferable transferable = (DataBoundTransferable) dropEvent
                        .getTransferable();
                Object itemIdOver = dropTargetData.getItemIdOver();
                Object itemId = transferable.getItemId();
                if (itemId == null || itemIdOver == null
                        || itemId.equals(itemIdOver)) {
                    return; // no move happened
                }

                // IndexedContainer goodies... (hint: don't use it in real apps)
                IndexedContainer containerDataSource = (IndexedContainer) table
                        .getContainerDataSource();
                int newIndex = containerDataSource.indexOfId(itemIdOver) - 1;
                if (dropTargetData
                        .getDropLocation() != VerticalDropLocation.TOP) {
                    newIndex++;
                }
                if (newIndex < 0) {
                    newIndex = 0;
                }
                Object idAfter = containerDataSource.getIdByIndex(newIndex);
                Collection<?> selections = (Collection<?>) table.getValue();
                if (selections != null && selections.contains(itemId)) {
                    // dragged a selected item, if multiple rows selected, drag
                    // them too (functionality similar to apple mail)
                    for (Object object : selections) {
                        moveAfter(containerDataSource, object, idAfter);
                    }

                } else {
                    // move just the dragged row, not considering selection at
                    // all
                    moveAfter(containerDataSource, itemId, idAfter);
                }

            }

            private void moveAfter(IndexedContainer containerDataSource,
                    Object itemId, Object idAfter) {
                try {
                    IndexedContainer clone = null;
                    clone = (IndexedContainer) containerDataSource.clone();
                    containerDataSource.removeItem(itemId);
                    Item newItem = containerDataSource.addItemAfter(idAfter,
                            itemId);
                    Item item = clone.getItem(itemId);
                    for (Object propId : item.getItemPropertyIds()) {
                        newItem.getItemProperty(propId).setValue(
                                item.getItemProperty(propId).getValue());
                    }

                } catch (CloneNotSupportedException e) {
                    e.printStackTrace();
                }
            }
        });
        return table;
    }

    private void populateTable(Table table) {
        table.addContainerProperty("Name", String.class, "");
        table.addContainerProperty("Weight", Integer.class, 0);

        PersonContainer testData = PersonContainer.createWithTestData();

        for (int i = 0; i < 40; i++) {
            Item addItem = table.addItem("Item" + i);
            Person p = testData.getIdByIndex(i);
            addItem.getItemProperty("Name")
                    .setValue(p.getFirstName() + " " + p.getLastName());
            addItem.getItemProperty("Weight").setValue(50 + r.nextInt(60));
        }

    }

    private void addTest(final Component t) {
        testSelector.addComponent(t);
    }

    private Component getBigComponent() {
        Layout l = new VerticalLayout();
        for (int i = 0; i < 100; i++) {
            Label c = new Label("Label" + i);
            l.addComponent(c);
        }
        return l;
    }

    @Override
    protected String getDescription() {
        return "Various components and setups suitable for testing scrolling on touch devices.";
    }

    @Override
    protected Integer getTicketNumber() {
        return null;
    }
}