changeset 288:e5e4e7faf0cf

RT-18969: ListView, selectionModel.selectedItem property doesn't change when model is changed
author jgiles
date Fri, 20 Jan 2012 13:55:49 +1300
parents b7684dc1fa3f
children 025dacc36e76
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
diffstat 4 files changed, 178 insertions(+), 126 deletions(-) [+]
line wrap: on
line diff
--- a/javafx-ui-controls/src/javafx/scene/control/ListView.java	Fri Jan 20 10:53:14 2012 +1300
+++ b/javafx-ui-controls/src/javafx/scene/control/ListView.java	Fri Jan 20 13:55:49 2012 +1300
@@ -898,17 +898,19 @@
         // watching for changes to the items list content
         private final ListChangeListener<T> itemsContentObserver = new ListChangeListener<T>() {
             @Override public void onChanged(Change<? extends T> c) {
-                if (listView.getItems() == null || listView.getItems().isEmpty()) {
-                    setSelectedIndex(-1);
-                    focus(-1);
-                } else if (getSelectedIndex() == -1 && getSelectedItem() != null) {
-                    int newIndex = listView.getItems().indexOf(getSelectedItem());
-                    if (newIndex != -1) {
-                        setSelectedIndex(newIndex);
+                while (c.next()) {
+                    if (listView.getItems() == null || listView.getItems().isEmpty()) {
+                        setSelectedIndex(-1);
+                        focus(-1);
+                    } else if (getSelectedIndex() == -1 && getSelectedItem() != null) {
+                        int newIndex = listView.getItems().indexOf(getSelectedItem());
+                        if (newIndex != -1) {
+                            setSelectedIndex(newIndex);
+                        }
                     }
+
+                    updateSelection(c);
                 }
-
-                updateSelection(c);
             }
         };
         
@@ -972,9 +974,15 @@
 //            if (c.wasPermutated()) {
 //                System.out.println("\tWas permutated");
 //            }
+            c.reset();
             while (c.next()) {
                 if (c.wasReplaced()) {
-                    // no-op
+                    // Fix for RT-18969: the list had setAll called on it
+                    if (getSelectedIndex() < getItemCount()) {
+                        int selectedIndex = getSelectedIndex();
+                        clearSelection(selectedIndex);
+                        select(selectedIndex);
+                    }
                 } else if (c.wasAdded() || c.wasRemoved()) {
                     int shift = c.wasAdded() ? c.getAddedSize() : -c.getRemovedSize();
                     shiftSelection(c.getFrom(), shift);
--- a/javafx-ui-controls/src/javafx/scene/control/TableView.java	Fri Jan 20 10:53:14 2012 +1300
+++ b/javafx-ui-controls/src/javafx/scene/control/TableView.java	Fri Jan 20 13:55:49 2012 +1300
@@ -1852,7 +1852,12 @@
         private void updateSelection(ListChangeListener.Change<? extends S> c) {
             while (c.next()) {
                 if (c.wasReplaced()) {
-                    // no-op
+                    // Fix for RT-18969: the items list had setAll called on it
+                    if (getSelectedIndex() < getRowCount()) {
+                        int selectedIndex = getSelectedIndex();
+                        clearSelection(selectedIndex);
+                        select(selectedIndex);
+                    }
                 } else if (c.wasAdded() || c.wasRemoved()) {
                     int position = c.getFrom();
                     int shift = c.wasAdded() ? c.getAddedSize() : -c.getRemovedSize();
--- a/javafx-ui-controls/test/javafx/scene/control/ListViewTest.java	Fri Jan 20 10:53:14 2012 +1300
+++ b/javafx-ui-controls/test/javafx/scene/control/ListViewTest.java	Fri Jan 20 13:55:49 2012 +1300
@@ -9,6 +9,8 @@
 
 import javafx.beans.property.ObjectProperty;
 import javafx.beans.property.SimpleObjectProperty;
+import javafx.beans.value.ChangeListener;
+import javafx.beans.value.ObservableValue;
 import javafx.collections.FXCollections;
 import javafx.collections.ObservableList;
 
@@ -17,9 +19,11 @@
 
 public class ListViewTest {
     private ListView<String> listView;
+    private MultipleSelectionModel<String> sm;
     
     @Before public void setup() {
         listView = new ListView<String>();
+        sm = listView.getSelectionModel();
     }
     
     /*********************************************************************
@@ -31,7 +35,7 @@
     }
     
     @Test public void noArgConstructorSetsNonNullSelectionModel() {
-        assertNotNull(listView.getSelectionModel());
+        assertNotNull(sm);
     }
     
     @Test public void noArgConstructorSetsNonNullItems() {
@@ -39,11 +43,11 @@
     }
     
     @Test public void noArgConstructor_selectedItemIsNull() {
-        assertNull(listView.getSelectionModel().getSelectedItem());
+        assertNull(sm.getSelectedItem());
     }
     
     @Test public void noArgConstructor_selectedIndexIsNegativeOne() {
-        assertEquals(-1, listView.getSelectionModel().getSelectedIndex());
+        assertEquals(-1, sm.getSelectedIndex());
     }
     
     @Test public void singleArgConstructorSetsTheStyleClass() {
@@ -90,124 +94,124 @@
         MultipleSelectionModel<String> sm = new ListView.ListViewBitSetSelectionModel<String>(listView);
         ObjectProperty<MultipleSelectionModel<String>> other = new SimpleObjectProperty<MultipleSelectionModel<String>>(sm);
         listView.selectionModelProperty().bind(other);
-        assertSame(sm, listView.getSelectionModel());
+        assertSame(sm, sm);
     }
 
     @Test public void selectionModelCanBeChanged() {
         MultipleSelectionModel<String> sm = new ListView.ListViewBitSetSelectionModel<String>(listView);
         listView.setSelectionModel(sm);
-        assertSame(sm, listView.getSelectionModel());
+        assertSame(sm, sm);
     }
     
     @Test public void canSetSelectedItemToAnItemEvenWhenThereAreNoItems() {
         final String randomString = new String("I AM A CRAZY RANDOM STRING");
-        listView.getSelectionModel().select(randomString);
-        assertEquals(-1, listView.getSelectionModel().getSelectedIndex());
-        assertSame(randomString, listView.getSelectionModel().getSelectedItem());
+        sm.select(randomString);
+        assertEquals(-1, sm.getSelectedIndex());
+        assertSame(randomString, sm.getSelectedItem());
     }
         
     @Test public void canSetSelectedItemToAnItemNotInTheDataModel() {
         listView.getItems().addAll("Apple", "Orange", "Banana");
         final String randomString = new String("I AM A CRAZY RANDOM STRING");
-        listView.getSelectionModel().select(randomString);
-        assertEquals(-1, listView.getSelectionModel().getSelectedIndex());
-        assertSame(randomString, listView.getSelectionModel().getSelectedItem());
+        sm.select(randomString);
+        assertEquals(-1, sm.getSelectedIndex());
+        assertSame(randomString, sm.getSelectedItem());
     }
         
     @Test public void settingTheSelectedItemToAnItemInItemsResultsInTheCorrectSelectedIndex() {
         listView.getItems().addAll("Apple", "Orange", "Banana");
-        listView.getSelectionModel().select("Orange");
-        assertEquals(1, listView.getSelectionModel().getSelectedIndex());
-        assertSame("Orange", listView.getSelectionModel().getSelectedItem());
+        sm.select("Orange");
+        assertEquals(1, sm.getSelectedIndex());
+        assertSame("Orange", sm.getSelectedItem());
     }
     
     @Test public void settingTheSelectedItemToANonexistantItemAndThenSettingItemsWhichContainsItResultsInCorrectSelectedIndex() {
-        listView.getSelectionModel().select("Orange");
+        sm.select("Orange");
         listView.getItems().addAll("Apple", "Orange", "Banana");
-        assertEquals(1, listView.getSelectionModel().getSelectedIndex());
-        assertSame("Orange", listView.getSelectionModel().getSelectedItem());
+        assertEquals(1, sm.getSelectedIndex());
+        assertSame("Orange", sm.getSelectedItem());
     }
     
     @Test public void ensureSelectionClearsWhenAllItemsAreRemoved_selectIndex0() {
         listView.getItems().addAll("Apple", "Orange", "Banana");
-        listView.getSelectionModel().select(0);
+        sm.select(0);
         listView.getItems().clear();
-        assertEquals(-1, listView.getSelectionModel().getSelectedIndex());
-        assertEquals(null, listView.getSelectionModel().getSelectedItem());
+        assertEquals(-1, sm.getSelectedIndex());
+        assertEquals(null, sm.getSelectedItem());
     }
     
     @Test public void ensureSelectionClearsWhenAllItemsAreRemoved_selectIndex2() {
         listView.getItems().addAll("Apple", "Orange", "Banana");
-        listView.getSelectionModel().select(2);
+        sm.select(2);
         listView.getItems().clear();
-        assertEquals(-1, listView.getSelectionModel().getSelectedIndex());
-        assertEquals(null, listView.getSelectionModel().getSelectedItem());
+        assertEquals(-1, sm.getSelectedIndex());
+        assertEquals(null, sm.getSelectedItem());
     }
     
     @Test public void ensureSelectedItemRemainsAccurateWhenItemsAreCleared() {
         listView.getItems().addAll("Apple", "Orange", "Banana");
-        listView.getSelectionModel().select(2);
+        sm.select(2);
         listView.getItems().clear();
-        assertNull(listView.getSelectionModel().getSelectedItem());
-        assertEquals(-1, listView.getSelectionModel().getSelectedIndex());
+        assertNull(sm.getSelectedItem());
+        assertEquals(-1, sm.getSelectedIndex());
         
         listView.getItems().addAll("Kiwifruit", "Mandarin", "Pineapple");
-        listView.getSelectionModel().select(2);
-        assertEquals("Pineapple", listView.getSelectionModel().getSelectedItem());
+        sm.select(2);
+        assertEquals("Pineapple", sm.getSelectedItem());
     }
     
     @Test public void ensureSelectionShiftsDownWhenOneNewItemIsAdded() {
         listView.getItems().addAll("Apple", "Orange", "Banana");
-        listView.getSelectionModel().select(1);
-        assertEquals(1, listView.getSelectionModel().getSelectedIndex());
-        assertEquals("Orange", listView.getSelectionModel().getSelectedItem());
+        sm.select(1);
+        assertEquals(1, sm.getSelectedIndex());
+        assertEquals("Orange", sm.getSelectedItem());
         
         listView.getItems().add(0, "Kiwifruit");
-        assertEquals(2, listView.getSelectionModel().getSelectedIndex());
-        assertEquals("Orange", listView.getSelectionModel().getSelectedItem());
+        assertEquals(2, sm.getSelectedIndex());
+        assertEquals("Orange", sm.getSelectedItem());
     }
     
     @Test public void ensureSelectionShiftsDownWhenMultipleNewItemAreAdded() {
         listView.getItems().addAll("Apple", "Orange", "Banana");
-        listView.getSelectionModel().select(1);
-        assertEquals(1, listView.getSelectionModel().getSelectedIndex());
-        assertEquals("Orange", listView.getSelectionModel().getSelectedItem());
+        sm.select(1);
+        assertEquals(1, sm.getSelectedIndex());
+        assertEquals("Orange", sm.getSelectedItem());
         
         listView.getItems().addAll(0, Arrays.asList("Kiwifruit", "Pineapple", "Mandarin"));
-        assertEquals("Orange", listView.getSelectionModel().getSelectedItem());
-        assertEquals(4, listView.getSelectionModel().getSelectedIndex());
+        assertEquals("Orange", sm.getSelectedItem());
+        assertEquals(4, sm.getSelectedIndex());
     }
     
     @Test public void ensureSelectionShiftsUpWhenOneItemIsRemoved() {
         listView.getItems().addAll("Apple", "Orange", "Banana");
-        listView.getSelectionModel().select(1);
-        assertEquals(1, listView.getSelectionModel().getSelectedIndex());
-        assertEquals("Orange", listView.getSelectionModel().getSelectedItem());
+        sm.select(1);
+        assertEquals(1, sm.getSelectedIndex());
+        assertEquals("Orange", sm.getSelectedItem());
         
         listView.getItems().remove("Apple");
-        assertEquals(0, listView.getSelectionModel().getSelectedIndex());
-        assertEquals("Orange", listView.getSelectionModel().getSelectedItem());
+        assertEquals(0, sm.getSelectedIndex());
+        assertEquals("Orange", sm.getSelectedItem());
     }
     
     @Test public void ensureSelectionShiftsUpWheMultipleItemsAreRemoved() {
         listView.getItems().addAll("Apple", "Orange", "Banana");
-        listView.getSelectionModel().select(2);
-        assertEquals(2, listView.getSelectionModel().getSelectedIndex());
-        assertEquals("Banana", listView.getSelectionModel().getSelectedItem());
+        sm.select(2);
+        assertEquals(2, sm.getSelectedIndex());
+        assertEquals("Banana", sm.getSelectedItem());
         
         listView.getItems().removeAll(Arrays.asList("Apple", "Orange"));
-        assertEquals(0, listView.getSelectionModel().getSelectedIndex());
-        assertEquals("Banana", listView.getSelectionModel().getSelectedItem());
+        assertEquals(0, sm.getSelectedIndex());
+        assertEquals("Banana", sm.getSelectedItem());
     }
     
     @Test public void ensureSelectionIsCorrectWhenItemsChange() {
         listView.setItems(FXCollections.observableArrayList("Item 1"));
-        listView.getSelectionModel().select(0);
-        assertEquals("Item 1", listView.getSelectionModel().getSelectedItem());
+        sm.select(0);
+        assertEquals("Item 1", sm.getSelectedItem());
         
         listView.setItems(FXCollections.observableArrayList("Item 2"));
-        assertEquals(-1, listView.getSelectionModel().getSelectedIndex());
-        assertEquals(null, listView.getSelectionModel().getSelectedItem());
+        assertEquals(-1, sm.getSelectedIndex());
+        assertEquals(null, sm.getSelectedItem());
     }
     
     @Test public void test_rt15793() {
@@ -301,10 +305,10 @@
     
     @Test public void test_rt18385() {
         listView.getItems().addAll("row1", "row2", "row3");
-        listView.getSelectionModel().select(1);
+        sm.select(1);
         listView.getItems().add("Another Row");
-        assertEquals(1, listView.getSelectionModel().getSelectedIndices().size());
-        assertEquals(1, listView.getSelectionModel().getSelectedItems().size());
+        assertEquals(1, sm.getSelectedIndices().size());
+        assertEquals(1, sm.getSelectedItems().size());
     }
     
     @Test public void test_rt18339_onlyEditWhenListViewIsEditable_editableIsFalse() {
@@ -321,8 +325,39 @@
     
     @Test public void test_rt14451() {
         listView.getItems().addAll("Apple", "Orange", "Banana");
-        listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
-        listView.getSelectionModel().selectRange(0, 2); // select from 0 (inclusive) to 2 (exclusive)
-        assertEquals(2, listView.getSelectionModel().getSelectedIndices().size());
+        sm.setSelectionMode(SelectionMode.MULTIPLE);
+        sm.selectRange(0, 2); // select from 0 (inclusive) to 2 (exclusive)
+        assertEquals(2, sm.getSelectedIndices().size());
+    }
+    
+    private int hitCount = 0;
+    @Test public void test_rt18969() {
+        hitCount = 0;
+        ObservableList<String> emptyModel = FXCollections.observableArrayList();
+        listView.setItems(emptyModel);
+        assertTrue(listView.getItems().isEmpty());
+        
+        sm.selectedItemProperty().addListener(new ChangeListener<String>() {
+            @Override public void changed(ObservableValue<? extends String> observable, String oldValue, final String newValue) {
+                hitCount++;
+            }
+        });
+        
+        ObservableList<String> mod = FXCollections.observableArrayList();
+        mod.add(System.currentTimeMillis()+"");
+        listView.getItems().setAll(mod);
+        
+        sm.select(0);
+        assertTrue(sm.isSelected(0));
+        assertEquals(1, hitCount);
+        
+        // the list is totally changing (it is being cleared), so we should 
+        // be nulling out the selection model state
+        mod = FXCollections.observableArrayList();
+        mod.add(System.currentTimeMillis()+"");
+        listView.getItems().setAll(mod);
+        
+        // it should be three, as there is a null event in between
+        assertEquals(3, hitCount);
     }
 }
--- a/javafx-ui-controls/test/javafx/scene/control/TableViewTest.java	Fri Jan 20 10:53:14 2012 +1300
+++ b/javafx-ui-controls/test/javafx/scene/control/TableViewTest.java	Fri Jan 20 13:55:49 2012 +1300
@@ -11,6 +11,8 @@
 
 import javafx.beans.property.ObjectProperty;
 import javafx.beans.property.SimpleObjectProperty;
+import javafx.beans.value.ChangeListener;
+import javafx.beans.value.ObservableValue;
 import javafx.collections.FXCollections;
 import javafx.collections.ObservableList;
 import javafx.scene.control.cell.PropertyValueFactory;
@@ -21,9 +23,11 @@
 
 public class TableViewTest {
     private TableView<String> table;
+    private TableView.TableViewSelectionModel sm;
 
     @Before public void setup() {
         table = new TableView<String>();
+        sm = table.getSelectionModel();
     }
 
     /*********************************************************************
@@ -35,7 +39,7 @@
     }
 
     @Test public void noArgConstructorSetsNonNullSelectionModel() {
-        assertNotNull(table.getSelectionModel());
+        assertNotNull(sm);
     }
 
     @Test public void noArgConstructorSetsNonNullItems() {
@@ -43,11 +47,11 @@
     }
 
     @Test public void noArgConstructor_selectedItemIsNull() {
-        assertNull(table.getSelectionModel().getSelectedItem());
+        assertNull(sm.getSelectedItem());
     }
 
     @Test public void noArgConstructor_selectedIndexIsNegativeOne() {
-        assertEquals(-1, table.getSelectionModel().getSelectedIndex());
+        assertEquals(-1, sm.getSelectedIndex());
     }
 
     @Test public void singleArgConstructorSetsTheStyleClass() {
@@ -94,126 +98,126 @@
         TableView.TableViewSelectionModel<String> sm = new TableView.TableViewArrayListSelectionModel<String>(table);
         ObjectProperty<TableView.TableViewSelectionModel<String>> other = new SimpleObjectProperty<TableView.TableViewSelectionModel<String>>(sm);
         table.selectionModelProperty().bind(other);
-        assertSame(sm, table.getSelectionModel());
+        assertSame(sm, sm);
     }
 
     @Test public void selectionModelCanBeChanged() {
         TableView.TableViewSelectionModel<String> sm = new TableView.TableViewArrayListSelectionModel<String>(table);
         table.setSelectionModel(sm);
-        assertSame(sm, table.getSelectionModel());
+        assertSame(sm, sm);
     }
 
     @Test public void canSetSelectedItemToAnItemEvenWhenThereAreNoItems() {
         final String randomString = new String("I AM A CRAZY RANDOM STRING");
-        table.getSelectionModel().select(randomString);
-        assertEquals(-1, table.getSelectionModel().getSelectedIndex());
-        assertSame(randomString, table.getSelectionModel().getSelectedItem());
+        sm.select(randomString);
+        assertEquals(-1, sm.getSelectedIndex());
+        assertSame(randomString, sm.getSelectedItem());
     }
 
     @Test public void canSetSelectedItemToAnItemNotInTheDataModel() {
         table.getItems().addAll("Apple", "Orange", "Banana");
         final String randomString = new String("I AM A CRAZY RANDOM STRING");
-        table.getSelectionModel().select(randomString);
-        assertEquals(-1, table.getSelectionModel().getSelectedIndex());
-        assertSame(randomString, table.getSelectionModel().getSelectedItem());
+        sm.select(randomString);
+        assertEquals(-1, sm.getSelectedIndex());
+        assertSame(randomString, sm.getSelectedItem());
     }
 
     @Test public void settingTheSelectedItemToAnItemInItemsResultsInTheCorrectSelectedIndex() {
         table.getItems().addAll("Apple", "Orange", "Banana");
-        table.getSelectionModel().select("Orange");
-        assertEquals(1, table.getSelectionModel().getSelectedIndex());
-        assertSame("Orange", table.getSelectionModel().getSelectedItem());
+        sm.select("Orange");
+        assertEquals(1, sm.getSelectedIndex());
+        assertSame("Orange", sm.getSelectedItem());
     }
 
     @Test public void settingTheSelectedItemToANonexistantItemAndThenSettingItemsWhichContainsItResultsInCorrectSelectedIndex() {
-        table.getSelectionModel().select("Orange");
+        sm.select("Orange");
         table.getItems().addAll("Apple", "Orange", "Banana");
-        assertEquals(1, table.getSelectionModel().getSelectedIndex());
-        assertSame("Orange", table.getSelectionModel().getSelectedItem());
+        assertEquals(1, sm.getSelectedIndex());
+        assertSame("Orange", sm.getSelectedItem());
     }
     
     @Test public void ensureSelectionClearsWhenAllItemsAreRemoved_selectIndex0() {
         table.getItems().addAll("Apple", "Orange", "Banana");
-        table.getSelectionModel().select(0);
+        sm.select(0);
         table.getItems().clear();
-        assertEquals(-1, table.getSelectionModel().getSelectedIndex());
+        assertEquals(-1, sm.getSelectedIndex());
     }
     
     @Test public void ensureSelectionClearsWhenAllItemsAreRemoved_selectIndex2() {
         table.getItems().addAll("Apple", "Orange", "Banana");
-        table.getSelectionModel().select(2);
+        sm.select(2);
         table.getItems().clear();
-        assertEquals(-1, table.getSelectionModel().getSelectedIndex());
+        assertEquals(-1, sm.getSelectedIndex());
     }
     
     @Test public void ensureSelectedItemRemainsAccurateWhenItemsAreCleared() {
         table.getItems().addAll("Apple", "Orange", "Banana");
-        table.getSelectionModel().select(2);
+        sm.select(2);
         table.getItems().clear();
-        assertNull("Selected Item: " + table.getSelectionModel().getSelectedItem(), table.getSelectionModel().getSelectedItem());
-        assertEquals(-1, table.getSelectionModel().getSelectedIndex());
+        assertNull("Selected Item: " + sm.getSelectedItem(), sm.getSelectedItem());
+        assertEquals(-1, sm.getSelectedIndex());
         
         table.getItems().addAll("Kiwifruit", "Mandarin", "Pineapple");
-        table.getSelectionModel().select(2);
-        assertEquals("Pineapple", table.getSelectionModel().getSelectedItem());
+        sm.select(2);
+        assertEquals("Pineapple", sm.getSelectedItem());
     }
     
     @Ignore("Not fixed yet")
     @Test public void ensureSelectionShiftsDownWhenOneNewItemIsAdded() {
         table.getItems().addAll("Apple", "Orange", "Banana");
-        table.getSelectionModel().select(1);
-        assertEquals(1, table.getSelectionModel().getSelectedIndex());
-        assertEquals("Orange", table.getSelectionModel().getSelectedItem());
+        sm.select(1);
+        assertEquals(1, sm.getSelectedIndex());
+        assertEquals("Orange", sm.getSelectedItem());
         
         table.getItems().add(0, "Kiwifruit");
-        assertEquals(2, table.getSelectionModel().getSelectedIndex());
-        assertEquals("Orange", table.getSelectionModel().getSelectedItem());
+        assertEquals(2, sm.getSelectedIndex());
+        assertEquals("Orange", sm.getSelectedItem());
     }
     
     @Ignore("Not fixed yet")
     @Test public void ensureSelectionShiftsDownWhenMultipleNewItemAreAdded() {
         table.getItems().addAll("Apple", "Orange", "Banana");
-        table.getSelectionModel().select(1);
-        assertEquals(1, table.getSelectionModel().getSelectedIndex());
-        assertEquals("Orange", table.getSelectionModel().getSelectedItem());
+        sm.select(1);
+        assertEquals(1, sm.getSelectedIndex());
+        assertEquals("Orange", sm.getSelectedItem());
         
         table.getItems().addAll(0, Arrays.asList("Kiwifruit", "Pineapple", "Mandarin"));
-        assertEquals("Orange", table.getSelectionModel().getSelectedItem());
-        assertEquals(4, table.getSelectionModel().getSelectedIndex());
+        assertEquals("Orange", sm.getSelectedItem());
+        assertEquals(4, sm.getSelectedIndex());
     }
     
     @Ignore("Not fixed yet")
     @Test public void ensureSelectionShiftsDownWhenOneItemIsRemoved() {
         table.getItems().addAll("Apple", "Orange", "Banana");
-        table.getSelectionModel().select(1);
-        assertEquals(1, table.getSelectionModel().getSelectedIndex());
-        assertEquals("Orange", table.getSelectionModel().getSelectedItem());
+        sm.select(1);
+        assertEquals(1, sm.getSelectedIndex());
+        assertEquals("Orange", sm.getSelectedItem());
         
         table.getItems().remove("Apple");
-        assertEquals(0, table.getSelectionModel().getSelectedIndex());
-        assertEquals("Orange", table.getSelectionModel().getSelectedItem());
+        assertEquals(0, sm.getSelectedIndex());
+        assertEquals("Orange", sm.getSelectedItem());
     }
     
     @Ignore("Not fixed yet")
     @Test public void ensureSelectionShiftsDownWheMultipleItemsAreRemoved() {
         table.getItems().addAll("Apple", "Orange", "Banana");
-        table.getSelectionModel().select(2);
-        assertEquals(2, table.getSelectionModel().getSelectedIndex());
-        assertEquals("Banana", table.getSelectionModel().getSelectedItem());
+        sm.select(2);
+        assertEquals(2, sm.getSelectedIndex());
+        assertEquals("Banana", sm.getSelectedItem());
         
         table.getItems().removeAll(Arrays.asList("Apple", "Orange"));
-        assertEquals(0, table.getSelectionModel().getSelectedIndex());
-        assertEquals("Banana", table.getSelectionModel().getSelectedItem());
+        assertEquals(0, sm.getSelectedIndex());
+        assertEquals("Banana", sm.getSelectedItem());
     }
     
     @Test public void ensureSelectionIsCorrectWhenItemsChange() {
         table.setItems(FXCollections.observableArrayList("Item 1"));
-        table.getSelectionModel().select(0);
-        assertEquals("Item 1", table.getSelectionModel().getSelectedItem());
+        sm.select(0);
+        assertEquals("Item 1", sm.getSelectedItem());
         
         table.setItems(FXCollections.observableArrayList("Item 2"));
-        assertEquals(-1, table.getSelectionModel().getSelectedIndex());
-        assertEquals(null, table.getSelectionModel().getSelectedItem());
+        assertEquals(-1, sm.getSelectedIndex());
+        assertEquals(null, sm.getSelectedItem());
     }
 
     /*********************************************************************
@@ -363,11 +367,11 @@
     
     @Test public void test_rt18385() {
         table.getItems().addAll("row1", "row2", "row3");
-        table.getSelectionModel().select(1);
+        sm.select(1);
         table.getItems().add("Another Row");
-        assertEquals(1, table.getSelectionModel().getSelectedIndices().size());
-        assertEquals(1, table.getSelectionModel().getSelectedItems().size());
-        assertEquals(1, table.getSelectionModel().getSelectedCells().size());
+        assertEquals(1, sm.getSelectedIndices().size());
+        assertEquals(1, sm.getSelectedItems().size());
+        assertEquals(1, sm.getSelectedCells().size());
     }
     
     @Test public void test_rt18339_onlyEditWhenTableViewIsEditable_tableEditableIsFalse_columnEditableIsFalse() {
@@ -408,8 +412,8 @@
     
     @Test public void test_rt14451() {
         table.getItems().addAll("Apple", "Orange", "Banana");
-        table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
-        table.getSelectionModel().selectRange(0, 2); // select from 0 (inclusive) to 2 (exclusive)
-        assertEquals(2, table.getSelectionModel().getSelectedIndices().size());
+        sm.setSelectionMode(SelectionMode.MULTIPLE);
+        sm.selectRange(0, 2); // select from 0 (inclusive) to 2 (exclusive)
+        assertEquals(2, sm.getSelectedIndices().size());
     }
 }