From 1817a82ed8f3c8839c18d9ce6b68e5838940a22c Mon Sep 17 00:00:00 2001 From: Adam Wagner Date: Thu, 8 Feb 2018 11:20:32 +0200 Subject: Add recursive expand and collapse method to TreeGrid and Tree (#10283) --- server/src/main/java/com/vaadin/ui/TreeGrid.java | 120 ++++++++++++++++++++++- 1 file changed, 118 insertions(+), 2 deletions(-) (limited to 'server/src/main/java/com/vaadin/ui/TreeGrid.java') diff --git a/server/src/main/java/com/vaadin/ui/TreeGrid.java b/server/src/main/java/com/vaadin/ui/TreeGrid.java index 65329d24c8..1c5716cc61 100644 --- a/server/src/main/java/com/vaadin/ui/TreeGrid.java +++ b/server/src/main/java/com/vaadin/ui/TreeGrid.java @@ -175,12 +175,12 @@ public class TreeGrid extends Grid userOriginated) -> { T item = getDataCommunicator().getKeyMapper().get(rowKey); if (collapse && getDataCommunicator().isExpanded(item)) { - getDataCommunicator().doCollapse(item, Optional.of(rowIndex)); + getDataCommunicator().collapse(item, rowIndex); fireCollapseEvent( getDataCommunicator().getKeyMapper().get(rowKey), userOriginated); } else if (!collapse && !getDataCommunicator().isExpanded(item)) { - getDataCommunicator().doExpand(item, Optional.of(rowIndex)); + getDataCommunicator().expand(item, rowIndex); fireExpandEvent( getDataCommunicator().getKeyMapper().get(rowKey), userOriginated); @@ -344,6 +344,64 @@ public class TreeGrid extends Grid }); } + /** + * Expands the given items and their children recursively until the given + * depth. + *

+ * {@code depth} describes the maximum distance between a given item and its + * descendant, meaning that {@code expandRecursively(items, 0)} expands only + * the given items while {@code expandRecursively(items, 2)} expands the + * given items as well as their children and grandchildren. + *

+ * This method will not fire events for expanded nodes. + * + * @param items + * the items to expand recursively + * @param depth + * the maximum depth of recursion + * @since + */ + public void expandRecursively(Collection items, int depth) { + expandRecursively(items.stream(), depth); + } + + /** + * Expands the given items and their children recursively until the given + * depth. + *

+ * {@code depth} describes the maximum distance between a given item and its + * descendant, meaning that {@code expandRecursively(items, 0)} expands only + * the given items while {@code expandRecursively(items, 2)} expands the + * given items as well as their children and grandchildren. + *

+ * This method will not fire events for expanded nodes. + * + * @param items + * the items to expand recursively + * @param depth + * the maximum depth of recursion + * @since + */ + public void expandRecursively(Stream items, int depth) { + if (depth < 0) { + return; + } + + HierarchicalDataCommunicator communicator = getDataCommunicator(); + items.forEach(item -> { + if (communicator.hasChildren(item)) { + communicator.expand(item, false); + + expandRecursively( + getDataProvider().fetchChildren( + new HierarchicalQuery<>(null, item)), + depth - 1); + } + }); + + getDataProvider().refreshAll(); + } + /** * Collapse the given items. *

@@ -374,6 +432,64 @@ public class TreeGrid extends Grid }); } + /** + * Collapse the given items and their children recursively until the given + * depth. + *

+ * {@code depth} describes the maximum distance between a given item and its + * descendant, meaning that {@code collapseRecursively(items, 0)} collapses + * only the given items while {@code collapseRecursively(items, 2)} + * collapses the given items as well as their children and grandchildren. + *

+ * This method will not fire events for collapsed nodes. + * + * @param items + * the items to collapse recursively + * @param depth + * the maximum depth of recursion + * @since + */ + public void collapseRecursively(Collection items, int depth) { + collapseRecursively(items.stream(), depth); + } + + /** + * Collapse the given items and their children recursively until the given + * depth. + *

+ * {@code depth} describes the maximum distance between a given item and its + * descendant, meaning that {@code collapseRecursively(items, 0)} collapses + * only the given items while {@code collapseRecursively(items, 2)} + * collapses the given items as well as their children and grandchildren. + *

+ * This method will not fire events for collapsed nodes. + * + * @param items + * the items to collapse recursively + * @param depth + * the maximum depth of recursion + * @since + */ + public void collapseRecursively(Stream items, int depth) { + if (depth < 0) { + return; + } + + HierarchicalDataCommunicator communicator = getDataCommunicator(); + items.forEach(item -> { + if (communicator.hasChildren(item)) { + collapseRecursively( + getDataProvider().fetchChildren( + new HierarchicalQuery<>(null, item)), + depth - 1); + + communicator.collapse(item, false); + } + }); + + getDataProvider().refreshAll(); + } + /** * Returns whether a given item is expanded or collapsed. * -- cgit v1.2.3