diff options
author | Teemu Suo-Anttila <tsuoanttila@users.noreply.github.com> | 2017-02-09 11:03:29 +0200 |
---|---|---|
committer | Henri Sara <henri.sara@gmail.com> | 2017-02-09 11:03:29 +0200 |
commit | 322ba2ab9d7a6af8a8e68c063da7480e08843118 (patch) | |
tree | c8157fd4e4b13c55bd0a60256c22e744ad7c1112 | |
parent | 568dab7a29f5944fb02929b888a40305d06f0333 (diff) | |
download | vaadin-framework-322ba2ab9d7a6af8a8e68c063da7480e08843118.tar.gz vaadin-framework-322ba2ab9d7a6af8a8e68c063da7480e08843118.zip |
Add shorthands for creating lazy DataProviders (#8511)
-rw-r--r-- | server/src/main/java/com/vaadin/ui/ComboBox.java | 55 | ||||
-rw-r--r-- | server/src/main/java/com/vaadin/ui/Grid.java | 57 |
2 files changed, 112 insertions, 0 deletions
diff --git a/server/src/main/java/com/vaadin/ui/ComboBox.java b/server/src/main/java/com/vaadin/ui/ComboBox.java index 515c581510..a2db94a8c6 100644 --- a/server/src/main/java/com/vaadin/ui/ComboBox.java +++ b/server/src/main/java/com/vaadin/ui/ComboBox.java @@ -16,6 +16,7 @@ package com.vaadin.ui; +import java.io.Serializable; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; @@ -28,6 +29,7 @@ import org.jsoup.nodes.Element; import com.vaadin.data.HasFilterableDataProvider; import com.vaadin.data.HasValue; +import com.vaadin.data.provider.CallbackDataProvider; import com.vaadin.data.provider.DataCommunicator; import com.vaadin.data.provider.DataKeyMapper; import com.vaadin.data.provider.DataProvider; @@ -44,6 +46,7 @@ import com.vaadin.server.ResourceReference; import com.vaadin.server.SerializableBiPredicate; import com.vaadin.server.SerializableConsumer; import com.vaadin.server.SerializableFunction; +import com.vaadin.server.SerializableToIntFunction; import com.vaadin.shared.Registration; import com.vaadin.shared.data.DataCommunicatorConstants; import com.vaadin.shared.ui.combobox.ComboBoxConstants; @@ -70,6 +73,35 @@ public class ComboBox<T> extends AbstractSingleSelect<T> FieldEvents.FocusNotifier, HasFilterableDataProvider<T, String> { /** + * A callback method for fetching items. The callback is provided with a + * non-null string filter, offset index and limit. + * + * @param <T> + * item (bean) type in ComboBox + */ + @FunctionalInterface + public interface FetchItemsCallback<T> extends Serializable { + + /** + * Returns a stream of items that match the given filter, limiting the + * results with given offset and limit. + * <p> + * This method is called after the size of the data set is asked from a + * related size callback. The offset and limit are promised to be within + * the size of the data set. + * + * @param filter + * a non-null filter string + * @param offset + * the first index to fetch + * @param limit + * the fetched item count + * @return stream of items + */ + public Stream<T> fetchItems(String filter, int offset, int limit); + } + + /** * Handler that adds a new item based on user input when the new items * allowed mode is active. */ @@ -730,6 +762,29 @@ public class ComboBox<T> extends AbstractSingleSelect<T> .accept(convertOrNull.apply(filter)); } + /** + * Sets a CallbackDataProvider using the given fetch items callback and a + * size callback. + * <p> + * This method is a shorthand for making a {@link CallbackDataProvider} that + * handles a partial {@link Query} object. + * + * @param fetchItems + * a callback for fetching items + * @param sizeCallback + * a callback for getting the count of items + * + * @see CallbackDataProvider + * @see #setDataProvider(DataProvider) + */ + public void setDataProvider(FetchItemsCallback<T> fetchItems, + SerializableToIntFunction<String> sizeCallback) { + setDataProvider(new CallbackDataProvider<>( + q -> fetchItems.fetchItems(q.getFilter().orElse(""), + q.getOffset(), q.getLimit()), + q -> sizeCallback.applyAsInt(q.getFilter().orElse("")))); + } + @Override protected void setSelectedFromClient(String key) { super.setSelectedFromClient(key); diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java index 5132229ec8..fb5eeb6428 100644 --- a/server/src/main/java/com/vaadin/ui/Grid.java +++ b/server/src/main/java/com/vaadin/ui/Grid.java @@ -15,6 +15,7 @@ */ package com.vaadin.ui; +import java.io.Serializable; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.ArrayList; @@ -49,6 +50,7 @@ import com.vaadin.data.HasValue; import com.vaadin.data.PropertyDefinition; import com.vaadin.data.PropertySet; import com.vaadin.data.ValueProvider; +import com.vaadin.data.provider.CallbackDataProvider; import com.vaadin.data.provider.DataCommunicator; import com.vaadin.data.provider.DataProvider; import com.vaadin.data.provider.GridSortOrder; @@ -68,6 +70,7 @@ import com.vaadin.server.Extension; import com.vaadin.server.JsonCodec; import com.vaadin.server.SerializableComparator; import com.vaadin.server.SerializableFunction; +import com.vaadin.server.SerializableSupplier; import com.vaadin.server.Setter; import com.vaadin.shared.MouseEventDetails; import com.vaadin.shared.Registration; @@ -134,6 +137,36 @@ import elemental.json.JsonValue; public class Grid<T> extends AbstractListing<T> implements HasComponents, HasDataProvider<T>, SortNotifier<GridSortOrder<T>> { + /** + * A callback method for fetching items. The callback is provided with a + * list of sort orders, offset index and limit. + * + * @param <T> + * the grid bean type + */ + @FunctionalInterface + public interface FetchItemsCallback<T> extends Serializable { + + /** + * Returns a stream of items ordered by given sort orders, limiting the + * results with given offset and limit. + * <p> + * This method is called after the size of the data set is asked from a + * related size callback. The offset and limit are promised to be within + * the size of the data set. + * + * @param sortOrder + * a list of sort orders + * @param offset + * the first index to fetch + * @param limit + * the fetched item count + * @return stream of items + */ + public Stream<T> fetchItems(List<QuerySortOrder> sortOrder, int offset, + int limit); + } + @Deprecated private static final Method COLUMN_REORDER_METHOD = ReflectTools.findMethod( ColumnReorderListener.class, "columnReorder", @@ -3419,6 +3452,30 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents, internalSetDataProvider(dataProvider); } + /** + * Sets a CallbackDataProvider using the given fetch items callback and a + * size callback. + * <p> + * This method is a shorthand for making a {@link CallbackDataProvider} that + * handles a partial {@link Query} object. + * + * @param fetchItems + * a callback for fetching items + * @param sizeCallback + * a callback for getting the count of items + * + * @see CallbackDataProvider + * @see #setDataProvider(DataProvider) + */ + public void setDataProvider(FetchItemsCallback<T> fetchItems, + SerializableSupplier<Integer> sizeCallback) { + internalSetDataProvider( + new CallbackDataProvider<>( + q -> fetchItems.fetchItems(q.getSortOrders(), + q.getOffset(), q.getLimit()), + q -> sizeCallback.get())); + } + @Override protected void doReadDesign(Element design, DesignContext context) { Attributes attrs = design.attributes(); |