diff options
author | Adam Wagner <wbadam@users.noreply.github.com> | 2017-03-30 13:18:10 +0300 |
---|---|---|
committer | Henri Sara <henri.sara@gmail.com> | 2017-04-12 14:58:11 +0300 |
commit | 659313e8c35e68d14fe2599b9bbb4dbba35fb4a3 (patch) | |
tree | 17c1c3c9bcad4043c79c7499262f076a56c96374 | |
parent | 1ac4f9724106fcf4abaab892210122231710c6be (diff) | |
download | vaadin-framework-659313e8c35e68d14fe2599b9bbb4dbba35fb4a3.tar.gz vaadin-framework-659313e8c35e68d14fe2599b9bbb4dbba35fb4a3.zip |
Provide dragged Grid items in server side (#8950)
Fixes #8931
16 files changed, 497 insertions, 81 deletions
diff --git a/client/src/main/java/com/vaadin/client/connectors/grid/GridDragSourceExtensionConnector.java b/client/src/main/java/com/vaadin/client/connectors/grid/GridDragSourceExtensionConnector.java index b31a2578eb..40eb80621c 100644 --- a/client/src/main/java/com/vaadin/client/connectors/grid/GridDragSourceExtensionConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/grid/GridDragSourceExtensionConnector.java @@ -19,7 +19,6 @@ import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; -import com.google.gwt.dom.client.NativeEvent; import com.google.gwt.dom.client.TableRowElement; import com.vaadin.client.ServerConnector; import com.vaadin.client.extensions.DragSourceExtensionConnector; @@ -29,15 +28,16 @@ import com.vaadin.client.widgets.Escalator; import com.vaadin.client.widgets.Grid; import com.vaadin.shared.Range; import com.vaadin.shared.ui.Connect; -import com.vaadin.shared.ui.dnd.DragSourceState; +import com.vaadin.shared.ui.dnd.DropEffect; +import com.vaadin.shared.ui.grid.GridDragSourceExtensionRpc; import com.vaadin.shared.ui.grid.GridDragSourceExtensionState; +import com.vaadin.shared.ui.grid.GridState; import com.vaadin.ui.GridDragSourceExtension; import elemental.events.Event; import elemental.json.Json; import elemental.json.JsonArray; import elemental.json.JsonObject; -import elemental.json.JsonValue; /** * Adds HTML5 drag and drop functionality to a {@link com.vaadin.client.widgets.Grid @@ -53,6 +53,11 @@ public class GridDragSourceExtensionConnector extends private GridConnector gridConnector; + /** + * List of dragged item keys. + */ + private List<String> draggedItemKeys; + @Override protected void extend(ServerConnector target) { this.gridConnector = (GridConnector) target; @@ -67,25 +72,66 @@ public class GridDragSourceExtensionConnector extends @Override protected void onDragStart(Event event) { + + // Collect the keys of dragged rows + draggedItemKeys = getDraggedRows(event).stream() + .map(row -> row.getString(GridState.JSONKEY_ROWKEY)) + .collect(Collectors.toList()); + super.onDragStart(event); + } + + @Override + protected String createDataTransferText(Event dragStartEvent) { + JsonArray dragData = toJsonArray( + getDraggedRows(dragStartEvent).stream().map(this::getDragData) + .collect(Collectors.toList())); + return dragData.toJson(); + } + + @Override + protected void sendDragStartEventToServer(Event dragStartEvent) { + + // Start server RPC with dragged item keys + getRpcProxy(GridDragSourceExtensionRpc.class) + .dragStart(draggedItemKeys); + } - if (event.getTarget() instanceof TableRowElement) { - TableRowElement row = (TableRowElement) event.getTarget(); + private List<JsonObject> getDraggedRows(Event dragStartEvent) { + List<JsonObject> draggedRows = new ArrayList<>(); + + if (dragStartEvent.getTarget() instanceof TableRowElement) { + TableRowElement row = (TableRowElement) dragStartEvent.getTarget(); int rowIndex = ((Escalator.AbstractRowContainer) getGridBody()) .getLogicalRowIndex(row); JsonObject rowData = gridConnector.getDataSource().getRow(rowIndex); - // Generate drag data. Dragged row or all the selected rows - JsonValue dragData = dragMultipleRows(rowData) ? toJsonArray( - getSelectedVisibleRows().stream().map(this::getDragData) - .collect(Collectors.toList())) - : getDragData(rowData); - - // Set drag data in DataTransfer object - ((NativeEvent) event).getDataTransfer() - .setData(DragSourceState.DATA_TYPE_TEXT, dragData.toJson()); + if (dragMultipleRows(rowData)) { + getSelectedVisibleRows().forEach(draggedRows::add); + } else { + draggedRows.add(rowData); + } } + + return draggedRows; + } + + @Override + protected void onDragEnd(Event event) { + super.onDragEnd(event); + + // Clear item key list + draggedItemKeys = null; + } + + @Override + protected void sendDragEndEventToServer(Event dragEndEvent, + DropEffect dropEffect) { + + // Send server RPC with dragged item keys + getRpcProxy(GridDragSourceExtensionRpc.class) + .dragEnd(dropEffect, draggedItemKeys); } /** diff --git a/client/src/main/java/com/vaadin/client/extensions/DragSourceExtensionConnector.java b/client/src/main/java/com/vaadin/client/extensions/DragSourceExtensionConnector.java index 52c70b362b..cf7db05830 100644 --- a/client/src/main/java/com/vaadin/client/extensions/DragSourceExtensionConnector.java +++ b/client/src/main/java/com/vaadin/client/extensions/DragSourceExtensionConnector.java @@ -137,13 +137,41 @@ public class DragSourceExtensionConnector extends AbstractExtensionConnector { // Set text data parameter nativeEvent.getDataTransfer().setData(DragSourceState.DATA_TYPE_TEXT, - getState().dataTransferText); + createDataTransferText(event)); // Initiate firing server side dragstart event when there is a // DragStartListener attached on the server side if (hasEventListener(DragSourceState.EVENT_DRAGSTART)) { - getRpcProxy(DragSourceRpc.class).dragStart(); + sendDragStartEventToServer(event); } + + // Stop event bubbling + nativeEvent.stopPropagation(); + } + + /** + * Creates data of type {@code "text"} for the {@code DataTransfer} object + * of the given event. + * + * @param dragStartEvent + * Event to set the data for. + * @return Textual data to be set for the event or {@literal null}. + */ + protected String createDataTransferText(Event dragStartEvent) { + return getState().dataTransferText; + } + + /** + * Initiates a server RPC for the drag start event. + * <p> + * This method is called only if there is a server side drag start event + * handler attached. + * + * @param dragStartEvent + * Client side dragstart event. + */ + protected void sendDragStartEventToServer(Event dragStartEvent) { + getRpcProxy(DragSourceRpc.class).dragStart(); } /** @@ -162,12 +190,26 @@ public class DragSourceExtensionConnector extends AbstractExtensionConnector { assert dropEffect != null : "Drop effect should never be null"; - getRpcProxy(DragSourceRpc.class) - .dragEnd(DropEffect.valueOf(dropEffect.toUpperCase())); + sendDragEndEventToServer(event, + DropEffect.valueOf(dropEffect.toUpperCase())); } } /** + * Initiates a server RPC for the drag end event. + * + * @param dragEndEvent + * Client side dragend event. + * @param dropEffect + * Drop effect of the dragend event, extracted from {@code + * DataTransfer.dropEffect} parameter. + */ + protected void sendDragEndEventToServer(Event dragEndEvent, + DropEffect dropEffect) { + getRpcProxy(DragSourceRpc.class).dragEnd(dropEffect); + } + + /** * Finds the draggable element within the widget. By default, returns the * topmost element. * 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 57f111e848..bb9b97a900 100644 --- a/server/src/main/java/com/vaadin/event/dnd/DragEndEvent.java +++ b/server/src/main/java/com/vaadin/event/dnd/DragEndEvent.java @@ -20,7 +20,7 @@ import com.vaadin.ui.AbstractComponent; import com.vaadin.ui.Component; /** - * Server side dragend event. Fired when an HTML5 dragend happens on the client. + * HTML5 drag end event. * * @param <T> * Type of the component that was dragged. @@ -29,41 +29,23 @@ import com.vaadin.ui.Component; * @since 8.1 */ public class DragEndEvent<T extends AbstractComponent> extends Component.Event { - private final String dataTransferText; private final DropEffect dropEffect; /** - * Creates a server side dragend event. + * Creates a drag end event. * * @param source * Component that was dragged. - * @param dataTransferText - * Data of type {@code "text"} from the {@code DataTransfer} - * object. * @param dropEffect * Drop effect from {@code DataTransfer.dropEffect} object. */ - public DragEndEvent(T source, String dataTransferText, - DropEffect dropEffect) { + public DragEndEvent(T source, DropEffect dropEffect) { super(source); - this.dataTransferText = dataTransferText; - this.dropEffect = dropEffect; } /** - * Get data of type {@code "text"} from the client side {@code DataTransfer} - * object. - * - * @return Data of type {@code "text"} if exists in the client side {@code - * DataTransfer} object, otherwise {@literal null}. - */ - public String getDataTransferText() { - return dataTransferText; - } - - /** * Get drop effect of the dragend event. * * @return The {@code DataTransfer.dropEffect} parameter of the client side 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 bce643ffdb..88b3133b2b 100644 --- a/server/src/main/java/com/vaadin/event/dnd/DragSourceExtension.java +++ b/server/src/main/java/com/vaadin/event/dnd/DragSourceExtension.java @@ -53,32 +53,49 @@ public class DragSourceExtension<T extends AbstractComponent> extends * Component to be extended. */ public DragSourceExtension(T target) { + + registerDragSourceRpc(target); + + super.extend(target); + + initListeners(); + } + + /** + * Initializes the event listeners this drag source is using. + */ + protected void initListeners() { + + // 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)); + } + + /** + * Register server RPC. + * + * @param target + * Extended component. + */ + protected void registerDragSourceRpc(T target) { registerRpc(new DragSourceRpc() { @Override public void dragStart() { DragStartEvent<T> event = new DragStartEvent<>(target, - getState(false).dataTransferText, getState(false).effectAllowed); fireEvent(event); } @Override public void dragEnd(DropEffect dropEffect) { - DragEndEvent<T> event = new DragEndEvent<>(target, - getState(false).dataTransferText, dropEffect); + DragEndEvent<T> event = new DragEndEvent<>(target, dropEffect); 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 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 bfe1bc8c83..2022f42a00 100644 --- a/server/src/main/java/com/vaadin/event/dnd/DragStartEvent.java +++ b/server/src/main/java/com/vaadin/event/dnd/DragStartEvent.java @@ -20,8 +20,7 @@ import com.vaadin.ui.AbstractComponent; import com.vaadin.ui.Component; /** - * Server side dragstart event. Fired when an HTML5 dragstart happens on the - * client. + * HTML5 drag start event. * * @param <T> * Type of the component that is dragged. @@ -31,41 +30,23 @@ import com.vaadin.ui.Component; */ public class DragStartEvent<T extends AbstractComponent> extends Component.Event { - private final String dataTransferText; private final EffectAllowed effectAllowed; /** - * Creates a server side dragstart event. + * Creates a drag start event. * * @param source * Component that is dragged. - * @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, String dataTransferText, - EffectAllowed effectAllowed) { + public DragStartEvent(T source, EffectAllowed effectAllowed) { super(source); - this.dataTransferText = dataTransferText; - this.effectAllowed = effectAllowed; } /** - * Get data of type {@code "text"} from the client side {@code DataTransfer} - * object. - * - * @return Data of type {@code "text"} if exists in the client side {@code - * DataTransfer} object, otherwise {@literal null}. - */ - public String getDataTransferText() { - return dataTransferText; - } - - /** * Returns the {@code effectAllowed} parameter of this event. * * @return This event's {@code effectAllowed} parameter. diff --git a/server/src/main/java/com/vaadin/event/dnd/grid/GridDragEndEvent.java b/server/src/main/java/com/vaadin/event/dnd/grid/GridDragEndEvent.java new file mode 100644 index 0000000000..8b4ceb1d4f --- /dev/null +++ b/server/src/main/java/com/vaadin/event/dnd/grid/GridDragEndEvent.java @@ -0,0 +1,62 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.event.dnd.grid; + +import java.util.Set; + +import com.vaadin.event.dnd.DragEndEvent; +import com.vaadin.shared.ui.dnd.DropEffect; +import com.vaadin.ui.Grid; + +/** + * Drop event on an HTML5 drop target {@link Grid} row. + * + * @param <T> + * The Grid bean type. + * @author Vaadin Ltd. + * @see com.vaadin.ui.GridDragSourceExtension#addGridDragStartListener(GridDragStartListener) + * @since + */ +public class GridDragEndEvent<T> extends DragEndEvent<Grid<T>> { + + private final Set<T> draggedItems; + + /** + * Creates a drag end event. + * + * @param source + * Grid component in which the items were dragged. + * @param dropEffect + * Drop effect from {@code DataTransfer.dropEffect} object. + * @param draggedItems + * Set of items having been dragged. + */ + public GridDragEndEvent(Grid<T> source, DropEffect dropEffect, + Set<T> draggedItems) { + super(source, dropEffect); + + this.draggedItems = draggedItems; + } + + /** + * Get the dragged row items. + * + * @return Set of row items that were being dragged. + */ + public Set<T> getDraggedItems() { + return draggedItems; + } +} diff --git a/server/src/main/java/com/vaadin/event/dnd/grid/GridDragEndListener.java b/server/src/main/java/com/vaadin/event/dnd/grid/GridDragEndListener.java new file mode 100644 index 0000000000..ad338e68db --- /dev/null +++ b/server/src/main/java/com/vaadin/event/dnd/grid/GridDragEndListener.java @@ -0,0 +1,45 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.event.dnd.grid; + +import java.lang.reflect.Method; + +import com.vaadin.event.ConnectorEventListener; + +/** + * Drop listener for HTML5 drop on a Grid row. + * + * @param <T> + * The Grid bean type. + * @author Vaadin Ltd. + * @see com.vaadin.ui.GridDragSourceExtension#addGridDragEndListener(GridDragEndListener) + * @since + */ +@FunctionalInterface +public interface GridDragEndListener<T> extends ConnectorEventListener { + + static final Method DRAG_END_METHOD = GridDragEndListener.class + .getDeclaredMethods()[0]; + + /** + * Invoked when the user has dropped the dragged grid rows, or canceled the + * drag. + * + * @param event + * The drag end event. + */ + void dragEnd(GridDragEndEvent<T> event); +} diff --git a/server/src/main/java/com/vaadin/event/dnd/grid/GridDragStartEvent.java b/server/src/main/java/com/vaadin/event/dnd/grid/GridDragStartEvent.java new file mode 100644 index 0000000000..50130043da --- /dev/null +++ b/server/src/main/java/com/vaadin/event/dnd/grid/GridDragStartEvent.java @@ -0,0 +1,62 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.event.dnd.grid; + +import java.util.Set; + +import com.vaadin.event.dnd.DragStartEvent; +import com.vaadin.shared.ui.dnd.EffectAllowed; +import com.vaadin.ui.Grid; + +/** + * Drag start event on an HTML5 drag source {@link Grid} row. + * + * @param <T> + * The Grid bean type. + * @author Vaadin Ltd. + * @see com.vaadin.ui.GridDragSourceExtension#addGridDragStartListener(GridDragStartListener) + * @since + */ +public class GridDragStartEvent<T> extends DragStartEvent<Grid<T>> { + + private final Set<T> draggedItems; + + /** + * Creates a drag start event. + * + * @param source + * The source grid where the rows are being dragged from. + * @param effectAllowed + * Allowed effect from {@code DataTransfer.effectAllowed} object. + * @param draggedItems + * Set of items being dragged. + */ + public GridDragStartEvent(Grid<T> source, EffectAllowed effectAllowed, + Set<T> draggedItems) { + super(source, effectAllowed); + + this.draggedItems = draggedItems; + } + + /** + * Get the dragged row items. + * + * @return Set of row items that are being dragged. + */ + public Set<T> getDraggedItems() { + return draggedItems; + } +} diff --git a/server/src/main/java/com/vaadin/event/dnd/grid/GridDragStartListener.java b/server/src/main/java/com/vaadin/event/dnd/grid/GridDragStartListener.java new file mode 100644 index 0000000000..f7dc06b0fe --- /dev/null +++ b/server/src/main/java/com/vaadin/event/dnd/grid/GridDragStartListener.java @@ -0,0 +1,44 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.event.dnd.grid; + +import java.lang.reflect.Method; + +import com.vaadin.event.ConnectorEventListener; + +/** + * Drag start listener for HTML5 drag start on a Grid row. + * + * @param <T> + * The Grid bean type. + * @author Vaadin Ltd. + * @see com.vaadin.ui.GridDragSourceExtension#addGridDragStartListener(GridDragStartListener) + * @since + */ +@FunctionalInterface +public interface GridDragStartListener<T> extends ConnectorEventListener { + + static final Method DRAG_START_METHOD = GridDragStartListener.class + .getDeclaredMethods()[0]; + + /** + * Invoked when the user has started dragging grid's rows. + * + * @param event + * The drag start event. + */ + void dragStart(GridDragStartEvent<T> event); +} diff --git a/server/src/main/java/com/vaadin/event/dnd/grid/GridDropEvent.java b/server/src/main/java/com/vaadin/event/dnd/grid/GridDropEvent.java index 96017e3a1d..07ba1327a6 100644 --- a/server/src/main/java/com/vaadin/event/dnd/grid/GridDropEvent.java +++ b/server/src/main/java/com/vaadin/event/dnd/grid/GridDropEvent.java @@ -26,7 +26,7 @@ import com.vaadin.ui.Grid; * @param <T> * The Grid bean type. * @author Vaadin Ltd. - * @see com.vaadin.ui.GridDropTargetExtension#addDropListener(GridDropListener) + * @see com.vaadin.ui.GridDropTargetExtension#addGridDropListener(GridDropListener) * @since */ public class GridDropEvent<T> extends DropEvent<Grid<T>> { diff --git a/server/src/main/java/com/vaadin/event/dnd/grid/GridDropListener.java b/server/src/main/java/com/vaadin/event/dnd/grid/GridDropListener.java index e1e365fbeb..1c148eb28d 100644 --- a/server/src/main/java/com/vaadin/event/dnd/grid/GridDropListener.java +++ b/server/src/main/java/com/vaadin/event/dnd/grid/GridDropListener.java @@ -18,8 +18,6 @@ package com.vaadin.event.dnd.grid; import java.lang.reflect.Method; import com.vaadin.event.ConnectorEventListener; -import com.vaadin.event.dnd.DropListener; -import com.vaadin.event.dnd.DropTargetExtension; /** * Drop listener for HTML5 drop on a Grid row. @@ -27,7 +25,7 @@ import com.vaadin.event.dnd.DropTargetExtension; * @param <T> * The Grid bean type. * @author Vaadin Ltd. - * @see com.vaadin.ui.GridDropTargetExtension#addDropListener(GridDropListener) + * @see com.vaadin.ui.GridDropTargetExtension#addGridDropListener(GridDropListener) * @since */ @FunctionalInterface diff --git a/server/src/main/java/com/vaadin/ui/GridDragSourceExtension.java b/server/src/main/java/com/vaadin/ui/GridDragSourceExtension.java index 908cfdb504..ca6e1b05fd 100644 --- a/server/src/main/java/com/vaadin/ui/GridDragSourceExtension.java +++ b/server/src/main/java/com/vaadin/ui/GridDragSourceExtension.java @@ -15,11 +15,22 @@ */ package com.vaadin.ui; +import java.util.List; import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; import com.vaadin.data.provider.DataGenerator; import com.vaadin.event.dnd.DragSourceExtension; +import com.vaadin.event.dnd.grid.GridDragEndEvent; +import com.vaadin.event.dnd.grid.GridDragEndListener; +import com.vaadin.event.dnd.grid.GridDragStartEvent; +import com.vaadin.event.dnd.grid.GridDragStartListener; import com.vaadin.server.SerializableFunction; +import com.vaadin.shared.Registration; +import com.vaadin.shared.ui.dnd.DragSourceState; +import com.vaadin.shared.ui.dnd.DropEffect; +import com.vaadin.shared.ui.grid.GridDragSourceExtensionRpc; import com.vaadin.shared.ui.grid.GridDragSourceExtensionState; import elemental.json.JsonObject; @@ -63,6 +74,45 @@ public class GridDragSourceExtension<T> extends DragSourceExtension<Grid<T>> { target.addDataGenerator(dragDataGenerator); } + @Override + protected void registerDragSourceRpc(Grid<T> target) { + registerRpc(new GridDragSourceExtensionRpc() { + @Override + public void dragStart(List<String> draggedItemKeys) { + + GridDragStartEvent<T> event = new GridDragStartEvent<>(target, + getState(false).effectAllowed, + getDraggedItems(target, draggedItemKeys)); + + fireEvent(event); + } + + @Override + public void dragEnd(DropEffect dropEffect, + List<String> draggedItemKeys) { + + GridDragEndEvent<T> event = new GridDragEndEvent<>(target, + dropEffect, getDraggedItems(target, draggedItemKeys)); + + fireEvent(event); + } + }); + } + + /** + * Collects the dragged items of a Grid given the list of item keys. + */ + private Set<T> getDraggedItems(Grid<T> grid, List<String> draggedItemKeys) { + if (draggedItemKeys == null || draggedItemKeys.isEmpty()) { + throw new IllegalStateException( + "The drag event does not contain dragged items"); + } + + return draggedItemKeys.stream() + .map(key -> grid.getDataCommunicator().getKeyMapper().get(key)) + .collect(Collectors.toSet()); + } + /** * Drag data generator. Appends drag data to row data json if generator * function is set by the user of this extension. @@ -100,6 +150,43 @@ public class GridDragSourceExtension<T> extends DragSourceExtension<Grid<T>> { generatorFunction = generator; } + @Override + public void setDataTransferText(String data) throws + UnsupportedOperationException { + throw new UnsupportedOperationException( + "Setting dataTransferText is not supported"); + } + + /** + * Attaches dragstart listener for the current drag source grid. + * + * @param listener + * Listener to handle the dragstart event. + * @return Handle to be used to remove this listener. + * @see GridDragStartEvent + */ + public Registration addGridDragStartListener( + GridDragStartListener<T> listener) { + return addListener(DragSourceState.EVENT_DRAGSTART, + GridDragStartEvent.class, listener, + GridDragStartListener.DRAG_START_METHOD); + } + + /** + * Attaches dragend listener for the current drag source grid. + * + * @param listener + * Listener to handle the dragend event. + * @return Handle to be used to remove this listener. + * @see GridDragEndEvent + */ + public Registration addGridDragEndListener( + GridDragEndListener<T> listener) { + return addListener(DragSourceState.EVENT_DRAGEND, + GridDragEndEvent.class, listener, + GridDragEndListener.DRAG_END_METHOD); + } + /** * Returns the generator function for customizing drag data. * diff --git a/server/src/main/java/com/vaadin/ui/GridDropTargetExtension.java b/server/src/main/java/com/vaadin/ui/GridDropTargetExtension.java index bf75e26783..1fcb7160f4 100644 --- a/server/src/main/java/com/vaadin/ui/GridDropTargetExtension.java +++ b/server/src/main/java/com/vaadin/ui/GridDropTargetExtension.java @@ -55,7 +55,7 @@ public class GridDropTargetExtension<T> extends DropTargetExtension<Grid<T>> { * Listener to handle drop event. * @return Handle to be used to remove this listener. */ - public Registration addDropListener(GridDropListener<T> listener) { + public Registration addGridDropListener(GridDropListener<T> listener) { return addListener(GridDropEvent.class, listener, GridDropListener.DROP_METHOD); } diff --git a/shared/src/main/java/com/vaadin/shared/ui/dnd/DragSourceRpc.java b/shared/src/main/java/com/vaadin/shared/ui/dnd/DragSourceRpc.java index 4faf7ca139..1f016d5908 100644 --- a/shared/src/main/java/com/vaadin/shared/ui/dnd/DragSourceRpc.java +++ b/shared/src/main/java/com/vaadin/shared/ui/dnd/DragSourceRpc.java @@ -27,7 +27,7 @@ import com.vaadin.shared.communication.ServerRpc; public interface DragSourceRpc extends ServerRpc { /** - * Called when dragsource event happens on client side. + * Called when dragstart event happens on client side. */ public void dragStart(); diff --git a/shared/src/main/java/com/vaadin/shared/ui/grid/GridDragSourceExtensionRpc.java b/shared/src/main/java/com/vaadin/shared/ui/grid/GridDragSourceExtensionRpc.java new file mode 100644 index 0000000000..c0e24fe847 --- /dev/null +++ b/shared/src/main/java/com/vaadin/shared/ui/grid/GridDragSourceExtensionRpc.java @@ -0,0 +1,50 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.shared.ui.grid; + +import java.util.List; + +import com.vaadin.shared.communication.ServerRpc; +import com.vaadin.shared.ui.dnd.DropEffect; + +/** + * RPC for firing server side drag start and drag end events when the + * corresponding client side events happen on the drag source Grid. + * + * @author Vaadin Ltd. + * @since + */ +public interface GridDragSourceExtensionRpc extends ServerRpc { + + /** + * Called when dragstart event happens on client side. + * + * @param draggedItemKeys + * Keys of the items in Grid being dragged. + */ + public void dragStart(List<String> draggedItemKeys); + + /** + * Called when dragend event happens on client side. + * + * @param dropEffect + * Drop effect of the dragend event, extracted from {@code + * DataTransfer.dropEffect} parameter. + * @param draggedItemKeys + * Keys of the items in Grid having been dragged. + */ + public void dragEnd(DropEffect dropEffect, List<String> draggedItemKeys); +} diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridDragAndDrop.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridDragAndDrop.java index 0c532e8762..83adbad218 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/grid/GridDragAndDrop.java +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/GridDragAndDrop.java @@ -59,7 +59,7 @@ public class GridDragAndDrop extends AbstractTestUIWithLog { GridDropTargetExtension<Bean> dropTarget = new GridDropTargetExtension<>( dropTargetComponent); - dropTarget.addDropListener((GridDropListener<Bean>) event -> { + dropTarget.addGridDropListener(event -> { log(event.getDataTransferText() + ", targetId=" + event .getDropTargetRow().getId()); }); |