changeset 1264:634e4bbb4cf5

RT-22077: TableView updates not working when ValueFactory returns a new Binding
author jgiles
date Fri, 08 Jun 2012 15:15:17 +1200
parents d09cc9910784
children cc8d7f8bbab8
files javafx-ui-controls/src/javafx/scene/control/TableCell.java javafx-ui-controls/src/javafx/scene/control/TableColumn.java
diffstat 2 files changed, 38 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/javafx-ui-controls/src/javafx/scene/control/TableCell.java	Fri Jun 08 13:56:28 2012 +1200
+++ b/javafx-ui-controls/src/javafx/scene/control/TableCell.java	Fri Jun 08 15:15:17 2012 +1200
@@ -24,22 +24,17 @@
  */
 package javafx.scene.control;
 
-import com.sun.javafx.logging.PlatformLogger;
 import java.util.Map;
 
 import javafx.beans.InvalidationListener;
 import javafx.beans.Observable;
 import javafx.beans.WeakInvalidationListener;
-import javafx.beans.property.ObjectProperty;
-import javafx.beans.property.ObjectPropertyBase;
-import javafx.beans.property.SimpleObjectProperty;
 import javafx.beans.value.ObservableValue;
 import javafx.collections.ListChangeListener;
 import javafx.event.Event;
 import javafx.scene.control.TableView.TableViewFocusModel;
 
 import com.sun.javafx.property.PropertyReference;
-import com.sun.javafx.scene.control.Logging;
 import com.sun.javafx.scene.control.WeakListChangeListener;
 import java.lang.ref.WeakReference;
 import javafx.beans.property.ReadOnlyObjectProperty;
@@ -81,7 +76,6 @@
         // we listen to changes in the index property as this indicates that
         // the cell has been reappropriated
         indexProperty().addListener(indexListener);
-        updateItem();
     }
 
 
@@ -103,7 +97,8 @@
     };
     
     @Override void indexChanged() {
-        updateItem();
+        itemDirty = true;
+        requestLayout();
     }
 
     /*
@@ -132,7 +127,7 @@
             requestLayout();
         }
     };
-
+    
     private final InvalidationListener editingListener = new InvalidationListener() {
         @Override public void invalidated(Observable value) {
             updateEditing();
@@ -478,6 +473,15 @@
                 getTableView().getSelectionModel() != null &&
                 getTableView().getSelectionModel().isCellSelectionEnabled();
     }
+    
+    /*
+     * This was brought in to fix the issue in RT-22077, namely that the 
+     * ObservableValue was being GC'd, meaning that changes to the value were
+     * no longer being delivered. By extracting this value out of the method, 
+     * it is now referred to from TableCell and will therefore no longer be
+     * GC'd.
+     */
+    private ObservableValue<T> currentObservableValue = null;
 
     /*
      * This is called when we think that the data within this TableCell may have
@@ -510,6 +514,10 @@
      * the responsibility of the TableModel developer).
      */
     private void updateItem() {
+        if (currentObservableValue != null) {
+            currentObservableValue.removeListener(weaktableRowUpdateObserver);
+        }
+        
         // there is a whole heap of reasons why we should just punt...
         if (getIndex() < 0 || 
                 columnIndex < 0 ||
@@ -519,32 +527,30 @@
                 getTableView().getItems() == null) {
             return;
         }
-
+        
         // get the total number of items in the data model
         int itemCount = getTableView().getItems().size();
         
-        ObservableValue<T> observableValue = null;
         if (getIndex() >= itemCount) {
             updateItem(null, true);
             return;
         } else {
             if (getIndex() < itemCount) {
-                observableValue = getTableColumn().getCellObservableValue(getIndex());
+                currentObservableValue = getTableColumn().getCellObservableValue(getIndex());
             }
 
-            T value = observableValue == null ? null : observableValue.getValue();
+            T value = currentObservableValue == null ? null : currentObservableValue.getValue();
             
             // update the 'item' property of this cell.
             updateItem(value, value == null);
         }
         
-        if (observableValue == null) {
+        if (currentObservableValue == null) {
             return;
         }
         
         // add property change listeners to this item
-        observableValue.removeListener(weaktableRowUpdateObserver);
-        observableValue.addListener(weaktableRowUpdateObserver);
+        currentObservableValue.addListener(weaktableRowUpdateObserver);
     }
 
     @Override protected void layoutChildren() {
--- a/javafx-ui-controls/src/javafx/scene/control/TableColumn.java	Fri Jun 08 13:56:28 2012 +1200
+++ b/javafx-ui-controls/src/javafx/scene/control/TableColumn.java	Fri Jun 08 15:15:17 2012 +1200
@@ -1097,18 +1097,18 @@
         return columns;
     }
     
-    /**
-     * Returns the actual value for a cell at a given row index (and which 
-     * belongs to this TableColumn).
-     * 
-     * @param index The row index for which the data is required.
-     * @return The data that belongs to the cell at the intersection of the given
-     *      row index and the TableColumn that this method is called on.
-     */
-    public final T getCellData(final int index) {
-        ObservableValue<T> result = getCellObservableValue(index);
-        return result == null ? null : result.getValue();
-    }
+//    /**
+//     * Returns the actual value for a cell at a given row index (and which 
+//     * belongs to this TableColumn).
+//     * 
+//     * @param index The row index for which the data is required.
+//     * @return The data that belongs to the cell at the intersection of the given
+//     *      row index and the TableColumn that this method is called on.
+//     */
+//    public final T getCellData(final int index) {
+//        ObservableValue<T> result = getCellObservableValue(index);
+//        return result == null ? null : result.getValue();
+//    }
 
     /**
      * Returns the actual value for a cell from the given item.
@@ -1142,12 +1142,15 @@
      */
     public final ObservableValue<T> getCellObservableValue(int index) {
         if (index < 0) return null;
+        
         // Get the table
         final TableView<S> table = getTableView();
         if (table == null || table.getItems() == null) return null;
+        
         // Get the rowData
         final List<S> items = table.getItems();
         if (index >= items.size()) return null; // Out of range
+        
         final S rowData = items.get(index);
         return getCellObservableValue(rowData);
     }
@@ -1172,9 +1175,11 @@
         // Get the factory
         final Callback<CellDataFeatures<S,T>, ObservableValue<T>> factory = getCellValueFactory();
         if (factory == null) return null;
+        
         // Get the table
         final TableView<S> table = getTableView();
         if (table == null) return null;
+        
         // Call the factory
         final CellDataFeatures<S,T> cdf = new CellDataFeatures<S,T>(table, this, item);
         return factory.call(cdf);