diff options
author | Sebastian Nyholm <sebastian@vaadin.com> | 2014-05-27 14:46:47 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2014-08-18 11:34:03 +0000 |
commit | 1020faa337eddeb07f141cb7ef12fbb20bd2ded2 (patch) | |
tree | 3fe93c549eed111988c9c8f5d057b8cbadd5c9f0 | |
parent | a960cfd12b8fb42ae78e39ffb61c04e3a3b831bf (diff) | |
download | vaadin-framework-1020faa337eddeb07f141cb7ef12fbb20bd2ded2.tar.gz vaadin-framework-1020faa337eddeb07f141cb7ef12fbb20bd2ded2.zip |
Fixes DragAndDropWrapper does not support choosing a custom component as the drag image (#13836)
Amend : Fixes the comments for the last commit.
Amend 2: Fixes a nullpointer found in a certain test scenario
Change-Id: I72327f5f8ab9e46d88e1ebed8f1db3a7da39d7d8
6 files changed, 125 insertions, 3 deletions
diff --git a/client/src/com/vaadin/client/ui/VDragAndDropWrapper.java b/client/src/com/vaadin/client/ui/VDragAndDropWrapper.java index 4010ffd542..7bf341a387 100644 --- a/client/src/com/vaadin/client/ui/VDragAndDropWrapper.java +++ b/client/src/com/vaadin/client/ui/VDragAndDropWrapper.java @@ -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; + } + } diff --git a/client/src/com/vaadin/client/ui/draganddropwrapper/DragAndDropWrapperConnector.java b/client/src/com/vaadin/client/ui/draganddropwrapper/DragAndDropWrapperConnector.java index afb521b141..f222721e24 100644 --- a/client/src/com/vaadin/client/ui/draganddropwrapper/DragAndDropWrapperConnector.java +++ b/client/src/com/vaadin/client/ui/draganddropwrapper/DragAndDropWrapperConnector.java @@ -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()); + } } diff --git a/server/src/com/vaadin/ui/DragAndDropWrapper.java b/server/src/com/vaadin/ui/DragAndDropWrapper.java index 3d3356b338..0e2e8f6d2f 100644 --- a/server/src/com/vaadin/ui/DragAndDropWrapper.java +++ b/server/src/com/vaadin/ui/DragAndDropWrapper.java @@ -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; diff --git a/shared/src/com/vaadin/shared/ui/draganddropwrapper/DragAndDropWrapperConstants.java b/shared/src/com/vaadin/shared/ui/draganddropwrapper/DragAndDropWrapperConstants.java index 8dd8ef513e..9dda86ad73 100644 --- a/shared/src/com/vaadin/shared/ui/draganddropwrapper/DragAndDropWrapperConstants.java +++ b/shared/src/com/vaadin/shared/ui/draganddropwrapper/DragAndDropWrapperConstants.java @@ -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"; + } diff --git a/uitest/src/com/vaadin/tests/components/draganddropwrapper/DragStartModes.java b/uitest/src/com/vaadin/tests/components/draganddropwrapper/DragStartModes.java index b143ddf2dc..8539f70b17 100644 --- a/uitest/src/com/vaadin/tests/components/draganddropwrapper/DragStartModes.java +++ b/uitest/src/com/vaadin/tests/components/draganddropwrapper/DragStartModes.java @@ -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); diff --git a/uitest/src/com/vaadin/tests/components/draganddropwrapper/DragStartModesTest.java b/uitest/src/com/vaadin/tests/components/draganddropwrapper/DragStartModesTest.java index 25aef1b815..ba27ee293e 100644 --- a/uitest/src/com/vaadin/tests/components/draganddropwrapper/DragStartModesTest.java +++ b/uitest/src/com/vaadin/tests/components/draganddropwrapper/DragStartModesTest.java @@ -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) |