aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/dd/TreeDragStart.java
blob: 3616ea823334bbd526b4f52b02ffdca4c2c9e638 (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
package com.vaadin.tests.dd;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import com.vaadin.event.DataBoundTransferable;
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.Not;
import com.vaadin.event.dd.acceptcriteria.Or;
import com.vaadin.shared.ui.dd.VerticalDropLocation;
import com.vaadin.tests.components.TestBase;
import com.vaadin.ui.CheckBox;
import com.vaadin.v7.data.Container;
import com.vaadin.v7.data.util.BeanItem;
import com.vaadin.v7.data.util.BeanItemContainer;
import com.vaadin.v7.data.util.HierarchicalContainer;
import com.vaadin.v7.ui.AbstractSelect.AbstractSelectTargetDetails;
import com.vaadin.v7.ui.AbstractSelect.VerticalLocationIs;
import com.vaadin.v7.ui.Table;
import com.vaadin.v7.ui.Table.TableDragMode;
import com.vaadin.v7.ui.Tree;
import com.vaadin.v7.ui.Tree.TreeDragMode;
import com.vaadin.v7.ui.Tree.TreeTargetDetails;

public class TreeDragStart extends TestBase {

    @Override
    protected void setup() {
        final Tree tree = new Tree("Inventory");

        CheckBox checkBox = new CheckBox("Enabled");
        checkBox.setImmediate(true);
        checkBox.setValue(true);
        checkBox.addValueChangeListener(
                event -> tree.setEnabled(!tree.isEnabled()));
        addComponent(checkBox);
        checkBox = new CheckBox("Drag start");
        checkBox.setImmediate(true);
        checkBox.setValue(true);
        checkBox.addValueChangeListener(event -> {
            if (event.getValue()) {
                tree.setDragMode(TreeDragMode.NODE);
            } else {
                tree.setDragMode(TreeDragMode.NONE);
            }
        });
        addComponent(checkBox);

        tree.setContainerDataSource(createTreeContent());
        tree.setItemCaptionMode(Tree.ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID);
        for (Object item : tree.getItemIds().toArray()) {
            tree.setItemCaption(item, (String) ((BeanItem<?>) item)
                    .getItemProperty("name").getValue());
        }
        getLayout().addComponent(tree);

        // Expand all items
        for (Iterator<?> it = tree.rootItemIds().iterator(); it.hasNext();) {
            tree.expandItemsRecursively(it.next());
        }

        // Set the tree in drag source mode
        tree.setDragMode(TreeDragMode.NODE);

        // Allow the tree to receive drag drops and handle them
        tree.setDropHandler(new DropHandler() {
            @Override
            public AcceptCriterion getAcceptCriterion() {
                // Accept drops in the middle of container items
                // and below and above all items.
                return new Or(Tree.TargetItemAllowsChildren.get(),
                        new Not(VerticalLocationIs.MIDDLE));
            }

            @Override
            public void drop(DragAndDropEvent event) {
                // Wrapper for the object that is dragged
                DataBoundTransferable t = (DataBoundTransferable) event
                        .getTransferable();

                TreeTargetDetails target = (TreeTargetDetails) event
                        .getTargetDetails();

                // Get ids of the dragged item and the target item
                Object sourceItemId = t.getData("itemId");
                Object targetItemId = target.getItemIdOver();

                // On which side of the target the item was dropped
                VerticalDropLocation location = target.getDropLocation();

                HierarchicalContainer container = (HierarchicalContainer) tree
                        .getContainerDataSource();

                BeanItem<?> beanItem = null;
                if (sourceItemId instanceof BeanItem<?>) {
                    beanItem = (BeanItem<?>) sourceItemId;
                } else if (sourceItemId instanceof InventoryObject) {
                    beanItem = new BeanItem<InventoryObject>(
                            (InventoryObject) sourceItemId);
                }

                // Remove the item from the source container and
                // add it to the tree's container
                Container sourceContainer = t.getSourceContainer();
                sourceContainer.removeItem(sourceItemId);
                tree.addItem(beanItem);
                InventoryObject bean = (InventoryObject) beanItem.getBean();
                tree.setChildrenAllowed(beanItem, bean.isContainer());

                // Drop right on an item -> make it a child
                if (location == VerticalDropLocation.MIDDLE) {
                    tree.setParent(beanItem, targetItemId);
                } else if (location == VerticalDropLocation.TOP) {
                    Object parentId = container.getParent(targetItemId);
                    tree.setParent(beanItem, parentId);
                    container.moveAfterSibling(beanItem, targetItemId);
                    container.moveAfterSibling(targetItemId, beanItem);
                }

                // Drop below another item -> make it next
                else if (location == VerticalDropLocation.BOTTOM) {
                    Object parentId = container.getParent(targetItemId);
                    tree.setParent(beanItem, parentId);
                    container.moveAfterSibling(beanItem, targetItemId);
                }

                tree.setItemCaption(beanItem, bean.getName());
            }
        });

        // Have a table that allows dragging from
        final Table table = new Table("Inventory List");
        table.setDragMode(TableDragMode.ROW);

        // Initialize the table container
        ArrayList<InventoryObject> collection = new ArrayList<InventoryObject>();
        collection.add(new InventoryObject("Dummy Item", 0.0, false));
        final BeanItemContainer<InventoryObject> tableContainer = new BeanItemContainer<InventoryObject>(
                collection);
        table.setContainerDataSource(tableContainer);
        table.setVisibleColumns(new String[] { "name", "weight" });
        table.removeAllItems();

        // Allow the table to receive drops and handle them
        table.setDropHandler(new DropHandler() {
            @Override
            public AcceptCriterion getAcceptCriterion() {
                return new Not(VerticalLocationIs.MIDDLE);
            }

            @Override
            public void drop(DragAndDropEvent event) {
                // Wrapper for the object that is dragged
                DataBoundTransferable t = (DataBoundTransferable) event
                        .getTransferable();

                // Make sure the drag source is the same tree
                if (t.getSourceComponent() != tree
                        && t.getSourceComponent() != table) {
                    return;
                }

                AbstractSelectTargetDetails target = (AbstractSelectTargetDetails) event
                        .getTargetDetails();

                // Get ids of the dragged item and the target item
                Object sourceItemId = t.getData("itemId");
                Object targetItemId = target.getItemIdOver();

                // Do not allow drop on the item itself
                if (sourceItemId.equals(targetItemId)) {
                    return;
                }

                InventoryObject bean = null;
                if (sourceItemId instanceof BeanItem<?>) {
                    bean = (InventoryObject) ((BeanItem<?>) sourceItemId)
                            .getBean();
                } else if (sourceItemId instanceof InventoryObject) {
                    bean = (InventoryObject) sourceItemId;
                }

                // Remove the item from the source container
                t.getSourceContainer().removeItem(sourceItemId);

                // On which side of the target the item was dropped
                VerticalDropLocation location = target.getDropLocation();

                // The table was empty or otherwise not on an item
                if (targetItemId == null) {
                    tableContainer.addItem(bean); // Add to the end
                } else if (location == VerticalDropLocation.TOP) {
                    tableContainer.addItemAt(
                            tableContainer.indexOfId(targetItemId), bean);
                } else if (location == VerticalDropLocation.BOTTOM) {
                    tableContainer.addItemAfter(targetItemId, bean);
                }
            }
        });
        getLayout().addComponent(table);
    }

    public class InventoryObject implements Serializable {
        private static final long serialVersionUID = -8943498783302996516L;

        String name;
        double weight;
        boolean container;

        public InventoryObject(String name, double weight, boolean container) {
            this.name = name;
            this.weight = weight;
            this.container = container;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public double getWeight() {
            return weight;
        }

        public void setWeight(double weight) {
            this.weight = weight;
        }

        public boolean isContainer() {
            return container;
        }

        public void setContainer(boolean container) {
            this.container = container;
        }
    }

    HashMap<String, InventoryObject> inventoryStore = new HashMap<String, InventoryObject>();

    public HierarchicalContainer createTreeContent() {
        final Object[] inventory = new Object[] {
                new InventoryObject("root", 0.0, true),
                new InventoryObject("+5 Quarterstaff (blessed)", 3.5, false),
                new InventoryObject("+3 Elven Dagger (blessed)", 0.2, false),
                new InventoryObject("+5 Helmet (greased)", 1.5, false),
                new Object[] { new InventoryObject("Sack", 0.2, true),
                        new InventoryObject("Pick-Axe", 2.5, false),
                        new InventoryObject("Lock Pick", 0.1, false),
                        new InventoryObject("Tinning Kit", 0.5, false),
                        new InventoryObject("Potion of Healing (blessed)", 0.7,
                                false), },
                new Object[] { new InventoryObject("Bag of Holding", 0.1, true),
                        new InventoryObject("Magic Marker", 0.05, false),
                        new InventoryObject("Can of Grease (blessed)", 0.5,
                                false), },
                new Object[] { new InventoryObject("Chest", 10.0, true),
                        new InventoryObject("Scroll of Identify", 0.1, false),
                        new InventoryObject("Scroll of Genocide", 0.1, false),
                        new InventoryObject("Towel", 0.3, false),
                        new Object[] {
                                new InventoryObject("Large Box", 8.0, true),
                                new InventoryObject("Figurine of Vaadin", 0.4,
                                        false),
                                new InventoryObject("Expensive Camera", 1.5,
                                        false), },
                        new InventoryObject("Tin Opener", 0.02, false), }, };

        HierarchicalContainer container = new HierarchicalContainer();

        new Object() {
            public void put(Object[] data, Object parent,
                    HierarchicalContainer container) {
                for (int i = 1; i < data.length; i++) {
                    BeanItem<InventoryObject> item;
                    if (data[i].getClass() == InventoryObject.class) {
                        InventoryObject object = (InventoryObject) data[i];
                        item = new BeanItem<InventoryObject>(object);
                        container.addItem(item);
                        container.setParent(item, parent);
                        container.setChildrenAllowed(item, false);
                    } else {// It's an Object[]
                        Object[] sub = (Object[]) data[i];
                        InventoryObject object = (InventoryObject) sub[0];
                        item = new BeanItem<InventoryObject>(object);
                        container.addItem(item);
                        container.setParent(item, parent);

                        // Add children recursively
                        put(sub, item, container);
                    }

                    inventoryStore.put(item.getBean().getName(),
                            item.getBean());
                }
            }
        }.put(inventory, null, container);

        return container;
    }

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

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

}