diff options
author | Adam Wagner <wbadam@users.noreply.github.com> | 2017-02-28 09:41:04 +0200 |
---|---|---|
committer | Henri Sara <henri.sara@gmail.com> | 2017-04-12 14:58:11 +0300 |
commit | c4f8524ea881e6a946ac9e9e1911fd2873f484e9 (patch) | |
tree | 05a175054cd7e3728b8ce6d80a2d6954a6494cc8 /server | |
parent | 8b94b1aeca0e8d7cdd2d13d2f9ba32264b310ac2 (diff) | |
download | vaadin-framework-c4f8524ea881e6a946ac9e9e1911fd2873f484e9.tar.gz vaadin-framework-c4f8524ea881e6a946ac9e9e1911fd2873f484e9.zip |
Make Grid rows draggable (#8690)
It is possible to customize the drag data for each row. (#8706)
Fixes #8396
Diffstat (limited to 'server')
-rw-r--r-- | server/src/main/java/com/vaadin/ui/GridDragSourceExtension.java | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/server/src/main/java/com/vaadin/ui/GridDragSourceExtension.java b/server/src/main/java/com/vaadin/ui/GridDragSourceExtension.java new file mode 100644 index 0000000000..9f7e9ec67d --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/GridDragSourceExtension.java @@ -0,0 +1,125 @@ +/* + * 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.Optional; + +import com.vaadin.data.provider.DataGenerator; +import com.vaadin.event.dnd.DragSourceExtension; +import com.vaadin.server.SerializableFunction; +import com.vaadin.shared.ui.grid.GridDragSourceExtensionState; + +import elemental.json.JsonObject; + +/** + * Makes a Grid's rows draggable for HTML5 drag and drop functionality. + * + * @param <T> + * The Grid bean type. + * @author Vaadin Ltd. + * @since + */ +public class GridDragSourceExtension<T> extends DragSourceExtension<Grid<T>> { + + /** + * Drag data generator that appends drag data to each row. + */ + private DataGenerator<T> dragDataGenerator; + + /** + * Drag data generator function that is executed for each row. + */ + private SerializableFunction<T, JsonObject> generatorFunction; + + /** + * Extends a Grid and makes it's rows draggable. + * + * @param target + * Grid to be extended. + */ + public GridDragSourceExtension(Grid<T> target) { + super(target); + + // Create drag data generator + dragDataGenerator = this::generateDragData; + + // Add drag data generator to Grid + target.addDataGenerator(dragDataGenerator); + } + + /** + * 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. + * <p> + * Example: + * <pre> + * dragSourceExtension.setDragDataGenerator(item -> { + * JsonObject dragData = Json.createObject(); + * dragData.put("someKey", item.getValue()); + * return dragData; + * }); + * </pre> + * + * @param generator + * Function to be executed on row data generation. + */ + public void setDragDataGenerator( + SerializableFunction<T, JsonObject> generator) { + generatorFunction = generator; + } + + /** + * Returns the generator function for customizing drag data. + * + * @return Drag data generator function. + */ + public SerializableFunction<T, JsonObject> 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); + } +} |