changeset 6540:12c8e820eb46

RT-36255 Selecting TreeItem programatically expands collapsed Items Reviewed by: jgiles
author Martin Sladecek <martin.sladecek@oracle.com>
date Mon, 24 Mar 2014 08:43:25 +0100
parents da21a84618ee
children eee373287ad8
files modules/controls/src/main/java/javafx/scene/control/TreeView.java modules/controls/src/test/java/javafx/scene/control/TreeViewTest.java
diffstat 2 files changed, 29 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/modules/controls/src/main/java/javafx/scene/control/TreeView.java	Mon Mar 24 08:32:38 2014 +0100
+++ b/modules/controls/src/main/java/javafx/scene/control/TreeView.java	Mon Mar 24 08:43:25 2014 +0100
@@ -1349,10 +1349,13 @@
             // we firstly expand the path down such that the given object is
             // visible. This fixes RT-14456, where selection was not happening
             // correctly on TreeItems that are not visible.
-            TreeItem<?> item = obj;
-            while (item != null) {
-                item.setExpanded(true);
-                item = item.getParent();
+
+            if (obj != null) {
+                TreeItem<?> item = obj.getParent();
+                while (item != null) {
+                    item.setExpanded(true);
+                    item = item.getParent();
+                }
             }
             
             // Fix for RT-15419. We eagerly update the tree item count, such that
--- a/modules/controls/src/test/java/javafx/scene/control/TreeViewTest.java	Mon Mar 24 08:32:38 2014 +0100
+++ b/modules/controls/src/test/java/javafx/scene/control/TreeViewTest.java	Mon Mar 24 08:43:25 2014 +0100
@@ -1705,4 +1705,26 @@
 
         assertEquals(0, rt_35889_cancel_count);
     }
+
+    @Test public void test_rt36255_selection_does_not_expand_item() {
+        TreeItem a = new TreeItem("a");
+        TreeItem b = new TreeItem("b");
+        b.getChildren().add(new TreeItem("bb"));
+
+        final TreeItem<String> root = new TreeItem<>();
+        root.getChildren().addAll(a, b);
+        root.setExpanded(true);
+        TreeView<String> view = new TreeView<>(root);
+        view.setCellFactory(TextFieldTreeCell.forTreeView());
+
+        view.getSelectionModel().select(a);
+
+        assertEquals(Arrays.asList(a), view.getSelectionModel().getSelectedItems());
+        assertFalse(b.isExpanded());
+
+        view.getSelectionModel().select(b);
+        assertEquals(Arrays.asList(b), view.getSelectionModel().getSelectedItems());
+        assertFalse(b.isExpanded());
+
+    }
 }