summaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/dd/DDTest2.java
blob: 7757513fc11aa9dce17c2d831878feb0f2cbbea4 (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
package com.vaadin.tests.dd;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.util.HierarchicalContainer;
import com.vaadin.event.DataBoundTransferable;
import com.vaadin.event.Transferable;
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.And;
import com.vaadin.event.dd.acceptcriteria.Or;
import com.vaadin.event.dd.acceptcriteria.SourceIs;
import com.vaadin.server.Resource;
import com.vaadin.server.ThemeResource;
import com.vaadin.tests.components.TestBase;
import com.vaadin.tests.util.Person;
import com.vaadin.tests.util.PersonContainer;
import com.vaadin.ui.AbstractSelect;
import com.vaadin.ui.AbstractSelect.AbstractSelectTargetDetails;
import com.vaadin.ui.AbstractSelect.AcceptItem;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.UI;
import com.vaadin.ui.Table;
import com.vaadin.ui.Table.TableTransferable;
import com.vaadin.ui.Tree;
import com.vaadin.ui.Tree.TargetItemAllowsChildren;
import com.vaadin.ui.Tree.TreeDragMode;
import com.vaadin.ui.Window;

public class DDTest2 extends TestBase {

    java.util.Random r = new java.util.Random(1);

    HorizontalLayout hl = new HorizontalLayout();
    Tree tree1 = new Tree("Tree that accepts table rows to folders");
    Table table = new Table("Drag rows to Tree on left or right");
    Tree tree2 = new Tree("Accepts items, copies values");

    private Tree tree3;

    @Override
    protected void setup() {
        UI w = getLayout().getUI();
        /* darn reindeer has no icons */

        /* Make all trees (their nodes actually) draggable */
        tree1.setDragMode(TreeDragMode.NODE);
        tree2.setDragMode(TreeDragMode.NODE);

        hl.addComponent(tree1);
        hl.addComponent(table);
        hl.addComponent(tree2);
        hl.setWidth("100%");
        hl.setSpacing(true);
        hl.setExpandRatio(table, 1);
        popuplateTrees();
        table.setWidth("100%");
        table.setPageLength(10);
        populateTable();
        addComponent(hl);

        tree3 = new Tree(
                "Tree with lazy loading criteria, of first server visit caches accept rules for all captions");
        tree3.setDragMode(TreeDragMode.NODE);

        tree3.addItem("Drag on me");
        tree3.addItem("Or me");
        /*
         * An example of lazy initializing drop criterion with component
         * specific api for easy rule writing.
         * 
         * Example is pretty stupid (accepts drop on all nodes, but by
         * explicitly defining them here), but demonstrates lazy initialization
         * option if rules are heavy.
         */
        final AcceptCriterion crit = new Tree.TreeDropCriterion() {

            @Override
            protected Set<Object> getAllowedItemIds(DragAndDropEvent dragEvent,
                    Tree tree) {
                return new HashSet<Object>(tree.getItemIds());
            }
        };

        tree3.setDropHandler(new DropHandler() {
            @Override
            public void drop(DragAndDropEvent dropEvent) {
                Transferable transferable = dropEvent.getTransferable();

                String data = (String) transferable.getData("Text");
                if (transferable instanceof TableTransferable) {
                    TableTransferable tr = (TableTransferable) transferable;
                    System.out.println("From table row" + tr.getPropertyId());
                    Object value = tr.getSourceContainer()
                            .getItem(tr.getItemId())
                            .getItemProperty(tr.getPropertyId()).getValue();
                    data = (null != value) ? value.toString() : null;
                }
                if (data == null) {
                    data = "-no Text data flavor-";
                }
                tree3.addItem(data);
                AbstractSelect.AbstractSelectTargetDetails dropTargetData = (AbstractSelect.AbstractSelectTargetDetails) dropEvent
                        .getTargetDetails();
                tree3.setParent(data, dropTargetData.getItemIdOver());

            }

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

        addComponent(tree3);

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

        TargetItemAllowsChildren onNode = TargetItemAllowsChildren.get();
        SourceIs fromTable = new SourceIs(table);

        SourceIs fromTree = new SourceIs(tree1);
        final Or fromTree1OrTable = new Or(fromTable, fromTree);
        // Or could in the case be replaced with, keeping here as an example and
        // test
        @SuppressWarnings("unused")
        SourceIs treeOrTable = new SourceIs(table, tree1);

        final And and = new And(fromTree1OrTable, onNode);

        DropHandler dropHandler = new DropHandler() {

            @Override
            public void drop(DragAndDropEvent event) {
                /*
                 * We know transferrable is from table, so it is of type
                 * DataBoundTransferrable
                 */
                DataBoundTransferable tr = (DataBoundTransferable) event
                        .getTransferable();
                Object itemId = tr.getItemId();
                Container sourceContainer = tr.getSourceContainer();
                if (tr.getSourceComponent() != tree1) {
                    // if the source is from table (not from tree1 itself),
                    // transfer Name property and use it as an identifier in
                    // tree1
                    Object nameValue = sourceContainer.getItem(itemId)
                            .getItemProperty("Name").getValue();
                    String name = (null != nameValue) ? nameValue.toString()
                            : null;

                    tree1.addItem(name);
                    tree1.setChildrenAllowed(name, false);

                    /*
                     * Remove the item from table
                     */
                    sourceContainer.removeItem(itemId);

                    itemId = name;

                }

                /*
                 * As we also accept only drops on folders, we know dropDetails
                 * is from Tree and it contains itemIdOver.
                 */
                AbstractSelectTargetDetails details = (AbstractSelectTargetDetails) event
                        .getTargetDetails();
                Object idOver = details.getItemIdOver();
                tree1.setParent(itemId, idOver);

            }

            @Override
            public AcceptCriterion getAcceptCriterion() {
                return and;
            }
        };
        tree1.setDropHandler(dropHandler);

        /*
         * First step done. tree1 now accepts drags only from table and only
         * over tree nodes aka "folders"
         */

        /*
         * Now set the rightmost tree accept any item drag. On drop, copy from
         * source. Also make drags from tree1 possible.
         */

        dropHandler = new DropHandler() {
            @Override
            public void drop(DragAndDropEvent event) {
                AbstractSelectTargetDetails details = (AbstractSelectTargetDetails) event
                        .getTargetDetails();
                Transferable transferable = event.getTransferable();

                if (transferable instanceof DataBoundTransferable) {
                    DataBoundTransferable tr = (DataBoundTransferable) transferable;

                    Object itemId = tree2.addItem();
                    tree2.setParent(itemId, details.getItemIdOver());
                    if (tr.getSourceComponent() == tree1) {
                        // use item id from tree1 as caption
                        tree2.setItemCaption(itemId, (String) tr.getItemId());
                        // if comes from tree1, move subtree too
                        copySubTree(tr.getItemId(), itemId);
                    } else if (tr.getSourceComponent() == table) {
                        // comes from table, override caption with name
                        String name = (String) table.getItem(tr.getItemId())
                                .getItemProperty("Name").getValue();
                        tree2.setItemCaption(itemId, name);
                    } else if (tr.getSourceComponent() == tree2) {
                        tree2.setItemCaption(itemId,
                                tree2.getItemCaption(tr.getItemId()));
                    }
                }
            }

            private void copySubTree(Object itemId, Object itemIdTo) {
                Collection<?> children = tree1.getChildren(itemId);
                if (children != null) {
                    for (Object childId : children) {
                        Object newItemId = tree2.addItem();
                        tree2.setItemCaption(newItemId, (String) childId);
                        tree2.setParent(newItemId, itemIdTo);
                        copySubTree(childId, newItemId);
                    }
                }
            }

            @Override
            public AcceptCriterion getAcceptCriterion() {
                return AcceptItem.ALL;
            }
        };

        tree2.setDropHandler(dropHandler);

        /*
         * Finally add two windows with DragDropPane. First accept anything,
         * second has server side accept rule to allow only drops from Tree1.
         * Check the code in implementing classes.
         */
        Window acceptAnyThing = new AcceptAnythingWindow();
        Window acceptFromTree1viaServerCheck = new AcceptFromComponent(tree1);

        w.addWindow(acceptAnyThing);
        acceptAnyThing.setPositionY(450);
        acceptAnyThing.setPositionX(150);
        w.addWindow(acceptFromTree1viaServerCheck);
        acceptFromTree1viaServerCheck.setPositionY(450);
        acceptFromTree1viaServerCheck.setPositionX(450);

    }

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

        PersonContainer testData = PersonContainer.createWithTestData();

        for (int i = 0; i < 10; 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 final static ThemeResource FOLDER = new ThemeResource(
            "../runo/icons/16/folder.png");
    private final static ThemeResource DOC = new ThemeResource(
            "../runo/icons/16/document.png");

    private void popuplateTrees() {
        HierarchicalContainer hc = new HierarchicalContainer();
        hc.addContainerProperty("icon", Resource.class, DOC);
        Item addItem = hc.addItem("Fats");
        addItem.getItemProperty("icon").setValue(FOLDER);
        hc.addItem("Tarja");
        hc.setParent("Tarja", "Fats");
        hc.setChildrenAllowed("Tarja", false);
        addItem = hc.addItem("Thins");
        addItem.getItemProperty("icon").setValue(FOLDER);
        addItem = hc.addItem("Anorectic");
        addItem.getItemProperty("icon").setValue(FOLDER);
        hc.setParent("Anorectic", "Thins");
        addItem = hc.addItem("Normal weighted");
        addItem.getItemProperty("icon").setValue(FOLDER);

        tree1.setContainerDataSource(hc);
        tree1.setItemIconPropertyId("icon");

        tree2.setContainerDataSource(new HierarchicalContainer());

        tree2.addItem("/");

    }

    @Override
    protected String getDescription() {
        return "dd";
    }

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

}