You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VDragAndDropManager.java 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.ui.dd;
  17. import com.google.gwt.core.client.GWT;
  18. import com.google.gwt.core.client.Scheduler;
  19. import com.google.gwt.core.client.Scheduler.RepeatingCommand;
  20. import com.google.gwt.dom.client.Element;
  21. import com.google.gwt.dom.client.EventTarget;
  22. import com.google.gwt.dom.client.NativeEvent;
  23. import com.google.gwt.dom.client.Node;
  24. import com.google.gwt.dom.client.Style;
  25. import com.google.gwt.dom.client.Style.Display;
  26. import com.google.gwt.dom.client.Style.Unit;
  27. import com.google.gwt.event.dom.client.KeyCodes;
  28. import com.google.gwt.event.shared.HandlerRegistration;
  29. import com.google.gwt.user.client.Command;
  30. import com.google.gwt.user.client.Event;
  31. import com.google.gwt.user.client.Event.NativePreviewEvent;
  32. import com.google.gwt.user.client.Event.NativePreviewHandler;
  33. import com.google.gwt.user.client.ui.RootPanel;
  34. import com.google.gwt.user.client.ui.Widget;
  35. import com.vaadin.client.ApplicationConnection;
  36. import com.vaadin.client.ComponentConnector;
  37. import com.vaadin.client.MouseEventDetailsBuilder;
  38. import com.vaadin.client.Profiler;
  39. import com.vaadin.client.UIDL;
  40. import com.vaadin.client.WidgetUtil;
  41. import com.vaadin.client.VConsole;
  42. import com.vaadin.client.ValueMap;
  43. import com.vaadin.client.ui.VOverlay;
  44. import com.vaadin.shared.ApplicationConstants;
  45. import com.vaadin.shared.MouseEventDetails;
  46. import com.vaadin.shared.ui.dd.DragEventType;
  47. /**
  48. * Helper class to manage the state of drag and drop event on Vaadin client
  49. * side. Can be used to implement most of the drag and drop operation
  50. * automatically via cross-browser event preview method or just as a helper when
  51. * implementing own low level drag and drop operation (like with HTML5 api).
  52. * <p>
  53. * Singleton. Only one drag and drop operation can be active anyways. Use
  54. * {@link #get()} to get instance.
  55. *
  56. * TODO cancel drag and drop if more than one touches !?
  57. */
  58. public class VDragAndDropManager {
  59. public static final String ACTIVE_DRAG_SOURCE_STYLENAME = "v-active-drag-source";
  60. private final class DefaultDragAndDropEventHandler implements
  61. NativePreviewHandler {
  62. @Override
  63. public void onPreviewNativeEvent(NativePreviewEvent event) {
  64. NativeEvent nativeEvent = event.getNativeEvent();
  65. int typeInt = event.getTypeInt();
  66. if (typeInt == Event.ONKEYDOWN) {
  67. int keyCode = event.getNativeEvent().getKeyCode();
  68. if (keyCode == KeyCodes.KEY_ESCAPE) {
  69. // end drag if ESC is hit
  70. interruptDrag();
  71. event.cancel();
  72. event.getNativeEvent().preventDefault();
  73. }
  74. // no use for handling for any key down event
  75. return;
  76. }
  77. currentDrag.setCurrentGwtEvent(nativeEvent);
  78. updateDragImagePosition();
  79. Node targetNode = Node.as(nativeEvent.getEventTarget());
  80. Element targetElement;
  81. if (Element.is(targetNode)) {
  82. targetElement = Element.as(targetNode);
  83. } else {
  84. targetElement = targetNode.getParentElement();
  85. }
  86. if (WidgetUtil.isTouchEvent(nativeEvent) || dragElement != null) {
  87. // to detect the "real" target, hide dragelement temporary and
  88. // use elementFromPoint
  89. String display = dragElement.getStyle().getDisplay();
  90. dragElement.getStyle().setDisplay(Display.NONE);
  91. try {
  92. int x = WidgetUtil.getTouchOrMouseClientX(nativeEvent);
  93. int y = WidgetUtil.getTouchOrMouseClientY(nativeEvent);
  94. // Util.browserDebugger();
  95. targetElement = WidgetUtil.getElementFromPoint(x, y);
  96. if (targetElement == null) {
  97. // ApplicationConnection.getConsole().log(
  98. // "Event on dragImage, ignored");
  99. event.cancel();
  100. nativeEvent.stopPropagation();
  101. return;
  102. } else {
  103. // ApplicationConnection.getConsole().log(
  104. // "Event on dragImage, target changed");
  105. // special handling for events over dragImage
  106. // pretty much all events are mousemove althout below
  107. // kind of happens mouseover
  108. switch (typeInt) {
  109. case Event.ONMOUSEOVER:
  110. case Event.ONMOUSEOUT:
  111. // ApplicationConnection
  112. // .getConsole()
  113. // .log(
  114. // "IGNORING proxy image event, fired because of hack or not significant");
  115. return;
  116. case Event.ONMOUSEMOVE:
  117. case Event.ONTOUCHMOVE:
  118. VDropHandler findDragTarget = findDragTarget(targetElement);
  119. if (findDragTarget != currentDropHandler) {
  120. // dragleave on old
  121. if (currentDropHandler != null) {
  122. currentDropHandler.dragLeave(currentDrag);
  123. currentDrag.getDropDetails().clear();
  124. serverCallback = null;
  125. }
  126. // dragenter on new
  127. currentDropHandler = findDragTarget;
  128. if (findDragTarget != null) {
  129. // ApplicationConnection.getConsole().log(
  130. // "DropHandler now"
  131. // + currentDropHandler
  132. // .getPaintable());
  133. }
  134. if (currentDropHandler != null) {
  135. currentDrag.setElementOver(targetElement);
  136. currentDropHandler.dragEnter(currentDrag);
  137. }
  138. } else if (findDragTarget != null) {
  139. currentDrag.setElementOver(targetElement);
  140. currentDropHandler.dragOver(currentDrag);
  141. }
  142. // prevent text selection on IE
  143. nativeEvent.preventDefault();
  144. return;
  145. default:
  146. // just update element over and let the actual
  147. // handling code do the thing
  148. // ApplicationConnection.getConsole().log(
  149. // "Target just modified on "
  150. // + event.getType());
  151. currentDrag.setElementOver(targetElement);
  152. break;
  153. }
  154. }
  155. } catch (RuntimeException e) {
  156. // ApplicationConnection.getConsole().log(
  157. // "ERROR during elementFromPoint hack.");
  158. throw e;
  159. } finally {
  160. dragElement.getStyle().setProperty("display", display);
  161. }
  162. }
  163. switch (typeInt) {
  164. case Event.ONMOUSEOVER:
  165. VDropHandler target = findDragTarget(targetElement);
  166. if (target != null && target != currentDropHandler) {
  167. if (currentDropHandler != null) {
  168. currentDropHandler.dragLeave(currentDrag);
  169. currentDrag.getDropDetails().clear();
  170. }
  171. currentDropHandler = target;
  172. // ApplicationConnection.getConsole().log(
  173. // "DropHandler now"
  174. // + currentDropHandler.getPaintable());
  175. currentDrag.setElementOver(targetElement);
  176. target.dragEnter(currentDrag);
  177. } else if (target == null && currentDropHandler != null) {
  178. // ApplicationConnection.getConsole().log("Invalid state!?");
  179. currentDropHandler.dragLeave(currentDrag);
  180. currentDrag.getDropDetails().clear();
  181. currentDropHandler = null;
  182. }
  183. break;
  184. case Event.ONMOUSEOUT:
  185. Element relatedTarget = Element.as(nativeEvent
  186. .getRelatedEventTarget());
  187. VDropHandler newDragHanler = findDragTarget(relatedTarget);
  188. if (dragElement != null
  189. && dragElement.isOrHasChild(relatedTarget)) {
  190. // ApplicationConnection.getConsole().log(
  191. // "Mouse out of dragImage, ignored");
  192. return;
  193. }
  194. if (currentDropHandler != null
  195. && currentDropHandler != newDragHanler) {
  196. currentDropHandler.dragLeave(currentDrag);
  197. currentDrag.getDropDetails().clear();
  198. currentDropHandler = null;
  199. serverCallback = null;
  200. }
  201. break;
  202. case Event.ONMOUSEMOVE:
  203. case Event.ONTOUCHMOVE:
  204. if (currentDropHandler != null) {
  205. currentDrag.setElementOver(targetElement);
  206. currentDropHandler.dragOver(currentDrag);
  207. }
  208. nativeEvent.preventDefault();
  209. break;
  210. case Event.ONTOUCHEND:
  211. /* Avoid simulated event on drag end */
  212. event.getNativeEvent().preventDefault();
  213. case Event.ONMOUSEUP:
  214. endDrag();
  215. break;
  216. default:
  217. break;
  218. }
  219. }
  220. }
  221. private static VDragAndDropManager instance;
  222. private HandlerRegistration handlerRegistration;
  223. private VDragEvent currentDrag;
  224. /**
  225. * If dragging is currently on a drophandler, this field has reference to it
  226. */
  227. private VDropHandler currentDropHandler;
  228. public VDropHandler getCurrentDropHandler() {
  229. return currentDropHandler;
  230. }
  231. /**
  232. * If drag and drop operation is not handled by {@link VDragAndDropManager}s
  233. * internal handler, this can be used to update current {@link VDropHandler}
  234. * .
  235. *
  236. * @param currentDropHandler
  237. */
  238. public void setCurrentDropHandler(VDropHandler currentDropHandler) {
  239. this.currentDropHandler = currentDropHandler;
  240. }
  241. private VDragEventServerCallback serverCallback;
  242. private HandlerRegistration deferredStartRegistration;
  243. public static VDragAndDropManager get() {
  244. if (instance == null) {
  245. instance = GWT.create(VDragAndDropManager.class);
  246. }
  247. return instance;
  248. }
  249. /* Singleton */
  250. protected VDragAndDropManager() {
  251. }
  252. private final NativePreviewHandler defaultDragAndDropEventHandler = new DefaultDragAndDropEventHandler();
  253. /**
  254. * Flag to indicate if drag operation has really started or not. Null check
  255. * of currentDrag field is not enough as a lazy start may be pending.
  256. */
  257. private boolean isStarted;
  258. /**
  259. * This method is used to start Vaadin client side drag and drop operation.
  260. * Operation may be started by virtually any Widget.
  261. * <p>
  262. * Cancels possible existing drag. TODO figure out if this is always a bug
  263. * if one is active. Maybe a good and cheap lifesaver thought.
  264. * <p>
  265. * If possible, method automatically detects current {@link VDropHandler}
  266. * and fires {@link VDropHandler#dragEnter(VDragEvent)} event on it.
  267. * <p>
  268. * May also be used to control the drag and drop operation. If this option
  269. * is used, {@link VDropHandler} is searched on mouse events and appropriate
  270. * methods on it called automatically.
  271. *
  272. * @param transferable
  273. * @param nativeEvent
  274. * @param handleDragEvents
  275. * if true, {@link VDragAndDropManager} handles the drag and drop
  276. * operation GWT event preview.
  277. * @return
  278. */
  279. public VDragEvent startDrag(VTransferable transferable,
  280. final NativeEvent startEvent, final boolean handleDragEvents) {
  281. interruptDrag();
  282. isStarted = false;
  283. currentDrag = new VDragEvent(transferable, startEvent);
  284. currentDrag.setCurrentGwtEvent(startEvent);
  285. final Command startDrag = new Command() {
  286. @Override
  287. public void execute() {
  288. isStarted = true;
  289. addActiveDragSourceStyleName();
  290. VDropHandler dh = null;
  291. if (startEvent != null) {
  292. dh = findDragTarget(Element.as(currentDrag
  293. .getCurrentGwtEvent().getEventTarget()));
  294. }
  295. if (dh != null) {
  296. // drag has started on a DropHandler, kind of drag over
  297. // happens
  298. currentDropHandler = dh;
  299. dh.dragEnter(currentDrag);
  300. }
  301. if (handleDragEvents) {
  302. handlerRegistration = Event
  303. .addNativePreviewHandler(defaultDragAndDropEventHandler);
  304. if (dragElement != null
  305. && dragElement.getParentElement() == null) {
  306. attachDragElement();
  307. }
  308. }
  309. // just capture something to prevent text selection in IE
  310. Event.setCapture(RootPanel.getBodyElement());
  311. }
  312. private void addActiveDragSourceStyleName() {
  313. ComponentConnector dragSource = currentDrag.getTransferable()
  314. .getDragSource();
  315. dragSource.getWidget().addStyleName(
  316. ACTIVE_DRAG_SOURCE_STYLENAME);
  317. }
  318. };
  319. final int eventType = Event.as(startEvent).getTypeInt();
  320. if (handleDragEvents
  321. && (eventType == Event.ONMOUSEDOWN || eventType == Event.ONTOUCHSTART)) {
  322. // only really start drag event on mousemove
  323. deferredStartRegistration = Event
  324. .addNativePreviewHandler(new NativePreviewHandler() {
  325. private int startX = WidgetUtil
  326. .getTouchOrMouseClientX(currentDrag
  327. .getCurrentGwtEvent());
  328. private int startY = WidgetUtil
  329. .getTouchOrMouseClientY(currentDrag
  330. .getCurrentGwtEvent());
  331. @Override
  332. public void onPreviewNativeEvent(
  333. NativePreviewEvent event) {
  334. int typeInt = event.getTypeInt();
  335. if (typeInt == -1
  336. && event.getNativeEvent().getType()
  337. .toLowerCase().contains("pointer")) {
  338. /*
  339. * Ignore PointerEvents since IE10 and IE11 send
  340. * also MouseEvents for backwards compatibility.
  341. */
  342. return;
  343. }
  344. switch (typeInt) {
  345. case Event.ONMOUSEOVER:
  346. if (dragElement == null) {
  347. break;
  348. }
  349. EventTarget currentEventTarget = event
  350. .getNativeEvent()
  351. .getCurrentEventTarget();
  352. if (Node.is(currentEventTarget)
  353. && !dragElement.isOrHasChild(Node
  354. .as(currentEventTarget))) {
  355. // drag image appeared below, ignore
  356. break;
  357. }
  358. case Event.ONKEYDOWN:
  359. case Event.ONKEYPRESS:
  360. case Event.ONKEYUP:
  361. case Event.ONBLUR:
  362. case Event.ONFOCUS:
  363. // don't cancel possible drag start
  364. break;
  365. case Event.ONMOUSEOUT:
  366. if (dragElement == null) {
  367. break;
  368. }
  369. EventTarget relatedEventTarget = event
  370. .getNativeEvent()
  371. .getRelatedEventTarget();
  372. if (Node.is(relatedEventTarget)
  373. && !dragElement.isOrHasChild(Node
  374. .as(relatedEventTarget))) {
  375. // drag image appeared below, ignore
  376. break;
  377. }
  378. case Event.ONMOUSEMOVE:
  379. case Event.ONTOUCHMOVE:
  380. int currentX = WidgetUtil
  381. .getTouchOrMouseClientX(event
  382. .getNativeEvent());
  383. int currentY = WidgetUtil
  384. .getTouchOrMouseClientY(event
  385. .getNativeEvent());
  386. if (Math.abs(startX - currentX) > 3
  387. || Math.abs(startY - currentY) > 3) {
  388. if (deferredStartRegistration != null) {
  389. deferredStartRegistration
  390. .removeHandler();
  391. deferredStartRegistration = null;
  392. }
  393. currentDrag.setCurrentGwtEvent(event
  394. .getNativeEvent());
  395. startDrag.execute();
  396. }
  397. break;
  398. default:
  399. // on any other events, clean up the
  400. // deferred drag start
  401. if (deferredStartRegistration != null) {
  402. deferredStartRegistration.removeHandler();
  403. deferredStartRegistration = null;
  404. }
  405. currentDrag = null;
  406. clearDragElement();
  407. break;
  408. }
  409. }
  410. });
  411. } else {
  412. startDrag.execute();
  413. }
  414. return currentDrag;
  415. }
  416. private void updateDragImagePosition() {
  417. if (currentDrag.getCurrentGwtEvent() != null && dragElement != null) {
  418. Style style = dragElement.getStyle();
  419. int clientY = WidgetUtil.getTouchOrMouseClientY(currentDrag
  420. .getCurrentGwtEvent());
  421. int clientX = WidgetUtil.getTouchOrMouseClientX(currentDrag
  422. .getCurrentGwtEvent());
  423. style.setTop(clientY, Unit.PX);
  424. style.setLeft(clientX, Unit.PX);
  425. }
  426. }
  427. /**
  428. * First seeks the widget from this element, then iterates widgets until one
  429. * implement HasDropHandler. Returns DropHandler from that.
  430. *
  431. * @param element
  432. * @return
  433. */
  434. private VDropHandler findDragTarget(Element element) {
  435. try {
  436. Widget w = WidgetUtil.findWidget(element, null);
  437. if (w == null) {
  438. return null;
  439. }
  440. while (!(w instanceof VHasDropHandler)
  441. || !isDropEnabled((VHasDropHandler) w)) {
  442. w = w.getParent();
  443. if (w == null) {
  444. break;
  445. }
  446. }
  447. if (w == null) {
  448. return null;
  449. } else {
  450. VDropHandler dh = ((VHasDropHandler) w).getDropHandler();
  451. return dh;
  452. }
  453. } catch (Exception e) {
  454. // ApplicationConnection.getConsole().log(
  455. // "FIXME: Exception when detecting drop handler");
  456. // e.printStackTrace();
  457. return null;
  458. }
  459. }
  460. /**
  461. * Checks if the given {@link VHasDropHandler} really is able to accept
  462. * drops.
  463. */
  464. private static boolean isDropEnabled(VHasDropHandler target) {
  465. VDropHandler dh = target.getDropHandler();
  466. return dh != null && dh.getConnector().isEnabled();
  467. }
  468. /**
  469. * Drag is ended (drop happened) on current drop handler. Calls drop method
  470. * on current drop handler and does appropriate cleanup.
  471. */
  472. public void endDrag() {
  473. endDrag(true);
  474. }
  475. /**
  476. * The drag and drop operation is ended, but drop did not happen. If
  477. * operation is currently on a drop handler, its dragLeave method is called
  478. * and appropriate cleanup happens.
  479. */
  480. public void interruptDrag() {
  481. endDrag(false);
  482. }
  483. private void endDrag(boolean doDrop) {
  484. if (handlerRegistration != null) {
  485. handlerRegistration.removeHandler();
  486. handlerRegistration = null;
  487. }
  488. boolean sendTransferableToServer = false;
  489. if (currentDropHandler != null) {
  490. if (doDrop) {
  491. // we have dropped on a drop target
  492. sendTransferableToServer = currentDropHandler.drop(currentDrag);
  493. if (sendTransferableToServer) {
  494. doRequest(DragEventType.DROP);
  495. /*
  496. * Clean active source class name deferred until response is
  497. * handled. E.g. hidden on start, removed in drophandler ->
  498. * would flicker in case removed eagerly.
  499. */
  500. final ComponentConnector dragSource = currentDrag
  501. .getTransferable().getDragSource();
  502. final ApplicationConnection client = currentDropHandler
  503. .getApplicationConnection();
  504. Scheduler.get().scheduleFixedDelay(new RepeatingCommand() {
  505. @Override
  506. public boolean execute() {
  507. if (!client.hasActiveRequest()) {
  508. removeActiveDragSourceStyleName(dragSource);
  509. return false;
  510. }
  511. return true;
  512. }
  513. }, 30);
  514. }
  515. } else {
  516. currentDrag.setCurrentGwtEvent(null);
  517. currentDropHandler.dragLeave(currentDrag);
  518. }
  519. currentDropHandler = null;
  520. serverCallback = null;
  521. visitId = 0; // reset to ignore ongoing server check
  522. }
  523. /*
  524. * Remove class name indicating drag source when server visit is done
  525. * iff server visit was not initiated. Otherwise it will be removed once
  526. * the server visit is done.
  527. */
  528. if (!sendTransferableToServer && currentDrag != null) {
  529. removeActiveDragSourceStyleName(currentDrag.getTransferable()
  530. .getDragSource());
  531. }
  532. currentDrag = null;
  533. clearDragElement();
  534. // release the capture (set to prevent text selection in IE)
  535. Event.releaseCapture(RootPanel.getBodyElement());
  536. }
  537. private void removeActiveDragSourceStyleName(ComponentConnector dragSource) {
  538. dragSource.getWidget().removeStyleName(ACTIVE_DRAG_SOURCE_STYLENAME);
  539. }
  540. private void clearDragElement() {
  541. if (dragElement != null) {
  542. if (dragElement.getParentElement() != null) {
  543. dragElement.removeFromParent();
  544. }
  545. dragElement = null;
  546. }
  547. }
  548. private int visitId = 0;
  549. private Element dragElement;
  550. /**
  551. * Visits server during drag and drop procedure. Transferable and event type
  552. * is given to server side counterpart of DropHandler.
  553. *
  554. * If another server visit is started before the current is received, the
  555. * current is just dropped. TODO consider if callback should have
  556. * interrupted() method for cleanup.
  557. *
  558. * @param acceptCallback
  559. */
  560. public void visitServer(VDragEventServerCallback acceptCallback) {
  561. doRequest(DragEventType.ENTER);
  562. serverCallback = acceptCallback;
  563. }
  564. private void doRequest(DragEventType drop) {
  565. if (currentDropHandler == null) {
  566. return;
  567. }
  568. ComponentConnector paintable = currentDropHandler.getConnector();
  569. ApplicationConnection client = currentDropHandler
  570. .getApplicationConnection();
  571. /*
  572. * For drag events we are using special id that are routed to
  573. * "drag service" which then again finds the corresponding DropHandler
  574. * on server side.
  575. *
  576. * TODO add rest of the data in Transferable
  577. *
  578. * TODO implement partial updates to Transferable (currently the whole
  579. * Transferable is sent on each request)
  580. */
  581. visitId++;
  582. client.updateVariable(ApplicationConstants.DRAG_AND_DROP_CONNECTOR_ID,
  583. "visitId", visitId, false);
  584. client.updateVariable(ApplicationConstants.DRAG_AND_DROP_CONNECTOR_ID,
  585. "eventId", currentDrag.getEventId(), false);
  586. client.updateVariable(ApplicationConstants.DRAG_AND_DROP_CONNECTOR_ID,
  587. "dhowner", paintable, false);
  588. VTransferable transferable = currentDrag.getTransferable();
  589. client.updateVariable(ApplicationConstants.DRAG_AND_DROP_CONNECTOR_ID,
  590. "component", transferable.getDragSource(), false);
  591. client.updateVariable(ApplicationConstants.DRAG_AND_DROP_CONNECTOR_ID,
  592. "type", drop.ordinal(), false);
  593. if (currentDrag.getCurrentGwtEvent() != null) {
  594. try {
  595. MouseEventDetails mouseEventDetails = MouseEventDetailsBuilder
  596. .buildMouseEventDetails(currentDrag
  597. .getCurrentGwtEvent());
  598. currentDrag.getDropDetails().put("mouseEvent",
  599. mouseEventDetails.serialize());
  600. } catch (Exception e) {
  601. // NOP, (at least oophm on Safari) can't serialize html dd event
  602. // to mouseevent
  603. }
  604. } else {
  605. currentDrag.getDropDetails().put("mouseEvent", null);
  606. }
  607. client.updateVariable(ApplicationConstants.DRAG_AND_DROP_CONNECTOR_ID,
  608. "evt", currentDrag.getDropDetails(), false);
  609. client.updateVariable(ApplicationConstants.DRAG_AND_DROP_CONNECTOR_ID,
  610. "tra", transferable.getVariableMap(), true);
  611. }
  612. public void handleServerResponse(ValueMap valueMap) {
  613. if (serverCallback == null) {
  614. return;
  615. }
  616. Profiler.enter("VDragAndDropManager.handleServerResponse");
  617. UIDL uidl = (UIDL) valueMap.cast();
  618. int visitId = uidl.getIntAttribute("visitId");
  619. if (this.visitId == visitId) {
  620. serverCallback.handleResponse(uidl.getBooleanAttribute("accepted"),
  621. uidl);
  622. serverCallback = null;
  623. }
  624. runDeferredCommands();
  625. Profiler.leave("VDragAndDropManager.handleServerResponse");
  626. }
  627. private void runDeferredCommands() {
  628. if (deferredCommand != null) {
  629. Command command = deferredCommand;
  630. deferredCommand = null;
  631. command.execute();
  632. if (!isBusy()) {
  633. runDeferredCommands();
  634. }
  635. }
  636. }
  637. void setDragElement(Element node) {
  638. if (currentDrag != null) {
  639. if (dragElement != null && dragElement != node) {
  640. clearDragElement();
  641. } else if (node == dragElement) {
  642. return;
  643. }
  644. dragElement = node;
  645. dragElement.addClassName("v-drag-element");
  646. updateDragImagePosition();
  647. if (isStarted) {
  648. attachDragElement();
  649. }
  650. }
  651. }
  652. Element getDragElement() {
  653. return dragElement;
  654. }
  655. private void attachDragElement() {
  656. if (dragElement != null && dragElement.getParentElement() == null) {
  657. ApplicationConnection connection = getCurrentDragApplicationConnection();
  658. Element dragImageParent;
  659. if (connection == null) {
  660. VConsole.error("Could not determine ApplicationConnection for current drag operation. The drag image will likely look broken");
  661. dragImageParent = RootPanel.getBodyElement();
  662. } else {
  663. dragImageParent = VOverlay.getOverlayContainer(connection);
  664. }
  665. dragImageParent.appendChild(dragElement);
  666. }
  667. }
  668. private Command deferredCommand;
  669. private boolean isBusy() {
  670. return serverCallback != null;
  671. }
  672. protected ApplicationConnection getCurrentDragApplicationConnection() {
  673. if (currentDrag == null) {
  674. return null;
  675. }
  676. final ComponentConnector dragSource = currentDrag.getTransferable()
  677. .getDragSource();
  678. if (dragSource == null) {
  679. return null;
  680. }
  681. return dragSource.getConnection();
  682. }
  683. /**
  684. * Method to que tasks until all dd related server visits are done
  685. *
  686. * @param command
  687. */
  688. private void defer(Command command) {
  689. deferredCommand = command;
  690. }
  691. /**
  692. * Method to execute commands when all existing dd related tasks are
  693. * completed (some may require server visit).
  694. * <p>
  695. * Using this method may be handy if criterion that uses lazy initialization
  696. * are used. Check
  697. * <p>
  698. * TODO Optimization: consider if we actually only need to keep the last
  699. * command in queue here.
  700. *
  701. * @param command
  702. */
  703. public void executeWhenReady(Command command) {
  704. if (isBusy()) {
  705. defer(command);
  706. } else {
  707. command.execute();
  708. }
  709. }
  710. }