diff options
author | Adam Wagner <wbadam@users.noreply.github.com> | 2017-02-21 09:52:32 +0200 |
---|---|---|
committer | Pekka Hyvönen <pekka@vaadin.com> | 2017-02-21 09:52:32 +0200 |
commit | c53f066d17274b64d0cf91977f9ef68767153d87 (patch) | |
tree | 73436844baf7f808630cdd4afc643f5f425ab00a /server | |
parent | c063168fe202783e3135087c49aa90a78e6b75bf (diff) | |
download | vaadin-framework-c53f066d17274b64d0cf91977f9ef68767153d87.tar.gz vaadin-framework-c53f066d17274b64d0cf91977f9ef68767153d87.zip |
Make DragSourceExtension and DragTargetExtension generic (#8628)
* Make DragSourceExtension and DropTargetExtension generic (#8440)
* Apply generalization of drag and drop extensions in #8440
* Override javadocs to be more specific
Diffstat (limited to 'server')
8 files changed, 150 insertions, 75 deletions
diff --git a/server/src/main/java/com/vaadin/event/dnd/DragEndEvent.java b/server/src/main/java/com/vaadin/event/dnd/DragEndEvent.java index 6f094ce9e2..47abea4eae 100644 --- a/server/src/main/java/com/vaadin/event/dnd/DragEndEvent.java +++ b/server/src/main/java/com/vaadin/event/dnd/DragEndEvent.java @@ -23,28 +23,31 @@ import com.vaadin.shared.ui.dnd.EffectAllowed; import com.vaadin.ui.Component; /** - * Server side dragend event. Fired when an HTML5 dragend happens. + * Server side dragend event. Fired when an HTML5 dragend happens on the client. * + * @param <T> + * Type of the component that was dragged. * @see DragSourceExtension#addDragEndListener(DragEndListener) */ -public class DragEndEvent extends Component.Event { +public class DragEndEvent<T extends Component> extends Component.Event { private final Map<String, String> data; private final EffectAllowed effectAllowed; /** - * Creates a new server side dragend event. + * Creates a server side dragend event. * * @param source - * Draggable component. + * Component that was dragged. * @param types - * List of data types from {@code DataTransfer.types}. + * List of data types from {@code DataTransfer.types} object. * @param data - * Map of all data from {@code DataTransfer}. + * Map containing all types and corresponding data from the {@code + * DataTransfer} object. * @param effectAllowed - * Parameter from {@code DataTransfer.effectAllowed}. + * Allowed effects from {@code DataTransfer.effectAllowed} object. */ - public DragEndEvent(Component source, List<String> types, - Map<String, String> data, EffectAllowed effectAllowed) { + public DragEndEvent(T source, List<String> types, Map<String, String> data, + EffectAllowed effectAllowed) { super(source); // Create a linked map that preserves the order of types @@ -74,4 +77,15 @@ public class DragEndEvent extends Component.Event { public EffectAllowed getEffectAllowed() { return effectAllowed; } + + /** + * Returns the drag source component where the dragend event occurred. + * + * @return Component which was dragged. + */ + @Override + @SuppressWarnings("unchecked") + public T getComponent() { + return (T) super.getComponent(); + } } diff --git a/server/src/main/java/com/vaadin/event/dnd/DragEndListener.java b/server/src/main/java/com/vaadin/event/dnd/DragEndListener.java index 00a020353d..7b35bcab1d 100644 --- a/server/src/main/java/com/vaadin/event/dnd/DragEndListener.java +++ b/server/src/main/java/com/vaadin/event/dnd/DragEndListener.java @@ -18,23 +18,27 @@ package com.vaadin.event.dnd; import java.lang.reflect.Method; import com.vaadin.event.ConnectorEventListener; +import com.vaadin.ui.Component; /** * Interface to be implemented when creating a dragend listener on a drag * source for HTML5 drag and drop. * + * @param <T> + * Type of draggable component. * @see DragSourceExtension#addDragEndListener(DragEndListener) */ @FunctionalInterface -public interface DragEndListener extends ConnectorEventListener { +public interface DragEndListener<T extends Component> extends + ConnectorEventListener { static final Method DRAGEND_METHOD = DragEndListener.class .getDeclaredMethods()[0]; /** - * Called when a server side dragend event is fired. + * Called when dragend event is fired. * * @param event - * The dragend event that is fired. + * Server side dragend event. */ - void dragEnd(DragEndEvent event); + void dragEnd(DragEndEvent<T> event); } diff --git a/server/src/main/java/com/vaadin/event/dnd/DragSourceExtension.java b/server/src/main/java/com/vaadin/event/dnd/DragSourceExtension.java index d90e1bd8b8..78f606c05a 100644 --- a/server/src/main/java/com/vaadin/event/dnd/DragSourceExtension.java +++ b/server/src/main/java/com/vaadin/event/dnd/DragSourceExtension.java @@ -20,7 +20,6 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; -import com.vaadin.server.AbstractClientConnector; import com.vaadin.server.AbstractExtension; import com.vaadin.shared.Registration; import com.vaadin.shared.ui.dnd.DragSourceRpc; @@ -29,41 +28,40 @@ import com.vaadin.shared.ui.dnd.EffectAllowed; import com.vaadin.ui.AbstractComponent; /** - * Extension to add drag source functionality to a component for using HTML5 - * drag and drop. + * Extension to make a component drag source for HTML5 drag and drop + * functionality. + * + * @param <T> + * Type of the component to be extended. */ -public class DragSourceExtension extends AbstractExtension { +public class DragSourceExtension<T extends AbstractComponent> extends + AbstractExtension { /** - * Constructor for {@link DragSourceExtension} + * Extends {@code target} component and makes it a drag source. + * + * @param target + * Component to be extended. */ - public DragSourceExtension() { + public DragSourceExtension(T target) { registerRpc(new DragSourceRpc() { @Override public void dragStart() { - DragStartEvent event = new DragStartEvent( - (AbstractComponent) getParent(), getState(false).types, - getState(false).data, getState(false).effectAllowed); + DragStartEvent<T> event = new DragStartEvent<>(target, + getState(false).types, getState(false).data, + getState(false).effectAllowed); fireEvent(event); } @Override public void dragEnd() { - DragEndEvent event = new DragEndEvent( - (AbstractComponent) getParent(), getState(false).types, - getState(false).data, getState(false).effectAllowed); + DragEndEvent<T> event = new DragEndEvent<>(target, + getState(false).types, getState(false).data, + getState(false).effectAllowed); fireEvent(event); } }); - } - /** - * Makes {@code target} component a drag source. - * - * @param target - * Component to be extended. - */ - public void extend(AbstractComponent target) { super.extend(target); } @@ -194,7 +192,7 @@ public class DragSourceExtension extends AbstractExtension { * Listener to handle dragstart event. * @return Handle to be used to remove this listener. */ - public Registration addDragStartListener(DragStartListener listener) { + public Registration addDragStartListener(DragStartListener<T> listener) { return addListener(DragSourceState.EVENT_DRAGSTART, DragStartEvent.class, listener, DragStartListener.DRAGSTART_METHOD); @@ -209,7 +207,7 @@ public class DragSourceExtension extends AbstractExtension { * Listener to handle dragend event. * @return Handle to be used to remove this listener. */ - public Registration addDragEndListener(DragEndListener listener) { + public Registration addDragEndListener(DragEndListener<T> listener) { return addListener(DragSourceState.EVENT_DRAGEND, DragEndEvent.class, listener, DragEndListener.DRAGEND_METHOD); } @@ -223,4 +221,15 @@ public class DragSourceExtension extends AbstractExtension { protected DragSourceState getState(boolean markAsDirty) { return (DragSourceState) super.getState(markAsDirty); } + + /** + * Returns the component this extension is attached to. + * + * @return Extended component. + */ + @Override + @SuppressWarnings("unchecked") + public T getParent() { + return (T) super.getParent(); + } } diff --git a/server/src/main/java/com/vaadin/event/dnd/DragStartEvent.java b/server/src/main/java/com/vaadin/event/dnd/DragStartEvent.java index 591a0e61a7..de6a1a79f4 100644 --- a/server/src/main/java/com/vaadin/event/dnd/DragStartEvent.java +++ b/server/src/main/java/com/vaadin/event/dnd/DragStartEvent.java @@ -23,27 +23,31 @@ import com.vaadin.shared.ui.dnd.EffectAllowed; import com.vaadin.ui.Component; /** - * Server side dragstart event. Fired when an HTML5 dragstart happens. + * Server side dragstart event. Fired when an HTML5 dragstart happens on the + * client. * + * @param <T> + * Type of the component that is dragged. * @see DragSourceExtension#addDragStartListener(DragStartListener) */ -public class DragStartEvent extends Component.Event { +public class DragStartEvent<T extends Component> extends Component.Event { private final Map<String, String> data; private final EffectAllowed effectAllowed; /** - * Creates a new server side dragend event. + * Creates a server side dragstart event. * * @param source - * Draggable component. + * Component that is dragged. * @param types - * List of data types from {@code DataTransfer.types}. + * List of data types from {@code DataTransfer.types} object. * @param data - * Map of all data from {@code DataTransfer}. + * Map containing all types and corresponding data from the {@code + * DataTransfer} object. * @param effectAllowed - * Parameter from {@code DataTransfer.effectAllowed}. + * Allowed effects from {@code DataTransfer.effectAllowed} object. */ - public DragStartEvent(Component source, List<String> types, + public DragStartEvent(T source, List<String> types, Map<String, String> data, EffectAllowed effectAllowed) { super(source); @@ -74,4 +78,15 @@ public class DragStartEvent extends Component.Event { public EffectAllowed getEffectAllowed() { return effectAllowed; } + + /** + * Returns the drag source component where the dragstart event occurred. + * + * @return Component which is dragged. + */ + @Override + @SuppressWarnings("unchecked") + public T getComponent() { + return (T) super.getComponent(); + } } diff --git a/server/src/main/java/com/vaadin/event/dnd/DragStartListener.java b/server/src/main/java/com/vaadin/event/dnd/DragStartListener.java index 8d691dee6b..a9536144d9 100644 --- a/server/src/main/java/com/vaadin/event/dnd/DragStartListener.java +++ b/server/src/main/java/com/vaadin/event/dnd/DragStartListener.java @@ -18,23 +18,27 @@ package com.vaadin.event.dnd; import java.lang.reflect.Method; import com.vaadin.event.ConnectorEventListener; +import com.vaadin.ui.Component; /** * Interface to be implemented when creating a dragstart listener on a drag * source for HTML5 drag and drop. * + * @param <T> + * Type of draggable component. * @see DragSourceExtension#addDragStartListener(DragStartListener) */ @FunctionalInterface -public interface DragStartListener extends ConnectorEventListener { +public interface DragStartListener<T extends Component> extends + ConnectorEventListener { static final Method DRAGSTART_METHOD = DragStartListener.class .getDeclaredMethods()[0]; /** - * Called when a server side dragstart event is fired. + * Called when dragstart event is fired. * * @param event - * The dragstart event that is fired. + * Server side dragstart event. */ - void dragStart(DragStartEvent event); + void dragStart(DragStartEvent<T> event); } diff --git a/server/src/main/java/com/vaadin/event/dnd/DropEvent.java b/server/src/main/java/com/vaadin/event/dnd/DropEvent.java index 382b93bc55..5257a06367 100644 --- a/server/src/main/java/com/vaadin/event/dnd/DropEvent.java +++ b/server/src/main/java/com/vaadin/event/dnd/DropEvent.java @@ -25,26 +25,29 @@ import com.vaadin.ui.Component; /** * Server side drop event. Fired when an HTML5 drop happens. * + * @param <T> + * Type of the drop target component. * @see DropTargetExtension#addDropListener(DropListener) */ -public class DropEvent extends Component.Event { +public class DropEvent<T extends Component> extends Component.Event { private final Map<String, String> data; private final DropEffect dropEffect; /** - * Creates a new server side drop event. + * Creates a server side drop event. * * @param source - * Drop target component. + * Component that is dragged. * @param types - * List of data types from {@code DataTransfer.types}. + * List of data types from {@code DataTransfer.types} object. * @param data - * Map of all data from {@code DataTransfer}. + * Map containing all types and corresponding data from the {@code + * DataTransfer} object. * @param dropEffect - * Parameter from {@code DataTransfer.dropEffect}. + * Drop effect from {@code DataTransfer.dropEffect} object. */ - public DropEvent(Component source, List<String> types, - Map<String, String> data, DropEffect dropEffect) { + public DropEvent(T source, List<String> types, Map<String, String> data, + DropEffect dropEffect) { super(source); // Create a linked map that preserves the order of types @@ -74,4 +77,15 @@ public class DropEvent extends Component.Event { public DropEffect getDropEffect() { return dropEffect; } + + /** + * Returns the drop target component where the drop event occurred. + * + * @return Component on which a drag source was dropped. + */ + @Override + @SuppressWarnings("unchecked") + public T getComponent() { + return (T) super.getComponent(); + } } diff --git a/server/src/main/java/com/vaadin/event/dnd/DropListener.java b/server/src/main/java/com/vaadin/event/dnd/DropListener.java index 8cb96d1852..41c1bfc6ff 100644 --- a/server/src/main/java/com/vaadin/event/dnd/DropListener.java +++ b/server/src/main/java/com/vaadin/event/dnd/DropListener.java @@ -18,21 +18,27 @@ package com.vaadin.event.dnd; import java.lang.reflect.Method; import com.vaadin.event.ConnectorEventListener; +import com.vaadin.ui.Component; /** * Interface to be implemented when creating a drop listener on a drop target - * for HTML5 drag and drop. See {@link DropTargetExtension#addDropListener(DropListener)}. + * for HTML5 drag and drop. + * + * @param <T> + * Type of the drop target component. + * @see DropTargetExtension#addDropListener(DropListener) */ @FunctionalInterface -public interface DropListener extends ConnectorEventListener { +public interface DropListener<T extends Component> extends + ConnectorEventListener { static final Method DROP_METHOD = DropListener.class .getDeclaredMethods()[0]; /** - * Called when a server side drop event is fired. + * Called when drop event is fired. * * @param event - * The drop event that is fired. + * Server side drop event. */ - void drop(DropEvent event); + void drop(DropEvent<T> event); } diff --git a/server/src/main/java/com/vaadin/event/dnd/DropTargetExtension.java b/server/src/main/java/com/vaadin/event/dnd/DropTargetExtension.java index 20819ec70f..4fdd606eb5 100644 --- a/server/src/main/java/com/vaadin/event/dnd/DropTargetExtension.java +++ b/server/src/main/java/com/vaadin/event/dnd/DropTargetExtension.java @@ -17,7 +17,6 @@ package com.vaadin.event.dnd; import java.util.Objects; -import com.vaadin.server.AbstractClientConnector; import com.vaadin.server.AbstractExtension; import com.vaadin.shared.Registration; import com.vaadin.shared.ui.dnd.DropEffect; @@ -28,28 +27,27 @@ import com.vaadin.ui.AbstractComponent; /** * Extension to add drop target functionality to a widget for using HTML5 drag * and drop. + * + * @param <T> + * Type of the component to be extended. */ -public class DropTargetExtension extends AbstractExtension { +public class DropTargetExtension<T extends AbstractComponent> extends + AbstractExtension { /** - * Constructor for {@link DropTargetExtension}. + * Extends {@code target} component and makes it a drop target. + * + * @param target + * Component to be extended. */ - public DropTargetExtension() { + public DropTargetExtension(T target) { registerRpc((DropTargetRpc) (types, data, dropEffect) -> { - DropEvent event = new DropEvent((AbstractComponent) getParent(), - types, data, dropEffect); + DropEvent<T> event = new DropEvent<>(target, types, data, + dropEffect); fireEvent(event); }); - } - /** - * Makes {@code target} component a drop target. - * - * @param target - * Component to be extended. - */ - public void extend(AbstractComponent target) { super.extend(target); } @@ -129,7 +127,7 @@ public class DropTargetExtension extends AbstractExtension { * Listener to handle drop event. * @return Handle to be used to remove this listener. */ - public Registration addDropListener(DropListener listener) { + public Registration addDropListener(DropListener<T> listener) { return addListener(DropEvent.class, listener, DropListener.DROP_METHOD); } @@ -142,4 +140,15 @@ public class DropTargetExtension extends AbstractExtension { protected DropTargetState getState(boolean markAsDirty) { return (DropTargetState) super.getState(markAsDirty); } + + /** + * Returns the component this extension is attached to. + * + * @return Extended component. + */ + @Override + @SuppressWarnings("unchecked") + public T getParent() { + return (T) super.getParent(); + } } |