mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-09 16:25:10 +03:00
PasswordField: support "reveal" button to show password (issue #173)
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2021 FormDev Software GmbH
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.formdev.flatlaf.icons;
|
||||
|
||||
import java.awt.Component;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.geom.Area;
|
||||
import java.awt.geom.Ellipse2D;
|
||||
import java.awt.geom.Path2D;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
import javax.swing.UIManager;
|
||||
|
||||
/**
|
||||
* "eye" icon for {@link javax.swing.JPasswordField}.
|
||||
*
|
||||
* @uiDefault PasswordField.revealIconColor Color
|
||||
*
|
||||
* @author Karl Tauber
|
||||
* @since 2
|
||||
*/
|
||||
public class FlatRevealIcon
|
||||
extends FlatAbstractIcon
|
||||
{
|
||||
public FlatRevealIcon() {
|
||||
super( 16, 16, UIManager.getColor( "PasswordField.revealIconColor" ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintIcon( Component c, Graphics2D g ) {
|
||||
Path2D path = new Path2D.Float( Path2D.WIND_EVEN_ODD );
|
||||
path.append( new Ellipse2D.Float( 5.15f, 6.15f, 5.7f, 5.7f ), false );
|
||||
path.append( new Ellipse2D.Float( 6, 7, 4, 4 ), false );
|
||||
g.fill( path );
|
||||
|
||||
Path2D path2 = new Path2D.Float( Path2D.WIND_EVEN_ODD );
|
||||
path2.append( new Ellipse2D.Float( 2.15f, 4.15f, 11.7f, 11.7f ), false );
|
||||
path2.append( new Ellipse2D.Float( 3, 5, 10, 10 ), false );
|
||||
Area area = new Area( path2 );
|
||||
area.subtract( new Area( new Rectangle2D.Float( 0, 9.5f, 16, 16 ) ) );
|
||||
g.fill( area );
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,7 @@ import javax.swing.Action;
|
||||
import javax.swing.ActionMap;
|
||||
import javax.swing.Icon;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JToggleButton;
|
||||
import javax.swing.LookAndFeel;
|
||||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.UIManager;
|
||||
@@ -73,18 +74,25 @@ import com.formdev.flatlaf.util.UIScale;
|
||||
*
|
||||
* @uiDefault PasswordField.echoChar character
|
||||
* @uiDefault PasswordField.showCapsLock boolean
|
||||
* @uiDefault PasswordField.showRevealButton boolean
|
||||
* @uiDefault PasswordField.capsLockIcon Icon
|
||||
* @uiDefault PasswordField.revealIcon Icon
|
||||
*
|
||||
* @author Karl Tauber
|
||||
*/
|
||||
public class FlatPasswordFieldUI
|
||||
extends FlatTextFieldUI
|
||||
{
|
||||
private Character echoChar;
|
||||
|
||||
@Styleable protected boolean showCapsLock;
|
||||
/** @since 2 */ @Styleable protected boolean showRevealButton;
|
||||
protected Icon capsLockIcon;
|
||||
/** @since 2 */ protected Icon revealIcon;
|
||||
|
||||
private KeyListener capsLockListener;
|
||||
private boolean capsLockIconShared = true;
|
||||
private JToggleButton revealButton;
|
||||
|
||||
public static ComponentUI createUI( JComponent c ) {
|
||||
return new FlatPasswordFieldUI();
|
||||
@@ -95,17 +103,33 @@ public class FlatPasswordFieldUI
|
||||
return "PasswordField";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void installUI( JComponent c ) {
|
||||
super.installUI( c );
|
||||
|
||||
installRevealButton();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void uninstallUI( JComponent c ) {
|
||||
uninstallRevealButton();
|
||||
|
||||
super.uninstallUI( c );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void installDefaults() {
|
||||
super.installDefaults();
|
||||
|
||||
String prefix = getPropertyPrefix();
|
||||
Character echoChar = (Character) UIManager.get( prefix + ".echoChar" );
|
||||
echoChar = (Character) UIManager.get( prefix + ".echoChar" );
|
||||
if( echoChar != null )
|
||||
LookAndFeel.installProperty( getComponent(), "echoChar", echoChar );
|
||||
|
||||
showCapsLock = UIManager.getBoolean( "PasswordField.showCapsLock" );
|
||||
showRevealButton = UIManager.getBoolean( "PasswordField.showRevealButton" );
|
||||
capsLockIcon = UIManager.getIcon( "PasswordField.capsLockIcon" );
|
||||
revealIcon = UIManager.getIcon( "PasswordField.revealIcon" );
|
||||
capsLockIconShared = true;
|
||||
}
|
||||
|
||||
@@ -114,6 +138,7 @@ public class FlatPasswordFieldUI
|
||||
super.uninstallDefaults();
|
||||
|
||||
capsLockIcon = null;
|
||||
revealIcon = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -168,6 +193,18 @@ public class FlatPasswordFieldUI
|
||||
return "PasswordField";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void applyStyle( Object style ) {
|
||||
boolean oldShowRevealButton = showRevealButton;
|
||||
|
||||
super.applyStyle( style );
|
||||
|
||||
if( showRevealButton != oldShowRevealButton ) {
|
||||
uninstallRevealButton();
|
||||
installRevealButton();
|
||||
}
|
||||
}
|
||||
|
||||
/** @since 2 */
|
||||
@Override
|
||||
protected Object applyStyleProperty( String key, Object value ) {
|
||||
@@ -236,4 +273,39 @@ public class FlatPasswordFieldUI
|
||||
return FlatUIUtils.isPermanentFocusOwner( c ) &&
|
||||
Toolkit.getDefaultToolkit().getLockingKeyState( KeyEvent.VK_CAPS_LOCK );
|
||||
}
|
||||
|
||||
/** @since 2 */
|
||||
protected void installRevealButton() {
|
||||
JTextComponent c = getComponent();
|
||||
if( showRevealButton ) {
|
||||
revealButton = createRevealButton();
|
||||
installLayout();
|
||||
c.add( revealButton );
|
||||
}
|
||||
}
|
||||
|
||||
/** @since 2 */
|
||||
protected JToggleButton createRevealButton() {
|
||||
JToggleButton button = new JToggleButton( revealIcon );
|
||||
prepareLeadingOrTrailingComponent( button );
|
||||
button.addActionListener( e -> {
|
||||
LookAndFeel.installProperty( getComponent(), "echoChar", button.isSelected()
|
||||
? '\0'
|
||||
: (echoChar != null ? echoChar : '*'));
|
||||
} );
|
||||
return button;
|
||||
}
|
||||
|
||||
/** @since 2 */
|
||||
protected void uninstallRevealButton() {
|
||||
if( revealButton != null ) {
|
||||
getComponent().remove( revealButton );
|
||||
revealButton = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JComponent[] getTrailingComponents() {
|
||||
return new JComponent[] { trailingComponent, revealButton, clearButton };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -484,7 +484,10 @@ PasswordField.placeholderForeground = @disabledForeground
|
||||
PasswordField.iconTextGap = 4
|
||||
PasswordField.echoChar = \u2022
|
||||
PasswordField.showCapsLock = true
|
||||
PasswordField.showRevealButton = false
|
||||
PasswordField.capsLockIcon = com.formdev.flatlaf.icons.FlatCapsLockIcon
|
||||
PasswordField.revealIcon = com.formdev.flatlaf.icons.FlatRevealIcon
|
||||
PasswordField.revealIconColor = lazy(Actions.Grey)
|
||||
|
||||
|
||||
#---- Popup ----
|
||||
|
||||
@@ -398,7 +398,8 @@ public class TestFlatStyleableInfo
|
||||
|
||||
Map<String, Class<?>> expected = new LinkedHashMap<>();
|
||||
expectedMap( expected,
|
||||
"showCapsLock", boolean.class
|
||||
"showCapsLock", boolean.class,
|
||||
"showRevealButton", boolean.class
|
||||
);
|
||||
|
||||
// FlatPasswordFieldUI extends FlatTextFieldUI
|
||||
|
||||
@@ -551,6 +551,7 @@ public class TestFlatStyling
|
||||
textField( ui );
|
||||
|
||||
ui.applyStyle( "showCapsLock: true" );
|
||||
ui.applyStyle( "showRevealButton: true" );
|
||||
|
||||
// capsLockIcon
|
||||
ui.applyStyle( "capsLockIconColor: #fff" );
|
||||
|
||||
Reference in New Issue
Block a user