]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fixes DragAndDropWrapper does not support choosing a custom component as the drag...
authorSebastian Nyholm <sebastian@vaadin.com>
Tue, 27 May 2014 11:46:47 +0000 (14:46 +0300)
committerVaadin Code Review <review@vaadin.com>
Mon, 18 Aug 2014 11:34:03 +0000 (11:34 +0000)
Amend : Fixes the comments for the last commit.
Amend 2: Fixes a nullpointer found in a certain test scenario

Change-Id: I72327f5f8ab9e46d88e1ebed8f1db3a7da39d7d8

client/src/com/vaadin/client/ui/VDragAndDropWrapper.java
client/src/com/vaadin/client/ui/draganddropwrapper/DragAndDropWrapperConnector.java
server/src/com/vaadin/ui/DragAndDropWrapper.java
shared/src/com/vaadin/shared/ui/draganddropwrapper/DragAndDropWrapperConstants.java
uitest/src/com/vaadin/tests/components/draganddropwrapper/DragStartModes.java
uitest/src/com/vaadin/tests/components/draganddropwrapper/DragStartModesTest.java

index 4010ffd5428855d4ad6c4c9c44493897e2329f32..7bf341a3878cdb704400714b5bbfbab743726670 100644 (file)
@@ -114,7 +114,8 @@ public class VDragAndDropWrapper extends VCustomComponent implements
      * @return true if the event was handled as a drag start event
      */
     private boolean startDrag(NativeEvent event) {
-        if (dragStartMode == WRAPPER || dragStartMode == COMPONENT) {
+        if (dragStartMode == WRAPPER || dragStartMode == COMPONENT
+                || dragStartMode == COMPONENT_OTHER) {
             VTransferable transferable = new VTransferable();
             transferable.setDragSource(getConnector());
 
@@ -130,6 +131,10 @@ public class VDragAndDropWrapper extends VCustomComponent implements
 
             if (dragStartMode == WRAPPER) {
                 dragEvent.createDragImage(getElement(), true);
+            } else if (dragStartMode == COMPONENT_OTHER
+                    && getDragImageWidget() != null) {
+                dragEvent.createDragImage(getDragImageWidget().getElement(),
+                        true);
             } else {
                 dragEvent.createDragImage(widget.getElement(), true);
             }
@@ -142,6 +147,7 @@ public class VDragAndDropWrapper extends VCustomComponent implements
     protected final static int COMPONENT = 1;
     protected final static int WRAPPER = 2;
     protected final static int HTML5 = 3;
+    protected final static int COMPONENT_OTHER = 4;
 
     /** For internal use only. May be removed or replaced in the future. */
     public int dragStartMode;
@@ -458,6 +464,7 @@ public class VDragAndDropWrapper extends VCustomComponent implements
      * Flag used by html5 dd
      */
     private boolean currentlyValid;
+    private Widget dragImageWidget;
 
     private static final String OVER_STYLE = "v-ddwrapper-over";
 
@@ -661,4 +668,22 @@ public class VDragAndDropWrapper extends VCustomComponent implements
         notifySizePotentiallyChanged();
     }
 
+    /**
+     * Set the widget that will be used as the drag image when using
+     * DragStartMode {@link COMPONENT_OTHER} .
+     * 
+     * @param widget
+     */
+    public void setDragAndDropWidget(Widget widget) {
+        dragImageWidget = widget;
+    }
+
+    /**
+     * @return the widget used as drag image. Returns <code>null</code> if no
+     *         widget is set.
+     */
+    public Widget getDragImageWidget() {
+        return dragImageWidget;
+    }
+
 }
index afb521b141a1b1b94ba65aa836111d90215d1714..f222721e24723ad135e6089adae2d5fb6760b2fc 100644 (file)
@@ -17,8 +17,12 @@ package com.vaadin.client.ui.draganddropwrapper;
 
 import java.util.HashMap;
 import java.util.Set;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import com.vaadin.client.ApplicationConnection;
+import com.vaadin.client.ComponentConnector;
+import com.vaadin.client.ConnectorMap;
 import com.vaadin.client.Paintable;
 import com.vaadin.client.UIDL;
 import com.vaadin.client.VConsole;
@@ -81,6 +85,25 @@ public class DragAndDropWrapperConnector extends CustomComponentConnector
 
             getWidget().dragStartMode = uidl
                     .getIntAttribute(DragAndDropWrapperConstants.DRAG_START_MODE);
+
+            String dragImageComponentConnectorId = uidl
+                    .getStringAttribute(DragAndDropWrapperConstants.DRAG_START_COMPONENT_ATTRIBUTE);
+
+            ComponentConnector connector = null;
+            if (dragImageComponentConnectorId != null) {
+                connector = (ComponentConnector) ConnectorMap.get(client)
+                        .getConnector(dragImageComponentConnectorId);
+
+                if (connector == null) {
+                    getLogger().log(
+                            Level.WARNING,
+                            "DragAndDropWrapper drag image component"
+                                    + " connector now found. Make sure the"
+                                    + " component is attached.");
+                } else {
+                    getWidget().setDragAndDropWidget(connector.getWidget());
+                }
+            }
             getWidget().initDragStartMode();
             getWidget().html5DataFlavors = uidl
                     .getMapAttribute(DragAndDropWrapperConstants.HTML5_DATA_FLAVORS);
@@ -95,4 +118,7 @@ public class DragAndDropWrapperConnector extends CustomComponentConnector
         return (VDragAndDropWrapper) super.getWidget();
     }
 
