Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/macosx/classes/sun/lwawt/LWTextFieldPeer.java
41153 views
1
/*
2
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
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
package sun.lwawt;
27
28
import java.awt.Dimension;
29
import java.awt.Point;
30
import java.awt.TextField;
31
import java.awt.event.ActionEvent;
32
import java.awt.event.ActionListener;
33
import java.awt.event.FocusEvent;
34
import java.awt.peer.TextFieldPeer;
35
36
import javax.swing.*;
37
import javax.swing.text.JTextComponent;
38
39
import sun.awt.AWTAccessor;
40
41
/**
42
* Lightweight implementation of {@link TextFieldPeer}. Delegates most of the
43
* work to the {@link JPasswordField}.
44
*/
45
final class LWTextFieldPeer
46
extends LWTextComponentPeer<TextField, JPasswordField>
47
implements TextFieldPeer, ActionListener {
48
49
LWTextFieldPeer(final TextField target,
50
final PlatformComponent platformComponent) {
51
super(target, platformComponent);
52
}
53
54
@Override
55
JPasswordField createDelegate() {
56
return new JPasswordFieldDelegate();
57
}
58
59
@Override
60
void initializeImpl() {
61
super.initializeImpl();
62
setEchoChar(getTarget().getEchoChar());
63
synchronized (getDelegateLock()) {
64
getDelegate().addActionListener(this);
65
}
66
}
67
68
@Override
69
public JTextComponent getTextComponent() {
70
return getDelegate();
71
}
72
73
@Override
74
public void setEchoChar(final char echoChar) {
75
synchronized (getDelegateLock()) {
76
getDelegate().setEchoChar(echoChar);
77
final boolean cutCopyAllowed;
78
final String focusInputMapKey;
79
if (echoChar != 0) {
80
cutCopyAllowed = false;
81
focusInputMapKey = "PasswordField.focusInputMap";
82
} else {
83
cutCopyAllowed = true;
84
focusInputMapKey = "TextField.focusInputMap";
85
}
86
getDelegate().putClientProperty("JPasswordField.cutCopyAllowed", cutCopyAllowed);
87
InputMap inputMap = (InputMap) UIManager.get(focusInputMapKey);
88
SwingUtilities.replaceUIInputMap(getDelegate(), JComponent.WHEN_FOCUSED, inputMap);
89
}
90
}
91
92
@Override
93
public Dimension getPreferredSize(final int columns) {
94
return getMinimumSize(columns);
95
}
96
97
@Override
98
public Dimension getMinimumSize(final int columns) {
99
return getMinimumSize(1, columns);
100
}
101
102
@Override
103
public void actionPerformed(final ActionEvent e) {
104
postEvent(new ActionEvent(getTarget(), ActionEvent.ACTION_PERFORMED,
105
getText(), e.getWhen(), e.getModifiers()));
106
}
107
108
/**
109
* Restoring native behavior. We should sets the selection range to zero,
110
* when component lost its focus.
111
*
112
* @param e the focus event
113
*/
114
@Override
115
void handleJavaFocusEvent(final FocusEvent e) {
116
if (e.getID() == FocusEvent.FOCUS_LOST) {
117
// In order to de-select the selection
118
setCaretPosition(0);
119
}
120
super.handleJavaFocusEvent(e);
121
}
122
123
@SuppressWarnings("serial")// Safe: outer class is non-serializable.
124
private final class JPasswordFieldDelegate extends JPasswordField {
125
126
@Override
127
public void replaceSelection(String content) {
128
getDocument().removeDocumentListener(LWTextFieldPeer.this);
129
super.replaceSelection(content);
130
// post only one text event in this case
131
postTextEvent();
132
getDocument().addDocumentListener(LWTextFieldPeer.this);
133
}
134
135
@Override
136
public boolean hasFocus() {
137
return getTarget().hasFocus();
138
}
139
140
@Override
141
public Point getLocationOnScreen() {
142
return LWTextFieldPeer.this.getLocationOnScreen();
143
}
144
145
@Override
146
public void setTransferHandler(final TransferHandler newHandler) {
147
// override the default implementation to avoid loading
148
// SystemFlavorMap and associated classes
149
Object key = AWTAccessor.getClientPropertyKeyAccessor()
150
.getJComponent_TRANSFER_HANDLER();
151
Object oldHandler = getClientProperty(key);
152
putClientProperty(key, newHandler);
153
firePropertyChange("transferHandler", oldHandler, newHandler);
154
}
155
}
156
}
157
158