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.

DDTest1.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package com.vaadin.tests.dd;
  2. import java.util.Collection;
  3. import com.vaadin.data.Item;
  4. import com.vaadin.data.util.HierarchicalContainer;
  5. import com.vaadin.event.DataBoundTransferable;
  6. import com.vaadin.event.Transferable;
  7. import com.vaadin.event.dd.DragAndDropEvent;
  8. import com.vaadin.event.dd.DropHandler;
  9. import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
  10. import com.vaadin.event.dd.acceptcriteria.IsDataBound;
  11. import com.vaadin.event.dd.acceptcriteria.ServerSideCriterion;
  12. import com.vaadin.terminal.ExternalResource;
  13. import com.vaadin.terminal.gwt.client.ui.dd.VerticalDropLocation;
  14. import com.vaadin.tests.components.TestBase;
  15. import com.vaadin.ui.Component;
  16. import com.vaadin.ui.GridLayout;
  17. import com.vaadin.ui.Label;
  18. import com.vaadin.ui.Layout;
  19. import com.vaadin.ui.Link;
  20. import com.vaadin.ui.Table;
  21. import com.vaadin.ui.Tree;
  22. import com.vaadin.ui.Table.TableDragMode;
  23. import com.vaadin.ui.Tree.TreeDragMode;
  24. import com.vaadin.ui.Tree.TreeDropTargetDetails;
  25. /**
  26. * DD playground. Better quality example/prototype codes in {@link DDTest2}.
  27. */
  28. public class DDTest1 extends TestBase {
  29. @Override
  30. protected void setup() {
  31. GridLayout gl = new GridLayout(3, 2);
  32. gl.setSizeFull();
  33. gl.setSpacing(true);
  34. Layout main = gl;
  35. DragDropPane pane1 = new DragDropPane();
  36. pane1.setSizeFull();
  37. pane1.setCaption("Pane1");
  38. Label label = new Label("Foo");
  39. label.setSizeUndefined();
  40. pane1.addComponent(label);
  41. Link l = new Link("This is link", new ExternalResource(
  42. "http://www.google.com/"));
  43. pane1.addComponent(l, "top:100px; left: 20px;");
  44. label = new Label("Bar");
  45. label.setSizeUndefined();
  46. pane1.addComponent(label);
  47. DragDropPane pane2 = new DragDropPane();
  48. pane2
  49. .setCaption("Pane2 (accept needs server side visit, check for \"Bar\")");
  50. final AcceptCriterion crit = new ServerSideCriterion() {
  51. /**
  52. *
  53. */
  54. private static final long serialVersionUID = 1L;
  55. public boolean accept(DragAndDropEvent dragEvent) {
  56. Transferable transferable = dragEvent.getTransferable();
  57. // System.out.println("Simulating 500ms processing...");
  58. // try {
  59. // Thread.sleep(200);
  60. // } catch (InterruptedException e) {
  61. // // TODO Auto-generated catch block
  62. // e.printStackTrace();
  63. // }
  64. // System.out.println("Done get to work.");
  65. Component component = (Component) transferable
  66. .getData("component");
  67. if (component == null) {
  68. component = transferable.getSourceComponent();
  69. }
  70. if (component != null) {
  71. if (component.toString() != null
  72. && component.toString().contains("Bar")) {
  73. return true;
  74. }
  75. }
  76. return false;
  77. }
  78. };
  79. pane2.setAcceptCriterion(crit);
  80. pane2.setDebugId("pane2");
  81. pane2.setSizeFull();
  82. DragDropPane pane3 = new DragDropPane();
  83. pane3.setSizeFull();
  84. pane3.setCaption("Pane3");
  85. final Tree t = new Tree(
  86. "Tree with sorting enabled. Also allows dragging elsewhere.");
  87. final HierarchicalContainer idx = new HierarchicalContainer();
  88. t.setContainerDataSource(idx);
  89. t.setDebugId("perseys");
  90. t.addItem("Foo");
  91. t.addItem("Bar");
  92. t.addItem("Bar1");
  93. t.addItem("Bar2");
  94. t.addItem("Bar3");
  95. t.addItem("Bar4");
  96. t.addItem("Bar5");
  97. t.addItem("Child");
  98. t.setParent("Child", "Foo");
  99. t.setSizeFull();
  100. t.setDragMode(TreeDragMode.NODE);
  101. /*
  102. * Moves items in tree (and could work in Table too). Also supports
  103. * "building" tree.
  104. *
  105. * TODO fix algorithm, broken in some cases.
  106. */
  107. DropHandler itemSorter = new DropHandler() {
  108. private void populateSubTree(HierarchicalContainer idx,
  109. HierarchicalContainer subtree, Object itemId) {
  110. Collection children = subtree.getChildren(itemId);
  111. if (children != null) {
  112. for (Object childId : children) {
  113. Item addItem = idx.addItem(childId);
  114. if (addItem != null) {
  115. // did not exist, populate properties
  116. Item item = subtree.getItem(itemId);
  117. Collection<?> itemPropertyIds = item
  118. .getItemPropertyIds();
  119. for (Object propId : itemPropertyIds) {
  120. addItem.getItemProperty(propId)
  121. .setValue(
  122. item.getItemProperty(propId)
  123. .getValue());
  124. }
  125. }
  126. idx.setParent(childId, itemId);
  127. populateSubTree(idx, subtree, childId);
  128. }
  129. }
  130. }
  131. private HierarchicalContainer getSubTree(HierarchicalContainer idx,
  132. Object itemId) {
  133. HierarchicalContainer hierarchicalContainer = new HierarchicalContainer();
  134. Collection containerPropertyIds = idx.getContainerPropertyIds();
  135. for (Object object : containerPropertyIds) {
  136. hierarchicalContainer.addContainerProperty(object, idx
  137. .getType(object), null);
  138. }
  139. hierarchicalContainer.addItem(itemId);
  140. copyChildren(idx, hierarchicalContainer, itemId);
  141. return hierarchicalContainer;
  142. }
  143. private void copyChildren(HierarchicalContainer source,
  144. HierarchicalContainer target, Object itemId) {
  145. Collection children = source.getChildren(itemId);
  146. if (children != null) {
  147. for (Object childId : children) {
  148. Item item = source.getItem(childId);
  149. Item addedItem = target.addItem(childId);
  150. target.setParent(childId, itemId);
  151. Collection<?> itemPropertyIds = item
  152. .getItemPropertyIds();
  153. for (Object propertyId : itemPropertyIds) {
  154. addedItem.getItemProperty(propertyId)
  155. .setValue(
  156. item.getItemProperty(propertyId)
  157. .getValue());
  158. }
  159. copyChildren(source, target, childId);
  160. }
  161. }
  162. }
  163. public void drop(DragAndDropEvent event) {
  164. TreeDropTargetDetails details = (TreeDropTargetDetails) event
  165. .getDropTargetDetails();
  166. // TODO set properties, so same sorter could be used in Table
  167. Transferable transferable = event.getTransferable();
  168. if (transferable instanceof DataBoundTransferable) {
  169. DataBoundTransferable transferrable2 = (DataBoundTransferable) transferable;
  170. Object itemId = transferrable2.getItemId();
  171. Object itemIdOver = details.getItemIdOver();
  172. // TODO could use the "folder" node id to make the drop
  173. // logic simpler
  174. Object itemIdInto = details.getItemIdInto();
  175. VerticalDropLocation dropLocation = details
  176. .getDropLocation();
  177. Object itemIdAfter = details.getItemIdAfter();
  178. if (itemIdOver.equals(itemIdInto)) { // directly on a node
  179. t.setParent(itemId, itemIdOver);
  180. return;
  181. }
  182. idx.setParent(itemId, itemIdInto);
  183. if (dropLocation == null) {
  184. System.err.println("No detail of drop place available");
  185. }
  186. idx.moveAfterSibling(itemId, itemIdAfter);
  187. }
  188. return;
  189. }
  190. public AcceptCriterion getAcceptCriterion() {
  191. // TODO should actually check that source is same as target
  192. return IsDataBound.get();
  193. }
  194. };
  195. t.setDropHandler(itemSorter);
  196. Table ta = new Table("Test table");
  197. ta.setContainerDataSource(idx);
  198. ta.addContainerProperty("Foos", String.class, "Foo");
  199. ta.addContainerProperty("Bars", String.class, "Bar");
  200. ta.setRowHeaderMode(Table.ROW_HEADER_MODE_ID);
  201. ta.setSizeFull();
  202. ta.setDragMode(TableDragMode.ROWS);
  203. main.addComponent(pane1);
  204. main.addComponent(pane2);
  205. main.addComponent(pane3);
  206. main.addComponent(t);
  207. main.addComponent(ta);
  208. main.addComponent(new Link("Foo", new ExternalResource(
  209. "http://www.itmill.com/")));
  210. getLayout().setSizeFull();
  211. addComponent(main);
  212. }
  213. @Override
  214. protected String getDescription() {
  215. return "Random DD tests";
  216. }
  217. @Override
  218. protected Integer getTicketNumber() {
  219. return 119;
  220. }
  221. }