Amend : Fixes the comments for the last commit.
Amend 2: Fixes a nullpointer found in a certain test scenario
Change-Id: I72327f5f8ab9e46d88e1ebed8f1db3a7da39d7d8
* @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());
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);
}
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;
* Flag used by html5 dd
*/
private boolean currentlyValid;
+ private Widget dragImageWidget;
private static final String OVER_STYLE = "v-ddwrapper-over";
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;
+ }
+
}
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;
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);
return (VDragAndDropWrapper) super.getWidget();
}
+ private static Logger getLogger() {
+ return Logger.getLogger(DragAndDropWrapperConnector.class.getName());
+ }
}
* 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>();
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);
}
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;
@Deprecated
public static final String DRAG_START_MODE = "dragStartMode";
+ public static final String DRAG_START_COMPONENT_ATTRIBUTE = "dragStartComponent";
+
}
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);
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)