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
|
package com.vaadin.tests.dd;
import java.util.Collection;
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.Or;
import com.vaadin.server.VaadinRequest;
import com.vaadin.shared.ui.dd.VerticalDropLocation;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.ui.AbstractSelect;
import com.vaadin.ui.Tree;
import com.vaadin.ui.Tree.TreeDragMode;
import com.vaadin.ui.Tree.TreeTargetDetails;
/**
* DD playground. Better quality example/prototype codes in {@link DDTest2}.
*/
public class DDTest8 extends AbstractTestUI {
@Override
protected void setup(VaadinRequest request) {
final Tree tree = new Tree(
"Tree with criteria from AbstractSelect (OverItem, ContainsItem). Foo can be dragged anywhere, anything can be dropped on Foo or Bar. Bar5 subtree is also valid drop target.");
final HierarchicalContainer container = new HierarchicalContainer();
tree.setContainerDataSource(container);
tree.addItem("Foo");
tree.addItem("Bar");
tree.addItem("Bar1");
tree.addItem("Bar2");
tree.addItem("Bar3");
tree.addItem("Bar4");
tree.addItem("Bar5");
tree.addItem("Child");
tree.setParent("Child", "Foo");
tree.setSizeFull();
tree.setDragMode(TreeDragMode.NODE);
tree.setDropHandler(getDropHandler(tree, container));
getLayout().setSizeFull();
getLayout().getParent().setSizeFull();
addComponent(tree);
}
/**
* Moves items in tree (and could work in Table too). Also supports
* "building" tree.
*
* TODO fix algorithm, broken in some cases.
*
* @param tree
* @param container
* @return drop handler
*/
private DropHandler getDropHandler(final Tree tree,
final HierarchicalContainer container) {
return new DropHandler() {
@SuppressWarnings({ "unused", "unchecked" })
private void populateSubTree(HierarchicalContainer idx,
HierarchicalContainer subtree, Object itemId) {
Collection<?> children = subtree.getChildren(itemId);
if (children != null) {
for (Object childId : children) {
Item addItem = idx.addItem(childId);
if (addItem != null) {
// did not exist, populate properties
Item item = subtree.getItem(itemId);
Collection<?> itemPropertyIds = item
.getItemPropertyIds();
for (Object propId : itemPropertyIds) {
addItem.getItemProperty(propId)
.setValue(
item.getItemProperty(propId)
.getValue());
}
}
idx.setParent(childId, itemId);
populateSubTree(idx, subtree, childId);
}
}
}
@SuppressWarnings("unused")
private HierarchicalContainer getSubTree(HierarchicalContainer idx,
Object itemId) {
HierarchicalContainer hierarchicalContainer = new HierarchicalContainer();
Collection<?> containerPropertyIds = idx
.getContainerPropertyIds();
for (Object object : containerPropertyIds) {
hierarchicalContainer.addContainerProperty(object,
idx.getType(object), null);
}
hierarchicalContainer.addItem(itemId);
copyChildren(idx, hierarchicalContainer, itemId);
return hierarchicalContainer;
}
@SuppressWarnings("unchecked")
private void copyChildren(HierarchicalContainer source,
HierarchicalContainer target, Object itemId) {
Collection<?> children = source.getChildren(itemId);
if (children != null) {
for (Object childId : children) {
Item item = source.getItem(childId);
Item addedItem = target.addItem(childId);
target.setParent(childId, itemId);
Collection<?> itemPropertyIds = item
.getItemPropertyIds();
for (Object propertyId : itemPropertyIds) {
addedItem.getItemProperty(propertyId)
.setValue(
item.getItemProperty(propertyId)
.getValue());
}
copyChildren(source, target, childId);
}
}
}
@Override
public void drop(DragAndDropEvent event) {
TreeTargetDetails details = (TreeTargetDetails) event
.getTargetDetails();
// TODO set properties, so same sorter could be used in Table
Transferable transferable = event.getTransferable();
if (transferable instanceof DataBoundTransferable) {
DataBoundTransferable transferrable2 = (DataBoundTransferable) transferable;
Object itemId = transferrable2.getItemId();
Object itemIdOver = details.getItemIdOver();
// TODO could use the "folder" node id to make the drop
// logic simpler
Object itemIdInto = details.getItemIdInto();
VerticalDropLocation dropLocation = details
.getDropLocation();
Object itemIdAfter = details.getItemIdAfter();
if (itemIdOver.equals(itemIdInto)) { // directly on a node
container.setParent(itemId, itemIdOver);
return;
}
container.setParent(itemId, itemIdInto);
if (dropLocation == null) {
System.err.println("No detail of drop place available");
}
container.moveAfterSibling(itemId, itemIdAfter);
}
return;
}
@Override
public AcceptCriterion getAcceptCriterion() {
return new Or(new AbstractSelect.TargetItemIs(tree, "Foo",
"Bar"), new AbstractSelect.AcceptItem(tree, "Foo"),
tree.new TargetInSubtree("Bar5") //
);
}
};
}
@Override
protected String getTestDescription() {
return "Random DD tests";
}
@Override
protected Integer getTicketNumber() {
return 119;
}
}
|