diff options
author | Adam Wagner <wbadam@users.noreply.github.com> | 2017-03-16 20:35:46 +0200 |
---|---|---|
committer | Henri Sara <henri.sara@gmail.com> | 2017-04-12 14:58:11 +0300 |
commit | 1ac4f9724106fcf4abaab892210122231710c6be (patch) | |
tree | cd63d9a7d1e689b041ca05d25385d4b39929782d /server | |
parent | 2df1b373aae547275b566fef957322af0b61b427 (diff) | |
download | vaadin-framework-1ac4f9724106fcf4abaab892210122231710c6be.tar.gz vaadin-framework-1ac4f9724106fcf4abaab892210122231710c6be.zip |
Make it possible to drop things on top of Grid rows (#8747)
Fixes #8400
Diffstat (limited to 'server')
4 files changed, 198 insertions, 2 deletions
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<T extends AbstractComponent> 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<T> 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 <T> + * The Grid bean type. + * @author Vaadin Ltd. + * @see com.vaadin.ui.GridDropTargetExtension#addDropListener(GridDropListener) + * @since + */ +public class GridDropEvent<T> extends DropEvent<Grid<T>> { + + 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<T> target, String dataTransferText, + DragSourceExtension<? extends AbstractComponent> 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 <T> + * The Grid bean type. + * @author Vaadin Ltd. + * @see com.vaadin.ui.GridDropTargetExtension#addDropListener(GridDropListener) + * @since + */ +@FunctionalInterface +public interface GridDropListener<T> 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<T> 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 <T> + * Type of the Grid bean. + * @author Vaadin Ltd + * @since + */ +public class GridDropTargetExtension<T> extends DropTargetExtension<Grid<T>> { + public GridDropTargetExtension(Grid<T> target) { + super(target); + } + + @Override + protected void registerDropTargetRpc(Grid<T> target) { + registerRpc((GridDropTargetExtensionRpc) (dataTransferText, rowKey) -> { + GridDropEvent<T> 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<T> 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); + } +} |