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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
|
/*
@VaadinApache2LicenseForJavaFiles@
*/
package com.vaadin.terminal.gwt.client.ui.draganddropwrapper;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArrayString;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.TouchStartEvent;
import com.google.gwt.event.dom.client.TouchStartHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.xhr.client.ReadyStateChangeHandler;
import com.google.gwt.xhr.client.XMLHttpRequest;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.ComponentConnector;
import com.vaadin.terminal.gwt.client.ConnectorMap;
import com.vaadin.terminal.gwt.client.LayoutManager;
import com.vaadin.terminal.gwt.client.MouseEventDetailsBuilder;
import com.vaadin.terminal.gwt.client.Util;
import com.vaadin.terminal.gwt.client.VConsole;
import com.vaadin.terminal.gwt.client.VTooltip;
import com.vaadin.terminal.gwt.client.ValueMap;
import com.vaadin.terminal.gwt.client.ui.customcomponent.VCustomComponent;
import com.vaadin.terminal.gwt.client.ui.dd.DDUtil;
import com.vaadin.terminal.gwt.client.ui.dd.HorizontalDropLocation;
import com.vaadin.terminal.gwt.client.ui.dd.VAbstractDropHandler;
import com.vaadin.terminal.gwt.client.ui.dd.VAcceptCallback;
import com.vaadin.terminal.gwt.client.ui.dd.VDragAndDropManager;
import com.vaadin.terminal.gwt.client.ui.dd.VDragEvent;
import com.vaadin.terminal.gwt.client.ui.dd.VDropHandler;
import com.vaadin.terminal.gwt.client.ui.dd.VHasDropHandler;
import com.vaadin.terminal.gwt.client.ui.dd.VHtml5DragEvent;
import com.vaadin.terminal.gwt.client.ui.dd.VHtml5File;
import com.vaadin.terminal.gwt.client.ui.dd.VTransferable;
import com.vaadin.terminal.gwt.client.ui.dd.VerticalDropLocation;
/**
*
* Must have features pending:
*
* drop details: locations + sizes in document hierarchy up to wrapper
*
*/
public class VDragAndDropWrapper extends VCustomComponent implements
VHasDropHandler {
public static final String DRAG_START_MODE = "dragStartMode";
public static final String HTML5_DATA_FLAVORS = "html5-data-flavors";
private static final String CLASSNAME = "v-ddwrapper";
protected static final String DRAGGABLE = "draggable";
public VDragAndDropWrapper() {
super();
sinkEvents(VTooltip.TOOLTIP_EVENTS);
hookHtml5Events(getElement());
setStyleName(CLASSNAME);
addDomHandler(new MouseDownHandler() {
public void onMouseDown(MouseDownEvent event) {
if (startDrag(event.getNativeEvent())) {
event.preventDefault(); // prevent text selection
}
}
}, MouseDownEvent.getType());
addDomHandler(new TouchStartHandler() {
public void onTouchStart(TouchStartEvent event) {
if (startDrag(event.getNativeEvent())) {
/*
* Dont let eg. panel start scrolling.
*/
event.stopPropagation();
}
}
}, TouchStartEvent.getType());
sinkEvents(Event.TOUCHEVENTS);
}
@Override
public void onBrowserEvent(Event event) {
super.onBrowserEvent(event);
if (client != null) {
client.handleTooltipEvent(event, this);
}
}
/**
* Starts a drag and drop operation from mousedown or touchstart event if
* required conditions are met.
*
* @param event
* @return true if the event was handled as a drag start event
*/
private boolean startDrag(NativeEvent event) {
if (dragStartMode == WRAPPER || dragStartMode == COMPONENT) {
VTransferable transferable = new VTransferable();
transferable.setDragSource(ConnectorMap.get(client).getConnector(
VDragAndDropWrapper.this));
ComponentConnector paintable = Util.findPaintable(client,
(Element) event.getEventTarget().cast());
Widget widget = paintable.getWidget();
transferable.setData("component", paintable);
VDragEvent dragEvent = VDragAndDropManager.get().startDrag(
transferable, event, true);
transferable.setData("mouseDown", MouseEventDetailsBuilder
.buildMouseEventDetails(event).serialize());
if (dragStartMode == WRAPPER) {
dragEvent.createDragImage(getElement(), true);
} else {
dragEvent.createDragImage(widget.getElement(), true);
}
return true;
}
return false;
}
protected final static int NONE = 0;
protected final static int COMPONENT = 1;
protected final static int WRAPPER = 2;
protected final static int HTML5 = 3;
protected int dragStartMode;
ApplicationConnection client;
VAbstractDropHandler dropHandler;
private VDragEvent vaadinDragEvent;
int filecounter = 0;
Map<String, String> fileIdToReceiver;
ValueMap html5DataFlavors;
private Element dragStartElement;
protected void initDragStartMode() {
Element div = getElement();
if (dragStartMode == HTML5) {
if (dragStartElement == null) {
dragStartElement = getDragStartElement();
dragStartElement.setPropertyBoolean(DRAGGABLE, true);
VConsole.log("draggable = "
+ dragStartElement.getPropertyBoolean(DRAGGABLE));
hookHtml5DragStart(dragStartElement);
VConsole.log("drag start listeners hooked.");
}
} else {
dragStartElement = null;
if (div.hasAttribute(DRAGGABLE)) {
div.removeAttribute(DRAGGABLE);
}
}
}
protected Element getDragStartElement() {
return getElement();
}
private boolean uploading;
private ReadyStateChangeHandler readyStateChangeHandler = new ReadyStateChangeHandler() {
public void onReadyStateChange(XMLHttpRequest xhr) {
if (xhr.getReadyState() == XMLHttpRequest.DONE) {
// visit server for possible
// variable changes
client.sendPendingVariableChanges();
uploading = false;
startNextUpload();
xhr.clearOnReadyStateChange();
}
}
};
private Timer dragleavetimer;
void startNextUpload() {
Scheduler.get().scheduleDeferred(new Command() {
public void execute() {
if (!uploading) {
if (fileIds.size() > 0) {
uploading = true;
final Integer fileId = fileIds.remove(0);
VHtml5File file = files.remove(0);
final String receiverUrl = client
.translateVaadinUri(fileIdToReceiver
.remove(fileId.toString()));
ExtendedXHR extendedXHR = (ExtendedXHR) ExtendedXHR
.create();
extendedXHR
.setOnReadyStateChange(readyStateChangeHandler);
extendedXHR.open("POST", receiverUrl);
extendedXHR.postFile(file);
}
}
}
});
}
public boolean html5DragStart(VHtml5DragEvent event) {
if (dragStartMode == HTML5) {
/*
* Populate html5 payload with dataflavors from the serverside
*/
JsArrayString flavors = html5DataFlavors.getKeyArray();
for (int i = 0; i < flavors.length(); i++) {
String flavor = flavors.get(i);
event.setHtml5DataFlavor(flavor,
html5DataFlavors.getString(flavor));
}
event.setEffectAllowed("copy");
return true;
}
return false;
}
public boolean html5DragEnter(VHtml5DragEvent event) {
if (dropHandler == null) {
return true;
}
try {
if (dragleavetimer != null) {
// returned quickly back to wrapper
dragleavetimer.cancel();
dragleavetimer = null;
}
if (VDragAndDropManager.get().getCurrentDropHandler() != getDropHandler()) {
VTransferable transferable = new VTransferable();
transferable.setDragSource(ConnectorMap.get(client)
.getConnector(this));
vaadinDragEvent = VDragAndDropManager.get().startDrag(
transferable, event, false);
VDragAndDropManager.get().setCurrentDropHandler(
getDropHandler());
}
try {
event.preventDefault();
event.stopPropagation();
} catch (Exception e) {
// VConsole.log("IE9 fails");
}
return false;
} catch (Exception e) {
GWT.getUncaughtExceptionHandler().onUncaughtException(e);
return true;
}
}
public boolean html5DragLeave(VHtml5DragEvent event) {
if (dropHandler == null) {
return true;
}
try {
dragleavetimer = new Timer() {
@Override
public void run() {
// Yes, dragleave happens before drop. Makes no sense to me.
// IMO shouldn't fire leave at all if drop happens (I guess
// this
// is what IE does).
// In Vaadin we fire it only if drop did not happen.
if (vaadinDragEvent != null
&& VDragAndDropManager.get()
.getCurrentDropHandler() == getDropHandler()) {
VDragAndDropManager.get().interruptDrag();
}
}
};
dragleavetimer.schedule(350);
try {
event.preventDefault();
event.stopPropagation();
} catch (Exception e) {
// VConsole.log("IE9 fails");
}
return false;
} catch (Exception e) {
GWT.getUncaughtExceptionHandler().onUncaughtException(e);
return true;
}
}
public boolean html5DragOver(VHtml5DragEvent event) {
if (dropHandler == null) {
return true;
}
if (dragleavetimer != null) {
// returned quickly back to wrapper
dragleavetimer.cancel();
dragleavetimer = null;
}
vaadinDragEvent.setCurrentGwtEvent(event);
getDropHandler().dragOver(vaadinDragEvent);
String s = event.getEffectAllowed();
if ("all".equals(s) || s.contains("opy")) {
event.setDropEffect("copy");
} else {
event.setDropEffect(s);
}
try {
event.preventDefault();
event.stopPropagation();
} catch (Exception e) {
// VConsole.log("IE9 fails");
}
return false;
}
public boolean html5DragDrop(VHtml5DragEvent event) {
if (dropHandler == null || !currentlyValid) {
return true;
}
try {
VTransferable transferable = vaadinDragEvent.getTransferable();
JsArrayString types = event.getTypes();
for (int i = 0; i < types.length(); i++) {
String type = types.get(i);
if (isAcceptedType(type)) {
String data = event.getDataAsText(type);
if (data != null) {
transferable.setData(type, data);
}
}
}
int fileCount = event.getFileCount();
if (fileCount > 0) {
transferable.setData("filecount", fileCount);
for (int i = 0; i < fileCount; i++) {
final int fileId = filecounter++;
final VHtml5File file = event.getFile(i);
transferable.setData("fi" + i, "" + fileId);
transferable.setData("fn" + i, file.getName());
transferable.setData("ft" + i, file.getType());
transferable.setData("fs" + i, file.getSize());
queueFilePost(fileId, file);
}
}
VDragAndDropManager.get().endDrag();
vaadinDragEvent = null;
try {
event.preventDefault();
event.stopPropagation();
} catch (Exception e) {
// VConsole.log("IE9 fails");
}
return false;
} catch (Exception e) {
GWT.getUncaughtExceptionHandler().onUncaughtException(e);
return true;
}
}
protected String[] acceptedTypes = new String[] { "Text", "Url",
"text/html", "text/plain", "text/rtf" };
private boolean isAcceptedType(String type) {
for (String t : acceptedTypes) {
if (t.equals(type)) {
return true;
}
}
return false;
}
static class ExtendedXHR extends XMLHttpRequest {
protected ExtendedXHR() {
}
public final native void postFile(VHtml5File file)
/*-{
this.setRequestHeader('Content-Type', 'multipart/form-data');
this.send(file);
}-*/;
}
/**
* Currently supports only FF36 as no other browser supports natively File
* api.
*
* @param fileId
* @param data
*/
List<Integer> fileIds = new ArrayList<Integer>();
List<VHtml5File> files = new ArrayList<VHtml5File>();
private void queueFilePost(final int fileId, final VHtml5File file) {
fileIds.add(fileId);
files.add(file);
}
public VDropHandler getDropHandler() {
return dropHandler;
}
protected VerticalDropLocation verticalDropLocation;
protected HorizontalDropLocation horizontalDropLocation;
private VerticalDropLocation emphasizedVDrop;
private HorizontalDropLocation emphasizedHDrop;
/**
* Flag used by html5 dd
*/
private boolean currentlyValid;
private static final String OVER_STYLE = "v-ddwrapper-over";
public class CustomDropHandler extends VAbstractDropHandler {
@Override
public void dragEnter(VDragEvent drag) {
updateDropDetails(drag);
currentlyValid = false;
super.dragEnter(drag);
}
@Override
public void dragLeave(VDragEvent drag) {
deEmphasis(true);
dragleavetimer = null;
}
@Override
public void dragOver(final VDragEvent drag) {
boolean detailsChanged = updateDropDetails(drag);
if (detailsChanged) {
currentlyValid = false;
validate(new VAcceptCallback() {
public void accepted(VDragEvent event) {
dragAccepted(drag);
}
}, drag);
}
}
@Override
public boolean drop(VDragEvent drag) {
deEmphasis(true);
Map<String, Object> dd = drag.getDropDetails();
// this is absolute layout based, and we may want to set
// component
// relatively to where the drag ended.
// need to add current location of the drop area
int absoluteLeft = getAbsoluteLeft();
int absoluteTop = getAbsoluteTop();
dd.put("absoluteLeft", absoluteLeft);
dd.put("absoluteTop", absoluteTop);
if (verticalDropLocation != null) {
dd.put("verticalLocation", verticalDropLocation.toString());
dd.put("horizontalLocation", horizontalDropLocation.toString());
}
return super.drop(drag);
}
@Override
protected void dragAccepted(VDragEvent drag) {
currentlyValid = true;
emphasis(drag);
}
@Override
public ComponentConnector getConnector() {
return ConnectorMap.get(client).getConnector(
VDragAndDropWrapper.this);
}
public ApplicationConnection getApplicationConnection() {
return client;
}
}
protected native void hookHtml5DragStart(Element el)
/*-{
var me = this;
el.addEventListener("dragstart", function(ev) {
return me.@com.vaadin.terminal.gwt.client.ui.draganddropwrapper.VDragAndDropWrapper::html5DragStart(Lcom/vaadin/terminal/gwt/client/ui/dd/VHtml5DragEvent;)(ev);
}, false);
}-*/;
/**
* Prototype code, memory leak risk.
*
* @param el
*/
protected native void hookHtml5Events(Element el)
/*-{
var me = this;
el.addEventListener("dragenter", function(ev) {
return me.@com.vaadin.terminal.gwt.client.ui.draganddropwrapper.VDragAndDropWrapper::html5DragEnter(Lcom/vaadin/terminal/gwt/client/ui/dd/VHtml5DragEvent;)(ev);
}, false);
el.addEventListener("dragleave", function(ev) {
return me.@com.vaadin.terminal.gwt.client.ui.draganddropwrapper.VDragAndDropWrapper::html5DragLeave(Lcom/vaadin/terminal/gwt/client/ui/dd/VHtml5DragEvent;)(ev);
}, false);
el.addEventListener("dragover", function(ev) {
return me.@com.vaadin.terminal.gwt.client.ui.draganddropwrapper.VDragAndDropWrapper::html5DragOver(Lcom/vaadin/terminal/gwt/client/ui/dd/VHtml5DragEvent;)(ev);
}, false);
el.addEventListener("drop", function(ev) {
return me.@com.vaadin.terminal.gwt.client.ui.draganddropwrapper.VDragAndDropWrapper::html5DragDrop(Lcom/vaadin/terminal/gwt/client/ui/dd/VHtml5DragEvent;)(ev);
}, false);
}-*/;
public boolean updateDropDetails(VDragEvent drag) {
VerticalDropLocation oldVL = verticalDropLocation;
verticalDropLocation = DDUtil.getVerticalDropLocation(getElement(),
drag.getCurrentGwtEvent(), 0.2);
drag.getDropDetails().put("verticalLocation",
verticalDropLocation.toString());
HorizontalDropLocation oldHL = horizontalDropLocation;
horizontalDropLocation = DDUtil.getHorizontalDropLocation(getElement(),
drag.getCurrentGwtEvent(), 0.2);
drag.getDropDetails().put("horizontalLocation",
horizontalDropLocation.toString());
if (oldHL != horizontalDropLocation || oldVL != verticalDropLocation) {
return true;
} else {
return false;
}
}
protected void deEmphasis(boolean doLayout) {
if (emphasizedVDrop != null) {
VDragAndDropWrapper.setStyleName(getElement(), OVER_STYLE, false);
VDragAndDropWrapper.setStyleName(getElement(), OVER_STYLE + "-"
+ emphasizedVDrop.toString().toLowerCase(), false);
VDragAndDropWrapper.setStyleName(getElement(), OVER_STYLE + "-"
+ emphasizedHDrop.toString().toLowerCase(), false);
}
if (doLayout) {
notifySizePotentiallyChanged();
}
}
private void notifySizePotentiallyChanged() {
LayoutManager.get(client).setNeedsMeasure(
ConnectorMap.get(client).getConnector(getElement()));
}
protected void emphasis(VDragEvent drag) {
deEmphasis(false);
VDragAndDropWrapper.setStyleName(getElement(), OVER_STYLE, true);
VDragAndDropWrapper.setStyleName(getElement(), OVER_STYLE + "-"
+ verticalDropLocation.toString().toLowerCase(), true);
VDragAndDropWrapper.setStyleName(getElement(), OVER_STYLE + "-"
+ horizontalDropLocation.toString().toLowerCase(), true);
emphasizedVDrop = verticalDropLocation;
emphasizedHDrop = horizontalDropLocation;
// TODO build (to be an example) an emphasis mode where drag image
// is fitted before or after the content
notifySizePotentiallyChanged();
}
}
|