changeset 5850:022ac7cdcad1

RT-33613: TableView, TreeTableView moves focus to the wrong item in selection
author jgiles
date Thu, 28 Nov 2013 15:12:18 +1300
parents 7d1dfcec57c0
children 24dda4be6617
files modules/controls/src/main/java/com/sun/javafx/scene/control/behavior/TableViewBehaviorBase.java modules/controls/src/test/java/javafx/scene/control/TableViewKeyInputTest.java modules/controls/src/test/java/javafx/scene/control/TreeTableViewKeyInputTest.java
diffstat 3 files changed, 454 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/modules/controls/src/main/java/com/sun/javafx/scene/control/behavior/TableViewBehaviorBase.java	Thu Nov 28 14:00:07 2013 -0500
+++ b/modules/controls/src/main/java/com/sun/javafx/scene/control/behavior/TableViewBehaviorBase.java	Thu Nov 28 15:12:18 2013 +1300
@@ -864,25 +864,10 @@
             sm.selectRange(startPos, endPos);
         } else {
             // we add all cells/rows between the current selection focus and
-            // the acnhor (inclusive) to the current selection.
-
-            // and then determine all row and columns which must be selected
-            int minRow = Math.min(focusedCell.getRow(), anchorRow);
-            int maxRow = Math.max(focusedCell.getRow(), anchorRow);
-            final int focusedCellColumnPos = getVisibleLeafIndex(focusedCell.getTableColumn());
-            final int anchorColumnPos = getVisibleLeafIndex(anchor.getTableColumn());
-            int minColumn = Math.min(focusedCellColumnPos, anchorColumnPos);
-            int maxColumn = Math.max(focusedCellColumnPos, anchorColumnPos);
-
-            // clear selection
-            sm.clearSelection();
-
-            // and then perform the selection
-            for (int _row = minRow; _row <= maxRow; _row++) {
-                for (int _col = minColumn; _col <= maxColumn; _col++) {
-                    sm.select(_row, getVisibleLeafColumn(_col));
-                }
-            }
+            // the anchor (inclusive) to the current selection.
+            // We want focus to end up on the current focus position.
+            sm.selectRange(anchor.getRow(), anchor.getTableColumn(),
+                           focusedCell.getRow(), focusedCell.getTableColumn());
         }
         
         setAnchor(setAnchorToFocusIndex ? focusedCell : anchor);
--- a/modules/controls/src/test/java/javafx/scene/control/TableViewKeyInputTest.java	Thu Nov 28 14:00:07 2013 -0500
+++ b/modules/controls/src/test/java/javafx/scene/control/TableViewKeyInputTest.java	Thu Nov 28 15:12:18 2013 +1300
@@ -2612,4 +2612,226 @@
         assertEquals(2, sm.getSelectedIndex());
         assertTrue(isSelected(1, 2));
     }
