1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.vaadin.ui;
17
18 import java.io.OutputStream;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.Iterator;
22 import java.util.LinkedHashMap;
23 import java.util.Map;
24 import java.util.Map.Entry;
25 import java.util.Set;
26
27 import org.jsoup.nodes.Element;
28
29 import com.vaadin.event.Transferable;
30 import com.vaadin.event.TransferableImpl;
31 import com.vaadin.event.dd.DragSource;
32 import com.vaadin.event.dd.DropHandler;
33 import com.vaadin.event.dd.DropTarget;
34 import com.vaadin.event.dd.TargetDetails;
35 import com.vaadin.event.dd.TargetDetailsImpl;
36 import com.vaadin.server.PaintException;
37 import com.vaadin.server.PaintTarget;
38 import com.vaadin.server.StreamVariable;
39 import com.vaadin.shared.MouseEventDetails;
40 import com.vaadin.shared.ui.dd.HorizontalDropLocation;
41 import com.vaadin.shared.ui.dd.VerticalDropLocation;
42 import com.vaadin.shared.ui.draganddropwrapper.DragAndDropWrapperConstants;
43 import com.vaadin.shared.ui.draganddropwrapper.DragAndDropWrapperServerRpc;
44 import com.vaadin.shared.ui.draganddropwrapper.DragAndDropWrapperState;
45 import com.vaadin.ui.declarative.DesignContext;
46 import com.vaadin.ui.dnd.DragSourceExtension;
47 import com.vaadin.ui.dnd.DropTargetExtension;
48
49
50
51
52
53
54 @SuppressWarnings("serial")
55 @Deprecated
56 public class DragAndDropWrapper extends CustomComponent
57 implements DropTarget, DragSource, LegacyComponent {
58
59
60
61
62
63
64 @Deprecated
65 public class WrapperTransferable extends TransferableImpl {
66
67 private Html5File[] files;
68
69 public WrapperTransferable(Component sourceComponent,
70 Map<String, Object> rawVariables) {
71 super(sourceComponent, rawVariables);
72 Integer fc = (Integer) rawVariables.get("filecount");
73 if (fc != null) {
74 files = new Html5File[fc];
75 for (int i = 0; i < fc; i++) {
76 Html5File file = new Html5File(
77 (String) rawVariables.get("fn" + i),
78 ((Double) rawVariables.get("fs" + i)).longValue(),
79 (String) rawVariables.get("ft" + i));
80 String id = (String) rawVariables.get("fi" + i);
81 files[i] = file;
82 receivers.put(id, new ProxyReceiver(id, file));
83 markAsDirty();
84 }
85 }
86 }
87
88
89
90
91
92
93
94 public Component getDraggedComponent() {
95 Component object = (Component) getData("component");
96 return object;
97 }
98
99
100
101
102 public MouseEventDetails getMouseDownEvent() {
103 return MouseEventDetails.deSerialize((String) getData("mouseDown"));
104 }
105
106 public Html5File[] getFiles() {
107 return files;
108 }
109
110 public String getText() {
111 String data = (String) getData("Text");
112 if (data == null) {
113
114 data = (String) getData("text/plain");
115 }
116 return data;
117 }
118
119 public String getHtml() {
120 String data = (String) getData("Html");
121 if (data == null) {
122
123 data = (String) getData("text/html");
124 }
125 return data;
126 }
127
128 }
129
130 private final DragAndDropWrapperServerRpc rpc = () -> {
131
132 };
133
134 private Map<String, ProxyReceiver> receivers = new HashMap<>();
135
136 public class WrapperTargetDetails extends TargetDetailsImpl {
137
138 public WrapperTargetDetails(Map<String, Object> rawDropData) {
139 super(rawDropData, DragAndDropWrapper.this);
140 }
141
142
143
144
145 public Integer getAbsoluteLeft() {
146 return (Integer) getData("absoluteLeft");
147 }
148
149
150
151
152
153 public Integer getAbsoluteTop() {
154 return (Integer) getData("absoluteTop");
155 }
156
157
158
159
160 public VerticalDropLocation getVerticalDropLocation() {
161 return VerticalDropLocation
162 .valueOf((String) getData("verticalLocation"));
163 }
164
165
166
167
168
169 public HorizontalDropLocation getHorizontalDropLocation() {
170 return HorizontalDropLocation
171 .valueOf((String) getData("horizontalLocation"));
172 }
173
174 }
175
176 public enum DragStartMode {
177
178
179
180 NONE,
181
182
183
184 COMPONENT,
185
186
187
188 WRAPPER,
189
190
191
192
193
194
195
196 HTML5,
197
198
199
200
201
202 COMPONENT_OTHER,
203 }
204
205 private final Map<String, Object> html5DataFlavors = new LinkedHashMap<>();
206 private DragStartMode dragStartMode = DragStartMode.NONE;
207 private Component dragImageComponent = null;
208
209 private Set<String> sentIds = new HashSet<>();
210
211
212
213
214
215
216
217 @Deprecated
218 public DragAndDropWrapper() {
219 super();
220 registerRpc(rpc);
221 }
222
223
224
225
226
227
228
229 public DragAndDropWrapper(Component root) {
230 this();
231 setCompositionRoot(root);
232 }
233
234
235
236
237
238
239
240
241
242
243
244
245 public void setHTML5DataFlavor(String type, Object value) {
246 html5DataFlavors.put(type, value);
247 markAsDirty();
248 }
249
250 @Override
251 public void changeVariables(Object source, Map<String, Object> variables) {
252
253 }
254
255 @Override
256 public void paintContent(PaintTarget target) throws PaintException {
257 target.addAttribute(DragAndDropWrapperConstants.DRAG_START_MODE,
258 dragStartMode.ordinal());
259
260 if (dragStartMode.equals(DragStartMode.COMPONENT_OTHER)) {
261 if (dragImageComponent != null) {
262 target.addAttribute(
263 DragAndDropWrapperConstants.DRAG_START_COMPONENT_ATTRIBUTE,
264 dragImageComponent.getConnectorId());
265 } else {
266 throw new IllegalArgumentException(
267 "DragStartMode.COMPONENT_OTHER set but no component "
268 + "was defined. Please set a component using DragAnd"
269 + "DropWrapper.setDragStartComponent(Component).");
270 }
271 }
272 if (getDropHandler() != null) {
273 getDropHandler().getAcceptCriterion().paint(target);
274 }
275 if (receivers != null && receivers.size() > 0) {
276 for (Iterator<Entry<String, ProxyReceiver>> it = receivers
277 .entrySet().iterator(); it.hasNext();) {
278 Entry<String, ProxyReceiver> entry = it.next();
279 String id = entry.getKey();
280 ProxyReceiver proxyReceiver = entry.getValue();
281 Html5File html5File = proxyReceiver.file;
282 if (html5File.getStreamVariable() != null) {
283 if (!sentIds.contains(id)) {
284 target.addVariable(this, "rec-" + id,
285 new ProxyReceiver(id, html5File));
286
287
288
289
290
291
292
293
294
295
296 sentIds.add(id);
297
298
299
300 }
301 } else {
302
303 target.addVariable(this, "rec-" + id, (String) null);
304
305 it.remove();
306 }
307 }
308 }
309 target.addAttribute(DragAndDropWrapperConstants.HTML5_DATA_FLAVORS,
310 html5DataFlavors);
311 }
312
313 private DropHandler dropHandler;
314
315 @Override
316 public DropHandler getDropHandler() {
317 return dropHandler;
318 }
319
320 public void setDropHandler(DropHandler dropHandler) {
321 this.dropHandler = dropHandler;
322 markAsDirty();
323 }
324
325 @Override
326 public TargetDetails translateDropTargetDetails(
327 Map<String, Object> clientVariables) {
328 return new WrapperTargetDetails(clientVariables);
329 }
330
331 @Override
332 public Transferable getTransferable(
333 final Map<String, Object> rawVariables) {
334 return new WrapperTransferable(this, rawVariables);
335 }
336
337 public void setDragStartMode(DragStartMode dragStartMode) {
338 this.dragStartMode = dragStartMode;
339 markAsDirty();
340 }
341
342 public DragStartMode getDragStartMode() {
343 return dragStartMode;
344 }
345
346
347
348
349
350
351
352 public void setDragImageComponent(Component dragImageComponent) {
353 this.dragImageComponent = dragImageComponent;
354 markAsDirty();
355 }
356
357
358
359
360
361
362
363 public Component getDragImageComponent() {
364 return dragImageComponent;
365 }
366
367 final class ProxyReceiver implements StreamVariable {
368
369 private final String id;
370 private Html5File file;
371
372 public ProxyReceiver(String id, Html5File file) {
373 this.id = id;
374 this.file = file;
375 }
376
377 private boolean listenProgressOfUploadedFile;
378
379 @Override
380 public OutputStream getOutputStream() {
381 if (file.getStreamVariable() == null) {
382 return null;
383 }
384 return file.getStreamVariable().getOutputStream();
385 }
386
387 @Override
388 public boolean listenProgress() {
389 return file.getStreamVariable().listenProgress();
390 }
391
392 @Override
393 public void onProgress(StreamingProgressEvent event) {
394 file.getStreamVariable()
395 .onProgress(new ReceivingEventWrapper(event));
396 }
397
398 @Override
399 public void streamingStarted(StreamingStartEvent event) {
400 listenProgressOfUploadedFile = file.getStreamVariable() != null;
401 if (listenProgressOfUploadedFile) {
402 file.getStreamVariable()
403 .streamingStarted(new ReceivingEventWrapper(event));
404 }
405
406 receivers.remove(id);
407 sentIds.remove(id);
408 }
409
410 @Override
411 public void streamingFinished(StreamingEndEvent event) {
412 if (listenProgressOfUploadedFile) {
413 file.getStreamVariable()
414 .streamingFinished(new ReceivingEventWrapper(event));
415 }
416 }
417
418 @Override
419 public void streamingFailed(final StreamingErrorEvent event) {
420 if (listenProgressOfUploadedFile) {
421 file.getStreamVariable()
422 .streamingFailed(new ReceivingEventWrapper(event));
423 }
424 }
425
426 @Override
427 public boolean isInterrupted() {
428 return file.getStreamVariable().isInterrupted();
429 }
430
431
432
433
434
435
436
437 class ReceivingEventWrapper implements StreamingErrorEvent,
438 StreamingEndEvent, StreamingStartEvent, StreamingProgressEvent {
439
440 private final StreamingEvent wrappedEvent;
441
442 ReceivingEventWrapper(StreamingEvent e) {
443 wrappedEvent = e;
444 }
445
446 @Override
447 public String getMimeType() {
448 return file.getType();
449 }
450
451 @Override
452 public String getFileName() {
453 return file.getFileName();
454 }
455
456 @Override
457 public long getContentLength() {
458 return file.getFileSize();
459 }
460
461 public StreamVariable getReceiver() {
462 return ProxyReceiver.this;
463 }
464
465 @Override
466 public Exception getException() {
467 if (wrappedEvent instanceof StreamingErrorEvent) {
468 return ((StreamingErrorEvent) wrappedEvent).getException();
469 }
470 return null;
471 }
472
473 @Override
474 public long getBytesReceived() {
475 return wrappedEvent.getBytesReceived();
476 }
477
478
479
480
481
482 @Override
483 public void disposeStreamVariable() {
484
485 }
486 }
487
488 }
489
490 @Override
491 public void readDesign(Element design, DesignContext designContext) {
492 super.readDesign(design, designContext);
493
494 for (Element child : design.children()) {
495 Component component = designContext.readDesign(child);
496 if (getDragStartMode() == DragStartMode.COMPONENT_OTHER
497 && child.hasAttr(":drag-image")) {
498 setDragImageComponent(component);
499 } else if (getCompositionRoot() == null) {
500 setCompositionRoot(component);
501 }
502 }
503 }
504
505 @Override
506 public void writeDesign(Element design, DesignContext designContext) {
507 super.writeDesign(design, designContext);
508
509 design.appendChild(designContext.createElement(getCompositionRoot()));
510 if (getDragStartMode() == DragStartMode.COMPONENT_OTHER) {
511 Element child = designContext
512 .createElement(getDragImageComponent());
513 child.attr(":drag-image", true);
514 design.appendChild(child);
515 }
516 }
517
518 @Override
519 protected DragAndDropWrapperState getState() {
520 return (DragAndDropWrapperState) super.getState();
521 }
522
523 @Override
524 protected DragAndDropWrapperState getState(boolean markAsDirty) {
525 return (DragAndDropWrapperState) super.getState(markAsDirty);
526 }
527 }