diff --git a/flow-data/src/main/java/com/vaadin/flow/data/provider/AbstractListDataView.java b/flow-data/src/main/java/com/vaadin/flow/data/provider/AbstractListDataView.java index b115e572bf5..c7b362a6d5d 100644 --- a/flow-data/src/main/java/com/vaadin/flow/data/provider/AbstractListDataView.java +++ b/flow-data/src/main/java/com/vaadin/flow/data/provider/AbstractListDataView.java @@ -130,6 +130,7 @@ public AbstractListDataView setFilter(SerializablePredicate filter) { fireFilteringOrSortingChangeEvent(filter, (SerializableComparator) DataViewUtils .getComponentSortComparator(component).orElse(null)); + refreshAll(); return this; } @@ -142,6 +143,7 @@ public AbstractListDataView setSortComparator( (SerializablePredicate) DataViewUtils .getComponentFilter(component).orElse(null), sortComparator); + refreshAll(); return this; } diff --git a/flow-data/src/main/java/com/vaadin/flow/data/provider/ListDataView.java b/flow-data/src/main/java/com/vaadin/flow/data/provider/ListDataView.java index 7f9aba93894..f50e5fcc548 100644 --- a/flow-data/src/main/java/com/vaadin/flow/data/provider/ListDataView.java +++ b/flow-data/src/main/java/com/vaadin/flow/data/provider/ListDataView.java @@ -101,6 +101,9 @@ public interface ListDataView> *

* The backing {@link List} must be mutable to use this method. Immutable * data structure will throw an exception. + *

