diff options
Diffstat (limited to 'server/src/main')
-rw-r--r-- | server/src/main/java/com/vaadin/data/HasHierarchicalDataProvider.java | 44 | ||||
-rw-r--r-- | server/src/main/java/com/vaadin/data/TreeData.java | 20 |
2 files changed, 64 insertions, 0 deletions
diff --git a/server/src/main/java/com/vaadin/data/HasHierarchicalDataProvider.java b/server/src/main/java/com/vaadin/data/HasHierarchicalDataProvider.java index e43b9d6b77..52ea205663 100644 --- a/server/src/main/java/com/vaadin/data/HasHierarchicalDataProvider.java +++ b/server/src/main/java/com/vaadin/data/HasHierarchicalDataProvider.java @@ -100,6 +100,50 @@ public interface HasHierarchicalDataProvider<T> extends HasDataProvider<T> { } /** + * Sets the root data items of this component provided as a stream and + * recursively populates them with child items with the given value + * provider. + * <p> + * The provided items are wrapped into a {@link TreeDataProvider} backed by + * a flat {@link TreeData} structure. The data provider instance is used as + * a parameter for the {@link #setDataProvider(DataProvider)} method. It + * means that the items collection can be accessed later on via + * {@link #getTreeData()}: + * + * <pre> + * <code> + * Stream<Person> grandParents = getGrandParents(); + * HasHierarchicalDataProvider<Person> treeGrid = new TreeGrid<>(); + * treeGrid.setItems(grandParents, Person::getChildren); + * ... + * + * TreeData<Person> data = treeGrid.getTreeData(); + * </code> + * </pre> + * <p> + * The returned {@link TreeData} instance may be used as-is to add, remove + * or modify items in the hierarchy. These modifications to the object are + * not automatically reflected back to the TreeGrid. Items modified should + * be refreshed with {@link HierarchicalDataProvider#refreshItem(Object)} + * and when adding or removing items + * {@link HierarchicalDataProvider#refreshAll()} should be called. + * + * @param rootItems + * the root items to display, not {@code null} + * @param childItemProvider + * the value provider used to recursively populate the given root + * items with child items, not {@code null} + */ + public default void setItems(Stream<T> rootItems, + ValueProvider<T, Stream<T>> childItemProvider) { + Objects.requireNonNull(rootItems, "Given root items may not be null"); + Objects.requireNonNull(childItemProvider, + "Given child item provider may not be null"); + setDataProvider(new TreeDataProvider<>( + new TreeData<T>().addItems(rootItems, childItemProvider))); + } + + /** * Sets the data items of this component provided as a collection. * <p> * The provided items are wrapped into a {@link TreeDataProvider} backed by diff --git a/server/src/main/java/com/vaadin/data/TreeData.java b/server/src/main/java/com/vaadin/data/TreeData.java index f5f9709462..2311a71984 100644 --- a/server/src/main/java/com/vaadin/data/TreeData.java +++ b/server/src/main/java/com/vaadin/data/TreeData.java @@ -23,6 +23,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.stream.Collectors; import java.util.stream.Stream; import com.vaadin.data.provider.TreeDataProvider; @@ -227,6 +228,25 @@ public class TreeData<T> implements Serializable { } /** + * Adds the given items as root items and uses the given value provider to + * recursively populate children of the root items. + * + * @param rootItems + * the root items to add + * @param childItemProvider + * the value provider used to recursively populate this TreeData + * from the given root items + * @return this + */ + public TreeData<T> addItems(Stream<T> rootItems, + ValueProvider<T, Stream<T>> childItemProvider) { + // Must collect to lists since the algorithm iterates multiple times + return addItems(rootItems.collect(Collectors.toList()), + item -> childItemProvider.apply(item) + .collect(Collectors.toList())); + } + + /** * Remove a given item from this structure. Additionally, this will * recursively remove any descendants of the item. * |