blob: ec8443e23740a71e3de10e24345d1a41f5b9b6d1 (
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
|
package com.vaadin.ui;
import java.util.Map;
import com.vaadin.event.ComponentTransferable;
import com.vaadin.event.DataBindedTransferable;
import com.vaadin.event.Transferable;
import com.vaadin.event.dd.DropEvent;
import com.vaadin.event.dd.DropHandler;
import com.vaadin.event.dd.DropTarget;
import com.vaadin.event.dd.TargetDetails;
import com.vaadin.event.dd.TargetDetailsImpl;
import com.vaadin.event.dd.acceptCriteria.AcceptAll;
import com.vaadin.event.dd.acceptCriteria.AcceptCriterion;
import com.vaadin.terminal.PaintException;
import com.vaadin.terminal.PaintTarget;
import com.vaadin.terminal.gwt.client.MouseEventDetails;
/**
* Test class that implements various component related Drag and Drop features.
*
* TODO consider implementing this kind of general purpose layout class:
*
* - extend simple component (CssLayout or CustomComponent, instead of absolute
* layout)
*
* - implement both drag source and drop handler with server side api
*
* - implement html5 drop target etc
*
* - include a lots of details for drop event (coordinates & sizes of drop
* position and widget/Paintable hierarchy up to drop handler)
*
* This way we could have one rather complex dd component that could be used (by
* wrapping layouts) in most common situations with server side api. Core
* layouts wouldn't need changes (and have regression risk/ performance
* penalty).
*
*/
@SuppressWarnings("serial")
@ClientWidget(com.vaadin.terminal.gwt.client.ui.VDragDropPane.class)
public class DragDropPane extends AbsoluteLayout implements DropTarget {
private DropHandler dropHandler;
public DragDropPane(DropHandler dropHandler) {
setWidth("400px");
setHeight("300px");
if (dropHandler == null) {
this.dropHandler = new ImportPrettyMuchAnything();
} else {
this.dropHandler = dropHandler;
}
}
public DragDropPane() {
this(null);
}
@Override
public void paintContent(PaintTarget target) throws PaintException {
super.paintContent(target);
dropHandler.getAcceptCriterion().paint(target);
}
public DropHandler getDropHandler() {
return dropHandler;
}
public static class ImportPrettyMuchAnything implements DropHandler {
public void drop(DropEvent event) {
DragDropPane pane = (DragDropPane) event.getDropTargetData()
.getTarget();
DragEventDetails ed = (DragEventDetails) event.getDropTargetData();
Transferable transferable = event.getTransferable();
if (transferable instanceof ComponentTransferable) {
ComponentTransferable ctr = (ComponentTransferable) transferable;
Component component = ctr.getSourceComponent();
if (component.getParent() != pane) {
if (transferable instanceof DataBindedTransferable) {
// Item has been dragged, construct a Label from
// Item id
Label l = new Label();
l.setSizeUndefined();
l.setValue("ItemId : "
+ ((DataBindedTransferable) transferable)
.getItemId());
pane.addComponent(l);
component = l;
} else {
// we have a component that is been dragged, add
// it
// to
// this
pane.addComponent(component);
}
Integer left = ed.getAbsoluteLeft();
Integer top = ed.getAbsoluteTop();
MouseEventDetails eventDetails = ed.getMouseEvent();
int clientX = eventDetails.getClientX();
int clientY = eventDetails.getClientY();
try {
pane.getPosition(component).setTopValue(clientY - top);
pane.getPosition(component)
.setLeftValue(clientX - left);
} catch (Exception e) {
// TODO: handle exception
}
} else {
// drag ended inside the this Pane
MouseEventDetails start = ed.getMouseDownEvent();
MouseEventDetails eventDetails = ed.getMouseEvent();
int deltaX = eventDetails.getClientX() - start.getClientX();
int deltaY = eventDetails.getClientY() - start.getClientY();
ComponentPosition p = pane.getPosition(component);
p.setTopValue(p.getTopValue() + deltaY);
p.setLeftValue(p.getLeftValue() + deltaX);
}
} else {
// drag coming outside of Vaadin
String object = (String) transferable.getData("text/plain");
String content = (String) transferable.getData("fileContents");
Label l = new Label();
l.setCaption("Generated from HTML5 drag:");
if (object != null) {
l.setValue(object);
} else {
l.setValue("HTML5 dd");
}
l.setDescription(content);
l.setSizeUndefined();
pane.addComponent(l);
}
return;
}
public AcceptCriterion getAcceptCriterion() {
return AcceptAll.get();
}
}
class DragEventDetails extends TargetDetailsImpl {
public DragEventDetails(Map<String, Object> rawVariables) {
super(rawVariables);
}
public Integer getAbsoluteTop() {
return (Integer) getData("absoluteTop");
}
public Integer getAbsoluteLeft() {
return (Integer) getData("absoluteLeft");
}
public MouseEventDetails getMouseDownEvent() {
return MouseEventDetails.deSerialize((String) getData("mouseDown"));
}
public MouseEventDetails getMouseEvent() {
return MouseEventDetails
.deSerialize((String) getData("mouseEvent"));
}
}
public TargetDetails translateDragDropDetails(
Map<String, Object> clientVariables) {
return new DragEventDetails(clientVariables);
}
public void setDropHandler(DropHandler dropHandler2) {
dropHandler = dropHandler2;
requestRepaint();
}
}
|