jdk/src/macosx/classes/sun/lwawt/LWTextFieldPeer.java
author mcherkas
Mon, 28 Jul 2014 16:09:26 +0400
changeset 26008 ccab11700f07
parent 20153 d5bf90bfcb6d
permissions -rw-r--r--
8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread. Reviewed-by: ant, pchelko Contributed-by: artem.malinko@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     1
/*
20153
d5bf90bfcb6d 8005255: [macosx] Cleanup warnings in sun.lwawt
serb
parents: 15326
diff changeset
     2
 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     4
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    10
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    15
 * accompanied this code).
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    16
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    20
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    23
 * questions.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    24
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    25
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    26
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    27
package sun.lwawt;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    28
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    29
import java.awt.Dimension;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    30
import java.awt.Point;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    31
import java.awt.TextField;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    32
import java.awt.event.ActionEvent;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    33
import java.awt.event.ActionListener;
12399
6221283c619f 7124528: [macosx] Selection is not cleared properly in text component.
serb
parents: 12047
diff changeset
    34
import java.awt.event.FocusEvent;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    35
import java.awt.peer.TextFieldPeer;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    36
15326
e0b5489e29a6 7147078: [macosx] Echo char set in TextField doesn't prevent word jumping
alexp
parents: 13993
diff changeset
    37
import javax.swing.*;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    38
import javax.swing.text.JTextComponent;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    39
20153
d5bf90bfcb6d 8005255: [macosx] Cleanup warnings in sun.lwawt
serb
parents: 15326
diff changeset
    40
/**
d5bf90bfcb6d 8005255: [macosx] Cleanup warnings in sun.lwawt
serb
parents: 15326
diff changeset
    41
 * Lightweight implementation of {@link TextFieldPeer}. Delegates most of the
d5bf90bfcb6d 8005255: [macosx] Cleanup warnings in sun.lwawt
serb
parents: 15326
diff changeset
    42
 * work to the {@link JPasswordField}.
d5bf90bfcb6d 8005255: [macosx] Cleanup warnings in sun.lwawt
serb
parents: 15326
diff changeset
    43
 */
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    44
final class LWTextFieldPeer
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    45
        extends LWTextComponentPeer<TextField, JPasswordField>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    46
        implements TextFieldPeer, ActionListener {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    47
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    48
    LWTextFieldPeer(final TextField target,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    49
                    final PlatformComponent platformComponent) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    50
        super(target, platformComponent);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    51
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    52
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    53
    @Override
20153
d5bf90bfcb6d 8005255: [macosx] Cleanup warnings in sun.lwawt
serb
parents: 15326
diff changeset
    54
    JPasswordField createDelegate() {
15326
e0b5489e29a6 7147078: [macosx] Echo char set in TextField doesn't prevent word jumping
alexp
parents: 13993
diff changeset
    55
        return new JPasswordFieldDelegate();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    56
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    57
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    58
    @Override
13143
31c70a66a053 7142091: [macosx] RFE: Refactoring of peer initialization/disposing
serb
parents: 12891
diff changeset
    59
    void initializeImpl() {
31c70a66a053 7142091: [macosx] RFE: Refactoring of peer initialization/disposing
serb
parents: 12891
diff changeset
    60
        super.initializeImpl();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    61
        setEchoChar(getTarget().getEchoChar());
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    62
        synchronized (getDelegateLock()) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    63
            getDelegate().addActionListener(this);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    64
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    65
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    66
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    67
    @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    68
    public JTextComponent getTextComponent() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    69
        return getDelegate();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    70
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    71
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    72
    @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    73
    public void setEchoChar(final char echoChar) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    74
        synchronized (getDelegateLock()) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    75
            getDelegate().setEchoChar(echoChar);
15326
e0b5489e29a6 7147078: [macosx] Echo char set in TextField doesn't prevent word jumping
alexp
parents: 13993
diff changeset
    76
            final boolean cutCopyAllowed;
e0b5489e29a6 7147078: [macosx] Echo char set in TextField doesn't prevent word jumping
alexp
parents: 13993
diff changeset
    77
            final String focusInputMapKey;
e0b5489e29a6 7147078: [macosx] Echo char set in TextField doesn't prevent word jumping
alexp
parents: 13993
diff changeset
    78
            if (echoChar != 0) {
e0b5489e29a6 7147078: [macosx] Echo char set in TextField doesn't prevent word jumping
alexp
parents: 13993
diff changeset
    79
                cutCopyAllowed = false;
e0b5489e29a6 7147078: [macosx] Echo char set in TextField doesn't prevent word jumping
alexp
parents: 13993
diff changeset
    80
                focusInputMapKey = "PasswordField.focusInputMap";
e0b5489e29a6 7147078: [macosx] Echo char set in TextField doesn't prevent word jumping
alexp
parents: 13993
diff changeset
    81
            } else {
e0b5489e29a6 7147078: [macosx] Echo char set in TextField doesn't prevent word jumping
alexp
parents: 13993
diff changeset
    82
                cutCopyAllowed = true;
e0b5489e29a6 7147078: [macosx] Echo char set in TextField doesn't prevent word jumping
alexp
parents: 13993
diff changeset
    83
                focusInputMapKey = "TextField.focusInputMap";
e0b5489e29a6 7147078: [macosx] Echo char set in TextField doesn't prevent word jumping
alexp
parents: 13993
diff changeset
    84
            }
e0b5489e29a6 7147078: [macosx] Echo char set in TextField doesn't prevent word jumping
alexp
parents: 13993
diff changeset
    85
            getDelegate().putClientProperty("JPasswordField.cutCopyAllowed", cutCopyAllowed);
e0b5489e29a6 7147078: [macosx] Echo char set in TextField doesn't prevent word jumping
alexp
parents: 13993
diff changeset
    86
            InputMap inputMap = (InputMap) UIManager.get(focusInputMapKey);
e0b5489e29a6 7147078: [macosx] Echo char set in TextField doesn't prevent word jumping
alexp
parents: 13993
diff changeset
    87
            SwingUtilities.replaceUIInputMap(getDelegate(), JComponent.WHEN_FOCUSED, inputMap);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    88
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    89
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    90
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    91
    @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    92
    public Dimension getPreferredSize(final int columns) {
13993
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
    93
        return getMinimumSize(columns);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    94
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    95
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    96
    @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    97
    public Dimension getMinimumSize(final int columns) {
13993
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
    98
        return getMinimumSize(1, columns);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    99
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   100
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   101
    @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   102
    public void actionPerformed(final ActionEvent e) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   103
        postEvent(new ActionEvent(getTarget(), ActionEvent.ACTION_PERFORMED,
12531
42a9335fd8b3 7124210: [macosx] Replacing text in a TextField does generate an extra TextEvent
alexp
parents: 12399
diff changeset
   104
                getText(), e.getWhen(), e.getModifiers()));
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   105
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   106
12399
6221283c619f 7124528: [macosx] Selection is not cleared properly in text component.
serb
parents: 12047
diff changeset
   107
    /**
6221283c619f 7124528: [macosx] Selection is not cleared properly in text component.
serb
parents: 12047
diff changeset
   108
     * Restoring native behavior. We should sets the selection range to zero,
6221283c619f 7124528: [macosx] Selection is not cleared properly in text component.
serb
parents: 12047
diff changeset
   109
     * when component lost its focus.
6221283c619f 7124528: [macosx] Selection is not cleared properly in text component.
serb
parents: 12047
diff changeset
   110
     *
6221283c619f 7124528: [macosx] Selection is not cleared properly in text component.
serb
parents: 12047
diff changeset
   111
     * @param e the focus event
6221283c619f 7124528: [macosx] Selection is not cleared properly in text component.
serb
parents: 12047
diff changeset
   112
     */
6221283c619f 7124528: [macosx] Selection is not cleared properly in text component.
serb
parents: 12047
diff changeset
   113
    @Override
20153
d5bf90bfcb6d 8005255: [macosx] Cleanup warnings in sun.lwawt
serb
parents: 15326
diff changeset
   114
    void handleJavaFocusEvent(final FocusEvent e) {
12399
6221283c619f 7124528: [macosx] Selection is not cleared properly in text component.
serb
parents: 12047
diff changeset
   115
        if (e.getID() == FocusEvent.FOCUS_LOST) {
6221283c619f 7124528: [macosx] Selection is not cleared properly in text component.
serb
parents: 12047
diff changeset
   116
            // In order to de-select the selection
6221283c619f 7124528: [macosx] Selection is not cleared properly in text component.
serb
parents: 12047
diff changeset
   117
            setCaretPosition(0);
6221283c619f 7124528: [macosx] Selection is not cleared properly in text component.
serb
parents: 12047
diff changeset
   118
        }
6221283c619f 7124528: [macosx] Selection is not cleared properly in text component.
serb
parents: 12047
diff changeset
   119
        super.handleJavaFocusEvent(e);
6221283c619f 7124528: [macosx] Selection is not cleared properly in text component.
serb
parents: 12047
diff changeset
   120
    }
6221283c619f 7124528: [macosx] Selection is not cleared properly in text component.
serb
parents: 12047
diff changeset
   121
20153
d5bf90bfcb6d 8005255: [macosx] Cleanup warnings in sun.lwawt
serb
parents: 15326
diff changeset
   122
    @SuppressWarnings("serial")// Safe: outer class is non-serializable.
15326
e0b5489e29a6 7147078: [macosx] Echo char set in TextField doesn't prevent word jumping
alexp
parents: 13993
diff changeset
   123
    private final class JPasswordFieldDelegate extends JPasswordField {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   124
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   125
        // Empty non private constructor was added because access to this
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   126
        // class shouldn't be emulated by a synthetic accessor method.
15326
e0b5489e29a6 7147078: [macosx] Echo char set in TextField doesn't prevent word jumping
alexp
parents: 13993
diff changeset
   127
        JPasswordFieldDelegate() {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   128
            super();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   129
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   130
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   131
        @Override
12531
42a9335fd8b3 7124210: [macosx] Replacing text in a TextField does generate an extra TextEvent
alexp
parents: 12399
diff changeset
   132
        public void replaceSelection(String content) {
42a9335fd8b3 7124210: [macosx] Replacing text in a TextField does generate an extra TextEvent
alexp
parents: 12399
diff changeset
   133
            getDocument().removeDocumentListener(LWTextFieldPeer.this);
42a9335fd8b3 7124210: [macosx] Replacing text in a TextField does generate an extra TextEvent
alexp
parents: 12399
diff changeset
   134
            super.replaceSelection(content);
42a9335fd8b3 7124210: [macosx] Replacing text in a TextField does generate an extra TextEvent
alexp
parents: 12399
diff changeset
   135
            // post only one text event in this case
42a9335fd8b3 7124210: [macosx] Replacing text in a TextField does generate an extra TextEvent
alexp
parents: 12399
diff changeset
   136
            postTextEvent();
42a9335fd8b3 7124210: [macosx] Replacing text in a TextField does generate an extra TextEvent
alexp
parents: 12399
diff changeset
   137
            getDocument().addDocumentListener(LWTextFieldPeer.this);
42a9335fd8b3 7124210: [macosx] Replacing text in a TextField does generate an extra TextEvent
alexp
parents: 12399
diff changeset
   138
        }
42a9335fd8b3 7124210: [macosx] Replacing text in a TextField does generate an extra TextEvent
alexp
parents: 12399
diff changeset
   139
42a9335fd8b3 7124210: [macosx] Replacing text in a TextField does generate an extra TextEvent
alexp
parents: 12399
diff changeset
   140
        @Override
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   141
        public boolean hasFocus() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   142
            return getTarget().hasFocus();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   143
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   144
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   145
        @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   146
        public Point getLocationOnScreen() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   147
            return LWTextFieldPeer.this.getLocationOnScreen();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   148
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   149
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   150
}