changeset 6859:bb19e2b54285

RT-36786: [TableView] Cell based selection table does not select item when gaining focus
author jgiles
date Wed, 23 Apr 2014 09:46:32 +1200
parents 82eec770a3b2
children 47fb67cb113f
files modules/controls/src/main/java/javafx/scene/control/TableView.java modules/controls/src/main/java/javafx/scene/control/TreeTableView.java modules/controls/src/test/java/javafx/scene/control/TableViewTest.java modules/controls/src/test/java/javafx/scene/control/TreeTableViewTest.java
diffstat 4 files changed, 80 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/modules/controls/src/main/java/javafx/scene/control/TableView.java	Wed Apr 23 09:18:16 2014 +1200
+++ b/modules/controls/src/main/java/javafx/scene/control/TableView.java	Wed Apr 23 09:46:32 2014 +1200
@@ -693,13 +693,20 @@
         // RT-25679 - we select the first item in the control if there is no
         // current selection or focus on any other cell
         List<S> items = getItems();
-        MultipleSelectionModel<S> sm = getSelectionModel();
+        TableSelectionModel<S> sm = getSelectionModel();
         FocusModel<S> fm = getFocusModel();
 
         if (items != null && items.size() > 0 &&
                 sm != null && sm.isEmpty() &&
-                fm != null && fm.getFocusedIndex() == -1) {
-            sm.select(0);
+                fm != null && fm.getFocusedItem() == null) {
+            if (sm.isCellSelectionEnabled()) {
+                TableColumn<S,?> firstVisibleColumn = getVisibleLeafColumn(0);
+                if (firstVisibleColumn != null) {
+                    sm.select(0, firstVisibleColumn);
+                }
+            } else {
+                sm.select(0);
+            }
         }
     };
 
--- a/modules/controls/src/main/java/javafx/scene/control/TreeTableView.java	Wed Apr 23 09:18:16 2014 +1200
+++ b/modules/controls/src/main/java/javafx/scene/control/TreeTableView.java	Wed Apr 23 09:46:32 2014 +1200
@@ -752,13 +752,20 @@
     private InvalidationListener focusedListener = observable -> {
         // RT-25679 - we select the first item in the control if there is no
         // current selection or focus on any other cell
-        MultipleSelectionModel<TreeItem<S>> sm = getSelectionModel();
+        TableSelectionModel<TreeItem<S>> sm = getSelectionModel();
         FocusModel<TreeItem<S>> fm = getFocusModel();
 
         if (getExpandedItemCount() > 0 &&
                 sm != null && sm.isEmpty() &&
-                fm != null && fm.getFocusedIndex() == -1) {
-            sm.select(0);
+                fm != null && fm.getFocusedItem() == null) {
+            if (sm.isCellSelectionEnabled()) {
+                TreeTableColumn<S,?> firstVisibleColumn = getVisibleLeafColumn(0);
+                if (firstVisibleColumn != null) {
+                    sm.select(0, firstVisibleColumn);
+                }
+            } else {
+                sm.select(0);
+            }
         }
     };
     
--- a/modules/controls/src/test/java/javafx/scene/control/TableViewTest.java	Wed Apr 23 09:18:16 2014 +1200
+++ b/modules/controls/src/test/java/javafx/scene/control/TableViewTest.java	Wed Apr 23 09:46:32 2014 +1200
@@ -2906,7 +2906,15 @@
         sl.dispose();
     }
 
