From 1ac4f9724106fcf4abaab892210122231710c6be Mon Sep 17 00:00:00 2001 From: Adam Wagner Date: Thu, 16 Mar 2017 20:35:46 +0200 Subject: Make it possible to drop things on top of Grid rows (#8747) Fixes #8400 --- .../com/vaadin/event/dnd/DropTargetExtension.java | 15 ++++- .../com/vaadin/event/dnd/grid/GridDropEvent.java | 67 ++++++++++++++++++++ .../vaadin/event/dnd/grid/GridDropListener.java | 46 ++++++++++++++ .../com/vaadin/ui/GridDropTargetExtension.java | 72 ++++++++++++++++++++++ 4 files changed, 198 insertions(+), 2 deletions(-) create mode 100644 server/src/main/java/com/vaadin/event/dnd/grid/GridDropEvent.java create mode 100644 server/src/main/java/com/vaadin/event/dnd/grid/GridDropListener.java create mode 100644 server/src/main/java/com/vaadin/ui/GridDropTargetExtension.java (limited to 'server') 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 c4a5f981ff..fcca5efadb 100644 --- a/server/src/main/java/com/vaadin/event/dnd/DropTargetExtension.java +++ b/server/src/main/java/com/vaadin/event/dnd/DropTargetExtension.java @@ -43,14 +43,25 @@ public class DropTargetExtension extends * Component to be extended. */ public DropTargetExtension(T target) { + + registerDropTargetRpc(target); + + super.extend(target); + } + + /** + * Register server RPC. + * + * @param target + * Extended component. + */ + protected void registerDropTargetRpc(T target) { registerRpc((DropTargetRpc) dataTransferText -> { DropEvent event = new DropEvent<>(target, dataTransferText, getUI().getActiveDragSource()); fireEvent(event); }); - - super.extend(target); } /** 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 new file mode 100644 index 0000000000..96017e3a1d --- /dev/null +++ b/server/src/main/java/com/vaadin/event/dnd/grid/GridDropEvent.java @@ -0,0 +1,67 @@ +/* + * 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 com.vaadin.event.dnd.DragSourceExtension; +import com.vaadin.event.dnd.DropEvent; +import com.vaadin.ui.AbstractComponent; +import com.vaadin.ui.Grid; + +/** + * Server side drop event on an HTML5 drop target {@link Grid} row. + * + * @param + * The Grid bean type. + * @author Vaadin Ltd. + * @see com.vaadin.ui.GridDropTargetExtension#addDropListener(GridDropListener) + * @since + */ +public class GridDropEvent extends DropEvent> { + + private final T dropTargetRow; + + /** + * Creates a server side Grid row drop event. + * + * @param target + * Grid that received the drop. + * @param dataTransferText + * Data of type {@code "text"} from the {@code DataTransfer} + * object. + * @param dragSourceExtension + * Drag source extension of the component that initiated the drop + * event. + * @param dropTargetRowKey + * Key of the target row that received the drop. + */ + public GridDropEvent(Grid target, String dataTransferText, + DragSourceExtension dragSourceExtension, + String dropTargetRowKey) { + super(target, dataTransferText, dragSourceExtension); + + dropTargetRow = target.getDataCommunicator().getKeyMapper() + .get(dropTargetRowKey); + } + + /** + * Get the row item source of this event. + * + * @return The row item this event was originated from. + */ + public T getDropTargetRow() { + return dropTargetRow; + } +} 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 new file mode 100644 index 0000000000..e1e365fbeb --- /dev/null +++ b/server/src/main/java/com/vaadin/event/dnd/grid/GridDropListener.java @@ -0,0 +1,46 @@ +/* + * 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; +import com.vaadin.event.dnd.DropListener; +import com.vaadin.event.dnd.DropTargetExtension; + +/** + * Drop listener for HTML5 drop on a Grid row. + * + * @param + * The Grid bean type. + * @author Vaadin Ltd. + * @see com.vaadin.ui.GridDropTargetExtension#addDropListener(GridDropListener) + * @since + */ +@FunctionalInterface +public interface GridDropListener extends ConnectorEventListener { + + static final Method DROP_METHOD = GridDropListener.class + .getDeclaredMethods()[0]; + + /** + * Called when drop event is fired on a Grid row. + * + * @param event + * Server side drop event. + */ + void drop(GridDropEvent event); +} diff --git a/server/src/main/java/com/vaadin/ui/GridDropTargetExtension.java b/server/src/main/java/com/vaadin/ui/GridDropTargetExtension.java new file mode 100644 index 0000000000..bf75e26783 --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/GridDropTargetExtension.java @@ -0,0 +1,72 @@ +/* + * 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.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> { + public GridDropTargetExtension(Grid target) { + super(target); + } + + @Override + protected void registerDropTargetRpc(Grid target) { + registerRpc((GridDropTargetExtensionRpc) (dataTransferText, rowKey) -> { + GridDropEvent event = new GridDropEvent<>(target, + dataTransferText, getUI().getActiveDragSource(), rowKey); + + fireEvent(event); + }); + } + + /** + * 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 addDropListener(GridDropListener listener) { + return addListener(GridDropEvent.class, listener, + GridDropListener.DROP_METHOD); + } + + @Override + protected GridDropTargetExtensionState getState() { + return (GridDropTargetExtensionState) super.getState(); + } + + @Override + protected GridDropTargetExtensionState getState(boolean markAsDirty) { + return (GridDropTargetExtensionState) super.getState(markAsDirty); + } +} -- cgit v1.2.3