changeset 1267:a636b9ba88ac

Fix for second unit test failure, this time related to listview selection events not firing properly when the row index does not change, but the selected item does.
author jgiles
date Tue, 12 Jun 2012 16:55:59 +1200
parents 388637d028f3
children 00a63ce77e97 8192bc3aa019 35c426930d23
files javafx-ui-controls/src/javafx/scene/control/ListView.java javafx-ui-controls/src/javafx/scene/control/MultipleSelectionModelBase.java javafx-ui-controls/test/javafx/scene/control/ListViewTest.java
diffstat 3 files changed, 25 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/javafx-ui-controls/src/javafx/scene/control/ListView.java	Tue Jun 12 15:17:17 2012 +1200
+++ b/javafx-ui-controls/src/javafx/scene/control/ListView.java	Tue Jun 12 16:55:59 2012 +1200
@@ -914,7 +914,8 @@
 
             this.listView.itemsProperty().addListener(weakItemsObserver);
             if (listView.getItems() != null) {
-                updateItemsObserver(null, listView.getItems());
+                this.listView.getItems().addListener(weakItemsContentObserver);
+//                updateItemsObserver(null, this.listView.getItems());
             }
         }
         
--- a/javafx-ui-controls/src/javafx/scene/control/MultipleSelectionModelBase.java	Tue Jun 12 15:17:17 2012 +1200
+++ b/javafx-ui-controls/src/javafx/scene/control/MultipleSelectionModelBase.java	Tue Jun 12 16:55:59 2012 +1200
@@ -281,6 +281,12 @@
         if (row < 0 || row >= getItemCount()) {
             return;
         }
+        
+        boolean isSameRow = row == getSelectedIndex();
+        T currentItem = getSelectedItem();
+        T newItem = getModelItem(row);
+        boolean isSameItem = newItem != null && newItem.equals(currentItem);
+        boolean fireUpdatedItemEvent = isSameRow && ! isSameItem;
 
         if (! selectedIndices.get(row)) {
             if (getSelectionMode() == SINGLE) {
@@ -294,6 +300,10 @@
         
         // TODO this isn't correct
         selectedIndicesSeq.callObservers(new NonIterableChange.SimpleAddChange<Integer>(0, 1, selectedIndicesSeq));
+        
+        if (fireUpdatedItemEvent) {
+            setSelectedItem(newItem);
+        }
     }
 
     @Override public void select(T obj) {
--- a/javafx-ui-controls/test/javafx/scene/control/ListViewTest.java	Tue Jun 12 15:17:17 2012 +1200
+++ b/javafx-ui-controls/test/javafx/scene/control/ListViewTest.java	Tue Jun 12 16:55:59 2012 +1200
@@ -6,6 +6,8 @@
 import static javafx.scene.control.ControlTestUtils.assertStyleClassContains;
 import static org.junit.Assert.*;
 import java.util.Arrays;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import javafx.beans.property.ObjectProperty;
 import javafx.beans.property.SimpleObjectProperty;
@@ -351,13 +353,22 @@
         assertTrue(sm.isSelected(0));
         assertEquals(1, hitCount);
         
+        // sleep for 100ms so that the currentTimeMillis is guaranteed to be
+        // a different value than the first one
+        try {
+            Thread.sleep(100);
+        } catch (InterruptedException ex) {
+            ex.printStackTrace();
+        }
+        
         // 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);
+        // it should be two, as there is no null event in between (although there
+        // used to be, so the test used to be for three hits)
+        assertEquals(2, hitCount);
     }
 }