From c1ecea9aa4729b1c8c3d00ebefa1a2a08a352ebf Mon Sep 17 00:00:00 2001 From: Adam Wagner Date: Fri, 7 Apr 2017 13:03:09 +0300 Subject: Remove Extension suffix from Grid DND classes (#9035) --- .../vaadin/event/dnd/grid/GridDragEndEvent.java | 3 +- .../vaadin/event/dnd/grid/GridDragEndListener.java | 3 +- .../vaadin/event/dnd/grid/GridDragStartEvent.java | 3 +- .../event/dnd/grid/GridDragStartListener.java | 3 +- .../com/vaadin/event/dnd/grid/GridDropEvent.java | 3 +- .../vaadin/event/dnd/grid/GridDropListener.java | 3 +- .../main/java/com/vaadin/ui/GridDragSource.java | 215 +++++++++++++++++++++ .../com/vaadin/ui/GridDragSourceExtension.java | 215 --------------------- .../main/java/com/vaadin/ui/GridDropTarget.java | 118 +++++++++++ .../com/vaadin/ui/GridDropTargetExtension.java | 118 ----------- 10 files changed, 345 insertions(+), 339 deletions(-) create mode 100644 server/src/main/java/com/vaadin/ui/GridDragSource.java delete mode 100644 server/src/main/java/com/vaadin/ui/GridDragSourceExtension.java create mode 100644 server/src/main/java/com/vaadin/ui/GridDropTarget.java delete mode 100644 server/src/main/java/com/vaadin/ui/GridDropTargetExtension.java (limited to 'server/src/main/java') 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 index 8b4ceb1d4f..e58196e6b7 100644 --- a/server/src/main/java/com/vaadin/event/dnd/grid/GridDragEndEvent.java +++ b/server/src/main/java/com/vaadin/event/dnd/grid/GridDragEndEvent.java @@ -20,6 +20,7 @@ import java.util.Set; import com.vaadin.event.dnd.DragEndEvent; import com.vaadin.shared.ui.dnd.DropEffect; import com.vaadin.ui.Grid; +import com.vaadin.ui.GridDragSource; /** * Drop event on an HTML5 drop target {@link Grid} row. @@ -27,7 +28,7 @@ import com.vaadin.ui.Grid; * @param * The Grid bean type. * @author Vaadin Ltd. - * @see com.vaadin.ui.GridDragSourceExtension#addGridDragStartListener(GridDragStartListener) + * @see GridDragSource#addGridDragStartListener(GridDragStartListener) * @since */ public class GridDragEndEvent extends DragEndEvent> { 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 index ad338e68db..c47a8f037f 100644 --- a/server/src/main/java/com/vaadin/event/dnd/grid/GridDragEndListener.java +++ b/server/src/main/java/com/vaadin/event/dnd/grid/GridDragEndListener.java @@ -18,6 +18,7 @@ package com.vaadin.event.dnd.grid; import java.lang.reflect.Method; import com.vaadin.event.ConnectorEventListener; +import com.vaadin.ui.GridDragSource; /** * Drop listener for HTML5 drop on a Grid row. @@ -25,7 +26,7 @@ import com.vaadin.event.ConnectorEventListener; * @param * The Grid bean type. * @author Vaadin Ltd. - * @see com.vaadin.ui.GridDragSourceExtension#addGridDragEndListener(GridDragEndListener) + * @see GridDragSource#addGridDragEndListener(GridDragEndListener) * @since */ @FunctionalInterface 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 index 50130043da..ac87c296cf 100644 --- a/server/src/main/java/com/vaadin/event/dnd/grid/GridDragStartEvent.java +++ b/server/src/main/java/com/vaadin/event/dnd/grid/GridDragStartEvent.java @@ -20,6 +20,7 @@ import java.util.Set; import com.vaadin.event.dnd.DragStartEvent; import com.vaadin.shared.ui.dnd.EffectAllowed; import com.vaadin.ui.Grid; +import com.vaadin.ui.GridDragSource; /** * Drag start event on an HTML5 drag source {@link Grid} row. @@ -27,7 +28,7 @@ import com.vaadin.ui.Grid; * @param * The Grid bean type. * @author Vaadin Ltd. - * @see com.vaadin.ui.GridDragSourceExtension#addGridDragStartListener(GridDragStartListener) + * @see GridDragSource#addGridDragStartListener(GridDragStartListener) * @since */ public class GridDragStartEvent extends DragStartEvent> { 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 index f7dc06b0fe..4ef1f7f205 100644 --- a/server/src/main/java/com/vaadin/event/dnd/grid/GridDragStartListener.java +++ b/server/src/main/java/com/vaadin/event/dnd/grid/GridDragStartListener.java @@ -18,6 +18,7 @@ package com.vaadin.event.dnd.grid; import java.lang.reflect.Method; import com.vaadin.event.ConnectorEventListener; +import com.vaadin.ui.GridDragSource; /** * Drag start listener for HTML5 drag start on a Grid row. @@ -25,7 +26,7 @@ import com.vaadin.event.ConnectorEventListener; * @param * The Grid bean type. * @author Vaadin Ltd. - * @see com.vaadin.ui.GridDragSourceExtension#addGridDragStartListener(GridDragStartListener) + * @see GridDragSource#addGridDragStartListener(GridDragStartListener) * @since */ @FunctionalInterface 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 98d1b3495b..41998354d1 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 @@ -20,6 +20,7 @@ import com.vaadin.event.dnd.DropEvent; import com.vaadin.shared.ui.grid.DropLocation; import com.vaadin.ui.AbstractComponent; import com.vaadin.ui.Grid; +import com.vaadin.ui.GridDropTarget; /** * Drop event on an HTML5 drop target {@link Grid} row. @@ -27,7 +28,7 @@ import com.vaadin.ui.Grid; * @param * The Grid bean type. * @author Vaadin Ltd. - * @see com.vaadin.ui.GridDropTargetExtension#addGridDropListener(GridDropListener) + * @see GridDropTarget#addGridDropListener(GridDropListener) * @since */ public class GridDropEvent extends DropEvent> { 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 1c148eb28d..bd4ed8e2fc 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,6 +18,7 @@ package com.vaadin.event.dnd.grid; import java.lang.reflect.Method; import com.vaadin.event.ConnectorEventListener; +import com.vaadin.ui.GridDropTarget; /** * Drop listener for HTML5 drop on a Grid row. @@ -25,7 +26,7 @@ import com.vaadin.event.ConnectorEventListener; * @param * The Grid bean type. * @author Vaadin Ltd. - * @see com.vaadin.ui.GridDropTargetExtension#addGridDropListener(GridDropListener) + * @see GridDropTarget#addGridDropListener(GridDropListener) * @since */ @FunctionalInterface diff --git a/server/src/main/java/com/vaadin/ui/GridDragSource.java b/server/src/main/java/com/vaadin/ui/GridDragSource.java new file mode 100644 index 0000000000..0cfd9964e2 --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/GridDragSource.java @@ -0,0 +1,215 @@ +/* + * 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.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.GridDragSourceRpc; +import com.vaadin.shared.ui.grid.GridDragSourceState; + +import elemental.json.JsonObject; + +/** + * Makes a Grid's rows draggable for HTML5 drag and drop functionality. + *

+ * When dragging a selected row, all the visible selected rows are dragged. Note + * that ONLY visible rows are taken into account. + * + * @param + * The Grid bean type. + * @author Vaadin Ltd. + * @since + */ +public class GridDragSource extends DragSourceExtension> { + + /** + * Drag data generator that appends drag data to each row. + */ + private DataGenerator dragDataGenerator; + + /** + * Drag data generator function that is executed for each row. + */ + private SerializableFunction generatorFunction; + + /** + * Extends a Grid and makes it's rows draggable. + * + * @param target + * Grid to be extended. + */ + public GridDragSource(Grid target) { + super(target); + + // Create drag data generator + dragDataGenerator = this::generateDragData; + + // Add drag data generator to Grid + target.addDataGenerator(dragDataGenerator); + } + + @Override + protected void registerDragSourceRpc(Grid target) { + registerRpc(new GridDragSourceRpc() { + @Override + public void dragStart(List draggedItemKeys) { + + GridDragStartEvent event = new GridDragStartEvent<>(target, + getState(false).effectAllowed, + getDraggedItems(target, draggedItemKeys)); + + fireEvent(event); + } + + @Override + public void dragEnd(DropEffect dropEffect, + List draggedItemKeys) { + + GridDragEndEvent 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 getDraggedItems(Grid grid, List 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. + * + * @param item + * Row item for data generation. + * @param jsonObject + * Row data in json format. + */ + private void generateDragData(T item, JsonObject jsonObject) { + Optional.ofNullable(generatorFunction).ifPresent(generator -> jsonObject + .put(GridDragSourceState.JSONKEY_DRAG_DATA, + generator.apply(item))); + } + + /** + * Sets a generator function for customizing drag data. The function is + * executed for each item in the Grid during data generation. Return a + * {@link JsonObject} to be appended to the row data. + *

+ * Example: + *

+     *     dragSourceExtension.setDragDataGenerator(item -> {
+     *         JsonObject dragData = Json.createObject();
+     *         dragData.put("someKey", item.getValue());
+     *         return dragData;
+     *     });
+     * 
+ * + * @param generator + * Function to be executed on row data generation. + */ + public void setDragDataGenerator( + SerializableFunction generator) { + 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 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 listener) { + return addListener(DragSourceState.EVENT_DRAGEND, + GridDragEndEvent.class, listener, + GridDragEndListener.DRAG_END_METHOD); + } + + /** + * Returns the generator function for customizing drag data. + * + * @return Drag data generator function. + */ + public SerializableFunction getDragDataGenerator() { + return generatorFunction; + } + + @Override + public void remove() { + super.remove(); + + getParent().removeDataGenerator(dragDataGenerator); + } + + @Override + protected GridDragSourceState getState() { + return (GridDragSourceState) super.getState(); + } + + @Override + protected GridDragSourceState getState(boolean markAsDirty) { + return (GridDragSourceState) super.getState(markAsDirty); + } +} diff --git a/server/src/main/java/com/vaadin/ui/GridDragSourceExtension.java b/server/src/main/java/com/vaadin/ui/GridDragSourceExtension.java deleted file mode 100644 index ca6e1b05fd..0000000000 --- a/server/src/main/java/com/vaadin/ui/GridDragSourceExtension.java +++ /dev/null @@ -1,215 +0,0 @@ -/* - * 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.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; - -/** - * Makes a Grid's rows draggable for HTML5 drag and drop functionality. - *

- * When dragging a selected row, all the visible selected rows are dragged. Note - * that ONLY visible rows are taken into account. - * - * @param - * The Grid bean type. - * @author Vaadin Ltd. - * @since - */ -public class GridDragSourceExtension extends DragSourceExtension> { - - /** - * Drag data generator that appends drag data to each row. - */ - private DataGenerator dragDataGenerator; - - /** - * Drag data generator function that is executed for each row. - */ - private SerializableFunction generatorFunction; - - /** - * Extends a Grid and makes it's rows draggable. - * - * @param target - * Grid to be extended. - */ - public GridDragSourceExtension(Grid target) { - super(target); - - // Create drag data generator - dragDataGenerator = this::generateDragData; - - // Add drag data generator to Grid - target.addDataGenerator(dragDataGenerator); - } - - @Override - protected void registerDragSourceRpc(Grid target) { - registerRpc(new GridDragSourceExtensionRpc() { - @Override - public void dragStart(List draggedItemKeys) { - - GridDragStartEvent event = new GridDragStartEvent<>(target, - getState(false).effectAllowed, - getDraggedItems(target, draggedItemKeys)); - - fireEvent(event); - } - - @Override - public void dragEnd(DropEffect dropEffect, - List draggedItemKeys) { - - GridDragEndEvent 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 getDraggedItems(Grid grid, List 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. - * - * @param item - * Row item for data generation. - * @param jsonObject - * Row data in json format. - */ - private void generateDragData(T item, JsonObject jsonObject) { - Optional.ofNullable(generatorFunction).ifPresent(generator -> jsonObject - .put(GridDragSourceExtensionState.JSONKEY_DRAG_DATA, - generator.apply(item))); - } - - /** - * Sets a generator function for customizing drag data. The function is - * executed for each item in the Grid during data generation. Return a - * {@link JsonObject} to be appended to the row data. - *

- * Example: - *

-     *     dragSourceExtension.setDragDataGenerator(item -> {
-     *         JsonObject dragData = Json.createObject();
-     *         dragData.put("someKey", item.getValue());
-     *         return dragData;
-     *     });
-     * 
- * - * @param generator - * Function to be executed on row data generation. - */ - public void setDragDataGenerator( - SerializableFunction generator) { - 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 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 listener) { - return addListener(DragSourceState.EVENT_DRAGEND, - GridDragEndEvent.class, listener, - GridDragEndListener.DRAG_END_METHOD); - } - - /** - * Returns the generator function for customizing drag data. - * - * @return Drag data generator function. - */ - public SerializableFunction getDragDataGenerator() { - return generatorFunction; - } - - @Override - public void remove() { - super.remove(); - - getParent().removeDataGenerator(dragDataGenerator); - } - - @Override - protected GridDragSourceExtensionState getState() { - return (GridDragSourceExtensionState) super.getState(); - } - - @Override - protected GridDragSourceExtensionState getState(boolean markAsDirty) { - return (GridDragSourceExtensionState) super.getState(markAsDirty); - } -} diff --git a/server/src/main/java/com/vaadin/ui/GridDropTarget.java b/server/src/main/java/com/vaadin/ui/GridDropTarget.java new file mode 100644 index 0000000000..0cd51d8634 --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/GridDropTarget.java @@ -0,0 +1,118 @@ +/* + * 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.ui; + +import com.vaadin.event.dnd.DropTargetExtension; +import com.vaadin.event.dnd.grid.GridDropEvent; +import com.vaadin.event.dnd.grid.GridDropListener; +import com.vaadin.shared.Registration; +import com.vaadin.shared.ui.grid.DropMode; +import com.vaadin.shared.ui.grid.GridDropTargetRpc; +import com.vaadin.shared.ui.grid.GridDropTargetState; + +/** + * Makes the rows of a Grid HTML5 drop targets. This is the server side + * counterpart of GridDropTargetExtensionConnector. + * + * @param + * Type of the Grid bean. + * @author Vaadin Ltd + * @since + */ +public class GridDropTarget extends DropTargetExtension> { + + /** + * Extends a Grid and makes it's rows drop targets for HTML5 drag and drop. + * + * @param target + * Grid to be extended. + * @param dropMode + * Drop mode that describes the allowed drop locations within the + * Grid's row. + * @see GridDropEvent#getDropLocation() + */ + public GridDropTarget(Grid target, DropMode dropMode) { + super(target); + + setDropMode(dropMode); + } + + /** + * Sets the drop mode of this drop target. + * + * @param dropMode + * Drop mode that describes the allowed drop locations within the + * Grid's row. + * @see GridDropEvent#getDropLocation() + */ + public void setDropMode(DropMode dropMode) { + if (dropMode == null) { + throw new IllegalArgumentException("Drop mode cannot be null"); + } + + getState().dropMode = dropMode; + } + + /** + * Gets the drop mode of this drop target. + * + * @return Drop mode that describes the allowed drop locations within the + * Grid's row. + */ + public DropMode getDropMode() { + return getState(false).dropMode; + } + + /** + * Attaches drop listener for the current drop target. {@link + * GridDropListener#drop(GridDropEvent)} is called when drop event happens + * on the client side. + * + * @param listener + * Listener to handle drop event. + * @return Handle to be used to remove this listener. + */ + public Registration addGridDropListener(GridDropListener listener) { + return addListener(GridDropEvent.class, listener, + GridDropListener.DROP_METHOD); + } + + @Override + protected void registerDropTargetRpc(Grid target) { + registerRpc( + (GridDropTargetRpc) (dataTransferText, rowKey, dropLocation) -> { + + T dropTargetRow = target.getDataCommunicator() + .getKeyMapper().get(rowKey); + + GridDropEvent event = new GridDropEvent<>(target, + dataTransferText, getUI().getActiveDragSource(), + dropTargetRow, dropLocation); + + fireEvent(event); + }); + } + + @Override + protected GridDropTargetState getState() { + return (GridDropTargetState) super.getState(); + } + + @Override + protected GridDropTargetState getState(boolean markAsDirty) { + return (GridDropTargetState) super.getState(markAsDirty); + } +} diff --git a/server/src/main/java/com/vaadin/ui/GridDropTargetExtension.java b/server/src/main/java/com/vaadin/ui/GridDropTargetExtension.java deleted file mode 100644 index 17633fa5ce..0000000000 --- a/server/src/main/java/com/vaadin/ui/GridDropTargetExtension.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * 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.ui; - -import com.vaadin.event.dnd.DropTargetExtension; -import com.vaadin.event.dnd.grid.GridDropEvent; -import com.vaadin.event.dnd.grid.GridDropListener; -import com.vaadin.shared.Registration; -import com.vaadin.shared.ui.grid.DropMode; -import com.vaadin.shared.ui.grid.GridDropTargetExtensionRpc; -import com.vaadin.shared.ui.grid.GridDropTargetExtensionState; - -/** - * Makes the rows of a Grid HTML5 drop targets. This is the server side - * counterpart of GridDropTargetExtensionConnector. - * - * @param - * Type of the Grid bean. - * @author Vaadin Ltd - * @since - */ -public class GridDropTargetExtension extends DropTargetExtension> { - - /** - * Extends a Grid and makes it's rows drop targets for HTML5 drag and drop. - * - * @param target - * Grid to be extended. - * @param dropMode - * Drop mode that describes the allowed drop locations within the - * Grid's row. - * @see GridDropEvent#getDropLocation() - */ - public GridDropTargetExtension(Grid target, DropMode dropMode) { - super(target); - - setDropMode(dropMode); - } - - /** - * Sets the drop mode of this drop target. - * - * @param dropMode - * Drop mode that describes the allowed drop locations within the - * Grid's row. - * @see GridDropEvent#getDropLocation() - */ - public void setDropMode(DropMode dropMode) { - if (dropMode == null) { - throw new IllegalArgumentException("Drop mode cannot be null"); - } - - getState().dropMode = dropMode; - } - - /** - * Gets the drop mode of this drop target. - * - * @return Drop mode that describes the allowed drop locations within the - * Grid's row. - */ - public DropMode getDropMode() { - return getState(false).dropMode; - } - - /** - * Attaches drop listener for the current drop target. {@link - * GridDropListener#drop(GridDropEvent)} is called when drop event happens - * on the client side. - * - * @param listener - * Listener to handle drop event. - * @return Handle to be used to remove this listener. - */ - public Registration addGridDropListener(GridDropListener listener) { - return addListener(GridDropEvent.class, listener, - GridDropListener.DROP_METHOD); - } - - @Override - protected void registerDropTargetRpc(Grid target) { - registerRpc( - (GridDropTargetExtensionRpc) (dataTransferText, rowKey, dropLocation) -> { - - T dropTargetRow = target.getDataCommunicator() - .getKeyMapper().get(rowKey); - - GridDropEvent event = new GridDropEvent<>(target, - dataTransferText, getUI().getActiveDragSource(), - dropTargetRow, dropLocation); - - fireEvent(event); - }); - } - - @Override - protected GridDropTargetExtensionState getState() { - return (GridDropTargetExtensionState) super.getState(); - } - - @Override - protected GridDropTargetExtensionState getState(boolean markAsDirty) { - return (GridDropTargetExtensionState) super.getState(markAsDirty); - } -} -- cgit v1.2.3