+ * Refreshes all items of the component after adding the item, i.e. runs + * {@link DataView#refreshAll()}. * * @param item * item to add @@ -121,6 +124,9 @@ public interface ListDataView> *

* If the item is already present in the data provider, then it is moved. *

+ * Refreshes all items of the component after adding the item, i.e. runs + * {@link DataView#refreshAll()}. + *

* Note! Item is added to the unfiltered and unsorted List. * * @param item @@ -145,6 +151,9 @@ public interface ListDataView> *

* If the item is already present in the data provider, then it is moved. *

+ * Refreshes all items of the component after adding the item, i.e. runs + * {@link DataView#refreshAll()}. + *

* Note! Item is added to the unfiltered and unsorted List. * * @param item @@ -168,6 +177,9 @@ public interface ListDataView> * data structure will throw an exception. *

* Any items that already present in the data provider are moved to the end. + *

+ * Refreshes all items of the component after adding the items, i.e. runs + * {@link DataView#refreshAll()}. * * @param items * collection of item to add @@ -188,6 +200,9 @@ public interface ListDataView> * data structure will throw an exception. Any items that already present in * the data provider are moved. *

+ * Refreshes all items of the component after adding the item, i.e. runs + * {@link DataView#refreshAll()}. + *

* Note! Item is added to the unfiltered and unsorted List. * * @param items @@ -213,6 +228,9 @@ public interface ListDataView> *

* Any items that already present in the data provider are moved. *

+ * Refreshes all items of the component after adding the item, i.e. runs + * {@link DataView#refreshAll()}. + *

* Note! Item is added to the unfiltered and unsorted List. * * @param items @@ -234,6 +252,9 @@ public interface ListDataView> *

* The backing {@link List} must be mutable to use this method. Immutable * data structure will throw an exception. + *

+ * Refreshes all items of the component after removing the item, i.e. runs + * {@link DataView#refreshAll()}. * * @param item * item to remove @@ -271,6 +292,10 @@ public interface ListDataView> * filter through data view of another component. A filter set by this * method won't be retained when a new {@link DataProvider} is set to the * component. + *

+ * Refreshes all items of the component after setting the filter, i.e. runs + * {@link DataView#refreshAll()}. + * * * @param filter * filter to be set, or null to clear any previously @@ -291,6 +316,9 @@ public interface ListDataView> * filter through data view of another component. A filter set by this * method won't be retained when a new {@link DataProvider} is set to the * component. + *

+ * Refreshes all items of the component after adding the filter, i.e. runs + * {@link DataView#refreshAll()}. * * @param filter * the filter to add, not null @@ -303,6 +331,9 @@ public interface ListDataView> /** * Removes all in-memory filters set or added. + *

+ * Refreshes all items of the component after removing the filter, i.e. runs + * {@link DataView#refreshAll()}. * * @return ListDataView instance * @@ -320,6 +351,9 @@ public interface ListDataView> * setting a sort comparator through data view of another component. A * sorting set by this method won't be retained when a new * {@link DataProvider} is set to the component. + *

+ * Refreshes all items of the component after setting the sorting, i.e. runs + * {@link DataView#refreshAll()}. * * @param sortComparator * a comparator to use, or null to clear any @@ -342,6 +376,9 @@ public interface ListDataView> * setting a sort comparator through data view of another component. A * sorting set by this method won't be retained when a new * {@link DataProvider} is set to the component. + *

+ * Refreshes all items of the component after adding the sorting, i.e. runs + * {@link DataView#refreshAll()}. * * @param sortComparator * a comparator to add, not null @@ -357,6 +394,9 @@ public interface ListDataView> * Any other component using the same {@link DataProvider} object would not * be affected by removing default sorting through data view of another * component. + *

+ * Refreshes all items of the component after removing the sorting, i.e. + * runs {@link DataView#refreshAll()}. * * @return ListDataView instance * @@ -375,6 +415,9 @@ public interface ListDataView> * setting a sort order through data view of another component. A sort order * set by this method won't be retained when a new {@link DataProvider} is * set to the component. + *

+ * Refreshes all items of the component after setting the sorting, i.e. runs + * {@link DataView#refreshAll()}. * * @param valueProvider * the value provider that defines the property do sort by, not @@ -403,6 +446,9 @@ > V setSortOrder( * setting a sort sort through data view of another component. A sorting set * by this method won't be retained when a new {@link DataProvider} is set * to the component. + *

+ * Refreshes all items of the component after adding the sorting, i.e. runs + * {@link DataView#refreshAll()}. * * @param valueProvider * the value provider that defines the property do sort by, not diff --git a/flow-data/src/test/java/com/vaadin/flow/data/provider/AbstractListDataViewTest.java b/flow-data/src/test/java/com/vaadin/flow/data/provider/AbstractListDataViewTest.java index f1c9f6638e5..a7be705f6a5 100644 --- a/flow-data/src/test/java/com/vaadin/flow/data/provider/AbstractListDataViewTest.java +++ b/flow-data/src/test/java/com/vaadin/flow/data/provider/AbstractListDataViewTest.java @@ -23,6 +23,7 @@ import java.util.Iterator; import java.util.Optional; import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -135,6 +136,16 @@ public void setFilter_filterIsSet_filteredItemsObtained() { "first", dataView.getItems().findFirst().get()); } + @Test + public void setFilter_filterIsSetAndDropped_allItemsRefreshed() { + dataView.setFilter(item -> item.equals("first")); + Assert.assertEquals(1, ((ListDataViewImpl) dataView).getRefreshCount()); + dataView.removeFilters(); + Assert.assertEquals(2, ((ListDataViewImpl) dataView).getRefreshCount()); + dataView.addFilter(ignored -> true); + Assert.assertEquals(3, ((ListDataViewImpl) dataView).getRefreshCount()); + } + @Test public void setFilter_resetFilterWithDataView_dataProviderFilterNotAffected() { dataProvider.setFilter(item -> item.equals("first")); @@ -155,6 +166,22 @@ public void setSortComparator_sortIsSet_sortedItemsObtained() { dataView.getItems().collect(Collectors.joining(","))); } + @Test + public void setSortComparator_sortIsSet_sortedItemsRefreshed() { + dataView.setSortComparator(String::compareTo); + Assert.assertEquals(1, ((ListDataViewImpl) dataView).getRefreshCount()); + dataView.removeSorting(); + Assert.assertEquals(2, ((ListDataViewImpl) dataView).getRefreshCount()); + dataView.addSortComparator(String::compareTo); + Assert.assertEquals(3, ((ListDataViewImpl) dataView).getRefreshCount()); + dataView.setSortOrder(ValueProvider.identity(), + SortDirection.ASCENDING); + Assert.assertEquals(4, ((ListDataViewImpl) dataView).getRefreshCount()); + dataView.addSortOrder(ValueProvider.identity(), + SortDirection.DESCENDING); + Assert.assertEquals(5, ((ListDataViewImpl) dataView).getRefreshCount()); + } + @Test public void setSortComparator_resetSortingWithDataView_dataProviderSortingNotAffected() { dataProvider.setSortComparator(String::compareTo); @@ -1274,6 +1301,8 @@ public void filterOrSortingChangedCallback_emptyCallbackProvided_throws() { private static class ListDataViewImpl extends AbstractListDataView { + private final AtomicInteger refreshCount = new AtomicInteger(0); + public ListDataViewImpl( SerializableSupplier> dataProviderSupplier, Component component) { @@ -1289,6 +1318,16 @@ public ListDataViewImpl( super(dataProviderSupplier, component, filterOrSortingChangedCallback); } + + @Override + public void refreshAll() { + super.refreshAll(); + refreshCount.incrementAndGet(); + } + + public int getRefreshCount() { + return refreshCount.get(); + } } private static class ItemListDataView extends AbstractListDataView {