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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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.server;
  17. import java.io.IOException;
  18. import java.io.Writer;
  19. import java.util.Collection;
  20. import java.util.Collections;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25. import com.vaadin.event.Transferable;
  26. import com.vaadin.event.TransferableImpl;
  27. import com.vaadin.event.dd.DragAndDropEvent;
  28. import com.vaadin.event.dd.DragSource;
  29. import com.vaadin.event.dd.DropHandler;
  30. import com.vaadin.event.dd.DropTarget;
  31. import com.vaadin.event.dd.TargetDetails;
  32. import com.vaadin.event.dd.TargetDetailsImpl;
  33. import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
  34. import com.vaadin.shared.ApplicationConstants;
  35. import com.vaadin.shared.communication.SharedState;
  36. import com.vaadin.shared.ui.dd.DragEventType;
  37. import com.vaadin.ui.Component;
  38. import com.vaadin.ui.UI;
  39. import elemental.json.JsonObject;
  40. public class DragAndDropService implements VariableOwner, ClientConnector {
  41. private int lastVisitId;
  42. private boolean lastVisitAccepted = false;
  43. private DragAndDropEvent dragEvent;
  44. private final VaadinSession session;
  45. private AcceptCriterion acceptCriterion;
  46. private ErrorHandler errorHandler;
  47. public DragAndDropService(VaadinSession session) {
  48. this.session = session;
  49. }
  50. @Override
  51. public void changeVariables(Object source, Map<String, Object> variables) {
  52. Object owner = variables.get("dhowner");
  53. final Component sourceComponent = (Component) variables
  54. .get("component");
  55. if (sourceComponent != null && !sourceComponent.isEnabled()) {
  56. // source component not supposed to be enabled
  57. getLogger().warning(
  58. "Client dropped from " + sourceComponent
  59. + " even though it's disabled");
  60. return;
  61. }
  62. // Validate drop handler owner
  63. if (!(owner instanceof DropTarget)) {
  64. getLogger()
  65. .severe("DropHandler owner " + owner
  66. + " must implement DropTarget");
  67. return;
  68. }
  69. // owner cannot be null here
  70. DropTarget dropTarget = (DropTarget) owner;
  71. if (!dropTarget.isEnabled()) {
  72. getLogger()
  73. .warning(
  74. "Client dropped on " + owner
  75. + " even though it's disabled");
  76. return;
  77. }
  78. lastVisitId = (Integer) variables.get("visitId");
  79. // request may be dropRequest or request during drag operation (commonly
  80. // dragover or dragenter)
  81. boolean dropRequest = isDropRequest(variables);
  82. if (dropRequest) {
  83. handleDropRequest(dropTarget, variables);
  84. } else {
  85. handleDragRequest(dropTarget, variables);
  86. }
  87. }
  88. /**
  89. * Handles a drop request from the VDragAndDropManager.
  90. *
  91. * @param dropTarget
  92. * @param variables
  93. */
  94. private void handleDropRequest(DropTarget dropTarget,
  95. Map<String, Object> variables) {
  96. DropHandler dropHandler = (dropTarget).getDropHandler();
  97. if (dropHandler == null) {
  98. // No dropHandler returned so no drop can be performed.
  99. getLogger().log(Level.FINE,
  100. "DropTarget.getDropHandler() returned null for owner: {0}",
  101. dropTarget);
  102. return;
  103. }
  104. /*
  105. * Construct the Transferable and the DragDropDetails for the drop
  106. * operation based on the info passed from the client widgets (drag
  107. * source for Transferable, drop target for DragDropDetails).
  108. */
  109. Transferable transferable = constructTransferable(dropTarget, variables);
  110. TargetDetails dropData = constructDragDropDetails(dropTarget, variables);
  111. DragAndDropEvent dropEvent = new DragAndDropEvent(transferable,
  112. dropData);
  113. if (dropHandler.getAcceptCriterion().accept(dropEvent)) {
  114. dropHandler.drop(dropEvent);
  115. }
  116. }
  117. /**
  118. * Handles a drag/move request from the VDragAndDropManager.
  119. *
  120. * @param dropTarget
  121. * @param variables
  122. */
  123. private void handleDragRequest(DropTarget dropTarget,
  124. Map<String, Object> variables) {
  125. lastVisitId = (Integer) variables.get("visitId");
  126. acceptCriterion = dropTarget.getDropHandler().getAcceptCriterion();
  127. /*
  128. * Construct the Transferable and the DragDropDetails for the drag
  129. * operation based on the info passed from the client widgets (drag
  130. * source for Transferable, current target for DragDropDetails).
  131. */
  132. Transferable transferable = constructTransferable(dropTarget, variables);
  133. TargetDetails dragDropDetails = constructDragDropDetails(dropTarget,
  134. variables);
  135. dragEvent = new DragAndDropEvent(transferable, dragDropDetails);
  136. lastVisitAccepted = acceptCriterion.accept(dragEvent);
  137. }
  138. /**
  139. * Construct DragDropDetails based on variables from client drop target.
  140. * Uses DragDropDetailsTranslator if available, otherwise a default
  141. * DragDropDetails implementation is used.
  142. *
  143. * @param dropTarget
  144. * @param variables
  145. * @return
  146. */
  147. @SuppressWarnings("unchecked")
  148. private TargetDetails constructDragDropDetails(DropTarget dropTarget,
  149. Map<String, Object> variables) {
  150. Map<String, Object> rawDragDropDetails = (Map<String, Object>) variables
  151. .get("evt");
  152. TargetDetails dropData = dropTarget
  153. .translateDropTargetDetails(rawDragDropDetails);
  154. if (dropData == null) {
  155. // Create a default DragDropDetails with all the raw variables
  156. dropData = new TargetDetailsImpl(rawDragDropDetails, dropTarget);
  157. }
  158. return dropData;
  159. }
  160. private boolean isDropRequest(Map<String, Object> variables) {
  161. return getRequestType(variables) == DragEventType.DROP;
  162. }
  163. private DragEventType getRequestType(Map<String, Object> variables) {
  164. int type = (Integer) variables.get("type");
  165. return DragEventType.values()[type];
  166. }
  167. @SuppressWarnings("unchecked")
  168. private Transferable constructTransferable(DropTarget dropHandlerOwner,
  169. Map<String, Object> variables) {
  170. final Component sourceComponent = (Component) variables
  171. .get("component");
  172. variables = (Map<String, Object>) variables.get("tra");
  173. Transferable transferable = null;
  174. if (sourceComponent != null && sourceComponent instanceof DragSource) {
  175. transferable = ((DragSource) sourceComponent)
  176. .getTransferable(variables);
  177. }
  178. if (transferable == null) {
  179. transferable = new TransferableImpl(sourceComponent, variables);
  180. }
  181. return transferable;
  182. }
  183. @Override
  184. public boolean isEnabled() {
  185. return isConnectorEnabled();
  186. }
  187. @Override
  188. public boolean isImmediate() {
  189. return true;
  190. }
  191. public void printJSONResponse(Writer outWriter) throws IOException {
  192. if (isDirty()) {
  193. outWriter.write(", \"dd\":");
  194. JsonPaintTarget jsonPaintTarget = new JsonPaintTarget(
  195. session.getCommunicationManager(), outWriter, false);
  196. jsonPaintTarget.startTag("dd");
  197. jsonPaintTarget.addAttribute("visitId", lastVisitId);
  198. if (acceptCriterion != null) {
  199. jsonPaintTarget.addAttribute("accepted", lastVisitAccepted);
  200. acceptCriterion.paintResponse(jsonPaintTarget);
  201. }
  202. jsonPaintTarget.endTag("dd");
  203. jsonPaintTarget.close();
  204. lastVisitId = -1;
  205. lastVisitAccepted = false;
  206. acceptCriterion = null;
  207. dragEvent = null;
  208. }
  209. }
  210. private boolean isDirty() {
  211. if (lastVisitId > 0) {
  212. return true;
  213. }
  214. return false;
  215. }
  216. @Override
  217. public String getConnectorId() {
  218. return ApplicationConstants.DRAG_AND_DROP_CONNECTOR_ID;
  219. }
  220. @Override
  221. public boolean isConnectorEnabled() {
  222. // Drag'n'drop can't be disabled
  223. return true;
  224. }
  225. @Override
  226. public List<ClientMethodInvocation> retrievePendingRpcCalls() {
  227. return null;
  228. }
  229. @Override
  230. public ServerRpcManager<?> getRpcManager(String interfaceName) {
  231. // TODO Use rpc for drag'n'drop
  232. return null;
  233. }
  234. @Override
  235. public Class<? extends SharedState> getStateType() {
  236. return SharedState.class;
  237. }
  238. @Override
  239. @Deprecated
  240. public void requestRepaint() {
  241. markAsDirty();
  242. }
  243. @Override
  244. public void markAsDirty() {
  245. // TODO Auto-generated method stub
  246. }
  247. @Override
  248. public ClientConnector getParent() {
  249. // TODO Auto-generated method stub
  250. return null;
  251. }
  252. @Override
  253. @Deprecated
  254. public void requestRepaintAll() {
  255. markAsDirtyRecursive();
  256. }
  257. @Override
  258. public void markAsDirtyRecursive() {
  259. // TODO Auto-generated method stub
  260. }
  261. @Override
  262. public void attach() {
  263. // TODO Auto-generated method stub
  264. }
  265. @Override
  266. public void detach() {
  267. // TODO Auto-generated method stub
  268. }
  269. @Override
  270. public Collection<Extension> getExtensions() {
  271. // TODO Auto-generated method stub
  272. return Collections.emptySet();
  273. }
  274. @Override
  275. public void removeExtension(Extension extension) {
  276. // TODO Auto-generated method stub
  277. }
  278. private Logger getLogger() {
  279. return Logger.getLogger(DragAndDropService.class.getName());
  280. }
  281. @Override
  282. public UI getUI() {
  283. return null;
  284. }
  285. @Override
  286. public void beforeClientResponse(boolean initial) {
  287. // Nothing to do
  288. }
  289. @Override
  290. public JsonObject encodeState() {
  291. // TODO Auto-generated method stub
  292. return null;
  293. }
  294. @Override
  295. public boolean handleConnectorRequest(VaadinRequest request,
  296. VaadinResponse response, String path) throws IOException {
  297. return false;
  298. }
  299. @Override
  300. public ErrorHandler getErrorHandler() {
  301. return errorHandler;
  302. }
  303. @Override
  304. public void setErrorHandler(ErrorHandler errorHandler) {
  305. this.errorHandler = errorHandler;
  306. }
  307. @Override
  308. public void addAttachListener(AttachListener listener) {
  309. }
  310. @Override
  311. public void removeAttachListener(AttachListener listener) {
  312. }
  313. @Override
  314. public void addDetachListener(DetachListener listener) {
  315. }
  316. @Override
  317. public void removeDetachListener(DetachListener listener) {
  318. }
  319. /*
  320. * (non-Javadoc)
  321. *
  322. * @see com.vaadin.server.ClientConnector#isAttached()
  323. */
  324. @Override
  325. public boolean isAttached() {
  326. return true;
  327. }
  328. }