1 /* 2 * Copyright (c) 2011, 2018, 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 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; 34 import java.awt.event.FocusEvent; 35 import java.awt.peer.TextFieldPeer; 36 37 import javax.swing.*; 38 import javax.swing.text.JTextComponent; 39 40 /** 41 * Lightweight implementation of {@link TextFieldPeer}. Delegates most of the 42 * work to the {@link JPasswordField}. 43 */ 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 54 JPasswordField createDelegate() { 55 return new JPasswordFieldDelegate(); 56 } 57 58 @Override 59 void initializeImpl() { 60 super.initializeImpl(); 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); 76 final boolean cutCopyAllowed; 77 final String focusInputMapKey; 78 if (echoChar != 0) { 79 cutCopyAllowed = false; 80 focusInputMapKey = "PasswordField.focusInputMap"; 81 } else { 82 cutCopyAllowed = true; 83 focusInputMapKey = "TextField.focusInputMap"; 84 } 85 getDelegate().putClientProperty("JPasswordField.cutCopyAllowed", cutCopyAllowed); 86 InputMap inputMap = (InputMap) UIManager.get(focusInputMapKey); 87 SwingUtilities.replaceUIInputMap(getDelegate(), JComponent.WHEN_FOCUSED, inputMap); 88 } 89 } 90 91 @Override 92 public Dimension getPreferredSize(final int columns) { 93 return getMinimumSize(columns); 94 } 95 96 @Override 97 public Dimension getMinimumSize(final int columns) { 98 return getMinimumSize(1, columns); 99 } 100 101 @Override 102 public void actionPerformed(final ActionEvent e) { 103 postEvent(new ActionEvent(getTarget(), ActionEvent.ACTION_PERFORMED, 104 getText(), e.getWhen(), e.getModifiers())); 105 } 106 107 /** 108 * Restoring native behavior. We should sets the selection range to zero, 109 * when component lost its focus. 110 * 111 * @param e the focus event 112 */ 113 @Override 114 void handleJavaFocusEvent(final FocusEvent e) { 115 if (e.getID() == FocusEvent.FOCUS_LOST) { 116 // In order to de-select the selection 117 setCaretPosition(0); 118 } 119 super.handleJavaFocusEvent(e); 120 } 121 122 @SuppressWarnings("serial")// Safe: outer class is non-serializable. 123 private final class JPasswordFieldDelegate extends JPasswordField { 124 125 @Override 126 public void replaceSelection(String content) { 127 getDocument().removeDocumentListener(LWTextFieldPeer.this); 128 super.replaceSelection(content); 129 // post only one text event in this case 130 postTextEvent(); 131 getDocument().addDocumentListener(LWTextFieldPeer.this); 132 } 133 134 @Override 135 public boolean hasFocus() { 136 return getTarget().hasFocus(); 137 } 138 139 @Override 140 public Point getLocationOnScreen() { 141 return LWTextFieldPeer.this.getLocationOnScreen(); 142 } 143 } 144 }