diff options
4 files changed, 52 insertions, 83 deletions
diff --git a/server/src/main/java/com/vaadin/ui/components/grid/DropIndexCalculator.java b/server/src/main/java/com/vaadin/ui/components/grid/DropIndexCalculator.java index 375b80f1ad..2900f0712d 100644 --- a/server/src/main/java/com/vaadin/ui/components/grid/DropIndexCalculator.java +++ b/server/src/main/java/com/vaadin/ui/components/grid/DropIndexCalculator.java @@ -21,7 +21,6 @@ import java.io.Serializable; * A handler for calculating the index of the dropped items on the drop target * grid. * - * @author Stephan Knitelius * @author Vaadin Ltd * @since * @see GridDragger @@ -30,12 +29,20 @@ import java.io.Serializable; */ @FunctionalInterface public interface DropIndexCalculator<T> extends Serializable { + + /** + * Calculator for always dropping items to the end of the target grid, + * regardless of drop position. + */ + static DropIndexCalculator<?> ALWAYS_DROP_TO_END = (event -> Integer.MAX_VALUE); + /** * Called when Items are dropped onto a target grid. * * @param event * the GridDropEvent. - * @return index the target index. + * @return index the target index, use {@link Integer#MAX_VALUE} for always + * dropping to end */ public int calculateDropIndex(GridDropEvent<T> event); } diff --git a/server/src/main/java/com/vaadin/ui/components/grid/GridDragger.java b/server/src/main/java/com/vaadin/ui/components/grid/GridDragger.java index 6f753db0d5..bfb7643aea 100644 --- a/server/src/main/java/com/vaadin/ui/components/grid/GridDragger.java +++ b/server/src/main/java/com/vaadin/ui/components/grid/GridDragger.java @@ -37,11 +37,10 @@ import com.vaadin.ui.Grid; * If you have another data provider, you should customize data provider * updating on drop with * {@link #setSourceDataProviderUpdater(SourceDataProviderUpdater)} and - * {@link #setTargetGridDropHandler(TargetDataProviderUpdater)}.</em> + * {@link #setTargetDataProviderUpdater(TargetDataProviderUpdater)}.</em> * * @param <T> * The Grid bean type. - * @author Stephan Knitelius * @author Vaadin Ltd * @since */ @@ -58,7 +57,6 @@ public class GridDragger<T> implements Serializable { * Set of items currently being dragged. */ private Set<T> draggedItems; - private boolean addItemsToEnd = false; private boolean removeFromSource = true; /** @@ -80,8 +78,8 @@ public class GridDragger<T> implements Serializable { * <em>NOTE: this only works when the grid has a * {@link ListDataProvider}.</em> Use the custom handlers * {@link #setSourceDataProviderUpdater(SourceDataProviderUpdater)} and - * {@link #setTargetGridDropHandler(TargetDataProviderUpdater)} for other - * data providers. + * {@link #setTargetDataProviderUpdater(TargetDataProviderUpdater)} for + * other data providers. * * @param grid * the grid to enable row DnD reordering on @@ -100,8 +98,8 @@ public class GridDragger<T> implements Serializable { * <em>NOTE: this only works when the grids have a * {@link ListDataProvider}.</em> Use the custom handlers * {@link #setSourceDataProviderUpdater(SourceDataProviderUpdater)} and - * {@link #setTargetGridDropHandler(TargetDataProviderUpdater)} for other - * data providers. + * {@link #setTargetDataProviderUpdater(TargetDataProviderUpdater)} for + * other data providers. * * @param source * the source grid dragged from. @@ -142,7 +140,7 @@ public class GridDragger<T> implements Serializable { * <em>NOTE: this only works when the grids have a * {@link ListDataProvider}.</em> Use the other constructors or custom * handlers {@link #setSourceDataProviderUpdater(SourceDataProviderUpdater)} - * and {@link #setTargetGridDropHandler(TargetDataProviderUpdater)} for + * and {@link #setTargetDataProviderUpdater(TargetDataProviderUpdater)} for * other data providers. * * @param source @@ -182,7 +180,7 @@ public class GridDragger<T> implements Serializable { * @param targetDataProviderUpdater * the target drop handler to set, or {@code null} to remove */ - public void setTargetGridDropHandler( + public void setTargetDataProviderUpdater( TargetDataProviderUpdater<T> targetDataProviderUpdater) { this.targetDataProviderUpdater = targetDataProviderUpdater; } @@ -192,7 +190,7 @@ public class GridDragger<T> implements Serializable { * * @return target grid drop handler */ - public TargetDataProviderUpdater<T> getTargetGridDropHandler() { + public TargetDataProviderUpdater<T> getTargetDataProviderUpdater() { return targetDataProviderUpdater; } @@ -204,9 +202,8 @@ public class GridDragger<T> implements Serializable { * another type of data provider is used, this updater should be set to * handle updating instead. * <p> - * <em>NOTE: this is not triggered when - * {@link #setRemoveItemsFromSource(boolean)} has been set to - * {@code false}</em> + * If you want to skip removing items from the source, you can use + * {@link SourceDataProviderUpdater#NOOP}. * * @param sourceDataProviderUpdater * the drag source data provider updater to set, or {@code null} @@ -236,7 +233,8 @@ public class GridDragger<T> implements Serializable { * By default, items are placed on the index they are dropped into in the * target grid. * <p> - * <em>NOTE: this will override {@link #setAddItemsToEnd(boolean)}.</em> + * If you want to always drop items to the end of the target grid, you can + * use {@link DropIndexCalculator#ALWAYS_DROP_TO_END}. * * @param dropIndexCalculator * the drop index calculator @@ -278,53 +276,6 @@ public class GridDragger<T> implements Serializable { } /** - * Sets whether the items should be always added to the end instead of the - * dropped position in target grid. - * <p> - * The default is {@code false} (added to dropped position). - * <p> - * <em>NOTE: this applies only when no custom index calculator is set with - * {@link #setDropIndexCalculator(DropIndexCalculator)}.</em> - * - * @param addItemsToEnd - * {@code true} for adding items to the end of the grid, - * {@code false} for adding to the dropped position - */ - public void setAddItemsToEnd(boolean addItemsToEnd) { - this.addItemsToEnd = addItemsToEnd; - } - - /** - * Returns whether items are added to end instead of selected position. - * <p> - * <em>NOTE: this applies only when no custom index calculator is set with - * {@link #setDropIndexCalculator(DropIndexCalculator)}.</em> - * - * @return return whether items are added to end or to the dropped position - */ - public boolean isAddItemsToEnd() { - return addItemsToEnd; - } - - /** - * Sets whether the items should be removed from the source grid or not. - * <p> - * Default value is {@code true} and the dropped items are removed from the - * source grid. - * <p> - * <em>NOTE: when this is set to {@code false}, any custom handler with - * {@link #setSourceDataProviderUpdater(SourceDataProviderUpdater)} is not - * triggered on drop.</em> - * - * @param removeFromSource - * {@code true} to remove dropped items, {@code false} to not - * remove. - */ - public void setRemoveItemsFromSource(boolean removeFromSource) { - this.removeFromSource = removeFromSource; - } - - /** * Returns whether dropped items are removed from the source grid or not. * <p> * Default value is {@code true} and the dropped items are removed from the @@ -368,35 +319,40 @@ public class GridDragger<T> implements Serializable { ListDataProvider<T> listDataProvider = (ListDataProvider<T>) target .getDataProvider(); List<T> targetItems = new ArrayList<>(listDataProvider.getItems()); - targetItems.addAll(index, droppedItems); + + // DropIndexCalculator can return this to make sure things are added + // to end + if (index == Integer.MAX_VALUE) { + targetItems.addAll(droppedItems); + } else { + targetItems.addAll(index, droppedItems); + } target.setItems(targetItems); } else { - getTargetGridDropHandler().onDrop(target.getDataProvider(), index, - droppedItems); + getTargetDataProviderUpdater().onDrop(target.getDataProvider(), + index, droppedItems); } } private int calculateDropIndex(GridDropEvent<T> event) { if (getDropIndexCalculator() == null) { - if (!addItemsToEnd) { - ListDataProvider<T> targetDataProvider = (ListDataProvider<T>) getGridDropTarget() - .getGrid().getDataProvider(); - List<T> items = new ArrayList<>(targetDataProvider.getItems()); - int index = items.size(); - if (event.getDropTargetRow().isPresent()) { - index = items.indexOf(event.getDropTargetRow().get()) - + (event.getDropLocation() == DropLocation.BELOW ? 1 - : 0); - } - return index; + ListDataProvider<T> targetDataProvider = (ListDataProvider<T>) getGridDropTarget() + .getGrid().getDataProvider(); + List<T> items = new ArrayList<>(targetDataProvider.getItems()); + int index = items.size(); + if (event.getDropTargetRow().isPresent()) { + index = items.indexOf(event.getDropTargetRow().get()) + + (event.getDropLocation() == DropLocation.BELOW ? 1 + : 0); } - return Integer.MAX_VALUE; + return index; } else { return getDropIndexCalculator().calculateDropIndex(event); } } - private static void throwIllegalStateExceptionForUnsupportedDataProvider(boolean sourceGrid) { + private static void throwIllegalStateExceptionForUnsupportedDataProvider( + boolean sourceGrid) { throw new IllegalStateException( new StringBuilder().append(sourceGrid ? "Source " : "Target ") .append("grid does not have a ListDataProvider, cannot automatically ") diff --git a/server/src/main/java/com/vaadin/ui/components/grid/SourceDataProviderUpdater.java b/server/src/main/java/com/vaadin/ui/components/grid/SourceDataProviderUpdater.java index a9be2b2282..e91ecb5b1d 100644 --- a/server/src/main/java/com/vaadin/ui/components/grid/SourceDataProviderUpdater.java +++ b/server/src/main/java/com/vaadin/ui/components/grid/SourceDataProviderUpdater.java @@ -26,7 +26,6 @@ import com.vaadin.data.provider.DataProvider; * Used to handle updates to the source grid's {@link DataProvider} after a * drop. * - * @author Stephan Knitelius * @author Vaadin Ltd * @since * @@ -35,6 +34,13 @@ import com.vaadin.data.provider.DataProvider; */ @FunctionalInterface public interface SourceDataProviderUpdater<T> extends Serializable { + + /** + * A NOOP updater that does not do anything for the source data provider. + */ + static SourceDataProviderUpdater<?> NOOP = (e, d) -> { + }; + /** * Called when Items have been dragged. * diff --git a/server/src/main/java/com/vaadin/ui/components/grid/TargetDataProviderUpdater.java b/server/src/main/java/com/vaadin/ui/components/grid/TargetDataProviderUpdater.java index 37373b8fdd..ae0ecd551f 100644 --- a/server/src/main/java/com/vaadin/ui/components/grid/TargetDataProviderUpdater.java +++ b/server/src/main/java/com/vaadin/ui/components/grid/TargetDataProviderUpdater.java @@ -26,7 +26,6 @@ import com.vaadin.data.provider.DataProvider; * Used to handle updates to the target grid's {@link DataProvider} after a * drop. * - * @author Stephan Knitelius * @author Vaadin Ltd * @since * @@ -42,8 +41,9 @@ public interface TargetDataProviderUpdater<T> extends Serializable { * @param dataProvider * the target grid data provider * @param index - * the Target index Integer.MAX when items should be added to - * end. + * the target index, {@link Integer#MAX_VALUE} is used for + * dropping things always to the end of the grid without having + * to fetch the size of the data provider * @param items * items to be added. */ |