+    private static Logger getLogger() {
+        return Logger.getLogger(DragAndDropWrapperConnector.class.getName());
+    }
 }
index 3d3356b338e75531949311ccbedca18210c4bb63..0e2e8f6d2f97b27692e291b2592f01e76bececc8 100644 (file)
@@ -180,10 +180,17 @@ public class DragAndDropWrapper extends CustomComponent implements DropTarget,
          * the wrapper will no longer work.
          */
         HTML5,
+
+        /**
+         * Uses the component defined in
+         * {@link #setDragImageComponent(Component)} as the drag image.
+         */
+        COMPONENT_OTHER,
     }
 
     private final Map<String, Object> html5DataFlavors = new LinkedHashMap<String, Object>();
     private DragStartMode dragStartMode = DragStartMode.NONE;
+    private Component dragImageComponent = null;
 
     private Set<String> sentIds = new HashSet<String>();
 
@@ -227,6 +234,19 @@ public class DragAndDropWrapper extends CustomComponent implements DropTarget,
     public void paintContent(PaintTarget target) throws PaintException {
         target.addAttribute(DragAndDropWrapperConstants.DRAG_START_MODE,
                 dragStartMode.ordinal());
+
+        if (dragStartMode.equals(DragStartMode.COMPONENT_OTHER)) {
+            if (dragImageComponent != null) {
+                target.addAttribute(
+                        DragAndDropWrapperConstants.DRAG_START_COMPONENT_ATTRIBUTE,
+                        dragImageComponent.getConnectorId());
+            } else {
+                throw new IllegalArgumentException(
+                        "DragStartMode.COMPONENT_OTHER set but no component "
+                                + "was defined. Please set a component using DragAnd"
+                                + "DropWrapper.setDragStartComponent(Component).");
+            }
+        }
         if (getDropHandler() != null) {
             getDropHandler().getAcceptCriterion().paint(target);
         }
@@ -300,6 +320,27 @@ public class DragAndDropWrapper extends CustomComponent implements DropTarget,
         return dragStartMode;
     }
 