+
+    @Test public void test_rt33613_up_oneColumn() {
+        final int items = 10;
+        tableView.getItems().clear();
+        for (int i = 0; i < items; i++) {
+            tableView.getItems().add("Row " + i);
+        }
+
+        new StageLoader(tableView);
+        final FocusModel fm = tableView.getFocusModel();
+        final TableSelectionModel sm = tableView.getSelectionModel();
+        sm.setCellSelectionEnabled(true);
+
+        sm.clearAndSelect(6, col0);
+        assertEquals(6, getAnchor().getRow());
+        assertEquals(0, getAnchor().getColumn());
+        assertEquals(6, fm.getFocusedIndex());
+        assertEquals(6, sm.getSelectedIndex());
+
+        keyboard.doKeyPress(KeyCode.UP, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.UP, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.UP, KeyModifier.getShortcutKey());
+        Toolkit.getToolkit().firePulse();
+        assertEquals(6, getAnchor().getRow());
+        assertEquals(0, getAnchor().getColumn());
+        assertEquals(3, fm.getFocusedIndex());
+        assertEquals(6, sm.getSelectedIndex());
+
+        keyboard.doKeyPress(KeyCode.SPACE, KeyModifier.SHIFT);
+        Toolkit.getToolkit().firePulse();
+        assertEquals(6, getAnchor().getRow());
+        assertEquals(0, getAnchor().getColumn());
+        assertEquals(3, fm.getFocusedIndex());
+        assertEquals(3, sm.getSelectedIndex());
+    }
+
+    @Test public void test_rt33613_up_multipleColumn_right() {
+        final int items = 10;
+        tableView.getItems().clear();
+        for (int i = 0; i < items; i++) {
+            tableView.getItems().add("Row " + i);
+        }
+
+        new StageLoader(tableView);
+        final TableFocusModel fm = tableView.getFocusModel();
+        final TableSelectionModel sm = tableView.getSelectionModel();
+        sm.setCellSelectionEnabled(true);
+
+        sm.clearAndSelect(6, col0);
+        assertEquals(6, getAnchor().getRow());
+        assertEquals(0, getAnchor().getColumn());
+        assertTrue(fm.isFocused(6, col0));
+        assertTrue(sm.isSelected(6, col0));
+
+        keyboard.doKeyPress(KeyCode.UP, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.UP, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.UP, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.RIGHT, KeyModifier.getShortcutKey());
+        Toolkit.getToolkit().firePulse();
+        assertEquals(6, getAnchor().getRow());
+        assertEquals(0, getAnchor().getColumn());
+        assertTrue(fm.isFocused(3, col1));
+        assertTrue(sm.isSelected(6, col0));
+        assertFalse(sm.isSelected(3, col1));
+
+        keyboard.doKeyPress(KeyCode.SPACE, KeyModifier.SHIFT);
+        Toolkit.getToolkit().firePulse();
+        assertEquals(6, getAnchor().getRow());
+        assertEquals(0, getAnchor().getColumn());
+        assertTrue(fm.isFocused(3, col1));
+        assertTrue(sm.isSelected(3, col1));
+        assertTrue(sm.isSelected(6, col0));
+    }
+
+    @Test public void test_rt33613_up_multipleColumn_left() {
+        final int items = 10;
+        tableView.getItems().clear();
+        for (int i = 0; i < items; i++) {
+            tableView.getItems().add("Row " + i);
+        }
+
+        new StageLoader(tableView);
+        final TableFocusModel fm = tableView.getFocusModel();
+        final TableSelectionModel sm = tableView.getSelectionModel();
+        sm.setCellSelectionEnabled(true);
+
+        sm.clearAndSelect(6, col1);
+        assertEquals(6, getAnchor().getRow());
+        assertEquals(1, getAnchor().getColumn());
+        assertTrue(fm.isFocused(6, col1));
+        assertTrue(sm.isSelected(6, col1));
+
+        keyboard.doKeyPress(KeyCode.UP, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.UP, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.UP, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.LEFT, KeyModifier.getShortcutKey());
+        Toolkit.getToolkit().firePulse();
+        assertEquals(6, getAnchor().getRow());
+        assertEquals(1, getAnchor().getColumn());
+        assertTrue(fm.isFocused(3, col0));
+        assertTrue(sm.isSelected(6, col1));
+        assertFalse(sm.isSelected(3, col0));
+
+        keyboard.doKeyPress(KeyCode.SPACE, KeyModifier.SHIFT);
+        Toolkit.getToolkit().firePulse();
+        assertEquals(6, getAnchor().getRow());
+        assertEquals(1, getAnchor().getColumn());
+        assertTrue(fm.isFocused(3, col0));
+        assertTrue(sm.isSelected(3, col0));
+        assertTrue(sm.isSelected(6, col1));
+    }
+
+    @Test public void test_rt33613_down_oneColumn() {
+        final int items = 10;
+        tableView.getItems().clear();
+        for (int i = 0; i < items; i++) {
+            tableView.getItems().add("Row " + i);
+        }
+
+        new StageLoader(tableView);
+        final FocusModel fm = tableView.getFocusModel();
+        final TableSelectionModel sm = tableView.getSelectionModel();
+        sm.setCellSelectionEnabled(true);
+
+        sm.clearAndSelect(3, col0);
+        assertEquals(3, getAnchor().getRow());
+        assertEquals(0, getAnchor().getColumn());
+        assertEquals(3, fm.getFocusedIndex());
+        assertEquals(3, sm.getSelectedIndex());
+
+        keyboard.doKeyPress(KeyCode.DOWN, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.DOWN, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.DOWN, KeyModifier.getShortcutKey());
+        Toolkit.getToolkit().firePulse();
+        assertEquals(3, getAnchor().getRow());
+        assertEquals(0, getAnchor().getColumn());
+        assertEquals(6, fm.getFocusedIndex());
+        assertEquals(3, sm.getSelectedIndex());
+
+        keyboard.doKeyPress(KeyCode.SPACE, KeyModifier.SHIFT);
+        Toolkit.getToolkit().firePulse();
+        assertEquals(3, getAnchor().getRow());
+        assertEquals(0, getAnchor().getColumn());
+        assertEquals(6, fm.getFocusedIndex());
+        assertEquals(6, sm.getSelectedIndex());
+    }
+
+    @Test public void test_rt33613_down_multipleColumn_right() {
+        final int items = 10;
+        tableView.getItems().clear();
+        for (int i = 0; i < items; i++) {
+            tableView.getItems().add("Row " + i);
+        }
+
+        new StageLoader(tableView);
+        final TableFocusModel fm = tableView.getFocusModel();
+        final TableSelectionModel sm = tableView.getSelectionModel();
+        sm.setCellSelectionEnabled(true);
+
+        sm.clearAndSelect(3, col0);
+        assertEquals(3, getAnchor().getRow());
+        assertEquals(0, getAnchor().getColumn());
+        assertTrue(fm.isFocused(3, col0));
+        assertTrue(sm.isSelected(3, col0));
+
+        keyboard.doKeyPress(KeyCode.DOWN, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.DOWN, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.DOWN, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.RIGHT, KeyModifier.getShortcutKey());
+        Toolkit.getToolkit().firePulse();
+        assertEquals(3, getAnchor().getRow());
+        assertEquals(0, getAnchor().getColumn());
+        assertTrue(fm.isFocused(6, col1));
+        assertTrue(sm.isSelected(3, col0));
+        assertFalse(sm.isSelected(6, col1));
+
+        keyboard.doKeyPress(KeyCode.SPACE, KeyModifier.SHIFT);
+        Toolkit.getToolkit().firePulse();
+        assertEquals(3, getAnchor().getRow());
+        assertEquals(0, getAnchor().getColumn());
+        assertTrue(fm.isFocused(6, col1));
+        assertTrue(sm.isSelected(6, col1));
+        assertTrue(sm.isSelected(3, col0));
+    }
+
+    @Test public void test_rt33613_down_multipleColumn_left() {
+        final int items = 10;
+        tableView.getItems().clear();
+        for (int i = 0; i < items; i++) {
+            tableView.getItems().add("Row " + i);
+        }
+
+        new StageLoader(tableView);
+        final TableFocusModel fm = tableView.getFocusModel();
+        final TableSelectionModel sm = tableView.getSelectionModel();
+        sm.setCellSelectionEnabled(true);
+
+        sm.clearAndSelect(3, col1);
+        assertEquals(3, getAnchor().getRow());
+        assertEquals(1, getAnchor().getColumn());
+        assertTrue(fm.isFocused(3, col1));
+        assertTrue(sm.isSelected(3, col1));
+
+        keyboard.doKeyPress(KeyCode.DOWN, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.DOWN, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.DOWN, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.LEFT, KeyModifier.getShortcutKey());
+        Toolkit.getToolkit().firePulse();
+        assertEquals(3, getAnchor().getRow());
+        assertEquals(1, getAnchor().getColumn());
+        assertTrue(fm.isFocused(6, col0));
+        assertTrue(sm.isSelected(3, col1));
+        assertFalse(sm.isSelected(6, col0));
+
+        keyboard.doKeyPress(KeyCode.SPACE, KeyModifier.SHIFT);
+        Toolkit.getToolkit().firePulse();
+        assertEquals(3, getAnchor().getRow());
+        assertEquals(1, getAnchor().getColumn());
+        assertTrue(fm.isFocused(6, col0));
+        assertTrue(sm.isSelected(6, col0));
+        assertTrue(sm.isSelected(3, col1));
+    }
 }
