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.

DragAndDropWrapper.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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.ui;
  17. import java.io.OutputStream;
  18. import java.util.HashMap;
  19. import java.util.HashSet;
  20. import java.util.Iterator;
  21. import java.util.LinkedHashMap;
  22. import java.util.Map;
  23. import java.util.Map.Entry;
  24. import java.util.Set;
  25. import org.jsoup.nodes.Element;
  26. import com.vaadin.event.Transferable;
  27. import com.vaadin.event.TransferableImpl;
  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.server.PaintException;
  34. import com.vaadin.server.PaintTarget;
  35. import com.vaadin.server.StreamVariable;
  36. import com.vaadin.shared.MouseEventDetails;
  37. import com.vaadin.shared.ui.dd.HorizontalDropLocation;
  38. import com.vaadin.shared.ui.dd.VerticalDropLocation;
  39. import com.vaadin.shared.ui.draganddropwrapper.DragAndDropWrapperConstants;
  40. import com.vaadin.ui.declarative.DesignContext;
  41. @SuppressWarnings("serial")
  42. public class DragAndDropWrapper extends CustomComponent implements DropTarget,
  43. DragSource, LegacyComponent {
  44. public class WrapperTransferable extends TransferableImpl {
  45. private Html5File[] files;
  46. public WrapperTransferable(Component sourceComponent,
  47. Map<String, Object> rawVariables) {
  48. super(sourceComponent, rawVariables);
  49. Integer fc = (Integer) rawVariables.get("filecount");
  50. if (fc != null) {
  51. files = new Html5File[fc];
  52. for (int i = 0; i < fc; i++) {
  53. Html5File file = new Html5File(
  54. (String) rawVariables.get("fn" + i), // name
  55. ((Double) rawVariables.get("fs" + i)).longValue(), // size
  56. (String) rawVariables.get("ft" + i)); // mime
  57. String id = (String) rawVariables.get("fi" + i);
  58. files[i] = file;
  59. receivers.put(id, new ProxyReceiver(id, file));
  60. markAsDirty(); // paint Receivers
  61. }
  62. }
  63. }
  64. /**
  65. * The component in wrapper that is being dragged or null if the
  66. * transferable is not a component (most likely an html5 drag).
  67. *
  68. * @return
  69. */
  70. public Component getDraggedComponent() {
  71. Component object = (Component) getData("component");
  72. return object;
  73. }
  74. /**
  75. * @return the mouse down event that started the drag and drop operation
  76. */
  77. public MouseEventDetails getMouseDownEvent() {
  78. return MouseEventDetails.deSerialize((String) getData("mouseDown"));
  79. }
  80. public Html5File[] getFiles() {
  81. return files;
  82. }
  83. public String getText() {
  84. String data = (String) getData("Text"); // IE, html5
  85. if (data == null) {
  86. // check for "text/plain" (webkit)
  87. data = (String) getData("text/plain");
  88. }
  89. return data;
  90. }
  91. public String getHtml() {
  92. String data = (String) getData("Html"); // IE, html5
  93. if (data == null) {
  94. // check for "text/plain" (webkit)
  95. data = (String) getData("text/html");
  96. }
  97. return data;
  98. }
  99. }
  100. private Map<String, ProxyReceiver> receivers = new HashMap<String, ProxyReceiver>();
  101. public class WrapperTargetDetails extends TargetDetailsImpl {
  102. public WrapperTargetDetails(Map<String, Object> rawDropData) {
  103. super(rawDropData, DragAndDropWrapper.this);
  104. }
  105. /**
  106. * @return the absolute position of wrapper on the page
  107. */
  108. public Integer getAbsoluteLeft() {
  109. return (Integer) getData("absoluteLeft");
  110. }
  111. /**
  112. *
  113. * @return the absolute position of wrapper on the page
  114. */
  115. public Integer getAbsoluteTop() {
  116. return (Integer) getData("absoluteTop");
  117. }
  118. /**
  119. * @return a detail about the drags vertical position over the wrapper.
  120. */
  121. public VerticalDropLocation getVerticalDropLocation() {
  122. return VerticalDropLocation
  123. .valueOf((String) getData("verticalLocation"));
  124. }
  125. /**
  126. * @return a detail about the drags horizontal position over the
  127. * wrapper.
  128. */
  129. public HorizontalDropLocation getHorizontalDropLocation() {
  130. return HorizontalDropLocation
  131. .valueOf((String) getData("horizontalLocation"));
  132. }
  133. }
  134. public enum DragStartMode {
  135. /**
  136. * {@link DragAndDropWrapper} does not start drag events at all
  137. */
  138. NONE,
  139. /**
  140. * The component on which the drag started will be shown as drag image.
  141. */
  142. COMPONENT,
  143. /**
  144. * The whole wrapper is used as a drag image when dragging.
  145. */
  146. WRAPPER,
  147. /**
  148. * The whole wrapper is used to start an HTML5 drag.
  149. *
  150. * NOTE: In Internet Explorer 6 to 8, this prevents user interactions
  151. * with the wrapper's contents. For example, clicking a button inside
  152. * the wrapper will no longer work.
  153. */
  154. HTML5,
  155. /**
  156. * Uses the component defined in
  157. * {@link #setDragImageComponent(Component)} as the drag image.
  158. */
  159. COMPONENT_OTHER,
  160. }
  161. private final Map<String, Object> html5DataFlavors = new LinkedHashMap<String, Object>();
  162. private DragStartMode dragStartMode = DragStartMode.NONE;
  163. private Component dragImageComponent = null;
  164. private Set<String> sentIds = new HashSet<String>();
  165. /**
  166. * This is an internal constructor. Use
  167. * {@link DragAndDropWrapper#DragAndDropWrapper(Component)} instead.
  168. */
  169. @Deprecated
  170. public DragAndDropWrapper() {
  171. super();
  172. }
  173. /**
  174. * Wraps given component in a {@link DragAndDropWrapper}.
  175. *
  176. * @param root
  177. * the component to be wrapped
  178. */
  179. public DragAndDropWrapper(Component root) {
  180. this();
  181. setCompositionRoot(root);
  182. }
  183. /**
  184. * Sets data flavors available in the DragAndDropWrapper is used to start an
  185. * HTML5 style drags. Most commonly the "Text" flavor should be set.
  186. * Multiple data types can be set.
  187. *
  188. * @param type
  189. * the string identifier of the drag "payload". E.g. "Text" or
  190. * "text/html"
  191. * @param value
  192. * the value
  193. */
  194. public void setHTML5DataFlavor(String type, Object value) {
  195. html5DataFlavors.put(type, value);
  196. markAsDirty();
  197. }
  198. @Override
  199. public void changeVariables(Object source, Map<String, Object> variables) {
  200. // TODO Remove once LegacyComponent is no longer implemented
  201. }
  202. @Override
  203. public void paintContent(PaintTarget target) throws PaintException {
  204. target.addAttribute(DragAndDropWrapperConstants.DRAG_START_MODE,
  205. dragStartMode.ordinal());
  206. if (dragStartMode.equals(DragStartMode.COMPONENT_OTHER)) {
  207. if (dragImageComponent != null) {
  208. target.addAttribute(
  209. DragAndDropWrapperConstants.DRAG_START_COMPONENT_ATTRIBUTE,
  210. dragImageComponent.getConnectorId());
  211. } else {
  212. throw new IllegalArgumentException(
  213. "DragStartMode.COMPONENT_OTHER set but no component "
  214. + "was defined. Please set a component using DragAnd"
  215. + "DropWrapper.setDragStartComponent(Component).");
  216. }
  217. }
  218. if (getDropHandler() != null) {
  219. getDropHandler().getAcceptCriterion().paint(target);
  220. }
  221. if (receivers != null && receivers.size() > 0) {
  222. for (Iterator<Entry<String, ProxyReceiver>> it = receivers
  223. .entrySet().iterator(); it.hasNext();) {
  224. Entry<String, ProxyReceiver> entry = it.next();
  225. String id = entry.getKey();
  226. ProxyReceiver proxyReceiver = entry.getValue();
  227. Html5File html5File = proxyReceiver.file;
  228. if (html5File.getStreamVariable() != null) {
  229. if (!sentIds.contains(id)) {
  230. target.addVariable(this, "rec-" + id,
  231. new ProxyReceiver(id, html5File));
  232. /*
  233. * if a new batch is requested to be uploaded before the
  234. * last one is done, any remaining ids will be replayed.
  235. * We want to avoid a new ProxyReceiver to be made since
  236. * it'll get a new URL, so we need to keep extra track
  237. * on what has been sent.
  238. *
  239. * See #12330.
  240. */
  241. sentIds.add(id);
  242. // these are cleaned from receivers once the upload has
  243. // started
  244. }
  245. } else {
  246. // instructs the client side not to send the file
  247. target.addVariable(this, "rec-" + id, (String) null);
  248. // forget the file from subsequent paints
  249. it.remove();
  250. }
  251. }
  252. }
  253. target.addAttribute(DragAndDropWrapperConstants.HTML5_DATA_FLAVORS,
  254. html5DataFlavors);
  255. }
  256. private DropHandler dropHandler;
  257. @Override
  258. public DropHandler getDropHandler() {
  259. return dropHandler;
  260. }
  261. public void setDropHandler(DropHandler dropHandler) {
  262. this.dropHandler = dropHandler;
  263. markAsDirty();
  264. }
  265. @Override
  266. public TargetDetails translateDropTargetDetails(
  267. Map<String, Object> clientVariables) {
  268. return new WrapperTargetDetails(clientVariables);
  269. }
  270. @Override
  271. public Transferable getTransferable(final Map<String, Object> rawVariables) {
  272. return new WrapperTransferable(this, rawVariables);
  273. }
  274. public void setDragStartMode(DragStartMode dragStartMode) {
  275. this.dragStartMode = dragStartMode;
  276. markAsDirty();
  277. }
  278. public DragStartMode getDragStartMode() {
  279. return dragStartMode;
  280. }
  281. /**
  282. * Sets the component that will be used as the drag image. Only used when
  283. * wrapper is set to {@link DragStartMode#COMPONENT_OTHER}
  284. *
  285. * @param dragImageComponent
  286. */
  287. public void setDragImageComponent(Component dragImageComponent) {
  288. this.dragImageComponent = dragImageComponent;
  289. markAsDirty();
  290. }
  291. /**
  292. * Gets the component that will be used as the drag image. Only used when
  293. * wrapper is set to {@link DragStartMode#COMPONENT_OTHER}
  294. *
  295. * @return <code>null</code> if no component is set.
  296. */
  297. public Component getDragImageComponent() {
  298. return dragImageComponent;
  299. }
  300. final class ProxyReceiver implements StreamVariable {
  301. private String id;
  302. private Html5File file;
  303. public ProxyReceiver(String id, Html5File file) {
  304. this.id = id;
  305. this.file = file;
  306. }
  307. private boolean listenProgressOfUploadedFile;
  308. @Override
  309. public OutputStream getOutputStream() {
  310. if (file.getStreamVariable() == null) {
  311. return null;
  312. }
  313. return file.getStreamVariable().getOutputStream();
  314. }
  315. @Override
  316. public boolean listenProgress() {
  317. return file.getStreamVariable().listenProgress();
  318. }
  319. @Override
  320. public void onProgress(StreamingProgressEvent event) {
  321. file.getStreamVariable().onProgress(
  322. new ReceivingEventWrapper(event));
  323. }
  324. @Override
  325. public void streamingStarted(StreamingStartEvent event) {
  326. listenProgressOfUploadedFile = file.getStreamVariable() != null;
  327. if (listenProgressOfUploadedFile) {
  328. file.getStreamVariable().streamingStarted(
  329. new ReceivingEventWrapper(event));
  330. }
  331. // no need tell to the client about this receiver on next paint
  332. receivers.remove(id);
  333. sentIds.remove(id);
  334. // let the terminal GC the streamvariable and not to accept other
  335. // file uploads to this variable
  336. event.disposeStreamVariable();
  337. }
  338. @Override
  339. public void streamingFinished(StreamingEndEvent event) {
  340. if (listenProgressOfUploadedFile) {
  341. file.getStreamVariable().streamingFinished(
  342. new ReceivingEventWrapper(event));
  343. }
  344. }
  345. @Override
  346. public void streamingFailed(final StreamingErrorEvent event) {
  347. if (listenProgressOfUploadedFile) {
  348. file.getStreamVariable().streamingFailed(
  349. new ReceivingEventWrapper(event));
  350. }
  351. }
  352. @Override
  353. public boolean isInterrupted() {
  354. return file.getStreamVariable().isInterrupted();
  355. }
  356. /*
  357. * With XHR2 file posts we can't provide as much information from the
  358. * terminal as with multipart request. This helper class wraps the
  359. * terminal event and provides the lacking information from the
  360. * Html5File.
  361. */
  362. class ReceivingEventWrapper implements StreamingErrorEvent,
  363. StreamingEndEvent, StreamingStartEvent, StreamingProgressEvent {
  364. private StreamingEvent wrappedEvent;
  365. ReceivingEventWrapper(StreamingEvent e) {
  366. wrappedEvent = e;
  367. }
  368. @Override
  369. public String getMimeType() {
  370. return file.getType();
  371. }
  372. @Override
  373. public String getFileName() {
  374. return file.getFileName();
  375. }
  376. @Override
  377. public long getContentLength() {
  378. return file.getFileSize();
  379. }
  380. public StreamVariable getReceiver() {
  381. return ProxyReceiver.this;
  382. }
  383. @Override
  384. public Exception getException() {
  385. if (wrappedEvent instanceof StreamingErrorEvent) {
  386. return ((StreamingErrorEvent) wrappedEvent).getException();
  387. }
  388. return null;
  389. }
  390. @Override
  391. public long getBytesReceived() {
  392. return wrappedEvent.getBytesReceived();
  393. }
  394. /**
  395. * Calling this method has no effect. DD files are receive only once
  396. * anyway.
  397. */
  398. @Override
  399. public void disposeStreamVariable() {
  400. }
  401. }
  402. }
  403. @Override
  404. public void readDesign(Element design, DesignContext designContext) {
  405. super.readDesign(design, designContext);
  406. for (Element child : design.children()) {
  407. Component component = designContext.readDesign(child);
  408. if (getDragStartMode() == DragStartMode.COMPONENT_OTHER
  409. && child.hasAttr(":drag-image")) {
  410. setDragImageComponent(component);
  411. } else if (getCompositionRoot() == null) {
  412. setCompositionRoot(component);
  413. }
  414. }
  415. }
  416. @Override
  417. public void writeDesign(Element design, DesignContext designContext) {
  418. super.writeDesign(design, designContext);
  419. design.appendChild(designContext.createElement(getCompositionRoot()));
  420. if (getDragStartMode() == DragStartMode.COMPONENT_OTHER) {
  421. Element child = designContext
  422. .createElement(getDragImageComponent());
  423. child.attr(":drag-image", "");
  424. design.appendChild(child);
  425. }
  426. }
  427. }