@@ -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; | |||
@@ -69,6 +72,35 @@ public class ComboBox<T> extends AbstractSingleSelect<T> | |||
implements HasValue<T>, FieldEvents.BlurNotifier, | |||
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); |
@@ -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(); |