Path: blob/master/src/java.desktop/macosx/classes/sun/lwawt/LWTextFieldPeer.java
41153 views
/*1* Copyright (c) 2011, 2019, 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*/2425package sun.lwawt;2627import java.awt.Dimension;28import java.awt.Point;29import java.awt.TextField;30import java.awt.event.ActionEvent;31import java.awt.event.ActionListener;32import java.awt.event.FocusEvent;33import java.awt.peer.TextFieldPeer;3435import javax.swing.*;36import javax.swing.text.JTextComponent;3738import sun.awt.AWTAccessor;3940/**41* Lightweight implementation of {@link TextFieldPeer}. Delegates most of the42* work to the {@link JPasswordField}.43*/44final class LWTextFieldPeer45extends LWTextComponentPeer<TextField, JPasswordField>46implements TextFieldPeer, ActionListener {4748LWTextFieldPeer(final TextField target,49final PlatformComponent platformComponent) {50super(target, platformComponent);51}5253@Override54JPasswordField createDelegate() {55return new JPasswordFieldDelegate();56}5758@Override59void initializeImpl() {60super.initializeImpl();61setEchoChar(getTarget().getEchoChar());62synchronized (getDelegateLock()) {63getDelegate().addActionListener(this);64}65}6667@Override68public JTextComponent getTextComponent() {69return getDelegate();70}7172@Override73public void setEchoChar(final char echoChar) {74synchronized (getDelegateLock()) {75getDelegate().setEchoChar(echoChar);76final boolean cutCopyAllowed;77final String focusInputMapKey;78if (echoChar != 0) {79cutCopyAllowed = false;80focusInputMapKey = "PasswordField.focusInputMap";81} else {82cutCopyAllowed = true;83focusInputMapKey = "TextField.focusInputMap";84}85getDelegate().putClientProperty("JPasswordField.cutCopyAllowed", cutCopyAllowed);86InputMap inputMap = (InputMap) UIManager.get(focusInputMapKey);87SwingUtilities.replaceUIInputMap(getDelegate(), JComponent.WHEN_FOCUSED, inputMap);88}89}9091@Override92public Dimension getPreferredSize(final int columns) {93return getMinimumSize(columns);94}9596@Override97public Dimension getMinimumSize(final int columns) {98return getMinimumSize(1, columns);99}100101@Override102public void actionPerformed(final ActionEvent e) {103postEvent(new ActionEvent(getTarget(), ActionEvent.ACTION_PERFORMED,104getText(), e.getWhen(), e.getModifiers()));105}106107/**108* Restoring native behavior. We should sets the selection range to zero,109* when component lost its focus.110*111* @param e the focus event112*/113@Override114void handleJavaFocusEvent(final FocusEvent e) {115if (e.getID() == FocusEvent.FOCUS_LOST) {116// In order to de-select the selection117setCaretPosition(0);118}119super.handleJavaFocusEvent(e);120}121122@SuppressWarnings("serial")// Safe: outer class is non-serializable.123private final class JPasswordFieldDelegate extends JPasswordField {124125@Override126public void replaceSelection(String content) {127getDocument().removeDocumentListener(LWTextFieldPeer.this);128super.replaceSelection(content);129// post only one text event in this case130postTextEvent();131getDocument().addDocumentListener(LWTextFieldPeer.this);132}133134@Override135public boolean hasFocus() {136return getTarget().hasFocus();137}138139@Override140public Point getLocationOnScreen() {141return LWTextFieldPeer.this.getLocationOnScreen();142}143144@Override145public void setTransferHandler(final TransferHandler newHandler) {146// override the default implementation to avoid loading147// SystemFlavorMap and associated classes148Object key = AWTAccessor.getClientPropertyKeyAccessor()149.getJComponent_TRANSFER_HANDLER();150Object oldHandler = getClientProperty(key);151putClientProperty(key, newHandler);152firePropertyChange("transferHandler", oldHandler, newHandler);153}154}155}156157158