changeset 6127:73ca8a2e497d

RT-35213: TreeTableView.getTreeItem(int index) returns wrong items after expand / collapse operations.
author jgiles
date Thu, 16 Jan 2014 11:52:08 +1300
parents ec8f70da59a4
children 7301f6f33da9
files modules/controls/src/main/java/javafx/scene/control/TreeTableView.java modules/controls/src/main/java/javafx/scene/control/TreeView.java modules/controls/src/test/java/javafx/scene/control/TreeTableViewTest.java modules/controls/src/test/java/javafx/scene/control/TreeViewTest.java
diffstat 4 files changed, 162 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/modules/controls/src/main/java/javafx/scene/control/TreeTableView.java	Thu Jan 16 11:12:04 2014 +1300
+++ b/modules/controls/src/main/java/javafx/scene/control/TreeTableView.java	Thu Jan 16 11:52:08 2014 +1300
@@ -1480,11 +1480,15 @@
         // normalize the requested row based on whether showRoot is set
         final int _row = isShowRoot() ? row : (row + 1);
 
-        if (treeItemCacheMap.containsKey(_row)) {
-            SoftReference<TreeItem<S>> treeItemRef = treeItemCacheMap.get(_row);
-            TreeItem<S> treeItem = treeItemRef.get();
-            if (treeItem != null) {
-                return treeItem;
+        if (expandedItemCountDirty) {
+            updateExpandedItemCount(getRoot());
+        } else {
+            if (treeItemCacheMap.containsKey(_row)) {
+                SoftReference<TreeItem<S>> treeItemRef = treeItemCacheMap.get(_row);
+                TreeItem<S> treeItem = treeItemRef.get();
+                if (treeItem != null) {
+                    return treeItem;
+                }
             }
         }
 
--- a/modules/controls/src/main/java/javafx/scene/control/TreeView.java	Thu Jan 16 11:12:04 2014 +1300
+++ b/modules/controls/src/main/java/javafx/scene/control/TreeView.java	Thu Jan 16 11:52:08 2014 +1300
@@ -934,11 +934,15 @@
         // normalize the requested row based on whether showRoot is set
         final int _row = isShowRoot() ? row : (row + 1);
 
-        if (treeItemCacheMap.containsKey(_row)) {
-            SoftReference<TreeItem<T>> treeItemRef = treeItemCacheMap.get(_row);
-            TreeItem<T> treeItem = treeItemRef.get();
-            if (treeItem != null) {
-                return treeItem;
+        if (expandedItemCountDirty) {
+            updateExpandedItemCount(getRoot());
+        } else {
+            if (treeItemCacheMap.containsKey(_row)) {
+                SoftReference<TreeItem<T>> treeItemRef = treeItemCacheMap.get(_row);
+                TreeItem<T> treeItem = treeItemRef.get();
+                if (treeItem != null) {
+                    return treeItem;
+                }
             }
         }
 
--- a/modules/controls/src/test/java/javafx/scene/control/TreeTableViewTest.java	Thu Jan 16 11:12:04 2014 +1300
+++ b/modules/controls/src/test/java/javafx/scene/control/TreeTableViewTest.java	Thu Jan 16 11:52:08 2014 +1300
@@ -3037,4 +3037,78 @@
         assertTrue(table.getSelectionModel().isSelected(0));
         assertTrue(table.getFocusModel().isFocused(0));
     }
+
+    private int test_rt_35213_eventCount = 0;
+    @Test public void test_rt35213() {
+        final TreeTableView<String> view = new TreeTableView<>();
+
+        TreeItem<String> root = new TreeItem<>("Boss");
+        view.setRoot(root);
+
+        TreeItem<String> group1 = new TreeItem<>("Group 1");
+        TreeItem<String> group2 = new TreeItem<>("Group 2");
+        TreeItem<String> group3 = new TreeItem<>("Group 3");
+
+        root.getChildren().addAll(group1, group2, group3);
+
+        TreeItem<String> employee1 = new TreeItem<>("Employee 1");
+        TreeItem<String> employee2 = new TreeItem<>("Employee 2");
+
+        group2.getChildren().addAll(employee1, employee2);
+
+        TreeTableColumn<String, String> nameColumn = new TreeTableColumn<>("Name");
+        nameColumn.setCellValueFactory(new TreeItemPropertyValueFactory<>("name"));
+        view.getColumns().add(nameColumn);
+
+        view.expandedItemCountProperty().addListener(new ChangeListener<Number>() {
+            @Override public void changed(
+                    ObservableValue<? extends Number> observableValue,
+                    Number oldCount, Number newCount) {
+
+                // DEBUG OUTPUT
+//                System.out.println("new expanded item count: " + newCount.intValue());
+//                for (int i = 0; i < newCount.intValue(); i++) {
+//                    TreeItem<String> item = view.getTreeItem(i);
+//                    String text = item.getValue();
+//                    System.out.println("person found at index " + i + " is " + text);
+//                }
+//                System.out.println("------------------------------------------");
+
+                if (test_rt_35213_eventCount == 0) {
+                    assertEquals(4, newCount);
+                    assertEquals("Boss", view.getTreeItem(0).getValue());
+                    assertEquals("Group 1", view.getTreeItem(1).getValue());
+                    assertEquals("Group 2", view.getTreeItem(2).getValue());
+                    assertEquals("Group 3", view.getTreeItem(3).getValue());
+                } else if (test_rt_35213_eventCount == 1) {
+                    assertEquals(6, newCount);
+                    assertEquals("Boss", view.getTreeItem(0).getValue());
+                    assertEquals("Group 1", view.getTreeItem(1).getValue());
+                    assertEquals("Group 2", view.getTreeItem(2).getValue());
+                    assertEquals("Employee 1", view.getTreeItem(3).getValue());
+                    assertEquals("Employee 2", view.getTreeItem(4).getValue());
+                    assertEquals("Group 3", view.getTreeItem(5).getValue());
+                } else if (test_rt_35213_eventCount == 2) {
+                    assertEquals(4, newCount);
+                    assertEquals("Boss", view.getTreeItem(0).getValue());
+                    assertEquals("Group 1", view.getTreeItem(1).getValue());
+                    assertEquals("Group 2", view.getTreeItem(2).getValue());
+                    assertEquals("Group 3", view.getTreeItem(3).getValue());
+                }
+
+                test_rt_35213_eventCount++;
+            }
+        });
+
+        new StageLoader(view);
+
+        root.setExpanded(true);
+        Toolkit.getToolkit().firePulse();
+
+        group2.setExpanded(true);
+        Toolkit.getToolkit().firePulse();
+
+        group2.setExpanded(false);
+        Toolkit.getToolkit().firePulse();
+    }
 }
--- a/modules/controls/src/test/java/javafx/scene/control/TreeViewTest.java	Thu Jan 16 11:12:04 2014 +1300
+++ b/modules/controls/src/test/java/javafx/scene/control/TreeViewTest.java	Thu Jan 16 11:52:08 2014 +1300
@@ -1441,4 +1441,74 @@
         assertTrue(treeView.getSelectionModel().isSelected(0));
         assertTrue(treeView.getFocusModel().isFocused(0));
     }
+
+    private int test_rt_35213_eventCount = 0;
+    @Test public void test_rt35213() {
+        final TreeView<String> view = new TreeView<>();
+
+        TreeItem<String> root = new TreeItem<>("Boss");
+        view.setRoot(root);
+
+        TreeItem<String> group1 = new TreeItem<>("Group 1");
+        TreeItem<String> group2 = new TreeItem<>("Group 2");
+        TreeItem<String> group3 = new TreeItem<>("Group 3");
+
+        root.getChildren().addAll(group1, group2, group3);
+
+        TreeItem<String> employee1 = new TreeItem<>("Employee 1");
+        TreeItem<String> employee2 = new TreeItem<>("Employee 2");
+
+        group2.getChildren().addAll(employee1, employee2);
+
+        view.expandedItemCountProperty().addListener(new ChangeListener<Number>() {
+            @Override public void changed(
+                    ObservableValue<? extends Number> observableValue,
+                    Number oldCount, Number newCount) {
+
+                // DEBUG OUTPUT
+//                System.out.println("new expanded item count: " + newCount.intValue());
+//                for (int i = 0; i < newCount.intValue(); i++) {
+//                    TreeItem<String> item = view.getTreeItem(i);
+//                    String text = item.getValue();
+//                    System.out.println("person found at index " + i + " is " + text);
+//                }
+//                System.out.println("------------------------------------------");
+
+                if (test_rt_35213_eventCount == 0) {
+                    assertEquals(4, newCount);
+                    assertEquals("Boss", view.getTreeItem(0).getValue());
+                    assertEquals("Group 1", view.getTreeItem(1).getValue());
+                    assertEquals("Group 2", view.getTreeItem(2).getValue());
+                    assertEquals("Group 3", view.getTreeItem(3).getValue());
+                } else if (test_rt_35213_eventCount == 1) {
+                    assertEquals(6, newCount);
+                    assertEquals("Boss", view.getTreeItem(0).getValue());
+                    assertEquals("Group 1", view.getTreeItem(1).getValue());
+                    assertEquals("Group 2", view.getTreeItem(2).getValue());
+                    assertEquals("Employee 1", view.getTreeItem(3).getValue());
+                    assertEquals("Employee 2", view.getTreeItem(4).getValue());
+                    assertEquals("Group 3", view.getTreeItem(5).getValue());
+                } else if (test_rt_35213_eventCount == 2) {
+                    assertEquals(4, newCount);
+                    assertEquals("Boss", view.getTreeItem(0).getValue());
+                    assertEquals("Group 1", view.getTreeItem(1).getValue());
+                    assertEquals("Group 2", view.getTreeItem(2).getValue());
+                    assertEquals("Group 3", view.getTreeItem(3).getValue());
+                }
+
+                test_rt_35213_eventCount++;
+            }
+        });
+
+        new StageLoader(view);
+
+        root.setExpanded(true);
+        Toolkit.getToolkit().firePulse();
+
+        group2.setExpanded(true);
+        Toolkit.getToolkit().firePulse();
+
+        group2.setExpanded(false);
+        Toolkit.getToolkit().firePulse();
+    }
 }