changeset 9884:45f59f317587 jdk-9+126

8144790: TableView consumes ENTER and ESC keys unnecessarily (even when not editable)
author jgiles
date Mon, 04 Jul 2016 11:38:48 +1200
parents 983965bf58fb
children 2b08224078ff bcfba7df0bd6
files modules/controls/src/main/java/com/sun/javafx/scene/control/behavior/TableViewBehavior.java modules/controls/src/main/java/com/sun/javafx/scene/control/behavior/TableViewBehaviorBase.java modules/controls/src/main/java/com/sun/javafx/scene/control/behavior/TreeTableViewBehavior.java
diffstat 3 files changed, 34 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/modules/controls/src/main/java/com/sun/javafx/scene/control/behavior/TableViewBehavior.java	Mon Jul 04 11:38:44 2016 +1200
+++ b/modules/controls/src/main/java/com/sun/javafx/scene/control/behavior/TableViewBehavior.java	Mon Jul 04 11:38:48 2016 +1200
@@ -132,6 +132,11 @@
         return getNode().getVisibleLeafColumn(index);
     }
 
+    /** {@inheritDoc} */
+    @Override protected boolean isControlEditable() {
+        return getNode().isEditable();
+    }
+
     /** {@inheritDoc}  */
     @Override protected void editCell(int row, TableColumnBase tc) {
         getNode().edit(row, (TableColumn)tc);
--- a/modules/controls/src/main/java/com/sun/javafx/scene/control/behavior/TableViewBehaviorBase.java	Mon Jul 04 11:38:44 2016 +1200
+++ b/modules/controls/src/main/java/com/sun/javafx/scene/control/behavior/TableViewBehaviorBase.java	Mon Jul 04 11:38:48 2016 +1200
@@ -135,6 +135,7 @@
         // create a map for TableView(Base)-specific mappings
         tableViewInputMap = createInputMap();
 
+        KeyMapping enterKeyActivateMapping, escapeKeyCancelEditMapping;
         addDefaultMapping(tableViewInputMap,
                 new KeyMapping(TAB, FocusTraversalInputMap::traverseNext),
                 new KeyMapping(new KeyBinding(TAB).shift(), FocusTraversalInputMap::traversePrevious),
@@ -204,14 +205,17 @@
                 new KeyMapping(new KeyBinding(HOME).shortcut().shift(), e -> discontinuousSelectAllToFirstRow()),
                 new KeyMapping(new KeyBinding(END).shortcut().shift(), e -> discontinuousSelectAllToLastRow()),
 
-                new KeyMapping(ENTER, e -> activate()),
-                new KeyMapping(SPACE, e -> activate()),
-                new KeyMapping(F2, e -> activate()),
-                new KeyMapping(ESCAPE, e -> cancelEdit()),
+                enterKeyActivateMapping = new KeyMapping(ENTER, this::activate),
+                new KeyMapping(SPACE, this::activate),
+                new KeyMapping(F2, this::activate),
+                escapeKeyCancelEditMapping = new KeyMapping(ESCAPE, this::cancelEdit),
 
                 new InputMap.MouseMapping(MouseEvent.MOUSE_PRESSED, this::mousePressed)
         );
 
+        enterKeyActivateMapping.setAutoConsume(false);
+        escapeKeyCancelEditMapping.setAutoConsume(false);
+
         // create OS-specific child mappings
         // --- mac OS
         InputMap<C> macInputMap = new InputMap<>(control);
@@ -307,6 +311,11 @@
     protected abstract TableColumnBase getVisibleLeafColumn(int index);
 
     /**
+     * Returns true if the control (i.e. TableView / TreeTableView) is editable
+     */
+    protected abstract boolean isControlEditable();
+
+    /**
      * Begins the edit process in the underlying control for the given row/column
      * position.
      */
@@ -859,11 +868,14 @@
         setAnchor(row, tc);
     }
 
-    protected void cancelEdit() {
-        editCell(-1, null);
+    protected void cancelEdit(KeyEvent e) {
+        if (isControlEditable()) {
+            editCell(-1, null);
+            e.consume();
+        }
     }
 
-    protected void activate() {
+    protected void activate(KeyEvent e) {
         TableSelectionModel sm = getSelectionModel();
         if (sm == null) return;
 
@@ -874,9 +886,13 @@
         sm.select(cell.getRow(), cell.getTableColumn());
         setAnchor(cell);
 
+        // check if we are editable
+        boolean isEditable = isControlEditable() && cell.getTableColumn().isEditable();
+
         // edit this row also
-        if (cell.getRow() >= 0) {
+        if (isEditable && cell.getRow() >= 0) {
             editCell(cell.getRow(), cell.getTableColumn());
+            e.consume();
         }
     }
 
--- a/modules/controls/src/main/java/com/sun/javafx/scene/control/behavior/TreeTableViewBehavior.java	Mon Jul 04 11:38:44 2016 +1200
+++ b/modules/controls/src/main/java/com/sun/javafx/scene/control/behavior/TreeTableViewBehavior.java	Mon Jul 04 11:38:48 2016 +1200
@@ -139,6 +139,11 @@
         return getNode().getVisibleLeafColumn(index);
     }
 
+    /** {@inheritDoc} */
+    @Override protected boolean isControlEditable() {
+        return getNode().isEditable();
+    }
+
     /** {@inheritDoc}  */
     @Override protected void editCell(int row, TableColumnBase tc) {
         getNode().edit(row, (TreeTableColumn)tc);