changeset 7725:dc016eecc27e

RT-38306: [TableView] TableView selection problem with SHIFT (visuals not updated, but the selection model was correct)
author jgiles
date Wed, 20 Aug 2014 09:21:10 +1200
parents 1c84e8f0fda4
children d8500a1bace1
files modules/controls/src/main/java/com/sun/javafx/scene/control/SelectedCellsMap.java modules/controls/src/main/java/com/sun/javafx/scene/control/behavior/TreeTableCellBehavior.java modules/controls/src/test/java/javafx/scene/control/TableViewMouseInputTest.java modules/controls/src/test/java/javafx/scene/control/TreeTableViewMouseInputTest.java
diffstat 4 files changed, 168 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/modules/controls/src/main/java/com/sun/javafx/scene/control/SelectedCellsMap.java	Tue Aug 19 13:40:18 2014 -0700
+++ b/modules/controls/src/main/java/com/sun/javafx/scene/control/SelectedCellsMap.java	Wed Aug 20 09:21:10 2014 +1200
@@ -55,7 +55,10 @@
 
     public SelectedCellsMap(final ListChangeListener<T> listener) {
         selectedCells = FXCollections.<T>observableArrayList();
-        sortedSelectedCells = new SortedList<>(selectedCells, (o1, o2) -> o1.getRow() - o2.getRow());
+        sortedSelectedCells = new SortedList<>(selectedCells, (T o1, T o2) -> {
+            int result = o1.getRow() - o2.getRow();
+            return result == 0 ? (o1.getColumn() - o2.getColumn()) : result;
+        });
         sortedSelectedCells.addListener(listener);
 
         selectedCellBitSetMap = new TreeMap<>((o1, o2) -> o1.compareTo(o2));
--- a/modules/controls/src/main/java/com/sun/javafx/scene/control/behavior/TreeTableCellBehavior.java	Tue Aug 19 13:40:18 2014 -0700
+++ b/modules/controls/src/main/java/com/sun/javafx/scene/control/behavior/TreeTableCellBehavior.java	Wed Aug 20 09:21:10 2014 +1200
@@ -93,7 +93,7 @@
 
     /** @{@inheritDoc} */
     @Override protected int getVisibleLeafIndex(TableColumnBase tc) {
-        return getCellContainer().getVisibleLeafIndex(null);
+        return getCellContainer().getVisibleLeafIndex((TreeTableColumn)tc);
     }
 
     /** @{@inheritDoc} */
--- a/modules/controls/src/test/java/javafx/scene/control/TableViewMouseInputTest.java	Tue Aug 19 13:40:18 2014 -0700
+++ b/modules/controls/src/test/java/javafx/scene/control/TableViewMouseInputTest.java	Wed Aug 20 09:21:10 2014 +1200
@@ -27,20 +27,19 @@
 
 import java.util.List;
 
+import com.sun.javafx.scene.control.test.Person;
 import com.sun.javafx.tk.Toolkit;
-import javafx.beans.value.ChangeListener;
-import javafx.beans.value.ObservableValue;
+import javafx.collections.FXCollections;
 import javafx.collections.ListChangeListener;
+import javafx.collections.ObservableList;
 import javafx.scene.Scene;
-import javafx.scene.layout.StackPane;
+import javafx.scene.control.cell.PropertyValueFactory;
 import javafx.scene.layout.VBox;
 import org.junit.After;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 
 import com.sun.javafx.scene.control.behavior.TableViewAnchorRetriever;
-import com.sun.javafx.scene.control.infrastructure.KeyEventFirer;
 import com.sun.javafx.scene.control.infrastructure.KeyModifier;
 import com.sun.javafx.scene.control.infrastructure.MouseEventFirer;
 import com.sun.javafx.scene.control.infrastructure.StageLoader;
@@ -75,7 +74,9 @@
     }
     
     @After public void tearDown() {
-        tableView.getSkin().dispose();
+        if (tableView.getSkin() != null) {
+            tableView.getSkin().dispose();
+        }
     }
     
     /***************************************************************************
@@ -510,4 +511,80 @@
 
         sl.dispose();
     }
+
+    @Test public void test_rt_38306_selectFirstRow() {
+        test_rt_38306(false);
+    }
+
+    @Test public void test_rt_38306_selectFirstTwoRows() {
+        test_rt_38306(true);
+    }
+
+    private void test_rt_38306(boolean selectTwoRows) {
+        final ObservableList<Person> data =
+                FXCollections.observableArrayList(
+                        new Person("Jacob", "Smith", "jacob.smith@example.com"),
+                        new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
+                        new Person("Ethan", "Williams", "ethan.williams@example.com"),
+                        new Person("Emma", "Jones", "emma.jones@example.com"),
+                        new Person("Michael", "Brown", "michael.brown@example.com"));
+
+        TableView<Person> table = new TableView<>();
+        table.setItems(data);
+
+        TableView.TableViewSelectionModel sm = table.getSelectionModel();
+        sm.setCellSelectionEnabled(true);
+        sm.setSelectionMode(SelectionMode.MULTIPLE);
+
+        TableColumn firstNameCol = new TableColumn("First Name");
+        firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
+
+        TableColumn lastNameCol = new TableColumn("Last Name");
+        lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
+
+        TableColumn emailCol = new TableColumn("Email");
+        emailCol.setCellValueFactory(new PropertyValueFactory<Person, String>("email"));
+
+        table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
+
+        sm.select(0, firstNameCol);
+
+        assertTrue(sm.isSelected(0, firstNameCol));
+        assertEquals(1, sm.getSelectedCells().size());
+
+        TableCell cell_0_1 = (TableCell) VirtualFlowTestUtils.getCell(table, 0, 1);
+        TableCell cell_0_2 = (TableCell) VirtualFlowTestUtils.getCell(table, 0, 2);
+        TableCell cell_0_3 = (TableCell) VirtualFlowTestUtils.getCell(table, 0, 3);
+
+        TableCell cell_1_1 = (TableCell) VirtualFlowTestUtils.getCell(table, 1, 1);
+        TableCell cell_1_2 = (TableCell) VirtualFlowTestUtils.getCell(table, 1, 2);
+        TableCell cell_1_3 = (TableCell) VirtualFlowTestUtils.getCell(table, 1, 3);
+
+        MouseEventFirer mouse = selectTwoRows ?
+                new MouseEventFirer(cell_1_3) : new MouseEventFirer(cell_0_3);
+
+        mouse.fireMousePressAndRelease(KeyModifier.SHIFT);
+
+        assertTrue(sm.isSelected(0, firstNameCol));
+        assertTrue(sm.isSelected(0, lastNameCol));
+        assertTrue(sm.isSelected(0, emailCol));
+
+        if (selectTwoRows) {
+            assertTrue(sm.isSelected(1, firstNameCol));
+            assertTrue(sm.isSelected(1, lastNameCol));
+            assertTrue(sm.isSelected(1, emailCol));
+        }
+
+        assertEquals(selectTwoRows ? 6 : 3, sm.getSelectedCells().size());
+
+        assertTrue(cell_0_1.isSelected());
+        assertTrue(cell_0_2.isSelected());
+        assertTrue(cell_0_3.isSelected());
+
+        if (selectTwoRows) {
+            assertTrue(cell_1_1.isSelected());
+            assertTrue(cell_1_2.isSelected());
+            assertTrue(cell_1_3.isSelected());
+        }
+    }
 }
--- a/modules/controls/src/test/java/javafx/scene/control/TreeTableViewMouseInputTest.java	Tue Aug 19 13:40:18 2014 -0700
+++ b/modules/controls/src/test/java/javafx/scene/control/TreeTableViewMouseInputTest.java	Wed Aug 20 09:21:10 2014 +1200
@@ -45,6 +45,7 @@
 import javafx.scene.Group;
 import javafx.scene.Node;
 import javafx.scene.Scene;
+import javafx.scene.control.cell.PropertyValueFactory;
 import javafx.scene.control.cell.TreeItemPropertyValueFactory;
 import javafx.scene.input.KeyCode;
 
@@ -693,4 +694,83 @@
 
         sl.dispose();
     }
+
+    @Test public void test_rt_38306_selectFirstRow() {
+        test_rt_38306(false);
+    }
+
+    @Test public void test_rt_38306_selectFirstTwoRows() {
+        test_rt_38306(true);
+    }
+
+    private void test_rt_38306(boolean selectTwoRows) {
+        TreeItem<Person> root = new TreeItem<>();
+        root.getChildren().addAll(
+                new TreeItem<>(new Person("Jacob", "Smith", "jacob.smith@example.com")),
+                new TreeItem<>(new Person("Isabella", "Johnson", "isabella.johnson@example.com")),
+                new TreeItem<>(new Person("Ethan", "Williams", "ethan.williams@example.com")),
+                new TreeItem<>(new Person("Emma", "Jones", "emma.jones@example.com")),
+                new TreeItem<>(new Person("Michael", "Brown", "michael.brown@example.com"))
+        );
+
+        TreeTableView<Person> table = new TreeTableView<>();
+        table.setRoot(root);
+        root.setExpanded(true);
+        table.setShowRoot(false);
+
+        TreeTableView.TreeTableViewSelectionModel sm = table.getSelectionModel();
+        sm.setCellSelectionEnabled(true);
+        sm.setSelectionMode(SelectionMode.MULTIPLE);
+
+        TreeTableColumn firstNameCol = new TreeTableColumn("First Name");
+        firstNameCol.setCellValueFactory(new TreeItemPropertyValueFactory<Person, String>("firstName"));
+
+        TreeTableColumn lastNameCol = new TreeTableColumn("Last Name");
+        lastNameCol.setCellValueFactory(new TreeItemPropertyValueFactory<Person, String>("lastName"));
+
+        TreeTableColumn emailCol = new TreeTableColumn("Email");
+        emailCol.setCellValueFactory(new TreeItemPropertyValueFactory<Person, String>("email"));
+
+        table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
+
+        sm.select(0, firstNameCol);
+
+        assertTrue(sm.isSelected(0, firstNameCol));
+        assertEquals(1, sm.getSelectedCells().size());
+
+        TreeTableCell cell_0_1 = (TreeTableCell) VirtualFlowTestUtils.getCell(table, 0, 1);
+        TreeTableCell cell_0_2 = (TreeTableCell) VirtualFlowTestUtils.getCell(table, 0, 2);
+        TreeTableCell cell_0_3 = (TreeTableCell) VirtualFlowTestUtils.getCell(table, 0, 3);
+
+        TreeTableCell cell_1_1 = (TreeTableCell) VirtualFlowTestUtils.getCell(table, 1, 1);
+        TreeTableCell cell_1_2 = (TreeTableCell) VirtualFlowTestUtils.getCell(table, 1, 2);
+        TreeTableCell cell_1_3 = (TreeTableCell) VirtualFlowTestUtils.getCell(table, 1, 3);
+
+        MouseEventFirer mouse = selectTwoRows ?
+                new MouseEventFirer(cell_1_3) : new MouseEventFirer(cell_0_3);
+
+        mouse.fireMousePressAndRelease(KeyModifier.SHIFT);
+
+        assertTrue(sm.isSelected(0, firstNameCol));
+        assertTrue(sm.isSelected(0, lastNameCol));
+        assertTrue(sm.isSelected(0, emailCol));
+
+        if (selectTwoRows) {
+            assertTrue(sm.isSelected(1, firstNameCol));
+            assertTrue(sm.isSelected(1, lastNameCol));
+            assertTrue(sm.isSelected(1, emailCol));
+        }
+
+        assertEquals(selectTwoRows ? 6 : 3, sm.getSelectedCells().size());
+
+        assertTrue(cell_0_1.isSelected());
+        assertTrue(cell_0_2.isSelected());
+        assertTrue(cell_0_3.isSelected());
+
+        if (selectTwoRows) {
+            assertTrue(cell_1_1.isSelected());
+            assertTrue(cell_1_2.isSelected());
+            assertTrue(cell_1_3.isSelected());
+        }
+    }
 }