diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatLabelUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatLabelUI.java index ae629b55..5ed7fd49 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatLabelUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatLabelUI.java @@ -24,6 +24,7 @@ import java.awt.Rectangle; import java.beans.PropertyChangeEvent; import java.util.Arrays; import java.util.HashSet; +import java.util.Map; import java.util.Set; import javax.swing.Icon; import javax.swing.JComponent; @@ -33,7 +34,9 @@ import javax.swing.UIManager; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.basic.BasicHTML; import javax.swing.plaf.basic.BasicLabelUI; +import com.formdev.flatlaf.FlatClientProperties; import com.formdev.flatlaf.FlatLaf; +import com.formdev.flatlaf.ui.FlatStyleSupport.Styleable; import com.formdev.flatlaf.util.HiDPIUtils; import com.formdev.flatlaf.util.UIScale; @@ -55,12 +58,30 @@ import com.formdev.flatlaf.util.UIScale; public class FlatLabelUI extends BasicLabelUI { - private Color disabledForeground; + @Styleable protected Color disabledForeground; + private final boolean shared; private boolean defaults_initialized = false; + private Map oldStyleValues; public static ComponentUI createUI( JComponent c ) { - return FlatUIUtils.createSharedUI( FlatLabelUI.class, FlatLabelUI::new ); + return FlatUIUtils.canUseSharedUI( c ) + ? FlatUIUtils.createSharedUI( FlatLabelUI.class, () -> new FlatLabelUI( true ) ) + : new FlatLabelUI( false ); + } + + /** + * @since TODO + */ + protected FlatLabelUI( boolean shared ) { + this.shared = shared; + } + + @Override + public void installUI( JComponent c ) { + super.installUI( c ); + + applyStyle( FlatStyleSupport.getStyle( c ) ); } @Override @@ -77,7 +98,9 @@ public class FlatLabelUI @Override protected void uninstallDefaults( JLabel c ) { super.uninstallDefaults( c ); + defaults_initialized = false; + oldStyleValues = null; } @Override @@ -94,10 +117,38 @@ public class FlatLabelUI if( name == "text" || name == "font" || name == "foreground" ) { JLabel label = (JLabel) e.getSource(); updateHTMLRenderer( label, label.getText(), true ); - } else + } else if( name.equals( FlatClientProperties.STYLE ) ) + applyStyle( (JLabel) e.getSource(), this, e.getNewValue() ); + else super.propertyChange( e ); } + private static void applyStyle( JLabel c, FlatLabelUI ui, Object style ) { + // unshare component UI if necessary + if( style != null && ui.shared ) { + c.updateUI(); + ui = (FlatLabelUI) c.getUI(); + } + + ui.applyStyle( style ); + c.revalidate(); + c.repaint(); + } + + /** + * @since TODO + */ + protected void applyStyle( Object style ) { + oldStyleValues = FlatStyleSupport.parseAndApply( oldStyleValues, style, this::applyStyleProperty ); + } + + /** + * @since TODO + */ + protected Object applyStyleProperty( String key, Object value ) { + return FlatStyleSupport.applyToAnnotatedObject( this, key, value ); + } + /** * Checks whether text contains HTML tags that use "absolute-size" keywords * (e.g. "x-large") for font-size in default style sheet diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTitlePane.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTitlePane.java index 207b05b2..b7ca5baf 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTitlePane.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTitlePane.java @@ -877,6 +877,10 @@ debug*/ protected class FlatTitleLabelUI extends FlatLabelUI { + protected FlatTitleLabelUI() { + super( false ); + } + @Override protected void paintEnabledText( JLabel l, Graphics g, String s, int textX, int textY ) { boolean hasEmbeddedMenuBar = hasVisibleEmbeddedMenuBar( rootPane.getJMenuBar() ); diff --git a/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/FlatStylingTests.java b/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/FlatStylingTests.java index 4016852a..1cd771c6 100644 --- a/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/FlatStylingTests.java +++ b/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/FlatStylingTests.java @@ -173,6 +173,13 @@ public class FlatStylingTests textField( ui ); } + @Test + void label() { + FlatLabelUI ui = new FlatLabelUI( false ); + + ui.applyStyle( "disabledForeground: #fff" ); + } + @Test void list() { FlatListUI ui = new FlatListUI();