From: Matti Tahvonen Date: Mon, 15 Mar 2010 13:04:35 +0000 (+0000) Subject: removed DragDropPane class, some cleanup X-Git-Tag: 6.7.0.beta1~1927 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c7bc85335cf48276eede6d53961fcfceaec49df7;p=vaadin-framework.git removed DragDropPane class, some cleanup svn changeset:11874/svn branch:6.3 --- diff --git a/src/com/vaadin/event/dd/DropHandler.java b/src/com/vaadin/event/dd/DropHandler.java index cf6d850b7b..555bd7136b 100644 --- a/src/com/vaadin/event/dd/DropHandler.java +++ b/src/com/vaadin/event/dd/DropHandler.java @@ -16,7 +16,7 @@ import com.vaadin.event.dd.acceptCriteria.AcceptCriterion; */ public interface DropHandler extends Serializable { - public void drop(DragAndDropEvent dropEvent); + public void drop(DragAndDropEvent event); /** * Returns the {@link AcceptCriterion} used to evaluate whether the diff --git a/src/com/vaadin/terminal/gwt/client/ui/VDragAndDropWrapper.java b/src/com/vaadin/terminal/gwt/client/ui/VDragAndDropWrapper.java index 606b382c26..990dee9f3c 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VDragAndDropWrapper.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VDragAndDropWrapper.java @@ -226,7 +226,6 @@ public class VDragAndDropWrapper extends VCustomComponent implements } /** - * * Currently supports only FF36 as no other browser supprots natively File * api. * @@ -238,42 +237,17 @@ public class VDragAndDropWrapper extends VCustomComponent implements public void execute() { /* * File contents is sent deferred to allow quick reaction on GUI - * although file upload may last long. TODO make this use apache - * file upload instead of our variable post like in upload. - * Currently stalls the GUI during upload. Also need to use - * dataurl to support all possible bytes in file content + * although file upload may last long. */ file.readAsBinary(new Callback() { public void handleFile(final JavaScriptObject object) { - DeferredCommand.addCommand(new Command() { - - public void execute() { - - ExtendedXHR extendedXHR = (ExtendedXHR) ExtendedXHR - .create(); - extendedXHR.open("POST", client.getAppUri()); - extendedXHR - .setRequestHeader( - "PaintableId", - client - .getPid(VDragAndDropWrapper.this)); - extendedXHR.setRequestHeader("FileId", "" - + fileId); - - // extendedXHR.setRequestHeader("Connection", - // "close"); - - multipartSend( - extendedXHR, - object, - "XHRFILE" - + client - .getPid(VDragAndDropWrapper.this) - + "." + fileId); - - } - }); + ExtendedXHR extendedXHR = (ExtendedXHR) ExtendedXHR + .create(); + extendedXHR.open("POST", client.getAppUri()); + String name = "XHRFILE" + getPid() + "." + fileId; + multipartSend(extendedXHR, object, name); + } }); @@ -282,6 +256,10 @@ public class VDragAndDropWrapper extends VCustomComponent implements } + private String getPid() { + return client.getPid(this); + } + private native void multipartSend(JavaScriptObject xhr, JavaScriptObject data, String name) /*-{ diff --git a/src/com/vaadin/terminal/gwt/client/ui/VDragDropPane.java b/src/com/vaadin/terminal/gwt/client/ui/VDragDropPane.java deleted file mode 100644 index a93d73e8da..0000000000 --- a/src/com/vaadin/terminal/gwt/client/ui/VDragDropPane.java +++ /dev/null @@ -1,274 +0,0 @@ -/* -@ITMillApache2LicenseForJavaFiles@ - */ -package com.vaadin.terminal.gwt.client.ui; - -import java.util.Map; - -import com.google.gwt.core.client.JsArrayString; -import com.google.gwt.dom.client.EventTarget; -import com.google.gwt.event.dom.client.MouseDownEvent; -import com.google.gwt.event.dom.client.MouseDownHandler; -import com.google.gwt.user.client.Command; -import com.google.gwt.user.client.DeferredCommand; -import com.google.gwt.user.client.Element; -import com.google.gwt.user.client.ui.Widget; -import com.vaadin.terminal.gwt.client.ApplicationConnection; -import com.vaadin.terminal.gwt.client.Container; -import com.vaadin.terminal.gwt.client.MouseEventDetails; -import com.vaadin.terminal.gwt.client.Paintable; -import com.vaadin.terminal.gwt.client.UIDL; -import com.vaadin.terminal.gwt.client.ui.dd.VAbstractDropHandler; -import com.vaadin.terminal.gwt.client.ui.dd.VDragAndDropManager; -import com.vaadin.terminal.gwt.client.ui.dd.VDragEvent; -import com.vaadin.terminal.gwt.client.ui.dd.VHasDropHandler; -import com.vaadin.terminal.gwt.client.ui.dd.VHtml5DragEvent; -import com.vaadin.terminal.gwt.client.ui.dd.VTransferable; - -public class VDragDropPane extends VAbsoluteLayout implements Container, - VHasDropHandler { - - private String paintableId; - - /** - * DragEvent is stored here in case of HTML5 drag event. - */ - private VDragEvent vaadinDragEvent; - - public VDragDropPane() { - super(); - addDomHandler(new MouseDownHandler() { - public void onMouseDown(MouseDownEvent event) { - EventTarget eventTarget = event.getNativeEvent() - .getEventTarget(); - Paintable paintable = client.getPaintable((Element) eventTarget - .cast()); - if (paintable != null) { - VTransferable transferable = new VTransferable(); - transferable.setDragSource(VDragDropPane.this); - transferable.setData("component", paintable); - VDragEvent drag = VDragAndDropManager.get().startDrag( - transferable, event.getNativeEvent(), true); - drag.createDragImage(((Widget) paintable).getElement(), - true); - drag.getDropDetails().put( - "mouseDown", - new MouseEventDetails(event.getNativeEvent()) - .serialize()); - event.preventDefault(); // prevent text selection - } - } - }, MouseDownEvent.getType()); - - hookHtml5Events(getElement()); - - getStyleElement().getStyle().setBackgroundColor("yellow"); - - } - - /** - * Prototype code, memory leak risk. - * - * @param el - */ - private native void hookHtml5Events(Element el) - /*-{ - - var me = this; - - if(el.addEventListener) { - el.addEventListener("dragenter", function(ev) { - return me.@com.vaadin.terminal.gwt.client.ui.VDragDropPane::html5DragEnter(Lcom/vaadin/terminal/gwt/client/ui/dd/VHtml5DragEvent;)(ev); - }, false); - - el.addEventListener("dragleave", function(ev) { - return me.@com.vaadin.terminal.gwt.client.ui.VDragDropPane::html5DragLeave(Lcom/vaadin/terminal/gwt/client/ui/dd/VHtml5DragEvent;)(ev); - }, false); - - el.addEventListener("dragover", function(ev) { - return me.@com.vaadin.terminal.gwt.client.ui.VDragDropPane::html5DragOver(Lcom/vaadin/terminal/gwt/client/ui/dd/VHtml5DragEvent;)(ev); - }, false); - - el.addEventListener("drop", function(ev) { - return me.@com.vaadin.terminal.gwt.client.ui.VDragDropPane::html5DragDrop(Lcom/vaadin/terminal/gwt/client/ui/dd/VHtml5DragEvent;)(ev); - }, false); - - } else { - el.attachEvent("ondragenter", function(ev) { - return me.@com.vaadin.terminal.gwt.client.ui.VDragDropPane::html5DragEnter(Lcom/vaadin/terminal/gwt/client/ui/dd/VHtml5DragEvent;)(ev); - }); - - el.attachEvent("ondragleave", function(ev) { - return me.@com.vaadin.terminal.gwt.client.ui.VDragDropPane::html5DragLeave(Lcom/vaadin/terminal/gwt/client/ui/dd/VHtml5DragEvent;)(ev); - }); - - el.attachEvent("ondragover", function(ev) { - return me.@com.vaadin.terminal.gwt.client.ui.VDragDropPane::html5DragOver(Lcom/vaadin/terminal/gwt/client/ui/dd/VHtml5DragEvent;)(ev); - }); - - el.attachEvent("ondrop", function(ev) { - return me.@com.vaadin.terminal.gwt.client.ui.VDragDropPane::html5DragDrop(Lcom/vaadin/terminal/gwt/client/ui/dd/VHtml5DragEvent;)(ev); - }); - } - - }-*/; - - public boolean html5DragEnter(VHtml5DragEvent event) { - ApplicationConnection.getConsole().log("HTML 5 Drag Enter"); - VTransferable transferable = new VTransferable(); - - // TODO refine api somehow so that we will now not use the event preview - // method provided by manager - vaadinDragEvent = VDragAndDropManager.get().startDrag(transferable, - event, false); - event.preventDefault(); - event.stopPropagation(); - return false; - } - - public boolean html5DragLeave(VHtml5DragEvent event) { - ApplicationConnection.getConsole().log("HTML 5 Drag Leave posponed..."); - DeferredCommand.addCommand(new Command() { - public void execute() { - // Yes, dragleave happens before drop. Makes no sense to me. - // IMO shouldn't fire leave at all if drop happens (I guess this - // is what IE does). - // In Vaadin we fire it only if drop did not happen. - if (vaadinDragEvent != null) { - ApplicationConnection.getConsole().log( - "...HTML 5 Drag Leave"); - getDropHandler().dragLeave(vaadinDragEvent); - } - } - }); - event.preventDefault(); - event.stopPropagation(); - return false; - } - - public boolean html5DragOver(VHtml5DragEvent event) { - ApplicationConnection.getConsole().log("HTML 5 Drag Over"); - getDropHandler().dragOver(vaadinDragEvent); - // needed to be set for Safari, otherwise drop will not happen - String s = event.getEffectAllowed(); - if ("all".equals(s) || s.contains("opy")) { - event.setDragEffect("copy"); - } else { - event.setDragEffect(s); - ApplicationConnection.getConsole().log("Drag effect set to " + s); - } - event.preventDefault(); - event.stopPropagation(); - return false; - } - - public boolean html5DragDrop(VHtml5DragEvent event) { - ApplicationConnection.getConsole().log("HTML 5 Drag Drop"); - VTransferable transferable = vaadinDragEvent.getTransferable(); - - JsArrayString types = event.getTypes(); - for (int i = 0; i < types.length(); i++) { - String type = types.get(i); - ApplicationConnection.getConsole().log("Type: " + type); - if ("text/plain".equals(type)) { - String data = event.getDataAsText(type); - ApplicationConnection.getConsole().log(type + " : " + data); - transferable.setData("text/plain", data); - } - } - - String fileAsString = event.getFileAsString(0); - if (fileAsString != null) { - ApplicationConnection.getConsole().log(fileAsString); - transferable.setData("fileContents", fileAsString); - } - - VDragAndDropManager.get().endDrag(); - vaadinDragEvent = null; - event.preventDefault(); - event.stopPropagation(); - - return false; - } - - @Override - public void updateFromUIDL(UIDL uidl, ApplicationConnection client) { - super.updateFromUIDL(uidl, client); - if (!uidl.hasAttribute("cached")) { - int childCount = uidl.getChildCount(); - UIDL childUIDL = uidl.getChildUIDL(childCount - 1); - getDropHandler().updateAcceptRules(childUIDL); - } - } - - private VAbstractDropHandler dropHandler; - - public VAbstractDropHandler getDropHandler() { - if (dropHandler == null) { - dropHandler = new VAbstractDropHandler() { - - @Override - public void dragEnter(VDragEvent drag) { - ApplicationConnection.getConsole().log("DDPane DragEnter"); - super.dragEnter(drag); - } - - @Override - public Paintable getPaintable() { - return VDragDropPane.this; - } - - @Override - public void dragLeave(VDragEvent drag) { - ApplicationConnection.getConsole().log("DragLeave"); - getStyleElement().getStyle().setBackgroundColor("yellow"); - } - - @Override - public boolean drop(VDragEvent drag) { - ApplicationConnection.getConsole().log( - "Drop" + drag.sinceStart()); - - if (getStyleElement().getStyle().getBackgroundColor() - .equals("yellow")) { - // not accepted - ApplicationConnection.getConsole().log( - "Drop was not accepted"); - return false; - } - - Map transferable = drag.getDropDetails(); - - // this is absolute layout based, and we may want to set - // component - // relatively to where the drag ended. - // need to add current location of the drop area - - int absoluteLeft = getAbsoluteLeft(); - int absoluteTop = getAbsoluteTop(); - - transferable.put("absoluteLeft", absoluteLeft); - transferable.put("absoluteTop", absoluteTop); - - getStyleElement().getStyle().setBackgroundColor("yellow"); - return super.drop(drag); - } - - @Override - protected void dragAccepted(VDragEvent drag) { - getStyleElement().getStyle().setBackgroundColor("green"); - } - - public ApplicationConnection getApplicationConnection() { - return client; - } - }; - } - return dropHandler; - } - - public Paintable getPaintable() { - return this; - } - -} diff --git a/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java b/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java index ada3869269..f326846c68 100644 --- a/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java +++ b/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java @@ -413,10 +413,15 @@ public abstract class AbstractCommunicationManager implements .split("\\."); DragAndDropWrapper ddw = (DragAndDropWrapper) idPaintableMap .get(split[0]); - - ddw.receiveFile(upstream, split[1]); - String debugId = ddw.getDebugId(); + try { + ddw.receiveFile(upstream, split[1]); + } catch (UploadException e) { + synchronized (application) { + handleChangeVariablesError(application, ddw, e, + new HashMap()); + } + } } else { diff --git a/src/com/vaadin/ui/DragAndDropWrapper.java b/src/com/vaadin/ui/DragAndDropWrapper.java index a848d7af40..65bcbaaa6a 100644 --- a/src/com/vaadin/ui/DragAndDropWrapper.java +++ b/src/com/vaadin/ui/DragAndDropWrapper.java @@ -26,6 +26,7 @@ import com.vaadin.terminal.gwt.client.ui.dd.VerticalDropLocation; import com.vaadin.terminal.gwt.server.AbstractApplicationServlet; import com.vaadin.ui.DragAndDropWrapper.WrapperTransferable.Html5File; import com.vaadin.ui.Upload.Receiver; +import com.vaadin.ui.Upload.UploadException; @ClientWidget(VDragAndDropWrapper.class) public class DragAndDropWrapper extends CustomComponent implements DropTarget, @@ -75,10 +76,14 @@ public class DragAndDropWrapper extends CustomComponent implements DropTarget, return files; } + /** + * {@link DragAndDropWrapper} can receive also files from client + * computer if appropriate HTML 5 features are supported on client side. + * This class wraps information about dragged file on server side. + */ public class Html5File { public String name; - private String id; private int size; private Receiver receiver; private String type; @@ -96,15 +101,20 @@ public class DragAndDropWrapper extends CustomComponent implements DropTarget, } /** - * HTML5 drags are read from client disk with a callback. This and - * possibly long transfer time forces us to receive dragged file - * contents with a callback. + * Sets the {@link Receiver} that into which the file contents will + * be written. Usage of Reveiver is similar to {@link Upload} + * component. + * + *

+ * Note! receiving file contents is experimental feature + * depending on HTML 5 API's. It is supported only by Firefox 3.6 at + * this time. * * @param receiver * the callback that returns stream where the * implementation writes the file contents as it arrives. */ - public void receive(Receiver receiver) { + public void setReceiver(Receiver receiver) { this.receiver = receiver; } @@ -178,6 +188,12 @@ public class DragAndDropWrapper extends CustomComponent implements DropTarget, private DragStartMode dragStartMode = DragStartMode.NONE; + /** + * Wraps given component in a {@link DragAndDropWrapper}. + * + * @param root + * the component to be wrapped + */ public DragAndDropWrapper(Component root) { super(root); } @@ -228,12 +244,15 @@ public class DragAndDropWrapper extends CustomComponent implements DropTarget, * This method should only be used by Vaadin terminal implementation. This * is not end user api. * - * TODO should fire progress events + end/succes events like upload + * TODO should fire progress events + end/succes events like upload. Not + * critical until we have a wider browser support for HTML5 File API * * @param upstream * @param fileId + * @throws UploadException */ - public void receiveFile(UploadStream upstream, String fileId) { + public void receiveFile(UploadStream upstream, String fileId) + throws UploadException { Html5File file = receivers.get(fileId); if (file != null && file.receiver != null) { OutputStream receiveUpload = file.receiver.receiveUpload(file @@ -247,10 +266,10 @@ public class DragAndDropWrapper extends CustomComponent implements DropTarget, receiveUpload.write(buf, 0, bytesRead); } } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + throw new UploadException(e); } - + // clean up the reference when file is downloaded + receivers.remove(fileId); } } diff --git a/src/com/vaadin/ui/DragDropPane.java b/src/com/vaadin/ui/DragDropPane.java deleted file mode 100644 index b3dc9bd30f..0000000000 --- a/src/com/vaadin/ui/DragDropPane.java +++ /dev/null @@ -1,205 +0,0 @@ -/* -@ITMillApache2LicenseForJavaFiles@ - */ -package com.vaadin.ui; - -import java.util.Map; - -import com.vaadin.event.DataBoundTransferable; -import com.vaadin.event.Transferable; -import com.vaadin.event.dd.DragAndDropEvent; -import com.vaadin.event.dd.DropHandler; -import com.vaadin.event.dd.DropTarget; -import com.vaadin.event.dd.DropTargetDetails; -import com.vaadin.event.dd.DropTargetDetailsImpl; -import com.vaadin.event.dd.acceptCriteria.AcceptAll; -import com.vaadin.event.dd.acceptCriteria.AcceptCriterion; -import com.vaadin.terminal.PaintException; -import com.vaadin.terminal.PaintTarget; -import com.vaadin.terminal.gwt.client.MouseEventDetails; - -/** - * Test class that implements various component related Drag and Drop features. - * - * TODO consider implementing this kind of general purpose layout class: - * - * - extend simple component (CssLayout or CustomComponent, instead of absolute - * layout) - * - * - implement both drag source and drop handler with server side api - * - * - implement html5 drop target etc - * - * - include a lots of details for drop event (coordinates & sizes of drop - * position and widget/Paintable hierarchy up to drop handler) - * - * This way we could have one rather complex dd component that could be used (by - * wrapping layouts) in most common situations with server side api. Core - * layouts wouldn't need changes (and have regression risk/ performance - * penalty). - * - * @deprecated use {@link DragAndDropWrapper} instead - * - */ -@Deprecated -@SuppressWarnings("serial") -@ClientWidget(com.vaadin.terminal.gwt.client.ui.VDragDropPane.class) -public class DragDropPane extends AbsoluteLayout implements DropTarget { - - private DropHandler dropHandler; - - public DragDropPane(DropHandler dropHandler) { - setWidth("400px"); - setHeight("300px"); - - if (dropHandler == null) { - this.dropHandler = new ImportPrettyMuchAnything(); - } else { - this.dropHandler = dropHandler; - } - - } - - public DragDropPane() { - this(null); - } - - @Override - public void paintContent(PaintTarget target) throws PaintException { - super.paintContent(target); - dropHandler.getAcceptCriterion().paint(target); - } - - public DropHandler getDropHandler() { - return dropHandler; - } - - public static class ImportPrettyMuchAnything implements DropHandler { - public void drop(DragAndDropEvent event) { - DragDropPane pane = (DragDropPane) event.getDropTargetDetails() - .getTarget(); - - DragEventDetails ed = (DragEventDetails) event - .getDropTargetDetails(); - Transferable ctr = event.getTransferable(); - if (ctr.getSourceComponent() != null) { - // use "component" (from DragDropPane) if available, else take - // the source component - Component component = (Component) ctr.getData("component"); - if (component == null) { - component = ctr.getSourceComponent(); - } - - if (component.getParent() != pane) { - if (ctr instanceof DataBoundTransferable) { - // Item has been dragged, construct a Label from - // Item id - Label l = new Label(); - l.setSizeUndefined(); - l.setValue("ItemId : " - + ((DataBoundTransferable) ctr).getItemId()); - pane.addComponent(l); - component = l; - - } else { - // we have a component that is been dragged, add - // it - // to - // this - pane.addComponent(component); - } - - Integer left = ed.getAbsoluteLeft(); - Integer top = ed.getAbsoluteTop(); - - MouseEventDetails eventDetails = ed.getMouseEvent(); - - int clientX = eventDetails.getClientX(); - int clientY = eventDetails.getClientY(); - - try { - pane.getPosition(component).setTopValue(clientY - top); - pane.getPosition(component) - .setLeftValue(clientX - left); - } catch (Exception e) { - // TODO: handle exception - } - } else { - // drag ended inside the this Pane - - MouseEventDetails start = ed.getMouseDownEvent(); - MouseEventDetails eventDetails = ed.getMouseEvent(); - - int deltaX = eventDetails.getClientX() - start.getClientX(); - int deltaY = eventDetails.getClientY() - start.getClientY(); - - ComponentPosition p = pane.getPosition(component); - p.setTopValue(p.getTopValue() + deltaY); - p.setLeftValue(p.getLeftValue() + deltaX); - - } - - } else { - // drag coming outside of Vaadin - String object = (String) ctr.getData("text/plain"); - - String content = (String) ctr.getData("fileContents"); - - Label l = new Label(); - l.setCaption("Generated from HTML5 drag:"); - if (object != null) { - l.setValue(object); - } else { - l.setValue("HTML5 dd"); - } - - l.setDescription(content); - l.setSizeUndefined(); - - pane.addComponent(l); - - } - return; - } - - public AcceptCriterion getAcceptCriterion() { - return AcceptAll.get(); - } - } - - class DragEventDetails extends DropTargetDetailsImpl { - - public DragEventDetails(Map rawVariables) { - super(rawVariables); - } - - public Integer getAbsoluteTop() { - return (Integer) getData("absoluteTop"); - } - - public Integer getAbsoluteLeft() { - return (Integer) getData("absoluteLeft"); - } - - public MouseEventDetails getMouseDownEvent() { - return MouseEventDetails.deSerialize((String) getData("mouseDown")); - } - - public MouseEventDetails getMouseEvent() { - return MouseEventDetails - .deSerialize((String) getData("mouseEvent")); - } - - } - - public DropTargetDetails translateDropTargetDetails( - Map clientVariables) { - return new DragEventDetails(clientVariables); - } - - public void setDropHandler(DropHandler dropHandler2) { - dropHandler = dropHandler2; - requestRepaint(); - } - -} diff --git a/src/com/vaadin/ui/Upload.java b/src/com/vaadin/ui/Upload.java index 7a3fac9167..fdad5a75cb 100644 --- a/src/com/vaadin/ui/Upload.java +++ b/src/com/vaadin/ui/Upload.java @@ -311,7 +311,7 @@ public class Upload extends AbstractComponent implements Component.Focusable { } - public class UploadException extends Exception { + public static class UploadException extends Exception { public UploadException(Exception e) { super("Upload failed", e); } diff --git a/tests/src/com/vaadin/tests/dd/AcceptFromComponent.java b/tests/src/com/vaadin/tests/dd/AcceptFromComponent.java index 7ae279b00f..f1b45a2cc1 100644 --- a/tests/src/com/vaadin/tests/dd/AcceptFromComponent.java +++ b/tests/src/com/vaadin/tests/dd/AcceptFromComponent.java @@ -1,22 +1,37 @@ package com.vaadin.tests.dd; -import com.vaadin.event.TransferableImpl; +import com.vaadin.event.DataBoundTransferable; import com.vaadin.event.Transferable; +import com.vaadin.event.TransferableImpl; import com.vaadin.event.dd.DragAndDropEvent; +import com.vaadin.event.dd.DropHandler; import com.vaadin.event.dd.acceptCriteria.AcceptCriterion; import com.vaadin.event.dd.acceptCriteria.ServerSideCriterion; -import com.vaadin.ui.DragDropPane; +import com.vaadin.terminal.gwt.client.MouseEventDetails; +import com.vaadin.ui.AbsoluteLayout; +import com.vaadin.ui.Component; +import com.vaadin.ui.DragAndDropWrapper; +import com.vaadin.ui.Label; import com.vaadin.ui.Tree; import com.vaadin.ui.Window; +import com.vaadin.ui.AbsoluteLayout.ComponentPosition; +import com.vaadin.ui.DragAndDropWrapper.WrapperDropDetails; +import com.vaadin.ui.DragAndDropWrapper.WrapperTransferable; public class AcceptFromComponent extends Window { + private AbsoluteLayout layout; + public AcceptFromComponent(final Tree tree1) { setCaption("Checks the source is tree1 on server"); - DragDropPane pane = new DragDropPane(); - setContent(pane); - pane.setSizeFull(); + layout = new AbsoluteLayout(); + DragAndDropWrapper wrapper = new DragAndDropWrapper(layout); + + setContent(wrapper); + + wrapper.setSizeFull(); + layout.setSizeFull(); setWidth("450px"); setHeight("150px"); @@ -34,13 +49,107 @@ public class AcceptFromComponent extends Window { } }; - pane.setDropHandler(new DragDropPane.ImportPrettyMuchAnything() { - @Override + wrapper.setDropHandler(new DropHandler() { + public AcceptCriterion getAcceptCriterion() { return serverSideCriterion; } + + public void drop(DragAndDropEvent event) { + + WrapperDropDetails ed = (WrapperDropDetails) event + .getDropTargetDetails(); + Transferable ctr = event.getTransferable(); + if (ctr.getSourceComponent() != null) { + // use "component" (from DragDropPane) if available, else + // take + // the source component + Component component = (Component) ctr.getData("component"); + if (component == null) { + component = ctr.getSourceComponent(); + } + + if (component.getParent() != layout) { + if (ctr instanceof DataBoundTransferable) { + // Item has been dragged, construct a Label from + // Item id + Label l = new Label(); + l.setSizeUndefined(); + l + .setValue("ItemId : " + + ((DataBoundTransferable) ctr) + .getItemId()); + layout.addComponent(l); + component = l; + + } else { + // we have a component that is been dragged, add + // it + // to + // this + layout.addComponent(component); + } + + Integer left = ed.getAbsoluteLeft(); + Integer top = ed.getAbsoluteTop(); + + MouseEventDetails eventDetails = ed.getMouseEvent(); + + int clientX = eventDetails.getClientX(); + int clientY = eventDetails.getClientY(); + + try { + layout.getPosition(component).setTopValue( + clientY - top); + layout.getPosition(component).setLeftValue( + clientX - left); + } catch (Exception e) { + // TODO: handle exception + } + } else { + // drag started and ended inside the this Pane + + DragAndDropWrapper.WrapperTransferable tr = (WrapperTransferable) event + .getTransferable(); + + MouseEventDetails start = tr.getMouseDownEvent(); + + MouseEventDetails eventDetails = ed.getMouseEvent(); + + int deltaX = eventDetails.getClientX() + - start.getClientX(); + int deltaY = eventDetails.getClientY() + - start.getClientY(); + + ComponentPosition p = layout.getPosition(component); + p.setTopValue(p.getTopValue() + deltaY); + p.setLeftValue(p.getLeftValue() + deltaX); + + } + + } else { + // drag coming outside of Vaadin + String object = (String) ctr.getData("text/plain"); + + String content = (String) ctr.getData("fileContents"); + + Label l = new Label(); + l.setCaption("Generated from HTML5 drag:"); + if (object != null) { + l.setValue(object); + } else { + l.setValue("HTML5 dd"); + } + + l.setDescription(content); + l.setSizeUndefined(); + + layout.addComponent(l); + + } + return; + } }); } - } diff --git a/tests/src/com/vaadin/tests/dd/DDTest1.java b/tests/src/com/vaadin/tests/dd/DDTest1.java index 324f770b9a..de6f86e588 100644 --- a/tests/src/com/vaadin/tests/dd/DDTest1.java +++ b/tests/src/com/vaadin/tests/dd/DDTest1.java @@ -15,7 +15,6 @@ import com.vaadin.terminal.ExternalResource; import com.vaadin.terminal.gwt.client.ui.dd.VerticalDropLocation; import com.vaadin.tests.components.TestBase; import com.vaadin.ui.Component; -import com.vaadin.ui.DragDropPane; import com.vaadin.ui.GridLayout; import com.vaadin.ui.Label; import com.vaadin.ui.Layout; @@ -58,42 +57,41 @@ public class DDTest1 extends TestBase { DragDropPane pane2 = new DragDropPane(); pane2 .setCaption("Pane2 (accept needs server side visit, check for \"Bar\")"); - DropHandler dropHandler = new DragDropPane.ImportPrettyMuchAnything() { - private final AcceptCriterion crit = new ServerSideCriterion() { - public boolean accepts(DragAndDropEvent dragEvent) { - Transferable transferable = dragEvent.getTransferable(); - // System.out.println("Simulating 500ms processing..."); - // try { - // Thread.sleep(200); - // } catch (InterruptedException e) { - // // TODO Auto-generated catch block - // e.printStackTrace(); - // } - // System.out.println("Done get to work."); - - Component component = (Component) transferable - .getData("component"); - if (component == null) { - component = transferable.getSourceComponent(); - } + final AcceptCriterion crit = new ServerSideCriterion() { + /** + * + */ + private static final long serialVersionUID = 1L; + + public boolean accepts(DragAndDropEvent dragEvent) { + Transferable transferable = dragEvent.getTransferable(); + // System.out.println("Simulating 500ms processing..."); + // try { + // Thread.sleep(200); + // } catch (InterruptedException e) { + // // TODO Auto-generated catch block + // e.printStackTrace(); + // } + // System.out.println("Done get to work."); + + Component component = (Component) transferable + .getData("component"); + if (component == null) { + component = transferable.getSourceComponent(); + } - if (component != null) { - if (component.toString() != null - && component.toString().contains("Bar")) { - return true; - } + if (component != null) { + if (component.toString() != null + && component.toString().contains("Bar")) { + return true; } - return false; } - }; - - @Override - public AcceptCriterion getAcceptCriterion() { - return crit; + return false; } - }; - pane2.setDropHandler(dropHandler); + + pane2.setAcceptCriterion(crit); + pane2.setDebugId("pane2"); pane2.setSizeFull(); diff --git a/tests/src/com/vaadin/tests/dd/DDTest6.java b/tests/src/com/vaadin/tests/dd/DDTest6.java index 771a3b536f..f45a6e22ec 100644 --- a/tests/src/com/vaadin/tests/dd/DDTest6.java +++ b/tests/src/com/vaadin/tests/dd/DDTest6.java @@ -381,7 +381,7 @@ public class DDTest6 extends TestBase { } }; - html5File.receive(receiver); + html5File.setReceiver(receiver); File file = new File(fileName, bas); file.setType(html5File.getType()); diff --git a/tests/src/com/vaadin/tests/dd/DragDropPane.java b/tests/src/com/vaadin/tests/dd/DragDropPane.java new file mode 100644 index 0000000000..400e5f9e26 --- /dev/null +++ b/tests/src/com/vaadin/tests/dd/DragDropPane.java @@ -0,0 +1,132 @@ +package com.vaadin.tests.dd; + +import com.vaadin.event.DataBoundTransferable; +import com.vaadin.event.Transferable; +import com.vaadin.event.dd.DragAndDropEvent; +import com.vaadin.event.dd.DropHandler; +import com.vaadin.event.dd.acceptCriteria.AcceptAll; +import com.vaadin.event.dd.acceptCriteria.AcceptCriterion; +import com.vaadin.terminal.gwt.client.MouseEventDetails; +import com.vaadin.ui.AbsoluteLayout; +import com.vaadin.ui.Component; +import com.vaadin.ui.DragAndDropWrapper; +import com.vaadin.ui.Label; +import com.vaadin.ui.AbsoluteLayout.ComponentPosition; + +/** + * replacement for a proto class to keep tests working + */ +public class DragDropPane extends DragAndDropWrapper implements DropHandler { + + private AbsoluteLayout root; + private AcceptCriterion crit; + + public DragDropPane() { + super(new AbsoluteLayout()); + root = (AbsoluteLayout) getCompositionRoot(); + setDropHandler(this); + setDragStartMode(DragStartMode.COMPONENT); + } + + @Override + public void addComponent(Component c) { + root.addComponent(c); + } + + /** + * + */ + private static final long serialVersionUID = 1L; + + public void addComponent(Component l, String string) { + root.addComponent(l, string); + } + + public void setAcceptCriterion(AcceptCriterion crit) { + this.crit = crit; + } + + public void drop(DragAndDropEvent event) { + + WrapperDropDetails ed = (WrapperDropDetails) event + .getDropTargetDetails(); + Transferable ctr = event.getTransferable(); + // use "component" (from DragDropPane) if available, else take + // the source component + Component component = null; + if (ctr instanceof WrapperTransferable) { + component = ((WrapperTransferable) ctr).getDraggedComponent(); + } else if (ctr instanceof DataBoundTransferable) { + // Item has been dragged, construct a Label from + // Item id + Label l = new Label(); + l.setSizeUndefined(); + l.setValue("ItemId : " + ((DataBoundTransferable) ctr).getItemId()); + component = l; + } + + if (component != null) { + + if (component.getParent() != root) { + + root.addComponent(component); + + Integer left = ed.getAbsoluteLeft(); + Integer top = ed.getAbsoluteTop(); + + MouseEventDetails eventDetails = ed.getMouseEvent(); + + int clientX = eventDetails.getClientX(); + int clientY = eventDetails.getClientY(); + + try { + root.getPosition(component).setTopValue(clientY - top); + root.getPosition(component).setLeftValue(clientX - left); + } catch (Exception e) { + // TODO: handle exception + } + } else { + // drag started and ended inside the this Pane + + MouseEventDetails start = ((WrapperTransferable) event + .getTransferable()).getMouseDownEvent(); + MouseEventDetails eventDetails = ed.getMouseEvent(); + + int deltaX = eventDetails.getClientX() - start.getClientX(); + int deltaY = eventDetails.getClientY() - start.getClientY(); + + ComponentPosition p = root.getPosition(component); + p.setTopValue(p.getTopValue() + deltaY); + p.setLeftValue(p.getLeftValue() + deltaX); + + } + } + + else { + // drag coming outside of Vaadin + String object = (String) ctr.getData("text/plain"); + + String content = (String) ctr.getData("fileContents"); + + Label l = new Label(); + l.setCaption("Generated from HTML5 drag:"); + if (object != null) { + l.setValue(object); + } else { + l.setValue("HTML5 dd"); + } + + l.setDescription(content); + l.setSizeUndefined(); + + root.addComponent(l); + + } + return; + } + + public AcceptCriterion getAcceptCriterion() { + return crit != null ? crit : AcceptAll.get(); + } + +}