changeset 1291:35c426930d23

RT-21586: ListView: Replacing all items does not clear selection
author jgiles
date Tue, 12 Jun 2012 20:21:58 +1200
parents a636b9ba88ac
children b6f81f47c4c8
files javafx-ui-controls/src/javafx/scene/control/ListView.java javafx-ui-controls/src/javafx/scene/control/TableView.java javafx-ui-controls/test/javafx/scene/control/ListViewTest.java javafx-ui-controls/test/javafx/scene/control/TableViewTest.java javafx-ui-controls/test/javafx/scene/control/TreeViewTest.java
diffstat 5 files changed, 71 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/javafx-ui-controls/src/javafx/scene/control/ListView.java	Tue Jun 12 16:55:59 2012 +1200
+++ b/javafx-ui-controls/src/javafx/scene/control/ListView.java	Tue Jun 12 20:21:58 2012 +1200
@@ -979,6 +979,8 @@
          **********************************************************************/
 
         private final ListView<T> listView;
+        
+        private int previousModelSize = 0;
 
         // Listen to changes in the listview items list, such that when it 
         // changes we can update the selected indices bitset to refer to the 
@@ -1007,9 +1009,13 @@
                         // the entire items list was emptied - clear selection
                         clearSelection();
                     } else {
-                        // Fix for RT-18969: the list had setAll called on it
                         int index = getSelectedIndex();
-                        if (index < getItemCount() && index >= 0) {
+                        
+                        if (previousModelSize == c.getRemovedSize()) {
+                            // all items were removed from the model
+                            clearSelection();
+                        } else if (index < getItemCount() && index >= 0) {
+                            // Fix for RT-18969: the list had setAll called on it
                             // Use of makeAtomic is a fix for RT-20945
                             makeAtomic = true;
                             clearSelection(index);
@@ -1077,6 +1083,8 @@
                     }
                 }
             }
+            
+            previousModelSize = getItemCount();
         }
 
 
--- a/javafx-ui-controls/src/javafx/scene/control/TableView.java	Tue Jun 12 16:55:59 2012 +1200
+++ b/javafx-ui-controls/src/javafx/scene/control/TableView.java	Tue Jun 12 20:21:58 2012 +1200
@@ -1845,6 +1845,7 @@
          *                                                                     *
          **********************************************************************/
 
+        private int previousModelSize = 0;
         
         // Listen to changes in the tableview items list, such that when it 
         // changes we can update the selected indices list to refer to the 
@@ -1852,12 +1853,26 @@
         private void updateSelection(ListChangeListener.Change<? extends S> c) {
             while (c.next()) {
                 if (c.wasReplaced()) {
-                    // Fix for RT-18969: the items list had setAll called on it
-                    if (getSelectedIndex() < getRowCount()) {
-                        int selectedIndex = getSelectedIndex();
-                        clearSelection(selectedIndex);
-                        select(selectedIndex);
+                    if (c.getList().isEmpty()) {
+                        // the entire items list was emptied - clear selection
+                        clearSelection();
+                    } else {
+                        int index = getSelectedIndex();
+                        
+                        if (previousModelSize == c.getRemovedSize()) {
+                            // all items were removed from the model
+                            clearSelection();
+                        } else if (index < getRowCount() && index >= 0) {
+                            // Fix for RT-18969: the list had setAll called on it
+                            clearSelection(index);
+                            select(index);
+                        } else {
+                            // Fix for RT-22079
+                            clearSelection();
+                        }
                     }
+                    
+                    
                 } else if (c.wasAdded() || c.wasRemoved()) {
                     int position = c.getFrom();
                     int shift = c.wasAdded() ? c.getAddedSize() : -c.getRemovedSize();
@@ -1925,6 +1940,8 @@
                     selectedCellsSeq.callObservers(new NonIterableChange.SimpleAddChange<TablePosition>(0, newIndices.size(), selectedCellsSeq));
                 }
             }
+            
+            previousModelSize = getRowCount();
         }
 
         /***********************************************************************
--- a/javafx-ui-controls/test/javafx/scene/control/ListViewTest.java	Tue Jun 12 16:55:59 2012 +1200
+++ b/javafx-ui-controls/test/javafx/scene/control/ListViewTest.java	Tue Jun 12 20:21:58 2012 +1200
@@ -371,4 +371,15 @@
         // used to be, so the test used to be for three hits)
         assertEquals(2, hitCount);
     }
+    
+    @Test public void test_rt21586() {
+        listView.getItems().setAll("Apple", "Orange", "Banana");
+        listView.getSelectionModel().select(1);
+        assertEquals(1, listView.getSelectionModel().getSelectedIndex());
+        assertEquals("Orange", listView.getSelectionModel().getSelectedItem());
+        
+        listView.getItems().setAll("Kiwifruit", "Pineapple", "Grape");
+        assertEquals(-1, listView.getSelectionModel().getSelectedIndex());
+        assertNull(listView.getSelectionModel().getSelectedItem());
+    }
 }
--- a/javafx-ui-controls/test/javafx/scene/control/TableViewTest.java	Tue Jun 12 16:55:59 2012 +1200
+++ b/javafx-ui-controls/test/javafx/scene/control/TableViewTest.java	Tue Jun 12 20:21:58 2012 +1200
@@ -416,4 +416,15 @@
         sm.selectRange(0, 2); // select from 0 (inclusive) to 2 (exclusive)
         assertEquals(2, sm.getSelectedIndices().size());
     }
+    
+    @Test public void test_rt21586() {
+        table.getItems().setAll("Apple", "Orange", "Banana");
+        table.getSelectionModel().select(1);
+        assertEquals(1, table.getSelectionModel().getSelectedIndex());
+        assertEquals("Orange", table.getSelectionModel().getSelectedItem());
+        
+        table.getItems().setAll("Kiwifruit", "Pineapple", "Grape");
+        assertEquals(-1, table.getSelectionModel().getSelectedIndex());
+        assertNull(table.getSelectionModel().getSelectedItem());
+    }
 }
--- a/javafx-ui-controls/test/javafx/scene/control/TreeViewTest.java	Tue Jun 12 16:55:59 2012 +1200
+++ b/javafx-ui-controls/test/javafx/scene/control/TreeViewTest.java	Tue Jun 12 20:21:58 2012 +1200
@@ -408,4 +408,21 @@
         treeView.getSelectionModel().selectRange(0, 2); // select from 0 (inclusive) to 2 (exclusive)
         assertEquals(2, treeView.getSelectionModel().getSelectedIndices().size());
     }
+    
+    @Test public void test_rt21586() {
+        installChildren();
+        treeView.getSelectionModel().select(1);
+        assertEquals(1, treeView.getSelectionModel().getSelectedIndex());
+        assertEquals(child1, treeView.getSelectionModel().getSelectedItem());
+        
+        TreeItem root = new TreeItem<String>("New Root");
+        TreeItem child1 = new TreeItem<String>("New Child 1");
+        TreeItem child2 = new TreeItem<String>("New Child 2");
+        TreeItem child3 = new TreeItem<String>("New Child 3");
+        root.setExpanded(true);
+        root.getChildren().setAll(child1, child2, child3);
+        treeView.setRoot(root);
+        assertEquals(-1, treeView.getSelectionModel().getSelectedIndex());
+        assertNull(treeView.getSelectionModel().getSelectedItem());
+    }
 }