diff options
author | Adam Wagner <wbadam@users.noreply.github.com> | 2017-03-22 16:41:35 +0200 |
---|---|---|
committer | Henri Sara <henri.sara@gmail.com> | 2017-03-22 16:41:35 +0200 |
commit | acbb5a787b8623745aa440b37705e76dfab21d4e (patch) | |
tree | b85b2e1fae185c54f88123c77db2b8b0c34177a7 /server/src/main | |
parent | 52359708adb2033eab2765377d7bcba0b8a97b54 (diff) | |
download | vaadin-framework-acbb5a787b8623745aa440b37705e76dfab21d4e.tar.gz vaadin-framework-acbb5a787b8623745aa440b37705e76dfab21d4e.zip |
Remove support for transfer data type (#8910)
* Remove API for setting arbitrary drag data
* Store the drag source component in the UI
Fixes #8893
Diffstat (limited to 'server/src/main')
6 files changed, 109 insertions, 154 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 d8244d1f16..f1176e1880 100644 --- a/server/src/main/java/com/vaadin/event/dnd/DragEndEvent.java +++ b/server/src/main/java/com/vaadin/event/dnd/DragEndEvent.java @@ -15,10 +15,6 @@ */ package com.vaadin.event.dnd; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - import com.vaadin.shared.ui.dnd.EffectAllowed; import com.vaadin.ui.AbstractComponent; import com.vaadin.ui.Component; @@ -33,7 +29,7 @@ import com.vaadin.ui.Component; * @since 8.1 */ public class DragEndEvent<T extends AbstractComponent> extends Component.Event { - private final Map<String, String> data; + private final String dataTransferText; private final EffectAllowed effectAllowed; /** @@ -41,35 +37,30 @@ public class DragEndEvent<T extends AbstractComponent> extends Component.Event { * * @param source * Component that was dragged. - * @param types - * List of data types from {@code DataTransfer.types} object. - * @param data - * Map containing all types and corresponding data from the {@code - * DataTransfer} object. + * @param dataTransferText + * Data of type {@code "text"} from the {@code DataTransfer} + * object. * @param effectAllowed * Allowed effects from {@code DataTransfer.effectAllowed} object. */ - public DragEndEvent(T source, List<String> types, Map<String, String> data, + public DragEndEvent(T source, String dataTransferText, EffectAllowed effectAllowed) { super(source); - // Create a linked map that preserves the order of types - this.data = new LinkedHashMap<>(); - types.forEach(type -> this.data.put(type, data.get(type))); + this.dataTransferText = dataTransferText; this.effectAllowed = effectAllowed; } /** - * Get data from the client side {@code DataTransfer} object. + * Get data of type {@code "text"} from the client side {@code DataTransfer} + * object. * - * @param format - * Data format, e.g. {@code text/plain} or {@code text/uri-list}. - * @return Data for the given format if exists in the client side {@code - * DataTransfer}, otherwise {@code null}. + * @return Data of type {@code "text"} if exists in the client side {@code + * DataTransfer} object, otherwise {@literal null}. */ - public String getTransferData(String format) { - return data != null ? data.get(format) : null; + public String getDataTransferText() { + return dataTransferText; } /** 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 2e3d7e1c82..f7089c9b9e 100644 --- a/server/src/main/java/com/vaadin/event/dnd/DragSourceExtension.java +++ b/server/src/main/java/com/vaadin/event/dnd/DragSourceExtension.java @@ -15,9 +15,6 @@ */ package com.vaadin.event.dnd; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.Map; import java.util.Objects; import com.vaadin.server.AbstractExtension; @@ -39,6 +36,9 @@ import com.vaadin.ui.AbstractComponent; public class DragSourceExtension<T extends AbstractComponent> extends AbstractExtension { + private Registration dragStartListenerHandle; + private Registration dragEndListenerHandle; + /** * Extends {@code target} component and makes it a drag source. * @@ -50,7 +50,7 @@ public class DragSourceExtension<T extends AbstractComponent> extends @Override public void dragStart() { DragStartEvent<T> event = new DragStartEvent<>(target, - getState(false).types, getState(false).data, + getState(false).dataTransferText, getState(false).effectAllowed); fireEvent(event); } @@ -58,13 +58,30 @@ public class DragSourceExtension<T extends AbstractComponent> extends @Override public void dragEnd() { DragEndEvent<T> event = new DragEndEvent<>(target, - getState(false).types, getState(false).data, + getState(false).dataTransferText, getState(false).effectAllowed); fireEvent(event); } }); super.extend(target); + + // Set current extension as active drag source in the UI + dragStartListenerHandle = addDragStartListener( + event -> getUI().setActiveDragSource(this)); + + // Remove current extension as active drag source from the UI + dragEndListenerHandle = addDragEndListener( + event -> getUI().setActiveDragSource(null)); + } + + @Override + public void remove() { + super.remove(); + + // Remove listeners attached on construction + dragStartListenerHandle.remove(); + dragEndListenerHandle.remove(); } /** @@ -100,89 +117,32 @@ public class DragSourceExtension<T extends AbstractComponent> extends } /** - * Sets the data for this drag source element. Used to set data for client - * side drag element using {@code DataTransfer.setData()}. To be used as a - * map, key-value pairs are stored. Order of entries are preserved. - * <p> - * Note that by HTML specification, the browser will change data type - * "{@code text}" to "{@code text/plain}" and "{@code url}" to "{@code - * text/uri-list}" during client side drag event. + * Sets data for this drag source element. The data is set for the client + * side draggable element using the {@code DataTransfer.setData("text", + * data)} method. * - * @param format - * Data type to store, e.g. {@code text/plain} or {@code - * text/uri-list}. Cannot be {@code null}. * @param data - * Data to store for the data type. Cannot be {@code null}. + * Data to be set for the client side draggable element. */ - public void setTransferData(String format, String data) { - if (format == null) { - throw new IllegalArgumentException("Data type cannot be null"); - } - - if (data == null) { - throw new IllegalArgumentException("Data cannot be null"); - } - - if (!getState(false).types.contains(format)) { - getState().types.add(format); - } - getState().data.put(format, data); + public void setDataTransferText(String data) { + getState().dataTransferText = data; } /** - * Returns the data stored for {@code format} type in this drag source + * Returns the data stored with type {@code "text"} in this drag source * element. * - * @param format - * Data type of the requested data, e.g. {@code text/plain} or - * {@code text/uri-list}. - * @return Data that is stored for {@code format} data type. + * @return Data of type {@code "text"} stored in this drag source element. */ - public String getTransferData(String format) { - return getState(false).data.get(format); - } - - /** - * Returns the map of data stored in this drag source element. The returned - * map preserves the order of storage and is unmodifiable. - * - * @return Unmodifiable copy of the map of data in the order the data was - * stored. - */ - public Map<String, String> getTransferData() { - Map<String, String> data = getState(false).data; - - // Create a map of data that preserves the order of types - LinkedHashMap<String, String> orderedData = new LinkedHashMap<>( - data.size()); - getState(false).types - .forEach(type -> orderedData.put(type, data.get(type))); - - return Collections.unmodifiableMap(orderedData); - } - - /** - * Clears data with the given type for this drag source element when - * present. - * - * @param format - * Type of data to be cleared. Cannot be {@code null}. - */ - public void clearTransferData(String format) { - if (format == null) { - throw new IllegalArgumentException("Data type cannot be null"); - } - - getState().types.remove(format); - getState().data.remove(format); + public String getDataTransferText() { + return getState(false).dataTransferText; } /** - * Clears all data for this drag source element. + * Clears data of type {@code "text"} in this drag source element. */ - public void clearTransferData() { - getState().types.clear(); - getState().data.clear(); + public void clearDataTransferText() { + getState().dataTransferText = null; } /** 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 642f9b3b06..bfe1bc8c83 100644 --- a/server/src/main/java/com/vaadin/event/dnd/DragStartEvent.java +++ b/server/src/main/java/com/vaadin/event/dnd/DragStartEvent.java @@ -15,10 +15,6 @@ */ package com.vaadin.event.dnd; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - import com.vaadin.shared.ui.dnd.EffectAllowed; import com.vaadin.ui.AbstractComponent; import com.vaadin.ui.Component; @@ -35,7 +31,7 @@ import com.vaadin.ui.Component; */ public class DragStartEvent<T extends AbstractComponent> extends Component.Event { - private final Map<String, String> data; + private final String dataTransferText; private final EffectAllowed effectAllowed; /** @@ -43,35 +39,30 @@ public class DragStartEvent<T extends AbstractComponent> extends * * @param source * Component that is dragged. - * @param types - * List of data types from {@code DataTransfer.types} object. - * @param data - * Map containing all types and corresponding data from the {@code - * DataTransfer} object. + * @param dataTransferText + * Data of type {@code "text"} from the {@code DataTransfer} + * object. * @param effectAllowed * Allowed effects from {@code DataTransfer.effectAllowed} object. */ - public DragStartEvent(T source, List<String> types, - Map<String, String> data, EffectAllowed effectAllowed) { + public DragStartEvent(T source, String dataTransferText, + EffectAllowed effectAllowed) { super(source); - // Create a linked map that preserves the order of types - this.data = new LinkedHashMap<>(); - types.forEach(type -> this.data.put(type, data.get(type))); + this.dataTransferText = dataTransferText; this.effectAllowed = effectAllowed; } /** - * Get data from the client side {@code DataTransfer} object. + * Get data of type {@code "text"} from the client side {@code DataTransfer} + * object. * - * @param format - * Data format, e.g. {@code text/plain} or {@code text/uri-list}. - * @return Data for the given format if exists in the client side {@code - * DataTransfer}, otherwise {@code null}. + * @return Data of type {@code "text"} if exists in the client side {@code + * DataTransfer} object, otherwise {@literal null}. */ - public String getTransferData(String format) { - return data != null ? data.get(format) : null; + public String getDataTransferText() { + return dataTransferText; } /** 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 433276550b..3fb655f176 100644 --- a/server/src/main/java/com/vaadin/event/dnd/DropEvent.java +++ b/server/src/main/java/com/vaadin/event/dnd/DropEvent.java @@ -15,9 +15,6 @@ */ package com.vaadin.event.dnd; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; import java.util.Optional; import com.vaadin.shared.ui.dnd.DropEffect; @@ -34,9 +31,9 @@ import com.vaadin.ui.Component; * @since 8.1 */ public class DropEvent<T extends AbstractComponent> extends Component.Event { - private final Map<String, String> data; + private final String dataTransferText; private final DropEffect dropEffect; - private final DragSourceExtension<AbstractComponent> dragSourceExtension; + private final DragSourceExtension<? extends AbstractComponent> dragSourceExtension; private final AbstractComponent dragSource; /** @@ -44,25 +41,20 @@ public class DropEvent<T extends AbstractComponent> extends Component.Event { * * @param target * Component that received the drop. - * @param types - * List of data types from {@code DataTransfer.types} object. - * @param data - * Map containing all types and corresponding data from the {@code - * DataTransfer} object. + * @param dataTransferText + * Data of type {@code "text"} from the {@code DataTransfer} + * object. * @param dropEffect * Drop effect from {@code DataTransfer.dropEffect} object. * @param dragSourceExtension * Drag source extension of the component that initiated the drop * event. */ - public DropEvent(T target, List<String> types, Map<String, String> data, - DropEffect dropEffect, - DragSourceExtension<AbstractComponent> dragSourceExtension) { + public DropEvent(T target, String dataTransferText, DropEffect dropEffect, + DragSourceExtension<? extends AbstractComponent> dragSourceExtension) { super(target); - // Create a linked map that preserves the order of types - this.data = new LinkedHashMap<>(); - types.forEach(type -> this.data.put(type, data.get(type))); + this.dataTransferText = dataTransferText; this.dropEffect = dropEffect; @@ -72,15 +64,14 @@ public class DropEvent<T extends AbstractComponent> extends Component.Event { } /** - * Get data from the client side {@code DataTransfer} object. + * Get data of type {@code "text"} from the client side {@code DataTransfer} + * object. * - * @param format - * Data format, e.g. {@code text/plain} or {@code text/uri-list}. - * @return Data for the given format if exists in the client side {@code - * DataTransfer}, otherwise {@code null}. + * @return Data of type {@code "text"} if exists in the client side {@code + * DataTransfer} object, otherwise {@literal null}. */ - public String getTransferData(String format) { - return data != null ? data.get(format) : null; + public String getDataTransferText() { + return dataTransferText; } /** @@ -110,7 +101,7 @@ public class DropEvent<T extends AbstractComponent> extends Component.Event { * * @return Drag source extension or an empty optional */ - public Optional<DragSourceExtension<AbstractComponent>> getDragSourceExtension() { + public Optional<DragSourceExtension<? extends AbstractComponent>> getDragSourceExtension() { return Optional.ofNullable(dragSourceExtension); } 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 fd363573d1..2df43027a1 100644 --- a/server/src/main/java/com/vaadin/event/dnd/DropTargetExtension.java +++ b/server/src/main/java/com/vaadin/event/dnd/DropTargetExtension.java @@ -18,7 +18,6 @@ package com.vaadin.event.dnd; import java.util.Objects; import com.vaadin.server.AbstractExtension; -import com.vaadin.server.ClientConnector; import com.vaadin.shared.Registration; import com.vaadin.shared.ui.dnd.DropEffect; import com.vaadin.shared.ui.dnd.DropTargetRpc; @@ -44,17 +43,9 @@ public class DropTargetExtension<T extends AbstractComponent> extends * Component to be extended. */ public DropTargetExtension(T target) { - registerRpc((DropTargetRpc) (types, data, dropEffect, dataSourceId) -> { - DragSourceExtension dragSource = null; - - ClientConnector connector = getUI().getConnectorTracker() - .getConnector(dataSourceId); - if (connector != null && connector instanceof DragSourceExtension) { - dragSource = (DragSourceExtension) connector; - } - - DropEvent<T> event = new DropEvent<>(target, types, data, - dropEffect, dragSource); + registerRpc((DropTargetRpc) (dataTransferText, dropEffect) -> { + DropEvent<T> event = new DropEvent<>(target, dataTransferText, + dropEffect, getUI().getActiveDragSource()); fireEvent(event); }); diff --git a/server/src/main/java/com/vaadin/ui/UI.java b/server/src/main/java/com/vaadin/ui/UI.java index 6341217e82..31de66c558 100644 --- a/server/src/main/java/com/vaadin/ui/UI.java +++ b/server/src/main/java/com/vaadin/ui/UI.java @@ -44,6 +44,7 @@ import com.vaadin.event.MouseEvents.ClickListener; import com.vaadin.event.UIEvents.PollEvent; import com.vaadin.event.UIEvents.PollListener; import com.vaadin.event.UIEvents.PollNotifier; +import com.vaadin.event.dnd.DragSourceExtension; import com.vaadin.navigator.Navigator; import com.vaadin.server.ClientConnector; import com.vaadin.server.ComponentSizeValidator; @@ -295,6 +296,11 @@ public abstract class UI extends AbstractSingleComponentContainer private int lastProcessedClientToServerId = -1; /** + * Stores the extension of the active drag source component + */ + private DragSourceExtension<? extends AbstractComponent> activeDragSource; + + /** * Creates a new empty UI without a caption. The content of the UI must be * set by calling {@link #setContent(Component)} before using the UI. */ @@ -1803,6 +1809,31 @@ public abstract class UI extends AbstractSingleComponentContainer } /** + * Sets the drag source of an active HTML5 drag event. + * + * @param extension + * Extension of the drag source component. + * @see DragSourceExtension + * @since + */ + public void setActiveDragSource( + DragSourceExtension<? extends AbstractComponent> extension) { + this.activeDragSource = extension; + } + + /** + * Gets the drag source of an active HTML5 drag event. + * + * @return Extension of the drag source component if the drag event is + * active and originated from this UI, {@literal null} otherwise. + * @see DragSourceExtension + * @since + */ + public DragSourceExtension<? extends AbstractComponent> getActiveDragSource() { + return this.activeDragSource; + } + + /** * Event which is fired when the ordering of the windows is updated. * <p> * The other way to listen window position for specific window is |