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 27KB

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