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.

DragAndDropService.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.server;
  5. import java.io.PrintWriter;
  6. import java.util.Map;
  7. import com.vaadin.event.Transferable;
  8. import com.vaadin.event.TransferableImpl;
  9. import com.vaadin.event.dd.DragAndDropEvent;
  10. import com.vaadin.event.dd.DragSource;
  11. import com.vaadin.event.dd.DropHandler;
  12. import com.vaadin.event.dd.DropTarget;
  13. import com.vaadin.event.dd.DropTargetDetails;
  14. import com.vaadin.event.dd.DropTargetDetailsImpl;
  15. import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
  16. import com.vaadin.terminal.PaintException;
  17. import com.vaadin.terminal.VariableOwner;
  18. import com.vaadin.terminal.gwt.client.ui.dd.VDragAndDropManager.DragEventType;
  19. import com.vaadin.ui.Component;
  20. public class DragAndDropService implements VariableOwner {
  21. private int lastVisitId;
  22. private int currentEventId;
  23. private boolean lastVisitAccepted = false;
  24. private DragAndDropEvent dragEvent;
  25. private final AbstractCommunicationManager manager;
  26. private AcceptCriterion acceptCriterion;
  27. public DragAndDropService(AbstractCommunicationManager manager) {
  28. this.manager = manager;
  29. }
  30. public void changeVariables(Object source, Map<String, Object> variables) {
  31. Object owner = variables.get("dhowner");
  32. // Validate drop handler owner
  33. if (!(owner instanceof DropTarget)) {
  34. System.err.println("DropHandler owner " + owner
  35. + " must implement DropTarget");
  36. return;
  37. }
  38. if (owner == null) {
  39. System.err.println("DropHandler owner is null");
  40. return;
  41. }
  42. DropTarget dropTarget = (DropTarget) owner;
  43. lastVisitId = (Integer) variables.get("visitId");
  44. // request may be dropRequest or request during drag operation (commonly
  45. // dragover or dragenter)
  46. boolean dropRequest = isDropRequest(variables);
  47. if (dropRequest) {
  48. handleDropRequest(dropTarget, variables);
  49. } else {
  50. handleDragRequest(dropTarget, variables);
  51. }
  52. }
  53. /**
  54. * Handles a drop request from the VDragAndDropManager.
  55. *
  56. * @param dropTarget
  57. * @param variables
  58. */
  59. private void handleDropRequest(DropTarget dropTarget,
  60. Map<String, Object> variables) {
  61. DropHandler dropHandler = (dropTarget).getDropHandler();
  62. if (dropHandler == null) {
  63. // No dropHandler returned so no drop can be performed.
  64. System.err
  65. .println("DropTarget.getDropHandler() returned null for owner: "
  66. + dropTarget);
  67. return;
  68. }
  69. /*
  70. * Construct the Transferable and the DragDropDetails for the drop
  71. * operation based on the info passed from the client widgets (drag
  72. * source for Transferable, drop target for DragDropDetails).
  73. */
  74. Transferable transferable = constructTransferable(dropTarget, variables);
  75. DropTargetDetails dropData = constructDragDropDetails(dropTarget,
  76. variables);
  77. DragAndDropEvent dropEvent = new DragAndDropEvent(transferable,
  78. dropData);
  79. if (dropHandler.getAcceptCriterion().accept(dropEvent)) {
  80. dropHandler.drop(dropEvent);
  81. }
  82. }
  83. /**
  84. * Handles a drag/move request from the VDragAndDropManager.
  85. *
  86. * @param dropTarget
  87. * @param variables
  88. */
  89. private void handleDragRequest(DropTarget dropTarget,
  90. Map<String, Object> variables) {
  91. lastVisitId = (Integer) variables.get("visitId");
  92. acceptCriterion = dropTarget.getDropHandler().getAcceptCriterion();
  93. /*
  94. * Construct the Transferable and the DragDropDetails for the drag
  95. * operation based on the info passed from the client widgets (drag
  96. * source for Transferable, current target for DragDropDetails).
  97. */
  98. Transferable transferable = constructTransferable(dropTarget, variables);
  99. DropTargetDetails dragDropDetails = constructDragDropDetails(
  100. dropTarget, variables);
  101. dragEvent = new DragAndDropEvent(transferable, dragDropDetails);
  102. lastVisitAccepted = acceptCriterion.accept(dragEvent);
  103. }
  104. /**
  105. * Construct DragDropDetails based on variables from client drop target.
  106. * Uses DragDropDetailsTranslator if available, otherwise a default
  107. * DragDropDetails implementation is used.
  108. *
  109. * @param dropTarget
  110. * @param variables
  111. * @return
  112. */
  113. @SuppressWarnings("unchecked")
  114. private DropTargetDetails constructDragDropDetails(DropTarget dropTarget,
  115. Map<String, Object> variables) {
  116. Map<String, Object> rawDragDropDetails = (Map<String, Object>) variables
  117. .get("evt");
  118. DropTargetDetails dropData = dropTarget
  119. .translateDropTargetDetails(rawDragDropDetails);
  120. if (dropData == null) {
  121. // Create a default DragDropDetails with all the raw variables
  122. dropData = new DropTargetDetailsImpl(rawDragDropDetails, dropTarget);
  123. }
  124. return dropData;
  125. }
  126. private boolean isDropRequest(Map<String, Object> variables) {
  127. return getRequestType(variables) == DragEventType.DROP;
  128. }
  129. private DragEventType getRequestType(Map<String, Object> variables) {
  130. int type = (Integer) variables.get("type");
  131. return DragEventType.values()[type];
  132. }
  133. @SuppressWarnings("unchecked")
  134. private Transferable constructTransferable(DropTarget dropHandlerOwner,
  135. Map<String, Object> variables) {
  136. int eventId = (Integer) variables.get("eventId");
  137. currentEventId = eventId;
  138. final Component sourceComponent = (Component) variables
  139. .get("component");
  140. variables = (Map<String, Object>) variables.get("tra");
  141. Transferable transferable = null;
  142. if (sourceComponent != null && sourceComponent instanceof DragSource) {
  143. transferable = ((DragSource) sourceComponent)
  144. .getTransferable(variables);
  145. }
  146. if (transferable == null) {
  147. transferable = new TransferableImpl(sourceComponent, variables);
  148. }
  149. return transferable;
  150. }
  151. public boolean isEnabled() {
  152. return true;
  153. }
  154. public boolean isImmediate() {
  155. return true;
  156. }
  157. void printJSONResponse(PrintWriter outWriter) throws PaintException {
  158. if (isDirty()) {
  159. outWriter.print(", \"dd\":");
  160. JsonPaintTarget jsonPaintTarget = new JsonPaintTarget(manager,
  161. outWriter, false);
  162. jsonPaintTarget.startTag("dd");
  163. jsonPaintTarget.addAttribute("visitId", lastVisitId);
  164. if (acceptCriterion != null) {
  165. jsonPaintTarget.addAttribute("accepted", lastVisitAccepted);
  166. acceptCriterion.paintResponse(jsonPaintTarget);
  167. }
  168. jsonPaintTarget.endTag("dd");
  169. jsonPaintTarget.close();
  170. lastVisitId = -1;
  171. lastVisitAccepted = false;
  172. acceptCriterion = null;
  173. dragEvent = null;
  174. }
  175. }
  176. private boolean isDirty() {
  177. if (lastVisitId > 0) {
  178. return true;
  179. }
  180. return false;
  181. }
  182. }