From 29a4578a8d52c0720ff16cedf783223fdc4ea533 Mon Sep 17 00:00:00 2001 From: Henri Sara Date: Wed, 28 Jun 2017 11:35:35 +0300 Subject: [PATCH] Add isExpanded() method to Tree and TreeGrid Fixes #9595 --- server/src/main/java/com/vaadin/ui/Tree.java | 15 +++++++++++++-- server/src/main/java/com/vaadin/ui/TreeGrid.java | 13 +++++++++++-- .../com/vaadin/tests/components/TreeTest.java | 5 +++-- .../tests/components/treegrid/TreeGridTest.java | 2 ++ 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/server/src/main/java/com/vaadin/ui/Tree.java b/server/src/main/java/com/vaadin/ui/Tree.java index 35bc680213..cd53d1bc08 100644 --- a/server/src/main/java/com/vaadin/ui/Tree.java +++ b/server/src/main/java/com/vaadin/ui/Tree.java @@ -412,6 +412,17 @@ public class Tree extends Composite treeGrid.collapse(items); } + /** + * Returns whether a given item is expanded or collapsed. + * + * @param item + * the item to check + * @return true if the item is expanded, false if collapsed + */ + public boolean isExpanded(T item) { + return treeGrid.isExpanded(item); + } + /** * This method is a shorthand that delegates to the currently set selection * model. @@ -767,7 +778,7 @@ public class Tree extends Composite /** * Sets the height of a row. If -1 (default), the row height is calculated * based on the theme for an empty row before the Tree is displayed. - * + * * @param rowHeight * The height of a row in pixels or -1 for automatic calculation */ @@ -777,7 +788,7 @@ public class Tree extends Composite /** * Sets the content mode of the item caption. - * + * * @param contentMode * the content mode */ diff --git a/server/src/main/java/com/vaadin/ui/TreeGrid.java b/server/src/main/java/com/vaadin/ui/TreeGrid.java index d32ff1464a..703a83192a 100644 --- a/server/src/main/java/com/vaadin/ui/TreeGrid.java +++ b/server/src/main/java/com/vaadin/ui/TreeGrid.java @@ -52,8 +52,6 @@ import com.vaadin.shared.ui.treegrid.TreeGridState; import com.vaadin.ui.declarative.DesignAttributeHandler; import com.vaadin.ui.declarative.DesignContext; import com.vaadin.ui.declarative.DesignFormatter; -import com.vaadin.ui.renderers.AbstractRenderer; -import com.vaadin.ui.renderers.Renderer; /** * A grid component for displaying hierarchical tabular data. @@ -383,6 +381,17 @@ public class TreeGrid extends Grid }); } + /** + * Returns whether a given item is expanded or collapsed. + * + * @param item + * the item to check + * @return true if the item is expanded, false if collapsed + */ + public boolean isExpanded(T item) { + return getDataCommunicator().isExpanded(item); + } + @Override protected TreeGridState getState() { return (TreeGridState) super.getState(); diff --git a/server/src/test/java/com/vaadin/tests/components/TreeTest.java b/server/src/test/java/com/vaadin/tests/components/TreeTest.java index 9224934a4c..c4ee9ae3d5 100644 --- a/server/src/test/java/com/vaadin/tests/components/TreeTest.java +++ b/server/src/test/java/com/vaadin/tests/components/TreeTest.java @@ -56,8 +56,7 @@ public class TreeTest { treeData.addItem(null, "Foo"); treeData.addItem("Foo", "Bar"); treeData.addItem("Foo", "Baz"); - tree.setDataProvider( - new TreeDataProvider<>(treeData)); + tree.setDataProvider(new TreeDataProvider<>(treeData)); TreeCollapseExpandListener listener = new TreeCollapseExpandListener( tree); @@ -66,9 +65,11 @@ public class TreeTest { Assert.assertFalse(listener.isExpanded()); tree.expand("Foo"); + Assert.assertTrue("Item not expanded", tree.isExpanded("Foo")); Assert.assertTrue("Expand event not fired", listener.isExpanded()); Assert.assertFalse(listener.isCollapsed()); tree.collapse("Foo"); + Assert.assertFalse("Item not collapsed", tree.isExpanded("Foo")); Assert.assertTrue("Collapse event not fired", listener.isCollapsed()); } diff --git a/server/src/test/java/com/vaadin/tests/components/treegrid/TreeGridTest.java b/server/src/test/java/com/vaadin/tests/components/treegrid/TreeGridTest.java index 1f900688c4..fdaacae48b 100644 --- a/server/src/test/java/com/vaadin/tests/components/treegrid/TreeGridTest.java +++ b/server/src/test/java/com/vaadin/tests/components/treegrid/TreeGridTest.java @@ -35,11 +35,13 @@ public class TreeGridTest { // Test expand event Assert.assertFalse(expandEventFired); treeGrid.expand("Foo"); + Assert.assertTrue("Item not expanded", treeGrid.isExpanded("Foo")); Assert.assertTrue("Expand event not fired", expandEventFired); // Test collapse event Assert.assertFalse(collapseEventFired); treeGrid.collapse("Foo"); + Assert.assertFalse("Item not collapsed", treeGrid.isExpanded("Foo")); Assert.assertTrue("Collapse event not fired", collapseEventFired); } } -- 2.39.5