mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-11 06:27:13 -06:00
Styling: support styling disabledBackground and inactiveBackground of text components
This commit is contained in:
@@ -66,8 +66,14 @@ public class FlatEditorPaneUI
|
||||
{
|
||||
@Styleable protected int minimumWidth;
|
||||
protected boolean isIntelliJTheme;
|
||||
private Color background;
|
||||
@Styleable protected Color disabledBackground;
|
||||
@Styleable protected Color inactiveBackground;
|
||||
@Styleable protected Color focusedBackground;
|
||||
|
||||
private Color oldDisabledBackground;
|
||||
private Color oldInactiveBackground;
|
||||
|
||||
private Object oldHonorDisplayProperties;
|
||||
private FocusListener focusListener;
|
||||
private Map<String, Object> oldStyleValues;
|
||||
@@ -90,6 +96,9 @@ public class FlatEditorPaneUI
|
||||
String prefix = getPropertyPrefix();
|
||||
minimumWidth = UIManager.getInt( "Component.minimumWidth" );
|
||||
isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" );
|
||||
background = UIManager.getColor( prefix + ".background" );
|
||||
disabledBackground = UIManager.getColor( prefix + ".disabledBackground" );
|
||||
inactiveBackground = UIManager.getColor( prefix + ".inactiveBackground" );
|
||||
focusedBackground = UIManager.getColor( prefix + ".focusedBackground" );
|
||||
|
||||
// use component font and foreground for HTML text
|
||||
@@ -101,8 +110,14 @@ public class FlatEditorPaneUI
|
||||
protected void uninstallDefaults() {
|
||||
super.uninstallDefaults();
|
||||
|
||||
background = null;
|
||||
disabledBackground = null;
|
||||
inactiveBackground = null;
|
||||
focusedBackground = null;
|
||||
|
||||
oldDisabledBackground = null;
|
||||
oldInactiveBackground = null;
|
||||
|
||||
getComponent().putClientProperty( JEditorPane.HONOR_DISPLAY_PROPERTIES, oldHonorDisplayProperties );
|
||||
}
|
||||
|
||||
@@ -125,6 +140,11 @@ public class FlatEditorPaneUI
|
||||
|
||||
@Override
|
||||
protected void propertyChange( PropertyChangeEvent e ) {
|
||||
// invoke updateBackground() before super.propertyChange()
|
||||
String propertyName = e.getPropertyName();
|
||||
if( "editable".equals( propertyName ) || "enabled".equals( propertyName ) )
|
||||
updateBackground();
|
||||
|
||||
super.propertyChange( e );
|
||||
propertyChange( getComponent(), e, this::applyStyle );
|
||||
}
|
||||
@@ -147,7 +167,12 @@ public class FlatEditorPaneUI
|
||||
* @since TODO
|
||||
*/
|
||||
protected void applyStyle( Object style ) {
|
||||
oldDisabledBackground = disabledBackground;
|
||||
oldInactiveBackground = inactiveBackground;
|
||||
|
||||
oldStyleValues = FlatStyleSupport.parseAndApply( oldStyleValues, style, this::applyStyleProperty );
|
||||
|
||||
updateBackground();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,6 +182,12 @@ public class FlatEditorPaneUI
|
||||
return FlatStyleSupport.applyToAnnotatedObject( this, key, value );
|
||||
}
|
||||
|
||||
private void updateBackground() {
|
||||
FlatTextFieldUI.updateBackground( getComponent(), background,
|
||||
disabledBackground, inactiveBackground,
|
||||
oldDisabledBackground, oldInactiveBackground );
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dimension getPreferredSize( JComponent c ) {
|
||||
return applyMinimumWidth( c, super.getPreferredSize( c ), minimumWidth );
|
||||
|
||||
@@ -78,11 +78,17 @@ public class FlatPasswordFieldUI
|
||||
{
|
||||
@Styleable protected int minimumWidth;
|
||||
protected boolean isIntelliJTheme;
|
||||
private Color background;
|
||||
@Styleable protected Color disabledBackground;
|
||||
@Styleable protected Color inactiveBackground;
|
||||
@Styleable protected Color placeholderForeground;
|
||||
@Styleable protected Color focusedBackground;
|
||||
@Styleable protected boolean showCapsLock;
|
||||
protected Icon capsLockIcon;
|
||||
|
||||
private Color oldDisabledBackground;
|
||||
private Color oldInactiveBackground;
|
||||
|
||||
private FocusListener focusListener;
|
||||
private KeyListener capsLockListener;
|
||||
private Map<String, Object> oldStyleValues;
|
||||
@@ -107,6 +113,9 @@ public class FlatPasswordFieldUI
|
||||
String prefix = getPropertyPrefix();
|
||||
minimumWidth = UIManager.getInt( "Component.minimumWidth" );
|
||||
isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" );
|
||||
background = UIManager.getColor( prefix + ".background" );
|
||||
disabledBackground = UIManager.getColor( prefix + ".disabledBackground" );
|
||||
inactiveBackground = UIManager.getColor( prefix + ".inactiveBackground" );
|
||||
placeholderForeground = UIManager.getColor( prefix + ".placeholderForeground" );
|
||||
focusedBackground = UIManager.getColor( prefix + ".focusedBackground" );
|
||||
showCapsLock = UIManager.getBoolean( "PasswordField.showCapsLock" );
|
||||
@@ -121,10 +130,16 @@ public class FlatPasswordFieldUI
|
||||
protected void uninstallDefaults() {
|
||||
super.uninstallDefaults();
|
||||
|
||||
background = null;
|
||||
disabledBackground = null;
|
||||
inactiveBackground = null;
|
||||
placeholderForeground = null;
|
||||
focusedBackground = null;
|
||||
capsLockIcon = null;
|
||||
|
||||
oldDisabledBackground = null;
|
||||
oldInactiveBackground = null;
|
||||
|
||||
MigLayoutVisualPadding.uninstall( getComponent() );
|
||||
}
|
||||
|
||||
@@ -173,7 +188,11 @@ public class FlatPasswordFieldUI
|
||||
|
||||
@Override
|
||||
protected void propertyChange( PropertyChangeEvent e ) {
|
||||
super.propertyChange( e );
|
||||
String propertyName = e.getPropertyName();
|
||||
if( "editable".equals( propertyName ) || "enabled".equals( propertyName ) )
|
||||
updateBackground();
|
||||
else
|
||||
super.propertyChange( e );
|
||||
FlatTextFieldUI.propertyChange( getComponent(), e, this::applyStyle );
|
||||
}
|
||||
|
||||
@@ -181,7 +200,12 @@ public class FlatPasswordFieldUI
|
||||
* @since TODO
|
||||
*/
|
||||
protected void applyStyle( Object style ) {
|
||||
oldDisabledBackground = disabledBackground;
|
||||
oldInactiveBackground = inactiveBackground;
|
||||
|
||||
oldStyleValues = FlatStyleSupport.parseAndApply( oldStyleValues, style, this::applyStyleProperty );
|
||||
|
||||
updateBackground();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -217,6 +241,12 @@ public class FlatPasswordFieldUI
|
||||
}
|
||||
}
|
||||
|
||||
private void updateBackground() {
|
||||
FlatTextFieldUI.updateBackground( getComponent(), background,
|
||||
disabledBackground, inactiveBackground,
|
||||
oldDisabledBackground, oldInactiveBackground );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintSafely( Graphics g ) {
|
||||
FlatTextFieldUI.paintBackground( g, getComponent(), isIntelliJTheme, focusedBackground );
|
||||
|
||||
@@ -27,9 +27,7 @@ import javax.swing.JComponent;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.plaf.ComponentUI;
|
||||
import javax.swing.plaf.UIResource;
|
||||
import javax.swing.plaf.basic.BasicTextAreaUI;
|
||||
import javax.swing.text.JTextComponent;
|
||||
import com.formdev.flatlaf.ui.FlatStyleSupport.Styleable;
|
||||
import com.formdev.flatlaf.util.HiDPIUtils;
|
||||
|
||||
@@ -64,11 +62,14 @@ public class FlatTextAreaUI
|
||||
{
|
||||
@Styleable protected int minimumWidth;
|
||||
protected boolean isIntelliJTheme;
|
||||
protected Color background;
|
||||
private Color background;
|
||||
@Styleable protected Color disabledBackground;
|
||||
@Styleable protected Color inactiveBackground;
|
||||
@Styleable protected Color focusedBackground;
|
||||
|
||||
private Color oldDisabledBackground;
|
||||
private Color oldInactiveBackground;
|
||||
|
||||
private FocusListener focusListener;
|
||||
private Map<String, Object> oldStyleValues;
|
||||
|
||||
@@ -103,6 +104,9 @@ public class FlatTextAreaUI
|
||||
disabledBackground = null;
|
||||
inactiveBackground = null;
|
||||
focusedBackground = null;
|
||||
|
||||
oldDisabledBackground = null;
|
||||
oldInactiveBackground = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -124,22 +128,24 @@ public class FlatTextAreaUI
|
||||
|
||||
@Override
|
||||
protected void propertyChange( PropertyChangeEvent e ) {
|
||||
// invoke updateBackground() before super.propertyChange()
|
||||
String propertyName = e.getPropertyName();
|
||||
if( "editable".equals( propertyName ) || "enabled".equals( propertyName ) )
|
||||
updateBackground();
|
||||
|
||||
super.propertyChange( e );
|
||||
FlatEditorPaneUI.propertyChange( getComponent(), e, this::applyStyle );
|
||||
|
||||
switch( e.getPropertyName() ) {
|
||||
case "editable":
|
||||
case "enabled":
|
||||
updateBackground();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @since TODO
|
||||
*/
|
||||
protected void applyStyle( Object style ) {
|
||||
oldDisabledBackground = disabledBackground;
|
||||
oldInactiveBackground = inactiveBackground;
|
||||
|
||||
oldStyleValues = FlatStyleSupport.parseAndApply( oldStyleValues, style, this::applyStyleProperty );
|
||||
|
||||
updateBackground();
|
||||
}
|
||||
|
||||
@@ -151,26 +157,9 @@ public class FlatTextAreaUI
|
||||
}
|
||||
|
||||
private void updateBackground() {
|
||||
JTextComponent c = getComponent();
|
||||
|
||||
Color background = c.getBackground();
|
||||
if( !(background instanceof UIResource) )
|
||||
return;
|
||||
|
||||
// do not update background if it currently has a unknown color (assigned from outside)
|
||||
if( background != this.background &&
|
||||
background != disabledBackground &&
|
||||
background != inactiveBackground )
|
||||
return;
|
||||
|
||||
Color newBackground = !c.isEnabled()
|
||||
? disabledBackground
|
||||
: (!c.isEditable()
|
||||
? inactiveBackground
|
||||
: this.background);
|
||||
|
||||
if( newBackground != background )
|
||||
c.setBackground( newBackground );
|
||||
FlatTextFieldUI.updateBackground( getComponent(), background,
|
||||
disabledBackground, inactiveBackground,
|
||||
oldDisabledBackground, oldInactiveBackground );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -80,9 +80,15 @@ public class FlatTextFieldUI
|
||||
{
|
||||
@Styleable protected int minimumWidth;
|
||||
protected boolean isIntelliJTheme;
|
||||
private Color background;
|
||||
@Styleable protected Color disabledBackground;
|
||||
@Styleable protected Color inactiveBackground;
|
||||
@Styleable protected Color placeholderForeground;
|
||||
@Styleable protected Color focusedBackground;
|
||||
|
||||
private Color oldDisabledBackground;
|
||||
private Color oldInactiveBackground;
|
||||
|
||||
private FocusListener focusListener;
|
||||
private Map<String, Object> oldStyleValues;
|
||||
private boolean borderShared = true;
|
||||
@@ -105,6 +111,9 @@ public class FlatTextFieldUI
|
||||
String prefix = getPropertyPrefix();
|
||||
minimumWidth = UIManager.getInt( "Component.minimumWidth" );
|
||||
isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" );
|
||||
background = UIManager.getColor( prefix + ".background" );
|
||||
disabledBackground = UIManager.getColor( prefix + ".disabledBackground" );
|
||||
inactiveBackground = UIManager.getColor( prefix + ".inactiveBackground" );
|
||||
placeholderForeground = UIManager.getColor( prefix + ".placeholderForeground" );
|
||||
focusedBackground = UIManager.getColor( prefix + ".focusedBackground" );
|
||||
|
||||
@@ -117,9 +126,15 @@ public class FlatTextFieldUI
|
||||
protected void uninstallDefaults() {
|
||||
super.uninstallDefaults();
|
||||
|
||||
background = null;
|
||||
disabledBackground = null;
|
||||
inactiveBackground = null;
|
||||
placeholderForeground = null;
|
||||
focusedBackground = null;
|
||||
|
||||
oldDisabledBackground = null;
|
||||
oldInactiveBackground = null;
|
||||
|
||||
MigLayoutVisualPadding.uninstall( getComponent() );
|
||||
}
|
||||
|
||||
@@ -148,7 +163,11 @@ public class FlatTextFieldUI
|
||||
|
||||
@Override
|
||||
protected void propertyChange( PropertyChangeEvent e ) {
|
||||
super.propertyChange( e );
|
||||
String propertyName = e.getPropertyName();
|
||||
if( "editable".equals( propertyName ) || "enabled".equals( propertyName ) )
|
||||
updateBackground();
|
||||
else
|
||||
super.propertyChange( e );
|
||||
propertyChange( getComponent(), e, this::applyStyle );
|
||||
}
|
||||
|
||||
@@ -175,7 +194,12 @@ public class FlatTextFieldUI
|
||||
* @since TODO
|
||||
*/
|
||||
protected void applyStyle( Object style ) {
|
||||
oldDisabledBackground = disabledBackground;
|
||||
oldInactiveBackground = inactiveBackground;
|
||||
|
||||
oldStyleValues = FlatStyleSupport.parseAndApply( oldStyleValues, style, this::applyStyleProperty );
|
||||
|
||||
updateBackground();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -203,6 +227,39 @@ public class FlatTextFieldUI
|
||||
}
|
||||
}
|
||||
|
||||
private void updateBackground() {
|
||||
updateBackground( getComponent(), background,
|
||||
disabledBackground, inactiveBackground,
|
||||
oldDisabledBackground, oldInactiveBackground );
|
||||
}
|
||||
|
||||
// same functionality as BasicTextUI.updateBackground()
|
||||
static void updateBackground( JTextComponent c, Color background,
|
||||
Color disabledBackground, Color inactiveBackground,
|
||||
Color oldDisabledBackground, Color oldInactiveBackground )
|
||||
{
|
||||
Color oldBackground = c.getBackground();
|
||||
if( !(oldBackground instanceof UIResource) )
|
||||
return;
|
||||
|
||||
// do not update background if it currently has a unknown color (assigned from outside)
|
||||
if( oldBackground != background &&
|
||||
oldBackground != disabledBackground &&
|
||||
oldBackground != inactiveBackground &&
|
||||
oldBackground != oldDisabledBackground &&
|
||||
oldBackground != oldInactiveBackground )
|
||||
return;
|
||||
|
||||
Color newBackground = !c.isEnabled()
|
||||
? disabledBackground
|
||||
: (!c.isEditable()
|
||||
? inactiveBackground
|
||||
: background);
|
||||
|
||||
if( newBackground != oldBackground )
|
||||
c.setBackground( newBackground );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintSafely( Graphics g ) {
|
||||
paintBackground( g, getComponent(), isIntelliJTheme, focusedBackground );
|
||||
|
||||
@@ -62,8 +62,14 @@ public class FlatTextPaneUI
|
||||
{
|
||||
@Styleable protected int minimumWidth;
|
||||
protected boolean isIntelliJTheme;
|
||||
private Color background;
|
||||
@Styleable protected Color disabledBackground;
|
||||
@Styleable protected Color inactiveBackground;
|
||||
@Styleable protected Color focusedBackground;
|
||||
|
||||
private Color oldDisabledBackground;
|
||||
private Color oldInactiveBackground;
|
||||
|
||||
private Object oldHonorDisplayProperties;
|
||||
private FocusListener focusListener;
|
||||
private Map<String, Object> oldStyleValues;
|
||||
@@ -86,6 +92,9 @@ public class FlatTextPaneUI
|
||||
String prefix = getPropertyPrefix();
|
||||
minimumWidth = UIManager.getInt( "Component.minimumWidth" );
|
||||
isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" );
|
||||
background = UIManager.getColor( prefix + ".background" );
|
||||
disabledBackground = UIManager.getColor( prefix + ".disabledBackground" );
|
||||
inactiveBackground = UIManager.getColor( prefix + ".inactiveBackground" );
|
||||
focusedBackground = UIManager.getColor( prefix + ".focusedBackground" );
|
||||
|
||||
// use component font and foreground for HTML text
|
||||
@@ -97,8 +106,14 @@ public class FlatTextPaneUI
|
||||
protected void uninstallDefaults() {
|
||||
super.uninstallDefaults();
|
||||
|
||||
background = null;
|
||||
disabledBackground = null;
|
||||
inactiveBackground = null;
|
||||
focusedBackground = null;
|
||||
|
||||
oldDisabledBackground = null;
|
||||
oldInactiveBackground = null;
|
||||
|
||||
getComponent().putClientProperty( JEditorPane.HONOR_DISPLAY_PROPERTIES, oldHonorDisplayProperties );
|
||||
}
|
||||
|
||||
@@ -121,6 +136,11 @@ public class FlatTextPaneUI
|
||||
|
||||
@Override
|
||||
protected void propertyChange( PropertyChangeEvent e ) {
|
||||
// invoke updateBackground() before super.propertyChange()
|
||||
String propertyName = e.getPropertyName();
|
||||
if( "editable".equals( propertyName ) || "enabled".equals( propertyName ) )
|
||||
updateBackground();
|
||||
|
||||
super.propertyChange( e );
|
||||
FlatEditorPaneUI.propertyChange( getComponent(), e, this::applyStyle );
|
||||
}
|
||||
@@ -129,7 +149,12 @@ public class FlatTextPaneUI
|
||||
* @since TODO
|
||||
*/
|
||||
protected void applyStyle( Object style ) {
|
||||
oldDisabledBackground = disabledBackground;
|
||||
oldInactiveBackground = inactiveBackground;
|
||||
|
||||
oldStyleValues = FlatStyleSupport.parseAndApply( oldStyleValues, style, this::applyStyleProperty );
|
||||
|
||||
updateBackground();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -139,6 +164,12 @@ public class FlatTextPaneUI
|
||||
return FlatStyleSupport.applyToAnnotatedObject( this, key, value );
|
||||
}
|
||||
|
||||
private void updateBackground() {
|
||||
FlatTextFieldUI.updateBackground( getComponent(), background,
|
||||
disabledBackground, inactiveBackground,
|
||||
oldDisabledBackground, oldInactiveBackground );
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dimension getPreferredSize( JComponent c ) {
|
||||
return FlatEditorPaneUI.applyMinimumWidth( c, super.getPreferredSize( c ), minimumWidth );
|
||||
|
||||
@@ -23,12 +23,14 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import javax.swing.JCheckBox;
|
||||
import javax.swing.JEditorPane;
|
||||
import javax.swing.JFormattedTextField;
|
||||
import javax.swing.JPasswordField;
|
||||
import javax.swing.JRadioButton;
|
||||
import javax.swing.JSplitPane;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.JTextPane;
|
||||
import javax.swing.UIManager;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import com.formdev.flatlaf.icons.FlatCapsLockIcon;
|
||||
@@ -85,7 +87,12 @@ public class FlatStylingTests
|
||||
void editorPane() {
|
||||
FlatEditorPaneUI ui = new FlatEditorPaneUI();
|
||||
|
||||
// for FlatEditorPaneUI.updateBackground()
|
||||
ui.installUI( new JEditorPane() );
|
||||
|
||||
ui.applyStyle( "minimumWidth: 100" );
|
||||
ui.applyStyle( "disabledBackground: #fff" );
|
||||
ui.applyStyle( "inactiveBackground: #fff" );
|
||||
ui.applyStyle( "focusedBackground: #fff" );
|
||||
}
|
||||
|
||||
@@ -111,6 +118,8 @@ public class FlatStylingTests
|
||||
ui.installUI( new JPasswordField() );
|
||||
|
||||
ui.applyStyle( "minimumWidth: 100" );
|
||||
ui.applyStyle( "disabledBackground: #fff" );
|
||||
ui.applyStyle( "inactiveBackground: #fff" );
|
||||
ui.applyStyle( "placeholderForeground: #fff" );
|
||||
ui.applyStyle( "focusedBackground: #fff" );
|
||||
ui.applyStyle( "showCapsLock: true" );
|
||||
@@ -279,6 +288,7 @@ public class FlatStylingTests
|
||||
void textArea() {
|
||||
FlatTextAreaUI ui = new FlatTextAreaUI();
|
||||
|
||||
// for FlatEditorPaneUI.updateBackground()
|
||||
ui.installUI( new JTextArea() );
|
||||
|
||||
ui.applyStyle( "minimumWidth: 100" );
|
||||
@@ -300,6 +310,8 @@ public class FlatStylingTests
|
||||
|
||||
private void textField( FlatTextFieldUI ui ) {
|
||||
ui.applyStyle( "minimumWidth: 100" );
|
||||
ui.applyStyle( "disabledBackground: #fff" );
|
||||
ui.applyStyle( "inactiveBackground: #fff" );
|
||||
ui.applyStyle( "placeholderForeground: #fff" );
|
||||
ui.applyStyle( "focusedBackground: #fff" );
|
||||
|
||||
@@ -311,7 +323,12 @@ public class FlatStylingTests
|
||||
void textPane() {
|
||||
FlatTextPaneUI ui = new FlatTextPaneUI();
|
||||
|
||||
// for FlatEditorPaneUI.updateBackground()
|
||||
ui.installUI( new JTextPane() );
|
||||
|
||||
ui.applyStyle( "minimumWidth: 100" );
|
||||
ui.applyStyle( "disabledBackground: #fff" );
|
||||
ui.applyStyle( "inactiveBackground: #fff" );
|
||||
ui.applyStyle( "focusedBackground: #fff" );
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
/*
|
||||
* 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.ui;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static com.formdev.flatlaf.FlatClientProperties.COMPONENT_STYLE;
|
||||
import java.util.function.Supplier;
|
||||
import javax.swing.JEditorPane;
|
||||
import javax.swing.JFormattedTextField;
|
||||
import javax.swing.JPasswordField;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.JTextPane;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.plaf.ColorUIResource;
|
||||
import javax.swing.plaf.basic.BasicTextFieldUI;
|
||||
import javax.swing.text.JTextComponent;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author Karl Tauber
|
||||
*/
|
||||
public class FlatTextComponentTests
|
||||
{
|
||||
@Test
|
||||
void editorPane_updateBackground() {
|
||||
textComponent_updateBackground( "EditorPane", () -> {
|
||||
JEditorPane c = new JEditorPane();
|
||||
c.setUI( new FlatEditorPaneUI() );
|
||||
return c;
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
void formattedTextField_updateBackground() {
|
||||
textComponent_updateBackground( "FormattedTextField", () -> {
|
||||
JFormattedTextField c = new JFormattedTextField();
|
||||
c.setUI( new FlatFormattedTextFieldUI() );
|
||||
return c;
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
void passwordField_updateBackground() {
|
||||
textComponent_updateBackground( "PasswordField", () -> {
|
||||
JPasswordField c = new JPasswordField();
|
||||
c.setUI( new FlatPasswordFieldUI() );
|
||||
return c;
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
void textArea_updateBackground() {
|
||||
textComponent_updateBackground( "TextArea", () -> {
|
||||
JTextArea c = new JTextArea();
|
||||
c.setUI( new FlatTextAreaUI() );
|
||||
return c;
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
void textField_updateBackground() {
|
||||
textComponent_updateBackground( "TextField", () -> {
|
||||
JTextField c = new JTextField();
|
||||
c.setUI( new FlatTextFieldUI() );
|
||||
return c;
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
void textPane_updateBackground() {
|
||||
textComponent_updateBackground( "TextPane", () -> {
|
||||
JTextPane c = new JTextPane();
|
||||
c.setUI( new FlatTextPaneUI() );
|
||||
return c;
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
void basicTextField_updateBackground() {
|
||||
textComponent_updateBackground( "TextField", () -> {
|
||||
JTextField c = new JTextField();
|
||||
c.setUI( new BasicTextFieldUI() );
|
||||
return c;
|
||||
} );
|
||||
}
|
||||
|
||||
private void textComponent_updateBackground( String prefix, Supplier<JTextComponent> createTextComponent ) {
|
||||
ColorUIResource background = new ColorUIResource( 0xff0000 );
|
||||
ColorUIResource inactiveBackground = new ColorUIResource( 0x00ff00 );
|
||||
ColorUIResource disabledBackground = new ColorUIResource( 0x0000ff );
|
||||
|
||||
UIManager.put( prefix + ".background", background );
|
||||
UIManager.put( prefix + ".inactiveBackground", inactiveBackground );
|
||||
UIManager.put( prefix + ".disabledBackground", disabledBackground );
|
||||
|
||||
JTextComponent c = createTextComponent.get();
|
||||
|
||||
// without styling
|
||||
assertEquals( background, c.getBackground() );
|
||||
c.setEditable( false ); assertEquals( inactiveBackground, c.getBackground() );
|
||||
c.setEnabled( false ); assertEquals( disabledBackground, c.getBackground() );
|
||||
c.setEditable( true ); assertEquals( disabledBackground, c.getBackground() );
|
||||
c.setEnabled( true ); assertEquals( background, c.getBackground() );
|
||||
|
||||
|
||||
if( !c.getUI().getClass().getSimpleName().startsWith( "Flat" ) )
|
||||
return;
|
||||
|
||||
|
||||
// with styling
|
||||
|
||||
ColorUIResource inactiveBackground1 = new ColorUIResource( 0x00ee00 );
|
||||
ColorUIResource disabledBackground1 = new ColorUIResource( 0x0000ee );
|
||||
ColorUIResource inactiveBackground2 = new ColorUIResource( 0x00dd00 );
|
||||
ColorUIResource disabledBackground2 = new ColorUIResource( 0x0000dd );
|
||||
String style1 = "inactiveBackground: #00ee00; disabledBackground: #0000ee";
|
||||
String style2 = "inactiveBackground: #00dd00; disabledBackground: #0000dd";
|
||||
|
||||
c.putClientProperty( COMPONENT_STYLE, style1 );
|
||||
|
||||
assertEquals( background, c.getBackground() );
|
||||
c.setEditable( false ); assertEquals( inactiveBackground1, c.getBackground() );
|
||||
c.setEnabled( false ); assertEquals( disabledBackground1, c.getBackground() );
|
||||
c.setEditable( true ); assertEquals( disabledBackground1, c.getBackground() );
|
||||
c.setEnabled( true ); assertEquals( background, c.getBackground() );
|
||||
|
||||
c.putClientProperty( COMPONENT_STYLE, null );
|
||||
assertEquals( background, c.getBackground() );
|
||||
|
||||
c.setEditable( false );
|
||||
c.putClientProperty( COMPONENT_STYLE, style1 );
|
||||
assertEquals( inactiveBackground1, c.getBackground() );
|
||||
|
||||
c.putClientProperty( COMPONENT_STYLE, null );
|
||||
assertEquals( inactiveBackground, c.getBackground() );
|
||||
|
||||
c.setEnabled( false );
|
||||
c.putClientProperty( COMPONENT_STYLE, style1 );
|
||||
assertEquals( disabledBackground1, c.getBackground() );
|
||||
|
||||
|
||||
// change from style1 to style2
|
||||
c.putClientProperty( COMPONENT_STYLE, style2 );
|
||||
assertEquals( disabledBackground2, c.getBackground() );
|
||||
|
||||
c.setEnabled( true );
|
||||
assertEquals( inactiveBackground2, c.getBackground() );
|
||||
|
||||
|
||||
// remove style
|
||||
c.putClientProperty( COMPONENT_STYLE, null );
|
||||
assertEquals( inactiveBackground, c.getBackground() );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user