Styling: support Label

This commit is contained in:
Karl Tauber
2021-06-24 14:02:32 +02:00
parent 4e7b0d11d0
commit 551f5fc929
3 changed files with 65 additions and 3 deletions

View File

@@ -24,6 +24,7 @@ import java.awt.Rectangle;
import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeEvent;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.swing.Icon; import javax.swing.Icon;
import javax.swing.JComponent; import javax.swing.JComponent;
@@ -33,7 +34,9 @@ import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI; import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicHTML; import javax.swing.plaf.basic.BasicHTML;
import javax.swing.plaf.basic.BasicLabelUI; import javax.swing.plaf.basic.BasicLabelUI;
import com.formdev.flatlaf.FlatClientProperties;
import com.formdev.flatlaf.FlatLaf; import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.ui.FlatStyleSupport.Styleable;
import com.formdev.flatlaf.util.HiDPIUtils; import com.formdev.flatlaf.util.HiDPIUtils;
import com.formdev.flatlaf.util.UIScale; import com.formdev.flatlaf.util.UIScale;
@@ -55,12 +58,30 @@ import com.formdev.flatlaf.util.UIScale;
public class FlatLabelUI public class FlatLabelUI
extends BasicLabelUI extends BasicLabelUI
{ {
private Color disabledForeground; @Styleable protected Color disabledForeground;
private final boolean shared;
private boolean defaults_initialized = false; private boolean defaults_initialized = false;
private Map<String, Object> oldStyleValues;
public static ComponentUI createUI( JComponent c ) { 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 @Override
@@ -77,7 +98,9 @@ public class FlatLabelUI
@Override @Override
protected void uninstallDefaults( JLabel c ) { protected void uninstallDefaults( JLabel c ) {
super.uninstallDefaults( c ); super.uninstallDefaults( c );
defaults_initialized = false; defaults_initialized = false;
oldStyleValues = null;
} }
@Override @Override
@@ -94,10 +117,38 @@ public class FlatLabelUI
if( name == "text" || name == "font" || name == "foreground" ) { if( name == "text" || name == "font" || name == "foreground" ) {
JLabel label = (JLabel) e.getSource(); JLabel label = (JLabel) e.getSource();
updateHTMLRenderer( label, label.getText(), true ); updateHTMLRenderer( label, label.getText(), true );
} else } else if( name.equals( FlatClientProperties.STYLE ) )
applyStyle( (JLabel) e.getSource(), this, e.getNewValue() );
else
super.propertyChange( e ); 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 * Checks whether text contains HTML tags that use "absolute-size" keywords
* (e.g. "x-large") for font-size in default style sheet * (e.g. "x-large") for font-size in default style sheet

View File

@@ -877,6 +877,10 @@ debug*/
protected class FlatTitleLabelUI protected class FlatTitleLabelUI
extends FlatLabelUI extends FlatLabelUI
{ {
protected FlatTitleLabelUI() {
super( false );
}
@Override @Override
protected void paintEnabledText( JLabel l, Graphics g, String s, int textX, int textY ) { protected void paintEnabledText( JLabel l, Graphics g, String s, int textX, int textY ) {
boolean hasEmbeddedMenuBar = hasVisibleEmbeddedMenuBar( rootPane.getJMenuBar() ); boolean hasEmbeddedMenuBar = hasVisibleEmbeddedMenuBar( rootPane.getJMenuBar() );

View File

@@ -173,6 +173,13 @@ public class FlatStylingTests
textField( ui ); textField( ui );
} }
@Test
void label() {
FlatLabelUI ui = new FlatLabelUI( false );
ui.applyStyle( "disabledForeground: #fff" );
}
@Test @Test
void list() { void list() {
FlatListUI ui = new FlatListUI(); FlatListUI ui = new FlatListUI();