-    @Test public void test_rt25679() {
+    @Test public void test_rt25679_rowSelection() {
+        test_rt25679(true);
+    }
+
+    @Test public void test_rt25679_cellSelection() {
+        test_rt25679(false);
+    }
+
+    private void test_rt25679(boolean rowSelection) {
         Button focusBtn = new Button("Focus here");
 
         TableView<String> tableView = new TableView<>(FXCollections.observableArrayList("A", "B", "C"));
@@ -2914,7 +2922,8 @@
         TableColumn<String, String> tableColumn = new TableColumn<>();
         tableColumn.setCellValueFactory(rowValue -> new SimpleStringProperty(rowValue.getValue()));
         tableView.getColumns().add(tableColumn);
-        SelectionModel sm = tableView.getSelectionModel();
+        TableView.TableViewSelectionModel<String> sm = tableView.getSelectionModel();
+        sm.setCellSelectionEnabled(! rowSelection);
 
         VBox vbox = new VBox(focusBtn, tableView);
 
@@ -2935,8 +2944,25 @@
         // ensure that there is a selection (where previously there was not one)
         assertEquals(sl.getStage().getScene().getFocusOwner(), tableView);
         assertTrue(tableView.isFocused());
-        assertEquals(0, sm.getSelectedIndex());
-        assertEquals("A", sm.getSelectedItem());
+
+        if (rowSelection) {
+            assertEquals(1, sm.getSelectedIndices().size());
+            assertEquals("A", sm.getSelectedItem());
+            assertTrue(sm.isSelected(0));
+
+            assertEquals(1, sm.getSelectedCells().size());
+            TablePosition selectedCell = sm.getSelectedCells().get(0);
+            assertEquals(0, selectedCell.getRow());
+            assertEquals(-1, selectedCell.getColumn());
+            assertNull(selectedCell.getTableColumn());
+        } else {
+            assertTrue(sm.isSelected(0, tableColumn));
+            assertEquals(1, sm.getSelectedCells().size());
+            TablePosition selectedCell = sm.getSelectedCells().get(0);
+            assertEquals(0, selectedCell.getRow());
+            assertEquals(0, selectedCell.getColumn());
+            assertEquals(tableColumn, selectedCell.getTableColumn());
+        }
 
         sl.dispose();
     }
--- a/modules/controls/src/test/java/javafx/scene/control/TreeTableViewTest.java	Wed Apr 23 09:18:16 2014 +1200
+++ b/modules/controls/src/test/java/javafx/scene/control/TreeTableViewTest.java	Wed Apr 23 09:46:32 2014 +1200
@@ -3282,7 +3282,15 @@
         sl.dispose();
     }
 
-    @Test public void test_rt25679() {
+    @Test public void test_rt25679_rowSelection() {
+        test_rt25679(true);
+    }
+
+    @Test public void test_rt25679_cellSelection() {
+        test_rt25679(false);
+    }
+
+    private void test_rt25679(boolean rowSelection) {
         Button focusBtn = new Button("Focus here");
 
         TreeItem<String> root = new TreeItem<>("Root");
@@ -3294,7 +3302,8 @@
         tableColumn.setCellValueFactory(rowValue -> new SimpleStringProperty(rowValue.getValue().getValue()));
         treeView.getColumns().add(tableColumn);
 
-        SelectionModel sm = treeView.getSelectionModel();
+        TreeTableView.TreeTableViewSelectionModel<String> sm = treeView.getSelectionModel();
+        sm.setCellSelectionEnabled(! rowSelection);
 
         VBox vbox = new VBox(focusBtn, treeView);
 
@@ -3315,8 +3324,25 @@
         // ensure that there is a selection (where previously there was not one)
         assertEquals(sl.getStage().getScene().getFocusOwner(), treeView);
         assertTrue(treeView.isFocused());
-        assertEquals(0, sm.getSelectedIndex());
-        assertEquals(root, sm.getSelectedItem());
+
+        if (rowSelection) {
+            assertEquals(1, sm.getSelectedIndices().size());
+            assertEquals(root, sm.getSelectedItem());
+            assertTrue(sm.isSelected(0));
+
+            assertEquals(1, sm.getSelectedCells().size());
+            TreeTablePosition selectedCell = sm.getSelectedCells().get(0);
+            assertEquals(0, selectedCell.getRow());
+            assertEquals(-1, selectedCell.getColumn());
+            assertNull(selectedCell.getTableColumn());
+        } else {
+            assertTrue(sm.isSelected(0, tableColumn));
+            assertEquals(1, sm.getSelectedCells().size());
+            TreeTablePosition selectedCell = sm.getSelectedCells().get(0);
+            assertEquals(0, selectedCell.getRow());
+            assertEquals(0, selectedCell.getColumn());
+            assertEquals(tableColumn, selectedCell.getTableColumn());
+        }
 
         sl.dispose();
     }