Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/classes/sun/lwawt/LWTextFieldPeer.java
38827 views
/*1* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526package sun.lwawt;2728import java.awt.Dimension;29import java.awt.Point;30import java.awt.TextField;31import java.awt.event.ActionEvent;32import java.awt.event.ActionListener;33import java.awt.event.FocusEvent;34import java.awt.peer.TextFieldPeer;3536import javax.swing.*;37import javax.swing.text.JTextComponent;3839/**40* Lightweight implementation of {@link TextFieldPeer}. Delegates most of the41* work to the {@link JPasswordField}.42*/43final class LWTextFieldPeer44extends LWTextComponentPeer<TextField, JPasswordField>45implements TextFieldPeer, ActionListener {4647LWTextFieldPeer(final TextField target,48final PlatformComponent platformComponent) {49super(target, platformComponent);50}5152@Override53JPasswordField createDelegate() {54return new JPasswordFieldDelegate();55}5657@Override58void initializeImpl() {59super.initializeImpl();60setEchoChar(getTarget().getEchoChar());61synchronized (getDelegateLock()) {62getDelegate().addActionListener(this);63}64}6566@Override67public JTextComponent getTextComponent() {68return getDelegate();69}7071@Override72public void setEchoChar(final char echoChar) {73synchronized (getDelegateLock()) {74getDelegate().setEchoChar(echoChar);75final boolean cutCopyAllowed;76final String focusInputMapKey;77if (echoChar != 0) {78cutCopyAllowed = false;79focusInputMapKey = "PasswordField.focusInputMap";80} else {81cutCopyAllowed = true;82focusInputMapKey = "TextField.focusInputMap";83}84getDelegate().putClientProperty("JPasswordField.cutCopyAllowed", cutCopyAllowed);85InputMap inputMap = (InputMap) UIManager.get(focusInputMapKey);86SwingUtilities.replaceUIInputMap(getDelegate(), JComponent.WHEN_FOCUSED, inputMap);87}88}8990@Override91public Dimension getPreferredSize(final int columns) {92return getMinimumSize(columns);93}9495@Override96public Dimension getMinimumSize(final int columns) {97return getMinimumSize(1, columns);98}99100@Override101public void actionPerformed(final ActionEvent e) {102postEvent(new ActionEvent(getTarget(), ActionEvent.ACTION_PERFORMED,103getText(), e.getWhen(), e.getModifiers()));104}105106/**107* Restoring native behavior. We should sets the selection range to zero,108* when component lost its focus.109*110* @param e the focus event111*/112@Override113void handleJavaFocusEvent(final FocusEvent e) {114if (e.getID() == FocusEvent.FOCUS_LOST) {115// In order to de-select the selection116setCaretPosition(0);117}118super.handleJavaFocusEvent(e);119}120121@SuppressWarnings("serial")// Safe: outer class is non-serializable.122private final class JPasswordFieldDelegate extends JPasswordField {123124// Empty non private constructor was added because access to this125// class shouldn't be emulated by a synthetic accessor method.126JPasswordFieldDelegate() {127super();128}129130@Override131public void replaceSelection(String content) {132getDocument().removeDocumentListener(LWTextFieldPeer.this);133super.replaceSelection(content);134// post only one text event in this case135postTextEvent();136getDocument().addDocumentListener(LWTextFieldPeer.this);137}138139@Override140public boolean hasFocus() {141return getTarget().hasFocus();142}143144@Override145public Point getLocationOnScreen() {146return LWTextFieldPeer.this.getLocationOnScreen();147}148}149}150151152