Browse Source

Simplify GridDragger API

grid_dragger
Pekka Hyvönen 6 years ago
parent
commit
c9a812fc8b

+ 9
- 2
server/src/main/java/com/vaadin/ui/components/grid/DropIndexCalculator.java View File

@@ -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);
}

+ 33
- 77
server/src/main/java/com/vaadin/ui/components/grid/GridDragger.java View File

@@ -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
@@ -277,53 +275,6 @@ public class GridDragger<T> implements Serializable {
return gridDragSource;
}

/**
* 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>
@@ -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 ")

+ 7
- 1
server/src/main/java/com/vaadin/ui/components/grid/SourceDataProviderUpdater.java View File

@@ -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.
*

+ 3
- 3
server/src/main/java/com/vaadin/ui/components/grid/TargetDataProviderUpdater.java View File

@@ -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.
*/

Loading…
Cancel
Save