Styling: support CheckBox and RadioButton icons

This commit is contained in:
Karl Tauber
2021-06-16 22:31:56 +02:00
parent 7eb642dd13
commit a983edde1e
4 changed files with 169 additions and 28 deletions

View File

@@ -27,6 +27,8 @@ import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.UIManager;
import com.formdev.flatlaf.ui.FlatButtonUI;
import com.formdev.flatlaf.ui.FlatStyleSupport;
import com.formdev.flatlaf.ui.FlatStyleSupport.Styleable;
import com.formdev.flatlaf.ui.FlatUIUtils;
/**
@@ -67,39 +69,39 @@ public class FlatCheckBoxIcon
extends FlatAbstractIcon
{
protected final String style = UIManager.getString( "CheckBox.icon.style" );
public final int focusWidth = getUIInt( "CheckBox.icon.focusWidth",
@Styleable public int focusWidth = getUIInt( "CheckBox.icon.focusWidth",
UIManager.getInt( "Component.focusWidth" ), style );
protected final Color focusColor = FlatUIUtils.getUIColor( "CheckBox.icon.focusColor",
@Styleable protected Color focusColor = FlatUIUtils.getUIColor( "CheckBox.icon.focusColor",
UIManager.getColor( "Component.focusColor" ) );
protected final int arc = FlatUIUtils.getUIInt( "CheckBox.arc", 2 );
@Styleable protected int arc = FlatUIUtils.getUIInt( "CheckBox.arc", 2 );
// enabled
protected final Color borderColor = getUIColor( "CheckBox.icon.borderColor", style );
protected final Color background = getUIColor( "CheckBox.icon.background", style );
protected final Color selectedBorderColor = getUIColor( "CheckBox.icon.selectedBorderColor", style );
protected final Color selectedBackground = getUIColor( "CheckBox.icon.selectedBackground", style );
protected final Color checkmarkColor = getUIColor( "CheckBox.icon.checkmarkColor", style );
@Styleable protected Color borderColor = getUIColor( "CheckBox.icon.borderColor", style );
@Styleable protected Color background = getUIColor( "CheckBox.icon.background", style );
@Styleable protected Color selectedBorderColor = getUIColor( "CheckBox.icon.selectedBorderColor", style );
@Styleable protected Color selectedBackground = getUIColor( "CheckBox.icon.selectedBackground", style );
@Styleable protected Color checkmarkColor = getUIColor( "CheckBox.icon.checkmarkColor", style );
// disabled
protected final Color disabledBorderColor = getUIColor( "CheckBox.icon.disabledBorderColor", style );
protected final Color disabledBackground = getUIColor( "CheckBox.icon.disabledBackground", style );
protected final Color disabledCheckmarkColor = getUIColor( "CheckBox.icon.disabledCheckmarkColor", style );
@Styleable protected Color disabledBorderColor = getUIColor( "CheckBox.icon.disabledBorderColor", style );
@Styleable protected Color disabledBackground = getUIColor( "CheckBox.icon.disabledBackground", style );
@Styleable protected Color disabledCheckmarkColor = getUIColor( "CheckBox.icon.disabledCheckmarkColor", style );
// focused
protected final Color focusedBorderColor = getUIColor( "CheckBox.icon.focusedBorderColor", style );
protected final Color focusedBackground = getUIColor( "CheckBox.icon.focusedBackground", style );
protected final Color selectedFocusedBorderColor = getUIColor( "CheckBox.icon.selectedFocusedBorderColor", style );
protected final Color selectedFocusedBackground = getUIColor( "CheckBox.icon.selectedFocusedBackground", style );
protected final Color selectedFocusedCheckmarkColor = getUIColor( "CheckBox.icon.selectedFocusedCheckmarkColor", style );
@Styleable protected Color focusedBorderColor = getUIColor( "CheckBox.icon.focusedBorderColor", style );
@Styleable protected Color focusedBackground = getUIColor( "CheckBox.icon.focusedBackground", style );
@Styleable protected Color selectedFocusedBorderColor = getUIColor( "CheckBox.icon.selectedFocusedBorderColor", style );
@Styleable protected Color selectedFocusedBackground = getUIColor( "CheckBox.icon.selectedFocusedBackground", style );
@Styleable protected Color selectedFocusedCheckmarkColor = getUIColor( "CheckBox.icon.selectedFocusedCheckmarkColor", style );
// hover
protected final Color hoverBorderColor = getUIColor( "CheckBox.icon.hoverBorderColor", style );
protected final Color hoverBackground = getUIColor( "CheckBox.icon.hoverBackground", style );
protected final Color selectedHoverBackground = getUIColor( "CheckBox.icon.selectedHoverBackground", style );
@Styleable protected Color hoverBorderColor = getUIColor( "CheckBox.icon.hoverBorderColor", style );
@Styleable protected Color hoverBackground = getUIColor( "CheckBox.icon.hoverBackground", style );
@Styleable protected Color selectedHoverBackground = getUIColor( "CheckBox.icon.selectedHoverBackground", style );
// pressed
protected final Color pressedBackground = getUIColor( "CheckBox.icon.pressedBackground", style );
protected final Color selectedPressedBackground = getUIColor( "CheckBox.icon.selectedPressedBackground", style );
@Styleable protected Color pressedBackground = getUIColor( "CheckBox.icon.pressedBackground", style );
@Styleable protected Color selectedPressedBackground = getUIColor( "CheckBox.icon.selectedPressedBackground", style );
protected static Color getUIColor( String key, String style ) {
if( style != null ) {
@@ -129,6 +131,13 @@ public class FlatCheckBoxIcon
super( ICON_SIZE, ICON_SIZE, null );
}
/**
* @since TODO
*/
public Object applyStyleProperty( String key, Object value ) {
return FlatStyleSupport.applyToAnnotatedObject( this, key, value );
}
@Override
protected void paintIcon( Component c, Graphics2D g ) {
boolean indeterminate = isIndeterminate( c );

View File

@@ -19,6 +19,7 @@ package com.formdev.flatlaf.icons;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import com.formdev.flatlaf.ui.FlatStyleSupport.Styleable;
/**
* Icon for {@link javax.swing.JRadioButton}.
@@ -34,7 +35,7 @@ import java.awt.geom.Ellipse2D;
public class FlatRadioButtonIcon
extends FlatCheckBoxIcon
{
protected final int centerDiameter = getUIInt( "RadioButton.icon.centerDiameter", 8, style );
@Styleable protected int centerDiameter = getUIInt( "RadioButton.icon.centerDiameter", 8, style );
@Override
protected void paintFocusBorder( Component c, Graphics2D g ) {

View File

@@ -68,6 +68,7 @@ public class FlatRadioButtonUI
private Color defaultBackground;
private final boolean shared;
private boolean iconShared;
private boolean defaults_initialized = false;
private Map<String, Object> oldStyleValues;
@@ -82,6 +83,7 @@ public class FlatRadioButtonUI
*/
protected FlatRadioButtonUI( boolean shared ) {
this.shared = shared;
iconShared = true;
}
@Override
@@ -159,7 +161,24 @@ public class FlatRadioButtonUI
* @since TODO
*/
protected Object applyStyleProperty( String key, Object value ) {
//TODO style icon
// style icon
if( key.startsWith( "icon." ) ) {
key = key.substring( "icon.".length() );
if( !(icon instanceof FlatCheckBoxIcon) )
return null;
if( iconShared ) {
try {
icon = icon.getClass().getDeclaredConstructor().newInstance();
} catch( Exception ex ) {
throw new IllegalArgumentException( "failed to clone icon '" + icon.getClass().getName() + "'" );
}
iconShared = false;
}
return ((FlatCheckBoxIcon)icon).applyStyleProperty( key, value );
}
return FlatStyleSupport.applyToAnnotatedObject( this, key, value );
}