0N/A/*
2362N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
2362N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2362N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A */
0N/A
0N/A
0N/Apackage sun.lwawt;
0N/A
0N/Aimport java.awt.Dimension;
0N/Aimport java.awt.Point;
0N/Aimport java.awt.TextField;
0N/Aimport java.awt.event.ActionEvent;
0N/Aimport java.awt.event.ActionListener;
0N/Aimport java.awt.event.FocusEvent;
0N/Aimport java.awt.peer.TextFieldPeer;
0N/A
0N/Aimport javax.swing.*;
0N/Aimport javax.swing.text.JTextComponent;
0N/A
0N/Afinal class LWTextFieldPeer
0N/A extends LWTextComponentPeer<TextField, JPasswordField>
0N/A implements TextFieldPeer, ActionListener {
0N/A
0N/A private static final int DEFAULT_COLUMNS = 1;
0N/A
0N/A LWTextFieldPeer(final TextField target,
0N/A final PlatformComponent platformComponent) {
0N/A super(target, platformComponent);
0N/A }
0N/A
0N/A @Override
0N/A protected JPasswordField createDelegate() {
0N/A return new JPasswordFieldDelegate();
0N/A }
0N/A
0N/A @Override
0N/A void initializeImpl() {
0N/A super.initializeImpl();
0N/A setEchoChar(getTarget().getEchoChar());
0N/A synchronized (getDelegateLock()) {
0N/A getDelegate().addActionListener(this);
0N/A }
}
@Override
public JTextComponent getTextComponent() {
return getDelegate();
}
@Override
public void setEchoChar(final char echoChar) {
synchronized (getDelegateLock()) {
getDelegate().setEchoChar(echoChar);
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);
}
}
@Override
public Dimension getPreferredSize(final int columns) {
return getPreferredSize(1, columns);
}
@Override
public Dimension getMinimumSize(final int columns) {
return getPreferredSize(columns);
}
@Override
public Dimension getMinimumSize() {
return getMinimumSize(DEFAULT_COLUMNS);
}
@Override
public void actionPerformed(final ActionEvent e) {
postEvent(new ActionEvent(getTarget(), ActionEvent.ACTION_PERFORMED,
getText(), e.getWhen(), e.getModifiers()));
}
/**
* Restoring native behavior. We should sets the selection range to zero,
* when component lost its focus.
*
* @param e the focus event
*/
@Override
protected void handleJavaFocusEvent(final FocusEvent e) {
if (e.getID() == FocusEvent.FOCUS_LOST) {
// In order to de-select the selection
setCaretPosition(0);
}
super.handleJavaFocusEvent(e);
}
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.
JPasswordFieldDelegate() {
super();
}
@Override
public void replaceSelection(String content) {
getDocument().removeDocumentListener(LWTextFieldPeer.this);
super.replaceSelection(content);
// post only one text event in this case
postTextEvent();
getDocument().addDocumentListener(LWTextFieldPeer.this);
}
@Override
public boolean hasFocus() {
return getTarget().hasFocus();
}
@Override
public Point getLocationOnScreen() {
return LWTextFieldPeer.this.getLocationOnScreen();
}
}
}