mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-08 06:50:56 +03:00
PasswordField: preserve reveal button state when switching theme (issue #173)
This commit is contained in:
@@ -38,6 +38,7 @@ import javax.swing.text.Element;
|
|||||||
import javax.swing.text.JTextComponent;
|
import javax.swing.text.JTextComponent;
|
||||||
import javax.swing.text.PasswordView;
|
import javax.swing.text.PasswordView;
|
||||||
import javax.swing.text.View;
|
import javax.swing.text.View;
|
||||||
|
import com.formdev.flatlaf.FlatClientProperties;
|
||||||
import com.formdev.flatlaf.icons.FlatCapsLockIcon;
|
import com.formdev.flatlaf.icons.FlatCapsLockIcon;
|
||||||
import com.formdev.flatlaf.ui.FlatStylingSupport.Styleable;
|
import com.formdev.flatlaf.ui.FlatStylingSupport.Styleable;
|
||||||
import com.formdev.flatlaf.util.UIScale;
|
import com.formdev.flatlaf.util.UIScale;
|
||||||
@@ -83,6 +84,9 @@ import com.formdev.flatlaf.util.UIScale;
|
|||||||
public class FlatPasswordFieldUI
|
public class FlatPasswordFieldUI
|
||||||
extends FlatTextFieldUI
|
extends FlatTextFieldUI
|
||||||
{
|
{
|
||||||
|
// used to preserve reveal button state when switching theme
|
||||||
|
private static final String KEY_REVEAL_SELECTED = "FlatLaf.internal.FlatPasswordFieldUI.revealSelected";
|
||||||
|
|
||||||
private Character echoChar;
|
private Character echoChar;
|
||||||
|
|
||||||
@Styleable protected boolean showCapsLock;
|
@Styleable protected boolean showCapsLock;
|
||||||
@@ -269,18 +273,16 @@ public class FlatPasswordFieldUI
|
|||||||
if( !showCapsLock )
|
if( !showCapsLock )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
JTextComponent c = getComponent();
|
return FlatUIUtils.isPermanentFocusOwner( getComponent() ) &&
|
||||||
return FlatUIUtils.isPermanentFocusOwner( c ) &&
|
|
||||||
Toolkit.getDefaultToolkit().getLockingKeyState( KeyEvent.VK_CAPS_LOCK );
|
Toolkit.getDefaultToolkit().getLockingKeyState( KeyEvent.VK_CAPS_LOCK );
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @since 2 */
|
/** @since 2 */
|
||||||
protected void installRevealButton() {
|
protected void installRevealButton() {
|
||||||
JTextComponent c = getComponent();
|
|
||||||
if( showRevealButton ) {
|
if( showRevealButton ) {
|
||||||
revealButton = createRevealButton();
|
revealButton = createRevealButton();
|
||||||
installLayout();
|
installLayout();
|
||||||
c.add( revealButton );
|
getComponent().add( revealButton );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -288,14 +290,24 @@ public class FlatPasswordFieldUI
|
|||||||
protected JToggleButton createRevealButton() {
|
protected JToggleButton createRevealButton() {
|
||||||
JToggleButton button = new JToggleButton( revealIcon );
|
JToggleButton button = new JToggleButton( revealIcon );
|
||||||
prepareLeadingOrTrailingComponent( button );
|
prepareLeadingOrTrailingComponent( button );
|
||||||
|
if( FlatClientProperties.clientPropertyBoolean( getComponent(), KEY_REVEAL_SELECTED, false ) ) {
|
||||||
|
button.setSelected( true );
|
||||||
|
updateEchoChar( true );
|
||||||
|
}
|
||||||
button.addActionListener( e -> {
|
button.addActionListener( e -> {
|
||||||
LookAndFeel.installProperty( getComponent(), "echoChar", button.isSelected()
|
boolean selected = button.isSelected();
|
||||||
? '\0'
|
updateEchoChar( selected );
|
||||||
: (echoChar != null ? echoChar : '*'));
|
getComponent().putClientProperty( KEY_REVEAL_SELECTED, selected );
|
||||||
} );
|
} );
|
||||||
return button;
|
return button;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void updateEchoChar( boolean selected ) {
|
||||||
|
LookAndFeel.installProperty( getComponent(), "echoChar", selected
|
||||||
|
? '\0'
|
||||||
|
: (echoChar != null ? echoChar : '*'));
|
||||||
|
}
|
||||||
|
|
||||||
/** @since 2 */
|
/** @since 2 */
|
||||||
protected void uninstallRevealButton() {
|
protected void uninstallRevealButton() {
|
||||||
if( revealButton != null ) {
|
if( revealButton != null ) {
|
||||||
|
|||||||
Reference in New Issue
Block a user