--- a/modules/controls/src/test/java/javafx/scene/control/TreeTableViewKeyInputTest.java	Thu Nov 28 14:00:07 2013 -0500
+++ b/modules/controls/src/test/java/javafx/scene/control/TreeTableViewKeyInputTest.java	Thu Nov 28 15:12:18 2013 +1300
@@ -3120,4 +3120,232 @@
         assertEquals(2, sm.getSelectedIndex());
         assertTrue(isSelected(1, 2));
     }
+
+    @Test public void test_rt33613_up_oneColumn() {
+        final int items = 10;
+        root.getChildren().clear();
+        root.setExpanded(true);
+        for (int i = 0; i < items; i++) {
+            root.getChildren().add(new TreeItem<>("Row " + i));
+        }
+
+        new StageLoader(tableView);
+        final FocusModel fm = tableView.getFocusModel();
+        final TableSelectionModel sm = tableView.getSelectionModel();
+        sm.setCellSelectionEnabled(true);
+
+        sm.clearAndSelect(6, col0);
+        assertEquals(6, getAnchor().getRow());
+        assertEquals(0, getAnchor().getColumn());
+        assertEquals(6, fm.getFocusedIndex());
+        assertEquals(6, sm.getSelectedIndex());
+
+        keyboard.doKeyPress(KeyCode.UP, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.UP, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.UP, KeyModifier.getShortcutKey());
+        Toolkit.getToolkit().firePulse();
+        assertEquals(6, getAnchor().getRow());
+        assertEquals(0, getAnchor().getColumn());
+        assertEquals(3, fm.getFocusedIndex());
+        assertEquals(6, sm.getSelectedIndex());
+
+        keyboard.doKeyPress(KeyCode.SPACE, KeyModifier.SHIFT);
+        Toolkit.getToolkit().firePulse();
+        assertEquals(6, getAnchor().getRow());
+        assertEquals(0, getAnchor().getColumn());
+        assertEquals(3, fm.getFocusedIndex());
+        assertEquals(3, sm.getSelectedIndex());
+    }
+
+    @Test public void test_rt33613_up_multipleColumn_right() {
+        final int items = 10;
+        root.getChildren().clear();
+        root.setExpanded(true);
+        for (int i = 0; i < items; i++) {
+            root.getChildren().add(new TreeItem<>("Row " + i));
+        }
+
+        new StageLoader(tableView);
+        final TableFocusModel fm = tableView.getFocusModel();
+        final TableSelectionModel sm = tableView.getSelectionModel();
+        sm.setCellSelectionEnabled(true);
+
+        sm.clearAndSelect(6, col0);
+        assertEquals(6, getAnchor().getRow());
+        assertEquals(0, getAnchor().getColumn());
+        assertTrue(fm.isFocused(6, col0));
+        assertTrue(sm.isSelected(6, col0));
+
+        keyboard.doKeyPress(KeyCode.UP, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.UP, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.UP, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.RIGHT, KeyModifier.getShortcutKey());
+        Toolkit.getToolkit().firePulse();
+        assertEquals(6, getAnchor().getRow());
+        assertEquals(0, getAnchor().getColumn());
+        assertTrue(fm.isFocused(3, col1));
+        assertTrue(sm.isSelected(6, col0));
+        assertFalse(sm.isSelected(3, col1));
+
+        keyboard.doKeyPress(KeyCode.SPACE, KeyModifier.SHIFT);
+        Toolkit.getToolkit().firePulse();
+        assertEquals(6, getAnchor().getRow());
+        assertEquals(0, getAnchor().getColumn());
+        assertTrue(fm.isFocused(3, col1));
+        assertTrue(sm.isSelected(3, col1));
+        assertTrue(sm.isSelected(6, col0));
+    }
+
+    @Test public void test_rt33613_up_multipleColumn_left() {
+        final int items = 10;
+        root.getChildren().clear();
+        root.setExpanded(true);
+        for (int i = 0; i < items; i++) {
+            root.getChildren().add(new TreeItem<>("Row " + i));
+        }
+
+        new StageLoader(tableView);
+        final TableFocusModel fm = tableView.getFocusModel();
+        final TableSelectionModel sm = tableView.getSelectionModel();
+        sm.setCellSelectionEnabled(true);
+
+        sm.clearAndSelect(6, col1);
+        assertEquals(6, getAnchor().getRow());
+        assertEquals(1, getAnchor().getColumn());
+        assertTrue(fm.isFocused(6, col1));
+        assertTrue(sm.isSelected(6, col1));
+
+        keyboard.doKeyPress(KeyCode.UP, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.UP, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.UP, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.LEFT, KeyModifier.getShortcutKey());
+        Toolkit.getToolkit().firePulse();
+        assertEquals(6, getAnchor().getRow());
+        assertEquals(1, getAnchor().getColumn());
+        assertTrue(fm.isFocused(3, col0));
+        assertTrue(sm.isSelected(6, col1));
+        assertFalse(sm.isSelected(3, col0));
+
+        keyboard.doKeyPress(KeyCode.SPACE, KeyModifier.SHIFT);
+        Toolkit.getToolkit().firePulse();
+        assertEquals(6, getAnchor().getRow());
+        assertEquals(1, getAnchor().getColumn());
+        assertTrue(fm.isFocused(3, col0));
+        assertTrue(sm.isSelected(3, col0));
+        assertTrue(sm.isSelected(6, col1));
+    }
+
+    @Test public void test_rt33613_down_oneColumn() {
+        final int items = 10;
+        root.getChildren().clear();
+        root.setExpanded(true);
+        for (int i = 0; i < items; i++) {
+            root.getChildren().add(new TreeItem<>("Row " + i));
+        }
+
+        new StageLoader(tableView);
+        final FocusModel fm = tableView.getFocusModel();
+        final TableSelectionModel sm = tableView.getSelectionModel();
+        sm.setCellSelectionEnabled(true);
+
+        sm.clearAndSelect(3, col0);
+        assertEquals(3, getAnchor().getRow());
+        assertEquals(0, getAnchor().getColumn());
+        assertEquals(3, fm.getFocusedIndex());
+        assertEquals(3, sm.getSelectedIndex());
+
+        keyboard.doKeyPress(KeyCode.DOWN, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.DOWN, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.DOWN, KeyModifier.getShortcutKey());
+        Toolkit.getToolkit().firePulse();
+        assertEquals(3, getAnchor().getRow());
+        assertEquals(0, getAnchor().getColumn());
+        assertEquals(6, fm.getFocusedIndex());
+        assertEquals(3, sm.getSelectedIndex());
+
+        keyboard.doKeyPress(KeyCode.SPACE, KeyModifier.SHIFT);
+        Toolkit.getToolkit().firePulse();
+        assertEquals(3, getAnchor().getRow());
+        assertEquals(0, getAnchor().getColumn());
+        assertEquals(6, fm.getFocusedIndex());
+        assertEquals(6, sm.getSelectedIndex());
+    }
+
+    @Test public void test_rt33613_down_multipleColumn_right() {
+        final int items = 10;
+        root.getChildren().clear();
+        root.setExpanded(true);
+        for (int i = 0; i < items; i++) {
+            root.getChildren().add(new TreeItem<>("Row " + i));
+        }
+
+        new StageLoader(tableView);
+        final TableFocusModel fm = tableView.getFocusModel();
+        final TableSelectionModel sm = tableView.getSelectionModel();
+        sm.setCellSelectionEnabled(true);
+
+        sm.clearAndSelect(3, col0);
+        assertEquals(3, getAnchor().getRow());
+        assertEquals(0, getAnchor().getColumn());
+        assertTrue(fm.isFocused(3, col0));
+        assertTrue(sm.isSelected(3, col0));
+
+        keyboard.doKeyPress(KeyCode.DOWN, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.DOWN, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.DOWN, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.RIGHT, KeyModifier.getShortcutKey());
+        Toolkit.getToolkit().firePulse();
+        assertEquals(3, getAnchor().getRow());
+        assertEquals(0, getAnchor().getColumn());
+        assertTrue(fm.isFocused(6, col1));
+        assertTrue(sm.isSelected(3, col0));
+        assertFalse(sm.isSelected(6, col1));
+
+        keyboard.doKeyPress(KeyCode.SPACE, KeyModifier.SHIFT);
+        Toolkit.getToolkit().firePulse();
+        assertEquals(3, getAnchor().getRow());
+        assertEquals(0, getAnchor().getColumn());
+        assertTrue(fm.isFocused(6, col1));
+        assertTrue(sm.isSelected(6, col1));
+        assertTrue(sm.isSelected(3, col0));
+    }
+
+    @Test public void test_rt33613_down_multipleColumn_left() {
+        final int items = 10;
+        root.getChildren().clear();
+        root.setExpanded(true);
+        for (int i = 0; i < items; i++) {
+            root.getChildren().add(new TreeItem<>("Row " + i));
+        }
+
+        new StageLoader(tableView);
+        final TableFocusModel fm = tableView.getFocusModel();
+        final TableSelectionModel sm = tableView.getSelectionModel();
+        sm.setCellSelectionEnabled(true);
+
+        sm.clearAndSelect(3, col1);
+        assertEquals(3, getAnchor().getRow());
+        assertEquals(1, getAnchor().getColumn());
+        assertTrue(fm.isFocused(3, col1));
+        assertTrue(sm.isSelected(3, col1));
+
+        keyboard.doKeyPress(KeyCode.DOWN, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.DOWN, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.DOWN, KeyModifier.getShortcutKey());
+        keyboard.doKeyPress(KeyCode.LEFT, KeyModifier.getShortcutKey());
+        Toolkit.getToolkit().firePulse();
+        assertEquals(3, getAnchor().getRow());
+        assertEquals(1, getAnchor().getColumn());
+        assertTrue(fm.isFocused(6, col0));
+        assertTrue(sm.isSelected(3, col1));
+        assertFalse(sm.isSelected(6, col0));
+
+        keyboard.doKeyPress(KeyCode.SPACE, KeyModifier.SHIFT);
+        Toolkit.getToolkit().firePulse();
+        assertEquals(3, getAnchor().getRow());
+        assertEquals(1, getAnchor().getColumn());
+        assertTrue(fm.isFocused(6, col0));
+        assertTrue(sm.isSelected(6, col0));
+        assertTrue(sm.isSelected(3, col1));
+    }
 }