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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package com.vaadin.tests.dd;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  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.AcceptAll;
  10. import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
  11. import com.vaadin.terminal.gwt.client.MouseEventDetails;
  12. import com.vaadin.ui.AbsoluteLayout;
  13. import com.vaadin.ui.Component;
  14. import com.vaadin.ui.DragAndDropWrapper;
  15. import com.vaadin.ui.Label;
  16. import com.vaadin.ui.AbsoluteLayout.ComponentPosition;
  17. import com.vaadin.ui.DragAndDropWrapper.WrapperTransferable.Html5File;
  18. import com.vaadin.ui.Upload.Receiver;
  19. /**
  20. * replacement for a proto class to keep tests working
  21. */
  22. public class DragDropPane extends DragAndDropWrapper implements DropHandler {
  23. private AbsoluteLayout root;
  24. private AcceptCriterion crit;
  25. public DragDropPane() {
  26. super(new AbsoluteLayout());
  27. root = (AbsoluteLayout) getCompositionRoot();
  28. setDropHandler(this);
  29. setDragStartMode(DragStartMode.COMPONENT);
  30. }
  31. @Override
  32. public void addComponent(Component c) {
  33. root.addComponent(c);
  34. }
  35. /**
  36. *
  37. */
  38. private static final long serialVersionUID = 1L;
  39. public void addComponent(Component l, String string) {
  40. root.addComponent(l, string);
  41. }
  42. public void setAcceptCriterion(AcceptCriterion crit) {
  43. this.crit = crit;
  44. }
  45. public void drop(DragAndDropEvent event) {
  46. WrapperDropDetails ed = (WrapperDropDetails) event
  47. .getDropTargetDetails();
  48. Transferable ctr = event.getTransferable();
  49. // use "component" (from DragDropPane) if available, else take
  50. // the source component
  51. Component component = null;
  52. if (ctr instanceof WrapperTransferable) {
  53. component = ((WrapperTransferable) ctr).getDraggedComponent();
  54. } else if (ctr instanceof DataBoundTransferable) {
  55. // Item has been dragged, construct a Label from
  56. // Item id
  57. Label l = new Label();
  58. l.setSizeUndefined();
  59. l.setValue("ItemId : " + ((DataBoundTransferable) ctr).getItemId());
  60. component = l;
  61. }
  62. if (component != null) {
  63. if (component.getParent() != root) {
  64. root.addComponent(component);
  65. Integer left = ed.getAbsoluteLeft();
  66. Integer top = ed.getAbsoluteTop();
  67. MouseEventDetails eventDetails = ed.getMouseEvent();
  68. int clientX = eventDetails.getClientX();
  69. int clientY = eventDetails.getClientY();
  70. try {
  71. root.getPosition(component).setTopValue(clientY - top);
  72. root.getPosition(component).setLeftValue(clientX - left);
  73. } catch (Exception e) {
  74. // TODO: handle exception
  75. }
  76. } else {
  77. // drag started and ended inside the this Pane
  78. MouseEventDetails start = ((WrapperTransferable) event
  79. .getTransferable()).getMouseDownEvent();
  80. MouseEventDetails eventDetails = ed.getMouseEvent();
  81. int deltaX = eventDetails.getClientX() - start.getClientX();
  82. int deltaY = eventDetails.getClientY() - start.getClientY();
  83. ComponentPosition p = root.getPosition(component);
  84. p.setTopValue(p.getTopValue() + deltaY);
  85. p.setLeftValue(p.getLeftValue() + deltaX);
  86. }
  87. }
  88. else {
  89. // drag coming outside of Vaadin
  90. WrapperTransferable wtr = (WrapperTransferable) ctr;
  91. String object = wtr.getText();
  92. String html = wtr.getHtml();
  93. String url = (String) ctr.getData("Url");
  94. final Label l = new Label();
  95. l.setCaption("Generated from HTML5 drag:");
  96. if (object != null) {
  97. if (object.length() > 80) {
  98. object = object.substring(0, 79);
  99. }
  100. l.setValue(object);
  101. } else {
  102. l.setValue("HTML5 dd");
  103. }
  104. Html5File[] files = wtr.getFiles();
  105. if (files != null) {
  106. for (Html5File html5File : files) {
  107. l.setCaption(html5File.getFileName());
  108. html5File.setReceiver(new Receiver() {
  109. public OutputStream receiveUpload(String filename,
  110. String MIMEType) {
  111. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream() {
  112. @Override
  113. public void close() throws IOException {
  114. super.close();
  115. l.setValue((new String(toByteArray())
  116. .substring(0, 80) + "..."));
  117. }
  118. };
  119. return byteArrayOutputStream;
  120. }
  121. });
  122. }
  123. }
  124. l.setSizeUndefined();
  125. root.addComponent(l);
  126. }
  127. return;
  128. }
  129. public AcceptCriterion getAcceptCriterion() {
  130. return crit != null ? crit : AcceptAll.get();
  131. }
  132. }