From 23d65678faf5b0ae59b380d7dbc86e0f9db858cf Mon Sep 17 00:00:00 2001 From: Leif Åstrand Date: Thu, 18 May 2017 16:11:58 +0300 Subject: Add stream variants for the child item provider shorthands (#9375) --- .../vaadin/data/HasHierarchicalDataProvider.java | 44 ++++++++++++++++++++++ server/src/main/java/com/vaadin/data/TreeData.java | 20 ++++++++++ 2 files changed, 64 insertions(+) (limited to 'server/src/main') 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 @@ -99,6 +99,50 @@ public interface HasHierarchicalDataProvider extends HasDataProvider { new TreeData().addItems(rootItems, childItemProvider))); } + /** + * Sets the root data items of this component provided as a stream and + * recursively populates them with child items with the given value + * provider. + *

+ * 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()}: + * + *

+     * 
+     * Stream grandParents = getGrandParents();
+     * HasHierarchicalDataProvider treeGrid = new TreeGrid<>();
+     * treeGrid.setItems(grandParents, Person::getChildren);
+     * ...
+     *
+     * TreeData data = treeGrid.getTreeData();
+     * 
+     * 
+ *

+ * 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 rootItems, + ValueProvider> 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().addItems(rootItems, childItemProvider))); + } + /** * Sets the data items of this component provided as a collection. *

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; @@ -226,6 +227,25 @@ public class TreeData implements Serializable { return this; } + /** + * 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 addItems(Stream rootItems, + ValueProvider> 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. -- cgit v1.2.3