+    /**
+     * Sets the component that will be used as the drag image. Only used when
+     * wrapper is set to {@link DragStartMode#COMPONENT_OTHER}
+     * 
+     * @param dragImageComponent
+     */
+    public void setDragImageComponent(Component dragImageComponent) {
+        this.dragImageComponent = dragImageComponent;
+        markAsDirty();
+    }
+
+    /**
+     * Gets the component that will be used as the drag image. Only used when
+     * wrapper is set to {@link DragStartMode#COMPONENT_OTHER}
+     * 
+     * @return <code>null</code> if no component is set.
+     */
+    public Component getDragImageComponent() {
+        return dragImageComponent;
+    }
+
     final class ProxyReceiver implements StreamVariable {
 
         private String id;
index 8dd8ef513e016152d6ddf4dc531be454942ac863..9dda86ad73c91f6678296959488266eeb8fed4e0 100644 (file)
@@ -25,4 +25,6 @@ public class DragAndDropWrapperConstants implements Serializable {
     @Deprecated
     public static final String DRAG_START_MODE = "dragStartMode";
 
+    public static final String DRAG_START_COMPONENT_ATTRIBUTE = "dragStartComponent";
+
 }
index b143ddf2dc0e83b58318965bd99bc44afd28fa76..8539f70b1778d5f739716e828d223a6ac370c0cf 100644 (file)
@@ -3,26 +3,53 @@ package com.vaadin.tests.components.draganddropwrapper;
 import com.vaadin.tests.components.TestBase;
 import com.vaadin.tests.util.TestUtils;
 import com.vaadin.ui.Component;
+import com.vaadin.ui.CssLayout;
 import com.vaadin.ui.DragAndDropWrapper;
 import com.vaadin.ui.DragAndDropWrapper.DragStartMode;
 import com.vaadin.ui.Label;
+import com.vaadin.ui.VerticalLayout;
 
 public class DragStartModes extends TestBase {
 
     @Override
     protected void setup() {
 
-        TestUtils.injectCSS(getMainWindow(),
-                ".v-ddwrapper { background: #ACF; }");
+        TestUtils
+                .injectCSS(getMainWindow(),
+                        ".v-ddwrapper { background: #ACF; } .extra{ background: #FFA500; }");
 
         addComponent(makeWrapper(DragStartMode.NONE));
         addComponent(makeWrapper(DragStartMode.COMPONENT));
         addComponent(makeWrapper(DragStartMode.WRAPPER));
         addComponent(makeWrapper(DragStartMode.HTML5));
+        addComponent(makeOtherComponentWrapper(DragStartMode.COMPONENT_OTHER));
 
         addComponent(new Label("Drop here"));
     }
 
+    private Component makeOtherComponentWrapper(DragStartMode componentOther) {
+        VerticalLayout parent = new VerticalLayout();
+        parent.setWidth("200px");
+        parent.setSpacing(true);
+
+        CssLayout header = new CssLayout();
+        header.addComponent(new Label("Drag start mode : COMPONENT_OTHER"));
+        header.setSizeUndefined();
+
+        DragAndDropWrapper wrapper = new DragAndDropWrapper(header);
+        wrapper.setDragStartMode(DragStartMode.COMPONENT_OTHER);
+        wrapper.setDragImageComponent(parent);
+        wrapper.setId("label" + "COMPONENT_OTHER");
+        parent.addComponent(wrapper);
+
+        Label extra = new Label(
+                "Extra label that is not part of the wrapper. This should be dragged along with COMPONENT_OTHER.");
+        extra.addStyleName("extra");
+        parent.addComponent(extra);
+
+        return parent;
+    }
+
     private Component makeWrapper(DragStartMode mode) {
         Label label = new Label("Drag start mode: " + mode);
         label.setId("label" + mode);
index 25aef1b815da565c7989c48885e30296faf17b36..ba27ee293ec838b822d3ca9c9a6bd16ec4642e31 100644 (file)
@@ -31,6 +31,7 @@ public class DragStartModesTest extends MultiBrowserTest {
         WebElement dropTarget = vaadinElement("/VVerticalLayout[0]/VVerticalLayout[0]/VLabel[0]");
         dragToTarget("COMPONENT", dropTarget);
         dragToTarget("WRAPPER", dropTarget);
+        dragToTarget("COMPONENT_OTHER", dropTarget);
     }
 
     private void dragToTarget(String dragMode, WebElement dropTarget)