Skip to content

Commit

Permalink
Clean DropEvent and DragEndEvent properties (#8925)
Browse files Browse the repository at this point in the history
* Add dropEffect parameter to DragEndEvent (#8895)

* Remove drop effect from drop event (#8895)

* Make sure that drop effect is not null
  • Loading branch information
wbadam authored and pleku committed Mar 24, 2017
1 parent ff3c31b commit e905e2b
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 41 deletions.
Expand Up @@ -25,6 +25,7 @@
import com.vaadin.shared.ui.Connect; import com.vaadin.shared.ui.Connect;
import com.vaadin.shared.ui.dnd.DragSourceRpc; import com.vaadin.shared.ui.dnd.DragSourceRpc;
import com.vaadin.shared.ui.dnd.DragSourceState; import com.vaadin.shared.ui.dnd.DragSourceState;
import com.vaadin.shared.ui.dnd.DropEffect;


import elemental.events.Event; import elemental.events.Event;
import elemental.events.EventListener; import elemental.events.EventListener;
Expand Down Expand Up @@ -113,12 +114,19 @@ protected void onDragStart(Event event) {
* event occurs. * event occurs.
* *
* @param event * @param event
* browser event to be handled
*/ */
protected void onDragEnd(Event event) { protected void onDragEnd(Event event) {
// Initiate server start dragend event when there is a DragEndListener // Initiate server start dragend event when there is a DragEndListener
// attached on the server side // attached on the server side
if (hasEventListener(DragSourceState.EVENT_DRAGEND)) { if (hasEventListener(DragSourceState.EVENT_DRAGEND)) {
getRpcProxy(DragSourceRpc.class).dragEnd(); String dropEffect = getDropEffect(
((NativeEvent) event).getDataTransfer());

assert dropEffect != null : "Drop effect should never be null";

getRpcProxy(DragSourceRpc.class)
.dragEnd(DropEffect.valueOf(dropEffect.toUpperCase()));
} }
} }


Expand All @@ -137,6 +145,10 @@ private native void setEffectAllowed(DataTransfer dataTransfer,
dataTransfer.effectAllowed = effectAllowed; dataTransfer.effectAllowed = effectAllowed;
}-*/; }-*/;


private native String getDropEffect(DataTransfer dataTransfer)/*-{
return dataTransfer.dropEffect;
}-*/;

@Override @Override
public DragSourceState getState() { public DragSourceState getState() {
return (DragSourceState) super.getState(); return (DragSourceState) super.getState();
Expand Down
Expand Up @@ -182,8 +182,7 @@ protected void onDrop(Event event) {
String dataTransferText = nativeEvent.getDataTransfer().getData( String dataTransferText = nativeEvent.getDataTransfer().getData(
DragSourceState.DATA_TYPE_TEXT); DragSourceState.DATA_TYPE_TEXT);


getRpcProxy(DropTargetRpc.class) getRpcProxy(DropTargetRpc.class).drop(dataTransferText);
.drop(dataTransferText, getState().dropEffect);
} }


removeTargetIndicator(getDropTargetElement()); removeTargetIndicator(getDropTargetElement());
Expand Down
21 changes: 11 additions & 10 deletions server/src/main/java/com/vaadin/event/dnd/DragEndEvent.java
Expand Up @@ -15,7 +15,7 @@
*/ */
package com.vaadin.event.dnd; package com.vaadin.event.dnd;


import com.vaadin.shared.ui.dnd.EffectAllowed; import com.vaadin.shared.ui.dnd.DropEffect;
import com.vaadin.ui.AbstractComponent; import com.vaadin.ui.AbstractComponent;
import com.vaadin.ui.Component; import com.vaadin.ui.Component;


Expand All @@ -30,7 +30,7 @@
*/ */
public class DragEndEvent<T extends AbstractComponent> extends Component.Event { public class DragEndEvent<T extends AbstractComponent> extends Component.Event {
private final String dataTransferText; private final String dataTransferText;
private final EffectAllowed effectAllowed; private final DropEffect dropEffect;


/** /**
* Creates a server side dragend event. * Creates a server side dragend event.
Expand All @@ -40,16 +40,16 @@ public class DragEndEvent<T extends AbstractComponent> extends Component.Event {
* @param dataTransferText * @param dataTransferText
* Data of type {@code "text"} from the {@code DataTransfer} * Data of type {@code "text"} from the {@code DataTransfer}
* object. * object.
* @param effectAllowed * @param dropEffect
* Allowed effects from {@code DataTransfer.effectAllowed} object. * Drop effect from {@code DataTransfer.dropEffect} object.
*/ */
public DragEndEvent(T source, String dataTransferText, public DragEndEvent(T source, String dataTransferText,
EffectAllowed effectAllowed) { DropEffect dropEffect) {
super(source); super(source);


this.dataTransferText = dataTransferText; this.dataTransferText = dataTransferText;


this.effectAllowed = effectAllowed; this.dropEffect = dropEffect;
} }


/** /**
Expand All @@ -64,12 +64,13 @@ public String getDataTransferText() {
} }


/** /**
* Returns the {@code effectAllowed} parameter of this event. * Get drop effect of the dragend event.
* *
* @return This event's {@code effectAllowed} parameter. * @return The {@code DataTransfer.dropEffect} parameter of the client side
* dragend event.
*/ */
public EffectAllowed getEffectAllowed() { public DropEffect getDropEffect() {
return effectAllowed; return dropEffect;
} }


/** /**
Expand Down
Expand Up @@ -21,6 +21,7 @@
import com.vaadin.shared.Registration; import com.vaadin.shared.Registration;
import com.vaadin.shared.ui.dnd.DragSourceRpc; import com.vaadin.shared.ui.dnd.DragSourceRpc;
import com.vaadin.shared.ui.dnd.DragSourceState; import com.vaadin.shared.ui.dnd.DragSourceState;
import com.vaadin.shared.ui.dnd.DropEffect;
import com.vaadin.shared.ui.dnd.EffectAllowed; import com.vaadin.shared.ui.dnd.EffectAllowed;
import com.vaadin.ui.AbstractComponent; import com.vaadin.ui.AbstractComponent;


Expand Down Expand Up @@ -62,10 +63,9 @@ public void dragStart() {
} }


@Override @Override
public void dragEnd() { public void dragEnd(DropEffect dropEffect) {
DragEndEvent<T> event = new DragEndEvent<>(target, DragEndEvent<T> event = new DragEndEvent<>(target,
getState(false).dataTransferText, getState(false).dataTransferText, dropEffect);
getState(false).effectAllowed);
fireEvent(event); fireEvent(event);
} }
}); });
Expand Down
16 changes: 1 addition & 15 deletions server/src/main/java/com/vaadin/event/dnd/DropEvent.java
Expand Up @@ -32,7 +32,6 @@
*/ */
public class DropEvent<T extends AbstractComponent> extends Component.Event { public class DropEvent<T extends AbstractComponent> extends Component.Event {
private final String dataTransferText; private final String dataTransferText;
private final DropEffect dropEffect;
private final DragSourceExtension<? extends AbstractComponent> dragSourceExtension; private final DragSourceExtension<? extends AbstractComponent> dragSourceExtension;
private final AbstractComponent dragSource; private final AbstractComponent dragSource;


Expand All @@ -44,20 +43,16 @@ public class DropEvent<T extends AbstractComponent> extends Component.Event {
* @param dataTransferText * @param dataTransferText
* Data of type {@code "text"} from the {@code DataTransfer} * Data of type {@code "text"} from the {@code DataTransfer}
* object. * object.
* @param dropEffect
* Drop effect from {@code DataTransfer.dropEffect} object.
* @param dragSourceExtension * @param dragSourceExtension
* Drag source extension of the component that initiated the drop * Drag source extension of the component that initiated the drop
* event. * event.
*/ */
public DropEvent(T target, String dataTransferText, DropEffect dropEffect, public DropEvent(T target, String dataTransferText,
DragSourceExtension<? extends AbstractComponent> dragSourceExtension) { DragSourceExtension<? extends AbstractComponent> dragSourceExtension) {
super(target); super(target);


this.dataTransferText = dataTransferText; this.dataTransferText = dataTransferText;


this.dropEffect = dropEffect;

this.dragSourceExtension = dragSourceExtension; this.dragSourceExtension = dragSourceExtension;
this.dragSource = Optional.ofNullable(dragSourceExtension) this.dragSource = Optional.ofNullable(dragSourceExtension)
.map(DragSourceExtension::getParent).orElse(null); .map(DragSourceExtension::getParent).orElse(null);
Expand All @@ -74,15 +69,6 @@ public String getDataTransferText() {
return dataTransferText; return dataTransferText;
} }


/**
* Get drop effect set for the current drop target.
*
* @return {@code dropEffect} parameter set for the current drop target.
*/
public DropEffect getDropEffect() {
return dropEffect;
}

/** /**
* Returns the drag source component if the drag originated from a * Returns the drag source component if the drag originated from a
* component in the same UI as the drop target component, or an empty * component in the same UI as the drop target component, or an empty
Expand Down
Expand Up @@ -43,9 +43,9 @@ public class DropTargetExtension<T extends AbstractComponent> extends
* Component to be extended. * Component to be extended.
*/ */
public DropTargetExtension(T target) { public DropTargetExtension(T target) {
registerRpc((DropTargetRpc) (dataTransferText, dropEffect) -> { registerRpc((DropTargetRpc) dataTransferText -> {
DropEvent<T> event = new DropEvent<>(target, dataTransferText, DropEvent<T> event = new DropEvent<>(target, dataTransferText,
dropEffect, getUI().getActiveDragSource()); getUI().getActiveDragSource());


fireEvent(event); fireEvent(event);
}); });
Expand Down
Expand Up @@ -33,6 +33,10 @@ public interface DragSourceRpc extends ServerRpc {


/** /**
* Called when dragend event happens on client side. * Called when dragend event happens on client side.
*
* @param dropEffect
* Drop effect of the dragend event, extracted from {@code
* DataTransfer.dropEffect} parameter.
*/ */
public void dragEnd(); public void dragEnd(DropEffect dropEffect);
} }
Expand Up @@ -32,8 +32,6 @@ public interface DropTargetRpc extends ServerRpc {
* @param dataTransferText * @param dataTransferText
* Data of type {@code "text"} from the {@code DataTransfer} * Data of type {@code "text"} from the {@code DataTransfer}
* object. * object.
* @param dropEffect
* Drop effect set for the drop target where drop happened.
*/ */
public void drop(String dataTransferText, DropEffect dropEffect); public void drop(String dataTransferText);
} }
Expand Up @@ -19,7 +19,6 @@
import com.vaadin.event.dnd.DropTargetExtension; import com.vaadin.event.dnd.DropTargetExtension;
import com.vaadin.server.Page; import com.vaadin.server.Page;
import com.vaadin.server.VaadinRequest; import com.vaadin.server.VaadinRequest;
import com.vaadin.shared.ui.dnd.DropEffect;
import com.vaadin.tests.components.AbstractTestUIWithLog; import com.vaadin.tests.components.AbstractTestUIWithLog;
import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label; import com.vaadin.ui.Label;
Expand Down Expand Up @@ -76,7 +75,8 @@ private void addDragSourceExtension(Label source) {


dragSource.addDragEndListener(event -> { dragSource.addDragEndListener(event -> {
event.getComponent().removeStyleName("dragged"); event.getComponent().removeStyleName("dragged");
log(event.getComponent().getValue() + " dragend"); log(event.getComponent().getValue() + " dragend, dropEffect="
+ event.getDropEffect());
}); });
} }


Expand All @@ -85,8 +85,6 @@ private void addDropTargetExtension(Label target) {
DropTargetExtension<Label> dropTarget = new DropTargetExtension<>( DropTargetExtension<Label> dropTarget = new DropTargetExtension<>(
target); target);


dropTarget.setDropEffect(DropEffect.MOVE);

// Add listener // Add listener
dropTarget.addDropListener(event -> { dropTarget.addDropListener(event -> {
event.getDragSourceExtension().ifPresent(dragSource -> { event.getDragSourceExtension().ifPresent(dragSource -> {
Expand Down

0 comments on commit e905e2b

Please sign in to comment.