diff options
author | Adam Wagner <wbadam@users.noreply.github.com> | 2018-02-08 11:20:32 +0200 |
---|---|---|
committer | Teemu Suo-Anttila <tsuoanttila@users.noreply.github.com> | 2018-02-08 11:20:32 +0200 |
commit | 1817a82ed8f3c8839c18d9ce6b68e5838940a22c (patch) | |
tree | 3a110ba043aa4bea415b4060c4eba4439703711b /server/src/main/java/com/vaadin/ui/TreeGrid.java | |
parent | 299520fac11dac9b31d6fd762a3eb76ef827ec5b (diff) | |
download | vaadin-framework-1817a82ed8f3c8839c18d9ce6b68e5838940a22c.tar.gz vaadin-framework-1817a82ed8f3c8839c18d9ce6b68e5838940a22c.zip |
Add recursive expand and collapse method to TreeGrid and Tree (#10283)
Diffstat (limited to 'server/src/main/java/com/vaadin/ui/TreeGrid.java')
-rw-r--r-- | server/src/main/java/com/vaadin/ui/TreeGrid.java | 120 |
1 files changed, 118 insertions, 2 deletions
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<T> extends Grid<T> 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); @@ -345,6 +345,64 @@ public class TreeGrid<T> extends Grid<T> } /** + * Expands the given items and their children recursively until the given + * depth. + * <p> + * {@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. + * <p> + * This method will <i>not</i> fire events for expanded nodes. + * + * @param items + * the items to expand recursively + * @param depth + * the maximum depth of recursion + * @since + */ + public void expandRecursively(Collection<T> items, int depth) { + expandRecursively(items.stream(), depth); + } + + /** + * Expands the given items and their children recursively until the given + * depth. + * <p> + * {@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. + * <p> + * This method will <i>not</i> fire events for expanded nodes. + * + * @param items + * the items to expand recursively + * @param depth + * the maximum depth of recursion + * @since + */ + public void expandRecursively(Stream<T> items, int depth) { + if (depth < 0) { + return; + } + + HierarchicalDataCommunicator<T> 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. * <p> * For items that are already collapsed, does nothing. @@ -375,6 +433,64 @@ public class TreeGrid<T> extends Grid<T> } /** + * Collapse the given items and their children recursively until the given + * depth. + * <p> + * {@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. + * <p> + * This method will <i>not</i> fire events for collapsed nodes. + * + * @param items + * the items to collapse recursively + * @param depth + * the maximum depth of recursion + * @since + */ + public void collapseRecursively(Collection<T> items, int depth) { + collapseRecursively(items.stream(), depth); + } + + /** + * Collapse the given items and their children recursively until the given + * depth. + * <p> + * {@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. + * <p> + * This method will <i>not</i> fire events for collapsed nodes. + * + * @param items + * the items to collapse recursively + * @param depth + * the maximum depth of recursion + * @since + */ + public void collapseRecursively(Stream<T> items, int depth) { + if (depth < 0) { + return; + } + + HierarchicalDataCommunicator<T> 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. * * @param item |