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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.server;
  5. import java.io.PrintWriter;
  6. import java.util.Map;
  7. import java.util.logging.Logger;
  8. import com.vaadin.event.Transferable;
  9. import com.vaadin.event.TransferableImpl;
  10. import com.vaadin.event.dd.DragAndDropEvent;
  11. import com.vaadin.event.dd.DragSource;
  12. import com.vaadin.event.dd.DropHandler;
  13. import com.vaadin.event.dd.DropTarget;
  14. import com.vaadin.event.dd.TargetDetails;
  15. import com.vaadin.event.dd.TargetDetailsImpl;
  16. import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
  17. import com.vaadin.terminal.PaintException;
  18. import com.vaadin.terminal.VariableOwner;
  19. import com.vaadin.terminal.gwt.client.Connector;
  20. import com.vaadin.terminal.gwt.client.communication.SharedState;
  21. import com.vaadin.terminal.gwt.client.ui.dd.VDragAndDropManager;
  22. import com.vaadin.terminal.gwt.client.ui.dd.VDragAndDropManager.DragEventType;
  23. import com.vaadin.ui.Component;
  24. public class DragAndDropService implements VariableOwner, Connector {
  25. private static final Logger logger = Logger
  26. .getLogger(DragAndDropService.class.getName());
  27. private int lastVisitId;
  28. private boolean lastVisitAccepted = false;
  29. private DragAndDropEvent dragEvent;
  30. private final AbstractCommunicationManager manager;
  31. private AcceptCriterion acceptCriterion;
  32. public DragAndDropService(AbstractCommunicationManager manager) {
  33. this.manager = manager;
  34. }
  35. public void changeVariables(Object source, Map<String, Object> variables) {
  36. Object owner = variables.get("dhowner");
  37. // Validate drop handler owner
  38. if (!(owner instanceof DropTarget)) {
  39. logger.severe("DropHandler owner " + owner
  40. + " must implement DropTarget");
  41. return;
  42. }
  43. // owner cannot be null here
  44. DropTarget dropTarget = (DropTarget) owner;
  45. lastVisitId = (Integer) variables.get("visitId");
  46. // request may be dropRequest or request during drag operation (commonly
  47. // dragover or dragenter)
  48. boolean dropRequest = isDropRequest(variables);
  49. if (dropRequest) {
  50. handleDropRequest(dropTarget, variables);
  51. } else {
  52. handleDragRequest(dropTarget, variables);
  53. }
  54. }
  55. /**
  56. * Handles a drop request from the VDragAndDropManager.
  57. *
  58. * @param dropTarget
  59. * @param variables
  60. */
  61. private void handleDropRequest(DropTarget dropTarget,
  62. Map<String, Object> variables) {
  63. DropHandler dropHandler = (dropTarget).getDropHandler();
  64. if (dropHandler == null) {
  65. // No dropHandler returned so no drop can be performed.
  66. logger.fine("DropTarget.getDropHandler() returned null for owner: "
  67. + dropTarget);
  68. return;
  69. }
  70. /*
  71. * Construct the Transferable and the DragDropDetails for the drop
  72. * operation based on the info passed from the client widgets (drag
  73. * source for Transferable, drop target for DragDropDetails).
  74. */
  75. Transferable transferable = constructTransferable(dropTarget, variables);
  76. TargetDetails dropData = constructDragDropDetails(dropTarget, 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. TargetDetails dragDropDetails = constructDragDropDetails(dropTarget,
  100. 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 TargetDetails constructDragDropDetails(DropTarget dropTarget,
  115. Map<String, Object> variables) {
  116. Map<String, Object> rawDragDropDetails = (Map<String, Object>) variables
  117. .get("evt");
  118. TargetDetails dropData = dropTarget
  119. .translateDropTargetDetails(rawDragDropDetails);
  120. if (dropData == null) {
  121. // Create a default DragDropDetails with all the raw variables
  122. dropData = new TargetDetailsImpl(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. final Component sourceComponent = (Component) variables
  137. .get("component");
  138. variables = (Map<String, Object>) variables.get("tra");
  139. Transferable transferable = null;
  140. if (sourceComponent != null && sourceComponent instanceof DragSource) {
  141. transferable = ((DragSource) sourceComponent)
  142. .getTransferable(variables);
  143. }
  144. if (transferable == null) {
  145. transferable = new TransferableImpl(sourceComponent, variables);
  146. }
  147. return transferable;
  148. }
  149. public boolean isEnabled() {
  150. return isConnectorEnabled();
  151. }
  152. public boolean isImmediate() {
  153. return true;
  154. }
  155. void printJSONResponse(PrintWriter outWriter) throws PaintException {
  156. if (isDirty()) {
  157. outWriter.print(", \"dd\":");
  158. JsonPaintTarget jsonPaintTarget = new JsonPaintTarget(manager,
  159. outWriter, false);
  160. jsonPaintTarget.startTag("dd");
  161. jsonPaintTarget.addAttribute("visitId", lastVisitId);
  162. if (acceptCriterion != null) {
  163. jsonPaintTarget.addAttribute("accepted", lastVisitAccepted);
  164. acceptCriterion.paintResponse(jsonPaintTarget);
  165. }
  166. jsonPaintTarget.endTag("dd");
  167. jsonPaintTarget.close();
  168. lastVisitId = -1;
  169. lastVisitAccepted = false;
  170. acceptCriterion = null;
  171. dragEvent = null;
  172. }
  173. }
  174. private boolean isDirty() {
  175. if (lastVisitId > 0) {
  176. return true;
  177. }
  178. return false;
  179. }
  180. public SharedState getState() {
  181. // TODO Auto-generated method stub
  182. return null;
  183. }
  184. public String getConnectorId() {
  185. return VDragAndDropManager.DD_SERVICE;
  186. }
  187. public boolean isConnectorEnabled() {
  188. // Drag'n'drop can't be disabled
  189. return true;
  190. }
  191. }