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.

DragDropPane.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package com.vaadin.ui;
  2. import java.util.Map;
  3. import com.vaadin.event.DataBoundTransferable;
  4. import com.vaadin.event.Transferable;
  5. import com.vaadin.event.dd.DragAndDropEvent;
  6. import com.vaadin.event.dd.DropHandler;
  7. import com.vaadin.event.dd.DropTarget;
  8. import com.vaadin.event.dd.DropTargetDetails;
  9. import com.vaadin.event.dd.DropTargetDetailsImpl;
  10. import com.vaadin.event.dd.acceptCriteria.AcceptAll;
  11. import com.vaadin.event.dd.acceptCriteria.AcceptCriterion;
  12. import com.vaadin.terminal.PaintException;
  13. import com.vaadin.terminal.PaintTarget;
  14. import com.vaadin.terminal.gwt.client.MouseEventDetails;
  15. /**
  16. * Test class that implements various component related Drag and Drop features.
  17. *
  18. * TODO consider implementing this kind of general purpose layout class:
  19. *
  20. * - extend simple component (CssLayout or CustomComponent, instead of absolute
  21. * layout)
  22. *
  23. * - implement both drag source and drop handler with server side api
  24. *
  25. * - implement html5 drop target etc
  26. *
  27. * - include a lots of details for drop event (coordinates & sizes of drop
  28. * position and widget/Paintable hierarchy up to drop handler)
  29. *
  30. * This way we could have one rather complex dd component that could be used (by
  31. * wrapping layouts) in most common situations with server side api. Core
  32. * layouts wouldn't need changes (and have regression risk/ performance
  33. * penalty).
  34. *
  35. * @deprecated use {@link DragAndDropWrapper} instead
  36. *
  37. */
  38. @Deprecated
  39. @SuppressWarnings("serial")
  40. @ClientWidget(com.vaadin.terminal.gwt.client.ui.VDragDropPane.class)
  41. public class DragDropPane extends AbsoluteLayout implements DropTarget {
  42. private DropHandler dropHandler;
  43. public DragDropPane(DropHandler dropHandler) {
  44. setWidth("400px");
  45. setHeight("300px");
  46. if (dropHandler == null) {
  47. this.dropHandler = new ImportPrettyMuchAnything();
  48. } else {
  49. this.dropHandler = dropHandler;
  50. }
  51. }
  52. public DragDropPane() {
  53. this(null);
  54. }
  55. @Override
  56. public void paintContent(PaintTarget target) throws PaintException {
  57. super.paintContent(target);
  58. dropHandler.getAcceptCriterion().paint(target);
  59. }
  60. public DropHandler getDropHandler() {
  61. return dropHandler;
  62. }
  63. public static class ImportPrettyMuchAnything implements DropHandler {
  64. public void drop(DragAndDropEvent event) {
  65. DragDropPane pane = (DragDropPane) event.getDropTargetDetails()
  66. .getTarget();
  67. DragEventDetails ed = (DragEventDetails) event
  68. .getDropTargetDetails();
  69. Transferable ctr = event.getTransferable();
  70. if (ctr.getSourceComponent() != null) {
  71. // use "component" (from DragDropPane) if available, else take
  72. // the source component
  73. Component component = (Component) ctr.getData("component");
  74. if (component == null) {
  75. component = ctr.getSourceComponent();
  76. }
  77. if (component.getParent() != pane) {
  78. if (ctr instanceof DataBoundTransferable) {
  79. // Item has been dragged, construct a Label from
  80. // Item id
  81. Label l = new Label();
  82. l.setSizeUndefined();
  83. l.setValue("ItemId : "
  84. + ((DataBoundTransferable) ctr).getItemId());
  85. pane.addComponent(l);
  86. component = l;
  87. } else {
  88. // we have a component that is been dragged, add
  89. // it
  90. // to
  91. // this
  92. pane.addComponent(component);
  93. }
  94. Integer left = ed.getAbsoluteLeft();
  95. Integer top = ed.getAbsoluteTop();
  96. MouseEventDetails eventDetails = ed.getMouseEvent();
  97. int clientX = eventDetails.getClientX();
  98. int clientY = eventDetails.getClientY();
  99. try {
  100. pane.getPosition(component).setTopValue(clientY - top);
  101. pane.getPosition(component)
  102. .setLeftValue(clientX - left);
  103. } catch (Exception e) {
  104. // TODO: handle exception
  105. }
  106. } else {
  107. // drag ended inside the this Pane
  108. MouseEventDetails start = ed.getMouseDownEvent();
  109. MouseEventDetails eventDetails = ed.getMouseEvent();
  110. int deltaX = eventDetails.getClientX() - start.getClientX();
  111. int deltaY = eventDetails.getClientY() - start.getClientY();
  112. ComponentPosition p = pane.getPosition(component);
  113. p.setTopValue(p.getTopValue() + deltaY);
  114. p.setLeftValue(p.getLeftValue() + deltaX);
  115. }
  116. } else {
  117. // drag coming outside of Vaadin
  118. String object = (String) ctr.getData("text/plain");
  119. String content = (String) ctr.getData("fileContents");
  120. Label l = new Label();
  121. l.setCaption("Generated from HTML5 drag:");
  122. if (object != null) {
  123. l.setValue(object);
  124. } else {
  125. l.setValue("HTML5 dd");
  126. }
  127. l.setDescription(content);
  128. l.setSizeUndefined();
  129. pane.addComponent(l);
  130. }
  131. return;
  132. }
  133. public AcceptCriterion getAcceptCriterion() {
  134. return AcceptAll.get();
  135. }
  136. }
  137. class DragEventDetails extends DropTargetDetailsImpl {
  138. public DragEventDetails(Map<String, Object> rawVariables) {
  139. super(rawVariables);
  140. }
  141. public Integer getAbsoluteTop() {
  142. return (Integer) getData("absoluteTop");
  143. }
  144. public Integer getAbsoluteLeft() {
  145. return (Integer) getData("absoluteLeft");
  146. }
  147. public MouseEventDetails getMouseDownEvent() {
  148. return MouseEventDetails.deSerialize((String) getData("mouseDown"));
  149. }
  150. public MouseEventDetails getMouseEvent() {
  151. return MouseEventDetails
  152. .deSerialize((String) getData("mouseEvent"));
  153. }
  154. }
  155. public DropTargetDetails translateDropTargetDetails(
  156. Map<String, Object> clientVariables) {
  157. return new DragEventDetails(clientVariables);
  158. }
  159. public void setDropHandler(DropHandler dropHandler2) {
  160. dropHandler = dropHandler2;
  161. requestRepaint();
  162. }
  163. }