diff options
author | Adam Wagner <wbadam@users.noreply.github.com> | 2017-06-28 10:29:57 +0200 |
---|---|---|
committer | Henri Sara <henri.sara@gmail.com> | 2017-06-28 11:29:57 +0300 |
commit | 6ed5f2a0df068dba713d8e24dc9f78e2a0829a73 (patch) | |
tree | 0cc6c37ba43e5d1371b9283b18042289f2e14db0 /server | |
parent | e4e2328a3a78d652cd09ef8293f233d31d899415 (diff) | |
download | vaadin-framework-6ed5f2a0df068dba713d8e24dc9f78e2a0829a73.tar.gz vaadin-framework-6ed5f2a0df068dba713d8e24dc9f78e2a0829a73.zip |
Create drag source and drop target extensions for tree grid (#9463)
Resolves #9372
Diffstat (limited to 'server')
4 files changed, 301 insertions, 0 deletions
diff --git a/server/src/main/java/com/vaadin/ui/components/grid/TreeGridDragSource.java b/server/src/main/java/com/vaadin/ui/components/grid/TreeGridDragSource.java new file mode 100644 index 0000000000..68d0c68b13 --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/components/grid/TreeGridDragSource.java @@ -0,0 +1,54 @@ +/* + * 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.components.grid; + +import com.vaadin.shared.ui.treegrid.TreeGridDragSourceState; +import com.vaadin.ui.TreeGrid; + +/** + * Makes a TreeGrid's rows draggable for HTML5 drag and drop functionality. + * <p> + * When dragging a selected row, all the visible selected rows are dragged. Note + * that ONLY visible rows are taken into account and the subtree belonging to a + * selected row is not dragged either. + * + * @param <T> + * The TreeGrid bean type. + * @author Vaadin Ltd. + * @since 8.1 + */ +public class TreeGridDragSource<T> extends GridDragSource<T> { + + /** + * Extends a TreeGrid and makes it's rows draggable. + * + * @param target + * TreeGrid to be extended. + */ + public TreeGridDragSource(TreeGrid<T> target) { + super(target); + } + + @Override + protected TreeGridDragSourceState getState() { + return (TreeGridDragSourceState) super.getState(); + } + + @Override + protected TreeGridDragSourceState getState(boolean markAsDirty) { + return (TreeGridDragSourceState) super.getState(markAsDirty); + } +} diff --git a/server/src/main/java/com/vaadin/ui/components/grid/TreeGridDropEvent.java b/server/src/main/java/com/vaadin/ui/components/grid/TreeGridDropEvent.java new file mode 100644 index 0000000000..015f6169d5 --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/components/grid/TreeGridDropEvent.java @@ -0,0 +1,99 @@ +/* + * 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.components.grid; + +import java.util.Map; +import java.util.Optional; + +import com.vaadin.shared.MouseEventDetails; +import com.vaadin.shared.ui.dnd.DropEffect; +import com.vaadin.shared.ui.grid.DropLocation; +import com.vaadin.ui.AbstractComponent; +import com.vaadin.ui.TreeGrid; +import com.vaadin.ui.dnd.DragSourceExtension; + +/** + * Drop event on an HTML5 drop target {@link TreeGrid} row. + * + * @param <T> + * The TreeGrid bean type. + * @author Vaadin Ltd. + * @see TreeGridDropTarget#addTreeGridDropListener(TreeGridDropListener) + * @since 8.1 + */ +public class TreeGridDropEvent<T> extends GridDropEvent<T> { + + private Integer depth; + private Boolean collapsed; + + /** + * Creates a TreeGrid row drop event. + * + * @param target + * TreeGrid that received the drop. + * @param data + * Map containing all types and corresponding data from the {@code + * DataTransfer} object. + * @param dropEffect + * the desired drop effect + * @param dragSourceExtension + * Drag source extension of the component that initiated the drop + * event. + * @param dropTargetRow + * Target row that received the drop, or {@code null} if dropped on + * empty grid + * @param dropLocation + * Location of the drop within the target row. + * @param mouseEventDetails + * Mouse event details object containing information about the drop + * event + * @param depth + * depth of the row in the hierarchy + * @param collapsed + * whether the target row is collapsed + */ + public TreeGridDropEvent(TreeGrid<T> target, Map<String, String> data, + DropEffect dropEffect, + DragSourceExtension<? extends AbstractComponent> dragSourceExtension, + T dropTargetRow, DropLocation dropLocation, + MouseEventDetails mouseEventDetails, Integer depth, + Boolean collapsed) { + super(target, data, dropEffect, dragSourceExtension, dropTargetRow, + dropLocation, mouseEventDetails); + + this.depth = depth; + this.collapsed = collapsed; + } + + /** + * Gets the depth of the drop target row in the hierarchy. + * + * @return the depth of the drop target row in the hierarchy + */ + public Optional<Integer> getDropTargetRowDepth() { + return Optional.ofNullable(depth); + } + + /** + * Tells whether the drop target row is collapsed. + * + * @return {@code true} if the drop target row is collapsed, {@code false} + * otherwise + */ + public Optional<Boolean> isDropTargetRowCollapsed() { + return Optional.ofNullable(collapsed); + } +} diff --git a/server/src/main/java/com/vaadin/ui/components/grid/TreeGridDropListener.java b/server/src/main/java/com/vaadin/ui/components/grid/TreeGridDropListener.java new file mode 100644 index 0000000000..d413a7b42b --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/components/grid/TreeGridDropListener.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.ui.components.grid; + +import java.lang.reflect.Method; + +import com.vaadin.event.ConnectorEventListener; + +/** + * Drop listener for HTML5 drop on a TreeGrid row. + * + * @param <T> + * The Grid bean type. + * @author Vaadin Ltd. + * @see TreeGridDropTarget#addTreeGridDropListener(TreeGridDropListener) + * @since 8.1 + */ +@FunctionalInterface +public interface TreeGridDropListener<T> extends ConnectorEventListener { + + static final Method DROP_METHOD = TreeGridDropListener.class + .getDeclaredMethods()[0]; + + /** + * Called when drop event is fired on a Grid row. + * + * @param event + * Server side drop event. + */ + void drop(TreeGridDropEvent<T> event); +} diff --git a/server/src/main/java/com/vaadin/ui/components/grid/TreeGridDropTarget.java b/server/src/main/java/com/vaadin/ui/components/grid/TreeGridDropTarget.java new file mode 100644 index 0000000000..1d440b5fec --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/components/grid/TreeGridDropTarget.java @@ -0,0 +1,104 @@ +/* + * 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.components.grid; + +import java.util.LinkedHashMap; +import java.util.Map; + +import com.vaadin.shared.Registration; +import com.vaadin.shared.ui.dnd.DropEffect; +import com.vaadin.shared.ui.grid.DropMode; +import com.vaadin.shared.ui.treegrid.TreeGridDropTargetRpc; +import com.vaadin.shared.ui.treegrid.TreeGridDropTargetState; +import com.vaadin.ui.TreeGrid; + +/** + * Makes the rows of a TreeGrid HTML5 drop targets. This is the server side + * counterpart of GridDropTargetExtensionConnector. + * + * @param <T> + * Type of the TreeGrid bean. + * @author Vaadin Ltd + * @since 8.1 + */ +public class TreeGridDropTarget<T> extends GridDropTarget<T> { + + /** + * Extends a TreeGrid and makes it's rows drop targets for HTML5 drag and + * drop. + * + * @param target + * TreeGrid to be extended. + * @param dropMode + * Drop mode that describes the allowed drop locations within the + * TreeGrid's row. + */ + public TreeGridDropTarget(TreeGrid<T> target, DropMode dropMode) { + super(target, dropMode); + } + + /** + * Attaches drop listener for the current drop target. {@link + * TreeGridDropListener#drop(TreeGridDropEvent)} 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 addTreeGridDropListener( + TreeGridDropListener<T> listener) { + return addListener(TreeGridDropEvent.class, listener, + TreeGridDropListener.DROP_METHOD); + } + + @Override + protected void registerDropTargetRpc() { + registerRpc((TreeGridDropTargetRpc) (types, data, dropEffect, rowKey, + depth, collapsed, dropLocation, mouseEventDetails) -> { + + // Create a linked map that preserves the order of types + Map<String, String> dataPreserveOrder = new LinkedHashMap<>(); + types.forEach(type -> dataPreserveOrder.put(type, data.get(type))); + + T dropTargetRow = getParent().getDataCommunicator().getKeyMapper() + .get(rowKey); + + TreeGridDropEvent<T> event = new TreeGridDropEvent<>(getParent(), + dataPreserveOrder, + DropEffect.valueOf(dropEffect.toUpperCase()), + getUI().getActiveDragSource(), dropTargetRow, dropLocation, + mouseEventDetails, depth, collapsed); + + fireEvent(event); + }); + } + + @Override + public TreeGrid<T> getParent() { + return (TreeGrid<T>) super.getParent(); + } + + @Override + protected TreeGridDropTargetState getState() { + return (TreeGridDropTargetState) super.getState(); + } + + @Override + protected TreeGridDropTargetState getState(boolean markAsDirty) { + return (TreeGridDropTargetState) super.getState(markAsDirty); + } +} |