7147078: [macosx] Echo char set in TextField doesn't prevent word jumping
authoralexp
Thu, 24 Jan 2013 15:26:40 +0400
changeset 15326 e0b5489e29a6
parent 15325 3cdd98194840
child 15327 37781d0e61b4
7147078: [macosx] Echo char set in TextField doesn't prevent word jumping Reviewed-by: art
jdk/src/macosx/classes/com/apple/laf/AquaKeyBindings.java
jdk/src/macosx/classes/com/apple/laf/AquaLookAndFeel.java
jdk/src/macosx/classes/sun/lwawt/LWTextFieldPeer.java
--- a/jdk/src/macosx/classes/com/apple/laf/AquaKeyBindings.java	Mon Jan 21 17:55:31 2013 +0400
+++ b/jdk/src/macosx/classes/com/apple/laf/AquaKeyBindings.java	Thu Jan 24 15:26:40 2013 +0400
@@ -142,6 +142,21 @@
         }));
     }
 
+    LateBoundInputMap getPasswordFieldInputMap() {
+        return new LateBoundInputMap(new SimpleBinding(getTextFieldInputMap().getBindings()),
+                // nullify all the bindings that may discover space characters in the text
+                new SimpleBinding(new String[] {
+                        "alt LEFT", null,
+                        "alt KP_LEFT", null,
+                        "alt RIGHT", null,
+                        "alt KP_RIGHT", null,
+                        "shift alt LEFT", null,
+                        "shift alt KP_LEFT", null,
+                        "shift alt RIGHT", null,
+                        "shift alt KP_RIGHT", null,
+                }));
+    }
+
     LateBoundInputMap getMultiLineTextInputMap() {
         return new LateBoundInputMap(new SimpleBinding(commonTextEditorBindings), new SimpleBinding(new String[] {
             "ENTER", DefaultEditorKit.insertBreakAction,
--- a/jdk/src/macosx/classes/com/apple/laf/AquaLookAndFeel.java	Mon Jan 21 17:55:31 2013 +0400
+++ b/jdk/src/macosx/classes/com/apple/laf/AquaLookAndFeel.java	Thu Jan 24 15:26:40 2013 +0400
@@ -697,7 +697,7 @@
             "Panel.foreground", black,
             "Panel.opaque", useOpaqueComponents,
 
-            "PasswordField.focusInputMap", aquaKeyBindings.getTextFieldInputMap(),
+            "PasswordField.focusInputMap", aquaKeyBindings.getPasswordFieldInputMap(),
             "PasswordField.font", controlFont,
             "PasswordField.background", textBackground,
             "PasswordField.foreground", textForeground,
--- a/jdk/src/macosx/classes/sun/lwawt/LWTextFieldPeer.java	Mon Jan 21 17:55:31 2013 +0400
+++ b/jdk/src/macosx/classes/sun/lwawt/LWTextFieldPeer.java	Thu Jan 24 15:26:40 2013 +0400
@@ -34,7 +34,7 @@
 import java.awt.event.FocusEvent;
 import java.awt.peer.TextFieldPeer;
 
-import javax.swing.JPasswordField;
+import javax.swing.*;
 import javax.swing.text.JTextComponent;
 
 final class LWTextFieldPeer
@@ -48,7 +48,7 @@
 
     @Override
     protected JPasswordField createDelegate() {
-        return new JTextAreaDelegate();
+        return new JPasswordFieldDelegate();
     }
 
     @Override
@@ -69,9 +69,18 @@
     public void setEchoChar(final char echoChar) {
         synchronized (getDelegateLock()) {
             getDelegate().setEchoChar(echoChar);
-            getDelegate().putClientProperty("JPasswordField.cutCopyAllowed",
-                                            getDelegate().echoCharIsSet()
-                                            ? Boolean.FALSE : Boolean.TRUE);
+            final boolean cutCopyAllowed;
+            final String focusInputMapKey;
+            if (echoChar != 0) {
+                cutCopyAllowed = false;
+                focusInputMapKey = "PasswordField.focusInputMap";
+            } else {
+                cutCopyAllowed = true;
+                focusInputMapKey = "TextField.focusInputMap";
+            }
+            getDelegate().putClientProperty("JPasswordField.cutCopyAllowed", cutCopyAllowed);
+            InputMap inputMap = (InputMap) UIManager.get(focusInputMapKey);
+            SwingUtilities.replaceUIInputMap(getDelegate(), JComponent.WHEN_FOCUSED, inputMap);
         }
     }
 
@@ -106,11 +115,11 @@
         super.handleJavaFocusEvent(e);
     }
 
-    private final class JTextAreaDelegate extends JPasswordField {
+    private final class JPasswordFieldDelegate extends JPasswordField {
 
         // Empty non private constructor was added because access to this
         // class shouldn't be emulated by a synthetic accessor method.
-        JTextAreaDelegate() {
+        JPasswordFieldDelegate() {
             super();
         }