author | mcherkas |
Mon, 28 Jul 2014 16:09:26 +0400 | |
changeset 26008 | ccab11700f07 |
parent 20153 | d5bf90bfcb6d |
permissions | -rw-r--r-- |
12047 | 1 |
/* |
20153 | 2 |
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. |
12047 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
||
27 |
package sun.lwawt; |
|
28 |
||
29 |
import java.awt.Dimension; |
|
30 |
import java.awt.Point; |
|
31 |
import java.awt.TextField; |
|
32 |
import java.awt.event.ActionEvent; |
|
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 | 35 |
import java.awt.peer.TextFieldPeer; |
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 | 38 |
import javax.swing.text.JTextComponent; |
39 |
||
20153 | 40 |
/** |
41 |
* Lightweight implementation of {@link TextFieldPeer}. Delegates most of the |
|
42 |
* work to the {@link JPasswordField}. |
|
43 |
*/ |
|
12047 | 44 |
final class LWTextFieldPeer |
45 |
extends LWTextComponentPeer<TextField, JPasswordField> |
|
46 |
implements TextFieldPeer, ActionListener { |
|
47 |
||
48 |
LWTextFieldPeer(final TextField target, |
|
49 |
final PlatformComponent platformComponent) { |
|
50 |
super(target, platformComponent); |
|
51 |
} |
|
52 |
||
53 |
@Override |
|
20153 | 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 | 56 |
} |
57 |
||
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 | 61 |
setEchoChar(getTarget().getEchoChar()); |
62 |
synchronized (getDelegateLock()) { |
|
63 |
getDelegate().addActionListener(this); |
|
64 |
} |
|
65 |
} |
|
66 |
||
67 |
@Override |
|
68 |
public JTextComponent getTextComponent() { |
|
69 |
return getDelegate(); |
|
70 |
} |
|
71 |
||
72 |
@Override |
|
73 |
public void setEchoChar(final char echoChar) { |
|
74 |
synchronized (getDelegateLock()) { |
|
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 | 88 |
} |
89 |
} |
|
90 |
||
91 |
@Override |
|
92 |
public Dimension getPreferredSize(final int columns) { |
|
13993 | 93 |
return getMinimumSize(columns); |
12047 | 94 |
} |
95 |
||
96 |
@Override |
|
97 |
public Dimension getMinimumSize(final int columns) { |
|
13993 | 98 |
return getMinimumSize(1, columns); |
12047 | 99 |
} |
100 |
||
101 |
@Override |
|
102 |
public void actionPerformed(final ActionEvent e) { |
|
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 | 105 |
} |
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 | 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 | 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 | 124 |
|
125 |
// Empty non private constructor was added because access to this |
|
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 | 128 |
super(); |
129 |
} |
|
130 |
||
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 | 141 |
public boolean hasFocus() { |
142 |
return getTarget().hasFocus(); |
|
143 |
} |
|
144 |
||
145 |
@Override |
|
146 |
public Point getLocationOnScreen() { |
|
147 |
return LWTextFieldPeer.this.getLocationOnScreen(); |
|
148 |
} |
|
149 |
} |
|
150 |
} |