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 /server | |
parent | 1ac4f9724106fcf4abaab892210122231710c6be (diff) | |
download | vaadin-framework-659313e8c35e68d14fe2599b9bbb4dbba35fb4a3.tar.gz vaadin-framework-659313e8c35e68d14fe2599b9bbb4dbba35fb4a3.zip |
Provide dragged Grid items in server side (#8950)
Fixes #8931
Diffstat (limited to 'server')
11 files changed, 339 insertions, 61 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 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); } |