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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui.dd;
  5. import java.util.LinkedList;
  6. import com.google.gwt.dom.client.Element;
  7. import com.google.gwt.dom.client.NativeEvent;
  8. import com.google.gwt.dom.client.Node;
  9. import com.google.gwt.dom.client.Style;
  10. import com.google.gwt.dom.client.Style.Display;
  11. import com.google.gwt.dom.client.Style.Unit;
  12. import com.google.gwt.event.shared.HandlerRegistration;
  13. import com.google.gwt.user.client.Command;
  14. import com.google.gwt.user.client.Event;
  15. import com.google.gwt.user.client.Timer;
  16. import com.google.gwt.user.client.Event.NativePreviewEvent;
  17. import com.google.gwt.user.client.Event.NativePreviewHandler;
  18. import com.google.gwt.user.client.ui.RootPanel;
  19. import com.google.gwt.user.client.ui.Widget;
  20. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  21. import com.vaadin.terminal.gwt.client.MouseEventDetails;
  22. import com.vaadin.terminal.gwt.client.Paintable;
  23. import com.vaadin.terminal.gwt.client.UIDL;
  24. import com.vaadin.terminal.gwt.client.Util;
  25. import com.vaadin.terminal.gwt.client.ValueMap;
  26. /**
  27. * Helper class to manage the state of drag and drop event on Vaadin client
  28. * side. Can be used to implement most of the drag and drop operation
  29. * automatically via cross-browser event preview method or just as a helper when
  30. * implementing own low level drag and drop operation (like with HTML5 api).
  31. * <p>
  32. * Singleton. Only one drag and drop operation can be active anyways. Use
  33. * {@link #get()} to get instance.
  34. *
  35. */
  36. public class VDragAndDropManager {
  37. private final class DefaultDragAndDropEventHandler implements
  38. NativePreviewHandler {
  39. public void onPreviewNativeEvent(NativePreviewEvent event) {
  40. NativeEvent nativeEvent = event.getNativeEvent();
  41. updateCurrentEvent(nativeEvent);
  42. updateDragImagePosition();
  43. int typeInt = event.getTypeInt();
  44. Element targetElement = (Element) nativeEvent.getEventTarget()
  45. .cast();
  46. if (dragElement != null && dragElement.isOrHasChild(targetElement)) {
  47. // to detect the "real" target, hide dragelement temporary and
  48. // use elementFromPoint
  49. String display = dragElement.getStyle().getDisplay();
  50. dragElement.getStyle().setDisplay(Display.NONE);
  51. try {
  52. int x = nativeEvent.getClientX();
  53. int y = nativeEvent.getClientY();
  54. // Util.browserDebugger();
  55. targetElement = Util.getElementFromPoint(x, y);
  56. if (targetElement == null) {
  57. ApplicationConnection.getConsole().log(
  58. "Event on dragImage, ignored");
  59. event.cancel();
  60. nativeEvent.stopPropagation();
  61. return;
  62. } else {
  63. // ApplicationConnection.getConsole().log(
  64. // "Event on dragImage, target changed");
  65. // special handling for events over dragImage
  66. // pretty much all events are mousemove althout below
  67. // kind of happens mouseover
  68. switch (typeInt) {
  69. case Event.ONMOUSEOVER:
  70. case Event.ONMOUSEOUT:
  71. ApplicationConnection
  72. .getConsole()
  73. .log(
  74. "IGNORING proxy image event, fired because of hack or not significant");
  75. return;
  76. case Event.ONMOUSEMOVE:
  77. VDropHandler findDragTarget = findDragTarget(targetElement);
  78. if (findDragTarget != currentDropHandler) {
  79. // dragleave on old
  80. if (currentDropHandler != null) {
  81. currentDropHandler.dragLeave(currentDrag);
  82. currentDrag.getDropDetails().clear();
  83. serverCallback = null;
  84. }
  85. // dragenter on new
  86. currentDropHandler = findDragTarget;
  87. if (findDragTarget != null) {
  88. ApplicationConnection.getConsole().log(
  89. "DropHandler now"
  90. + currentDropHandler
  91. .getPaintable());
  92. }
  93. if (currentDropHandler != null) {
  94. currentDrag
  95. .setElementOver((com.google.gwt.user.client.Element) targetElement);
  96. currentDropHandler.dragEnter(currentDrag);
  97. }
  98. } else if (findDragTarget != null) {
  99. currentDrag
  100. .setElementOver((com.google.gwt.user.client.Element) targetElement);
  101. currentDropHandler.dragOver(currentDrag);
  102. }
  103. // prevent text selection on IE
  104. nativeEvent.preventDefault();
  105. return;
  106. default:
  107. // just update element over and let the actual
  108. // handling code do the thing
  109. ApplicationConnection.getConsole().log(
  110. "Target just modified on "
  111. + event.getType());
  112. currentDrag
  113. .setElementOver((com.google.gwt.user.client.Element) targetElement);
  114. break;
  115. }
  116. }
  117. } catch (RuntimeException e) {
  118. ApplicationConnection.getConsole().log(
  119. "ERROR during elementFromPoint hack.");
  120. throw e;
  121. } finally {
  122. dragElement.getStyle().setProperty("display", display);
  123. }
  124. }
  125. switch (typeInt) {
  126. case Event.ONMOUSEOVER:
  127. ApplicationConnection.getConsole().log(
  128. event.getNativeEvent().getType());
  129. VDropHandler target = findDragTarget(targetElement);
  130. if (target != null && target != currentDropHandler) {
  131. if (currentDropHandler != null) {
  132. currentDropHandler.dragLeave(currentDrag);
  133. currentDrag.getDropDetails().clear();
  134. }
  135. currentDropHandler = target;
  136. ApplicationConnection.getConsole().log(
  137. "DropHandler now"
  138. + currentDropHandler.getPaintable());
  139. target.dragEnter(currentDrag);
  140. } else if (target == null && currentDropHandler != null) {
  141. ApplicationConnection.getConsole().log("Invalid state!?");
  142. currentDropHandler.dragLeave(currentDrag);
  143. currentDrag.getDropDetails().clear();
  144. currentDropHandler = null;
  145. }
  146. break;
  147. case Event.ONMOUSEOUT:
  148. ApplicationConnection.getConsole().log(
  149. event.getNativeEvent().getType());
  150. Element relatedTarget = (Element) nativeEvent
  151. .getRelatedEventTarget().cast();
  152. VDropHandler newDragHanler = findDragTarget(relatedTarget);
  153. if (dragElement != null
  154. && dragElement.isOrHasChild(relatedTarget)) {
  155. ApplicationConnection.getConsole().log(
  156. "Mouse out of dragImage, ignored");
  157. return;
  158. }
  159. if (currentDropHandler != null
  160. && currentDropHandler != newDragHanler) {
  161. currentDropHandler.dragLeave(currentDrag);
  162. currentDrag.getDropDetails().clear();
  163. currentDropHandler = null;
  164. serverCallback = null;
  165. }
  166. break;
  167. case Event.ONMOUSEMOVE:
  168. if (currentDropHandler != null) {
  169. currentDropHandler.dragOver(currentDrag);
  170. }
  171. nativeEvent.preventDefault();
  172. break;
  173. case Event.ONMOUSEUP:
  174. endDrag();
  175. break;
  176. default:
  177. break;
  178. }
  179. }
  180. }
  181. public enum DragEventType {
  182. ENTER, LEAVE, OVER, DROP
  183. }
  184. private static final String DD_SERVICE = "DD";
  185. private static VDragAndDropManager instance;
  186. private HandlerRegistration handlerRegistration;
  187. private VDragEvent currentDrag;
  188. /**
  189. * If dragging is currently on a drophandler, this field has reference to it
  190. */
  191. private VDropHandler currentDropHandler;
  192. public VDropHandler getCurrentDropHandler() {
  193. return currentDropHandler;
  194. }
  195. /**
  196. * If drag and drop operation is not handled by {@link VDragAndDropManager}s
  197. * internal handler, this can be used to update current {@link VDropHandler}
  198. * .
  199. *
  200. * @param currentDropHandler
  201. */
  202. public void setCurrentDropHandler(VDropHandler currentDropHandler) {
  203. this.currentDropHandler = currentDropHandler;
  204. }
  205. private VDragEventServerCallback serverCallback;
  206. private HandlerRegistration deferredStartRegistration;
  207. public static VDragAndDropManager get() {
  208. if (instance == null) {
  209. instance = new VDragAndDropManager();
  210. }
  211. return instance;
  212. }
  213. /* Singleton */
  214. private VDragAndDropManager() {
  215. }
  216. private NativePreviewHandler defaultDragAndDropEventHandler = new DefaultDragAndDropEventHandler();
  217. /**
  218. * Flag to indicate if drag operation has really started or not. Null check
  219. * of currentDrag field is not enough as a lazy start may be pending.
  220. */
  221. private boolean isStarted;
  222. /**
  223. * This method is used to start Vaadin client side drag and drop operation.
  224. * Operation may be started by virtually any Widget.
  225. * <p>
  226. * Cancels possible existing drag. TODO figure out if this is always a bug
  227. * if one is active. Maybe a good and cheap lifesaver thought.
  228. * <p>
  229. * If possible, method automatically detects current {@link VDropHandler}
  230. * and fires {@link VDropHandler#dragEnter(VDragEvent)} event on it.
  231. * <p>
  232. * May also be used to control the drag and drop operation. If this option
  233. * is used, {@link VDropHandler} is searched on mouse events and appropriate
  234. * methods on it called automatically.
  235. *
  236. * @param transferable
  237. * @param nativeEvent
  238. * @param handleDragEvents
  239. * if true, {@link VDragAndDropManager} handles the drag and drop
  240. * operation GWT event preview.
  241. * @return
  242. */
  243. public VDragEvent startDrag(VTransferable transferable,
  244. final NativeEvent startEvent, final boolean handleDragEvents) {
  245. interruptDrag();
  246. isStarted = false;
  247. currentDrag = new VDragEvent(transferable, startEvent);
  248. updateCurrentEvent(startEvent);
  249. final Command startDrag = new Command() {
  250. public void execute() {
  251. isStarted = true;
  252. VDropHandler dh = null;
  253. if (startEvent != null) {
  254. dh = findDragTarget((Element) currentDrag.currentGwtEvent
  255. .getEventTarget().cast());
  256. }
  257. if (dh != null) {
  258. // drag has started on a DropHandler, kind of drag over
  259. // happens
  260. currentDropHandler = dh;
  261. dh.dragEnter(currentDrag);
  262. }
  263. if (handleDragEvents) {
  264. handlerRegistration = Event
  265. .addNativePreviewHandler(defaultDragAndDropEventHandler);
  266. if (dragElement != null
  267. && dragElement.getParentElement() == null) {
  268. // deferred attaching drag image is on going, we can
  269. // hurry with it now
  270. lazyAttachDragElement.cancel();
  271. lazyAttachDragElement.run();
  272. }
  273. }
  274. }
  275. };
  276. if (handleDragEvents
  277. && Event.as(startEvent).getTypeInt() == Event.ONMOUSEDOWN) {
  278. // only really start drag event on mousemove
  279. deferredStartRegistration = Event
  280. .addNativePreviewHandler(new NativePreviewHandler() {
  281. public void onPreviewNativeEvent(
  282. NativePreviewEvent event) {
  283. int typeInt = event.getTypeInt();
  284. switch (typeInt) {
  285. case Event.ONMOUSEOVER:
  286. if (dragElement == null
  287. || !dragElement
  288. .isOrHasChild((Node) event
  289. .getNativeEvent()
  290. .getCurrentEventTarget()
  291. .cast())) {
  292. // drag image appeared below, ignore
  293. ApplicationConnection.getConsole().log(
  294. "Drag image appeared");
  295. break;
  296. }
  297. case Event.ONKEYDOWN:
  298. case Event.ONKEYPRESS:
  299. case Event.ONKEYUP:
  300. case Event.ONBLUR:
  301. // don't cancel possible drag start
  302. break;
  303. case Event.ONMOUSEOUT:
  304. if (dragElement == null
  305. || !dragElement
  306. .isOrHasChild((Node) event
  307. .getNativeEvent()
  308. .getRelatedEventTarget()
  309. .cast())) {
  310. // drag image appeared below, ignore
  311. ApplicationConnection.getConsole().log(
  312. "Drag image appeared");
  313. break;
  314. }
  315. case Event.ONMOUSEMOVE:
  316. deferredStartRegistration.removeHandler();
  317. deferredStartRegistration = null;
  318. updateCurrentEvent(event.getNativeEvent());
  319. startDrag.execute();
  320. break;
  321. default:
  322. // on any other events, clean up the
  323. // deferred drag start
  324. ApplicationConnection.getConsole().log(
  325. "Drag did not start due event"
  326. + event.getNativeEvent()
  327. .getType());
  328. deferredStartRegistration.removeHandler();
  329. deferredStartRegistration = null;
  330. currentDrag = null;
  331. clearDragElement();
  332. break;
  333. }
  334. }
  335. });
  336. } else {
  337. startDrag.execute();
  338. }
  339. return currentDrag;
  340. }
  341. private void interruptDrag() {
  342. if (currentDrag != null) {
  343. ApplicationConnection.getConsole()
  344. .log("Drag operation interrupted");
  345. if (currentDropHandler != null) {
  346. currentDrag.currentGwtEvent = null;
  347. currentDropHandler.dragLeave(currentDrag);
  348. currentDropHandler = null;
  349. }
  350. currentDrag = null;
  351. }
  352. }
  353. private void updateDragImagePosition() {
  354. if (currentDrag.currentGwtEvent != null && dragElement != null) {
  355. Style style = dragElement.getStyle();
  356. int clientY = currentDrag.currentGwtEvent.getClientY();
  357. int clientX = currentDrag.currentGwtEvent.getClientX();
  358. style.setTop(clientY, Unit.PX);
  359. style.setLeft(clientX, Unit.PX);
  360. }
  361. }
  362. /**
  363. * First seeks the widget from this element, then iterates widgets until one
  364. * implement HasDropHandler. Returns DropHandler from that.
  365. *
  366. * @param element
  367. * @return
  368. */
  369. private VDropHandler findDragTarget(Element element) {
  370. try {
  371. Widget w = Util.findWidget(
  372. (com.google.gwt.user.client.Element) element, null);
  373. if (w == null) {
  374. return null;
  375. }
  376. while (!(w instanceof VHasDropHandler)) {
  377. w = w.getParent();
  378. if (w == null) {
  379. break;
  380. }
  381. }
  382. if (w == null) {
  383. ApplicationConnection.getConsole().log(
  384. "No suitable DropHandler found2");
  385. return null;
  386. } else {
  387. VDropHandler dh = ((VHasDropHandler) w).getDropHandler();
  388. if (dh == null) {
  389. ApplicationConnection.getConsole().log(
  390. "No suitable DropHandler found3");
  391. }
  392. return dh;
  393. }
  394. } catch (Exception e) {
  395. ApplicationConnection.getConsole().log(
  396. "FIXME: Exception when detecting drop handler");
  397. e.printStackTrace();
  398. return null;
  399. }
  400. }
  401. private void updateCurrentEvent(NativeEvent event) {
  402. currentDrag.currentGwtEvent = event;
  403. }
  404. public void endDrag() {
  405. if (handlerRegistration != null) {
  406. handlerRegistration.removeHandler();
  407. handlerRegistration = null;
  408. }
  409. if (currentDropHandler != null) {
  410. // we have dropped on a drop target
  411. boolean sendTransferableToServer = currentDropHandler
  412. .drop(currentDrag);
  413. if (sendTransferableToServer) {
  414. doRequest(DragEventType.DROP);
  415. }
  416. currentDropHandler = null;
  417. serverCallback = null;
  418. }
  419. currentDrag = null;
  420. clearDragElement();
  421. }
  422. private void clearDragElement() {
  423. if (dragElement != null) {
  424. if (dragElement.getParentElement() != null) {
  425. RootPanel.getBodyElement().removeChild(dragElement);
  426. }
  427. dragElement = null;
  428. }
  429. }
  430. private int visitId = 0;
  431. private Element dragElement;
  432. /**
  433. * Visits server during drag and drop procedure. Transferable and event type
  434. * is given to server side counterpart of DropHandler.
  435. *
  436. * If another server visit is started before the current is received, the
  437. * current is just dropped. TODO consider if callback should have
  438. * interrupted() method for cleanup.
  439. *
  440. * @param acceptCallback
  441. */
  442. public void visitServer(VDragEventServerCallback acceptCallback) {
  443. doRequest(DragEventType.ENTER);
  444. serverCallback = acceptCallback;
  445. }
  446. private void doRequest(DragEventType drop) {
  447. Paintable paintable = currentDropHandler.getPaintable();
  448. ApplicationConnection client = currentDropHandler
  449. .getApplicationConnection();
  450. /*
  451. * For drag events we are using special id that are routed to
  452. * "drag service" which then again finds the corresponding DropHandler
  453. * on server side.
  454. *
  455. * TODO add rest of the data in Transferable
  456. *
  457. * TODO implement partial updates to Transferable (currently the whole
  458. * Transferable is sent on each request)
  459. */
  460. visitId++;
  461. client.updateVariable(DD_SERVICE, "visitId", visitId, false);
  462. client.updateVariable(DD_SERVICE, "eventId", currentDrag.getEventId(),
  463. false);
  464. client.updateVariable(DD_SERVICE, "dhowner", paintable, false);
  465. VTransferable transferable = currentDrag.getTransferable();
  466. client.updateVariable(DD_SERVICE, "component", transferable
  467. .getDragSource(), false);
  468. client.updateVariable(DD_SERVICE, "type", drop.ordinal(), false);
  469. if (currentDrag.currentGwtEvent != null) {
  470. try {
  471. MouseEventDetails mouseEventDetails = new MouseEventDetails(
  472. currentDrag.currentGwtEvent);
  473. currentDrag.getDropDetails().put("mouseEvent",
  474. mouseEventDetails.serialize());
  475. } catch (Exception e) {
  476. // NOP, (at least oophm on Safari) can't serialize html dd event
  477. // to mouseevent
  478. }
  479. } else {
  480. currentDrag.getDropDetails().put("mouseEvent", null);
  481. }
  482. client.updateVariable(DD_SERVICE, "evt", currentDrag.getDropDetails(),
  483. false);
  484. client.updateVariable(DD_SERVICE, "tra", transferable.getVariableMap(),
  485. true);
  486. }
  487. public void handleServerResponse(ValueMap valueMap) {
  488. if (serverCallback == null) {
  489. return;
  490. }
  491. UIDL uidl = (UIDL) valueMap.cast();
  492. int visitId = uidl.getIntAttribute("visitId");
  493. if (this.visitId == visitId) {
  494. serverCallback.handleResponse(uidl.getBooleanAttribute("accepted"),
  495. uidl);
  496. serverCallback = null;
  497. }
  498. runDeferredCommands();
  499. }
  500. private void runDeferredCommands() {
  501. if (deferredCommands != null && !deferredCommands.isEmpty()) {
  502. Command command = deferredCommands.poll();
  503. command.execute();
  504. if (!isBusy()) {
  505. runDeferredCommands();
  506. }
  507. }
  508. }
  509. void setDragElement(Element node) {
  510. if (currentDrag != null) {
  511. if (dragElement != null && dragElement != node) {
  512. clearDragElement();
  513. } else if (node == dragElement) {
  514. return;
  515. }
  516. dragElement = node;
  517. dragElement.addClassName("v-drag-element");
  518. updateDragImagePosition();
  519. if (isStarted) {
  520. lazyAttachDragElement.run();
  521. } else {
  522. /*
  523. * To make our default dnd handler as compatible as possible, we
  524. * need to defer the appearance of dragElement. Otherwise events
  525. * that are derived from sequences of other events might not
  526. * fire as domchanged will fire between them or mouse up might
  527. * happen on dragElement.
  528. */
  529. lazyAttachDragElement.schedule(300);
  530. }
  531. }
  532. }
  533. Element getDragElement() {
  534. return dragElement;
  535. }
  536. private final Timer lazyAttachDragElement = new Timer() {
  537. @Override
  538. public void run() {
  539. if (dragElement != null && dragElement.getParentElement() == null) {
  540. RootPanel.getBodyElement().appendChild(dragElement);
  541. }
  542. }
  543. };
  544. private LinkedList<Command> deferredCommands;
  545. private boolean isBusy() {
  546. return serverCallback != null;
  547. }
  548. /**
  549. * Method to que tasks until all dd related server visits are done
  550. *
  551. * @param command
  552. */
  553. private void defer(Command command) {
  554. if (deferredCommands == null) {
  555. deferredCommands = new LinkedList<Command>();
  556. }
  557. deferredCommands.add(command);
  558. }
  559. /**
  560. * Method to execute commands when all existing dd related tasks are
  561. * completed (some may require server visit).
  562. * <p>
  563. * Using this method may be handy if criterion that uses lazy initialization
  564. * are used. Check
  565. * <p>
  566. * TODO Optimization: consider if we actually only need to keep the last
  567. * command in queue here.
  568. *
  569. * @param command
  570. */
  571. public void executeWhenReady(Command command) {
  572. if (isBusy()) {
  573. defer(command);
  574. } else {
  575. command.execute();
  576. }
  577. }
  578. }