Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Refresh items after changing filtering or sorting with DataViews #20263

Merged
merged 6 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public AbstractListDataView<T> setFilter(SerializablePredicate<T> filter) {
fireFilteringOrSortingChangeEvent(filter,
(SerializableComparator<T>) DataViewUtils
.getComponentSortComparator(component).orElse(null));
refreshAll();
return this;
}

Expand All @@ -142,6 +143,7 @@ public AbstractListDataView<T> setSortComparator(
(SerializablePredicate<T>) DataViewUtils
.getComponentFilter(component).orElse(null),
sortComparator);
refreshAll();
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ public interface ListDataView<T, V extends ListDataView<T, ?>>
* <p>
* The backing {@link List} must be mutable to use this method. Immutable
* data structure will throw an exception.
* <p>
* Refreshes all items of the component after adding the item, i.e. runs
* {@link DataView#refreshAll()}.
*
* @param item
* item to add
Expand All @@ -121,6 +124,9 @@ public interface ListDataView<T, V extends ListDataView<T, ?>>
* <p>
* If the item is already present in the data provider, then it is moved.
* <p>
* Refreshes all items of the component after adding the item, i.e. runs
* {@link DataView#refreshAll()}.
* <p>
* Note! Item is added to the unfiltered and unsorted List.
*
* @param item
Expand All @@ -145,6 +151,9 @@ public interface ListDataView<T, V extends ListDataView<T, ?>>
* <p>
* If the item is already present in the data provider, then it is moved.
* <p>
* Refreshes all items of the component after adding the item, i.e. runs
* {@link DataView#refreshAll()}.
* <p>
* Note! Item is added to the unfiltered and unsorted List.
*
* @param item
Expand All @@ -168,6 +177,9 @@ public interface ListDataView<T, V extends ListDataView<T, ?>>
* data structure will throw an exception.
* <p>
* Any items that already present in the data provider are moved to the end.
* <p>
* Refreshes all items of the component after adding the items, i.e. runs
* {@link DataView#refreshAll()}.
*
* @param items
* collection of item to add
Expand All @@ -188,6 +200,9 @@ public interface ListDataView<T, V extends ListDataView<T, ?>>
* data structure will throw an exception. Any items that already present in
* the data provider are moved.
* <p>
* Refreshes all items of the component after adding the item, i.e. runs
* {@link DataView#refreshAll()}.
* <p>
* Note! Item is added to the unfiltered and unsorted List.
*
* @param items
Expand All @@ -213,6 +228,9 @@ public interface ListDataView<T, V extends ListDataView<T, ?>>
* <p>
* Any items that already present in the data provider are moved.
* <p>
* Refreshes all items of the component after adding the item, i.e. runs
* {@link DataView#refreshAll()}.
* <p>
* Note! Item is added to the unfiltered and unsorted List.
*
* @param items
Expand All @@ -234,6 +252,9 @@ public interface ListDataView<T, V extends ListDataView<T, ?>>
* <p>
* The backing {@link List} must be mutable to use this method. Immutable
* data structure will throw an exception.
* <p>
* Refreshes all items of the component after removing the item, i.e. runs
* {@link DataView#refreshAll()}.
*
* @param item
* item to remove
Expand Down Expand Up @@ -271,6 +292,10 @@ public interface ListDataView<T, V extends ListDataView<T, ?>>
* 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.
* <p>
* Refreshes all items of the component after setting the filter, i.e. runs
* {@link DataView#refreshAll()}.
*
*
* @param filter
* filter to be set, or <code>null</code> to clear any previously
Expand All @@ -291,6 +316,9 @@ public interface ListDataView<T, V extends ListDataView<T, ?>>
* 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.
* <p>
* Refreshes all items of the component after adding the filter, i.e. runs
* {@link DataView#refreshAll()}.
*
* @param filter
* the filter to add, not <code>null</code>
Expand All @@ -303,6 +331,9 @@ public interface ListDataView<T, V extends ListDataView<T, ?>>

/**
* Removes all in-memory filters set or added.
* <p>
* Refreshes all items of the component after removing the filter, i.e. runs
* {@link DataView#refreshAll()}.
*
* @return ListDataView instance
*
Expand All @@ -320,6 +351,9 @@ public interface ListDataView<T, V extends ListDataView<T, ?>>
* 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.
* <p>
* Refreshes all items of the component after setting the sorting, i.e. runs
* {@link DataView#refreshAll()}.
*
* @param sortComparator
* a comparator to use, or <code>null</code> to clear any
Expand All @@ -342,6 +376,9 @@ public interface ListDataView<T, V extends ListDataView<T, ?>>
* 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.
* <p>
* Refreshes all items of the component after adding the sorting, i.e. runs
* {@link DataView#refreshAll()}.
*
* @param sortComparator
* a comparator to add, not <code>null</code>
Expand All @@ -357,6 +394,9 @@ public interface ListDataView<T, V extends ListDataView<T, ?>>
* Any other component using the same {@link DataProvider} object would not
* be affected by removing default sorting through data view of another
* component.
* <p>
* Refreshes all items of the component after removing the sorting, i.e.
* runs {@link DataView#refreshAll()}.
*
* @return ListDataView instance
*
Expand All @@ -375,6 +415,9 @@ public interface ListDataView<T, V extends ListDataView<T, ?>>
* 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.
* <p>
* 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
Expand Down Expand Up @@ -403,6 +446,9 @@ <V1 extends Comparable<? super V1>> 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.
* <p>
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
Expand All @@ -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);
Expand Down Expand Up @@ -1274,6 +1301,8 @@ public void filterOrSortingChangedCallback_emptyCallbackProvided_throws() {

private static class ListDataViewImpl extends AbstractListDataView<String> {

private final AtomicInteger refreshCount = new AtomicInteger(0);

public ListDataViewImpl(
SerializableSupplier<? extends DataProvider<String, ?>> dataProviderSupplier,
Component component) {
Expand All @@ -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<Item> {
Expand Down
Loading