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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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.getServerCommunicationHandler()
  437. .hasActiveRequest()) {
  438. removeActiveDragSourceStyleName(dragSource);
  439. return false;
  440. }
  441. return true;
  442. }
  443. }, 30);
  444. }
  445. } else {
  446. currentDropHandler.dragLeave(currentDrag);
  447. currentDrag.setCurrentGwtEvent(null);
  448. }
  449. currentDropHandler = null;
  450. serverCallback = null;
  451. visitId = 0; // reset to ignore ongoing server check
  452. }
  453. /*
  454. * Remove class name indicating drag source when server visit is done
  455. * iff server visit was not initiated. Otherwise it will be removed once
  456. * the server visit is done.
  457. */
  458. if (!sendTransferableToServer && currentDrag != null) {
  459. removeActiveDragSourceStyleName(currentDrag.getTransferable()
  460. .getDragSource());
  461. }
  462. currentDrag = null;
  463. clearDragElement();
  464. // release the capture (set to prevent text selection in IE)
  465. Event.releaseCapture(RootPanel.getBodyElement());
  466. }
  467. private void ensureHandlerRegistrationCleanup() {
  468. if (handlerRegistration != null) {
  469. handlerRegistration.removeHandler();
  470. handlerRegistration = null;
  471. }
  472. }
  473. private void ensureDeferredRegistrationCleanup() {
  474. if (deferredStartRegistration != null) {
  475. deferredStartRegistration.removeHandler();
  476. deferredStartRegistration = null;
  477. }
  478. }
  479. private void removeActiveDragSourceStyleName(ComponentConnector dragSource) {
  480. dragSource.getWidget().removeStyleName(ACTIVE_DRAG_SOURCE_STYLENAME);
  481. }
  482. private void clearDragElement() {
  483. if (dragElement != null) {
  484. if (dragElement.getParentElement() != null) {
  485. dragElement.removeFromParent();
  486. }
  487. dragElement = null;
  488. }
  489. }
  490. private int visitId = 0;
  491. private Element dragElement;
  492. /**
  493. * Visits server during drag and drop procedure. Transferable and event type
  494. * is given to server side counterpart of DropHandler.
  495. *
  496. * If another server visit is started before the current is received, the
  497. * current is just dropped. TODO consider if callback should have
  498. * interrupted() method for cleanup.
  499. *
  500. * @param acceptCallback
  501. */
  502. public void visitServer(VDragEventServerCallback acceptCallback) {
  503. doRequest(DragEventType.ENTER);
  504. serverCallback = acceptCallback;
  505. }
  506. private void doRequest(DragEventType drop) {
  507. if (currentDropHandler == null) {
  508. return;
  509. }
  510. ComponentConnector paintable = currentDropHandler.getConnector();
  511. ApplicationConnection client = currentDropHandler
  512. .getApplicationConnection();
  513. /*
  514. * For drag events we are using special id that are routed to
  515. * "drag service" which then again finds the corresponding DropHandler
  516. * on server side.
  517. *
  518. * TODO add rest of the data in Transferable
  519. *
  520. * TODO implement partial updates to Transferable (currently the whole
  521. * Transferable is sent on each request)
  522. */
  523. visitId++;
  524. client.updateVariable(ApplicationConstants.DRAG_AND_DROP_CONNECTOR_ID,
  525. "visitId", visitId, false);
  526. client.updateVariable(ApplicationConstants.DRAG_AND_DROP_CONNECTOR_ID,
  527. "eventId", currentDrag.getEventId(), false);
  528. client.updateVariable(ApplicationConstants.DRAG_AND_DROP_CONNECTOR_ID,
  529. "dhowner", paintable, false);
  530. VTransferable transferable = currentDrag.getTransferable();
  531. client.updateVariable(ApplicationConstants.DRAG_AND_DROP_CONNECTOR_ID,
  532. "component", transferable.getDragSource(), false);
  533. client.updateVariable(ApplicationConstants.DRAG_AND_DROP_CONNECTOR_ID,
  534. "type", drop.ordinal(), false);
  535. if (currentDrag.getCurrentGwtEvent() != null) {
  536. try {
  537. MouseEventDetails mouseEventDetails = MouseEventDetailsBuilder
  538. .buildMouseEventDetails(currentDrag
  539. .getCurrentGwtEvent());
  540. currentDrag.getDropDetails().put("mouseEvent",
  541. mouseEventDetails.serialize());
  542. } catch (Exception e) {
  543. // NOP, (at least oophm on Safari) can't serialize html dd event
  544. // to mouseevent
  545. }
  546. } else {
  547. currentDrag.getDropDetails().put("mouseEvent", null);
  548. }
  549. client.updateVariable(ApplicationConstants.DRAG_AND_DROP_CONNECTOR_ID,
  550. "evt", currentDrag.getDropDetails(), false);
  551. client.updateVariable(ApplicationConstants.DRAG_AND_DROP_CONNECTOR_ID,
  552. "tra", transferable.getVariableMap(), true);
  553. }
  554. public void handleServerResponse(ValueMap valueMap) {
  555. if (serverCallback == null) {
  556. return;
  557. }
  558. Profiler.enter("VDragAndDropManager.handleServerResponse");
  559. UIDL uidl = (UIDL) valueMap.cast();
  560. int visitId = uidl.getIntAttribute("visitId");
  561. if (this.visitId == visitId) {
  562. serverCallback.handleResponse(uidl.getBooleanAttribute("accepted"),
  563. uidl);
  564. serverCallback = null;
  565. }
  566. runDeferredCommands();
  567. Profiler.leave("VDragAndDropManager.handleServerResponse");
  568. }
  569. /**
  570. * Returns DnD strategy to handle native preview events used by the manager.
  571. *
  572. * Subclasses can override this method to return custom strategy or use GWT
  573. * deferred binding.
  574. *
  575. * @return internal DnD native preview event handler
  576. */
  577. protected DDEventHandleStrategy getEventHandleStrategy() {
  578. if (eventHandleStrategy == null) {
  579. eventHandleStrategy = GWT.create(DDEventHandleStrategy.class);
  580. }
  581. return eventHandleStrategy;
  582. }
  583. private void runDeferredCommands() {
  584. if (deferredCommand != null) {
  585. Command command = deferredCommand;
  586. deferredCommand = null;
  587. command.execute();
  588. if (!isBusy()) {
  589. runDeferredCommands();
  590. }
  591. }
  592. }
  593. void setDragElement(Element node) {
  594. if (currentDrag != null) {
  595. if (dragElement != null && dragElement != node) {
  596. clearDragElement();
  597. } else if (node == dragElement) {
  598. return;
  599. }
  600. dragElement = node;
  601. dragElement.addClassName("v-drag-element");
  602. updateDragImagePosition(currentDrag.getCurrentGwtEvent(),
  603. dragElement);
  604. if (isStarted) {
  605. attachDragElement();
  606. }
  607. }
  608. }
  609. Element getDragElement() {
  610. return dragElement;
  611. }
  612. private void attachDragElement() {
  613. if (dragElement != null && dragElement.getParentElement() == null) {
  614. ApplicationConnection connection = getCurrentDragApplicationConnection();
  615. Element dragImageParent;
  616. if (connection == null) {
  617. VConsole.error("Could not determine ApplicationConnection for current drag operation. The drag image will likely look broken");
  618. dragImageParent = RootPanel.getBodyElement();
  619. } else {
  620. dragImageParent = VOverlay.getOverlayContainer(connection);
  621. }
  622. dragImageParent.appendChild(dragElement);
  623. }
  624. }
  625. private Command deferredCommand;
  626. private boolean isBusy() {
  627. return serverCallback != null;
  628. }
  629. protected ApplicationConnection getCurrentDragApplicationConnection() {
  630. if (currentDrag == null) {
  631. return null;
  632. }
  633. final ComponentConnector dragSource = currentDrag.getTransferable()
  634. .getDragSource();
  635. if (dragSource == null) {
  636. return null;
  637. }
  638. return dragSource.getConnection();
  639. }
  640. /**
  641. * Method to que tasks until all dd related server visits are done
  642. *
  643. * @param command
  644. */
  645. private void defer(Command command) {
  646. deferredCommand = command;
  647. }
  648. /**
  649. * Method to execute commands when all existing dd related tasks are
  650. * completed (some may require server visit).
  651. * <p>
  652. * Using this method may be handy if criterion that uses lazy initialization
  653. * are used. Check
  654. * <p>
  655. * TODO Optimization: consider if we actually only need to keep the last
  656. * command in queue here.
  657. *
  658. * @param command
  659. */
  660. public void executeWhenReady(Command command) {
  661. if (isBusy()) {
  662. defer(command);
  663. } else {
  664. command.execute();
  665. }
  666. }
  667. }