mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-06 14:00:55 +03:00
CheckBox and RadioButton:
- fixed styling of custom icon - fixed focus width (and preferred size) if using custom icon - added unit tests
This commit is contained in:
@@ -203,16 +203,12 @@ public class FlatRadioButtonUI
|
|||||||
protected Object applyStyleProperty( AbstractButton b, String key, Object value ) {
|
protected Object applyStyleProperty( AbstractButton b, String key, Object value ) {
|
||||||
// style icon
|
// style icon
|
||||||
if( key.startsWith( "icon." ) ) {
|
if( key.startsWith( "icon." ) ) {
|
||||||
Icon styleIcon = b.getIcon();
|
Icon icon = getRealIcon( b );
|
||||||
|
if( !(icon instanceof FlatCheckBoxIcon) )
|
||||||
if (styleIcon == null)
|
|
||||||
styleIcon = icon;
|
|
||||||
|
|
||||||
if( !(styleIcon instanceof FlatCheckBoxIcon) )
|
|
||||||
return new UnknownStyleException( key );
|
return new UnknownStyleException( key );
|
||||||
|
|
||||||
if( styleIcon == icon && iconShared ) {
|
if( icon == this.icon && iconShared ) {
|
||||||
icon = FlatStylingSupport.cloneIcon( icon );
|
this.icon = icon = FlatStylingSupport.cloneIcon( icon );
|
||||||
iconShared = false;
|
iconShared = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -230,6 +226,7 @@ public class FlatRadioButtonUI
|
|||||||
@Override
|
@Override
|
||||||
public Map<String, Class<?>> getStyleableInfos( JComponent c ) {
|
public Map<String, Class<?>> getStyleableInfos( JComponent c ) {
|
||||||
Map<String, Class<?>> infos = FlatStylingSupport.getAnnotatedStyleableInfos( this );
|
Map<String, Class<?>> infos = FlatStylingSupport.getAnnotatedStyleableInfos( this );
|
||||||
|
Icon icon = getRealIcon( c );
|
||||||
if( icon instanceof FlatCheckBoxIcon ) {
|
if( icon instanceof FlatCheckBoxIcon ) {
|
||||||
for( Map.Entry<String, Class<?>> e : ((FlatCheckBoxIcon)icon).getStyleableInfos().entrySet() )
|
for( Map.Entry<String, Class<?>> e : ((FlatCheckBoxIcon)icon).getStyleableInfos().entrySet() )
|
||||||
infos.put( "icon.".concat( e.getKey() ), e.getValue() );
|
infos.put( "icon.".concat( e.getKey() ), e.getValue() );
|
||||||
@@ -242,6 +239,7 @@ public class FlatRadioButtonUI
|
|||||||
public Object getStyleableValue( JComponent c, String key ) {
|
public Object getStyleableValue( JComponent c, String key ) {
|
||||||
// style icon
|
// style icon
|
||||||
if( key.startsWith( "icon." ) ) {
|
if( key.startsWith( "icon." ) ) {
|
||||||
|
Icon icon = getRealIcon( c );
|
||||||
return (icon instanceof FlatCheckBoxIcon)
|
return (icon instanceof FlatCheckBoxIcon)
|
||||||
? ((FlatCheckBoxIcon)icon).getStyleableValue( key.substring( "icon.".length() ) )
|
? ((FlatCheckBoxIcon)icon).getStyleableValue( key.substring( "icon.".length() ) )
|
||||||
: null;
|
: null;
|
||||||
@@ -337,16 +335,18 @@ public class FlatRadioButtonUI
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int getIconFocusWidth( JComponent c ) {
|
private int getIconFocusWidth( JComponent c ) {
|
||||||
AbstractButton b = (AbstractButton) c;
|
Icon icon = getRealIcon( c );
|
||||||
Icon icon = b.getIcon();
|
|
||||||
if( icon == null )
|
|
||||||
icon = getDefaultIcon();
|
|
||||||
|
|
||||||
return (icon instanceof FlatCheckBoxIcon)
|
return (icon instanceof FlatCheckBoxIcon)
|
||||||
? Math.round( UIScale.scale( ((FlatCheckBoxIcon)icon).getFocusWidth() ) )
|
? Math.round( UIScale.scale( ((FlatCheckBoxIcon)icon).getFocusWidth() ) )
|
||||||
: 0;
|
: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Icon getRealIcon( JComponent c ) {
|
||||||
|
AbstractButton b = (AbstractButton) c;
|
||||||
|
Icon icon = b.getIcon();
|
||||||
|
return (icon != null) ? icon : getDefaultIcon();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getBaseline( JComponent c, int width, int height ) {
|
public int getBaseline( JComponent c, int width, int height ) {
|
||||||
return FlatButtonUI.getBaselineImpl( c, width, height );
|
return FlatButtonUI.getBaselineImpl( c, width, height );
|
||||||
|
|||||||
@@ -30,6 +30,9 @@ import org.junit.jupiter.api.AfterAll;
|
|||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import com.formdev.flatlaf.icons.*;
|
import com.formdev.flatlaf.icons.*;
|
||||||
|
import com.formdev.flatlaf.ui.TestFlatStyling.CustomCheckBoxIcon;
|
||||||
|
import com.formdev.flatlaf.ui.TestFlatStyling.CustomIcon;
|
||||||
|
import com.formdev.flatlaf.ui.TestFlatStyling.CustomRadioButtonIcon;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Karl Tauber
|
* @author Karl Tauber
|
||||||
@@ -144,7 +147,12 @@ public class TestFlatStyleableInfo
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void checkBox() {
|
void checkBox() {
|
||||||
JCheckBox c = new JCheckBox();
|
checkBox( new JCheckBox() );
|
||||||
|
checkBox( new JCheckBox( new CustomIcon() ) );
|
||||||
|
checkBox( new JCheckBox( new CustomCheckBoxIcon() ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkBox( JCheckBox c ) {
|
||||||
FlatCheckBoxUI ui = (FlatCheckBoxUI) c.getUI();
|
FlatCheckBoxUI ui = (FlatCheckBoxUI) c.getUI();
|
||||||
|
|
||||||
assertTrue( ui.getDefaultIcon() instanceof FlatCheckBoxIcon );
|
assertTrue( ui.getDefaultIcon() instanceof FlatCheckBoxIcon );
|
||||||
@@ -153,6 +161,11 @@ public class TestFlatStyleableInfo
|
|||||||
Map<String, Class<?>> expected = new LinkedHashMap<>();
|
Map<String, Class<?>> expected = new LinkedHashMap<>();
|
||||||
radioButton( expected );
|
radioButton( expected );
|
||||||
|
|
||||||
|
// remove "icon." keys if check box has custom icon
|
||||||
|
Icon icon = c.getIcon();
|
||||||
|
if( icon != null && !(icon instanceof FlatCheckBoxIcon) )
|
||||||
|
expected.keySet().removeIf( key -> key.startsWith( "icon." ) );
|
||||||
|
|
||||||
assertMapEquals( expected, ui.getStyleableInfos( c ) );
|
assertMapEquals( expected, ui.getStyleableInfos( c ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -492,7 +505,12 @@ public class TestFlatStyleableInfo
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void radioButton() {
|
void radioButton() {
|
||||||
JRadioButton c = new JRadioButton();
|
radioButton( new JRadioButton() );
|
||||||
|
radioButton( new JRadioButton( new CustomIcon() ) );
|
||||||
|
radioButton( new JRadioButton( new CustomRadioButtonIcon() ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void radioButton( JRadioButton c ) {
|
||||||
FlatRadioButtonUI ui = (FlatRadioButtonUI) c.getUI();
|
FlatRadioButtonUI ui = (FlatRadioButtonUI) c.getUI();
|
||||||
|
|
||||||
assertTrue( ui.getDefaultIcon() instanceof FlatRadioButtonIcon );
|
assertTrue( ui.getDefaultIcon() instanceof FlatRadioButtonIcon );
|
||||||
@@ -504,6 +522,11 @@ public class TestFlatStyleableInfo
|
|||||||
"icon.centerDiameter", float.class
|
"icon.centerDiameter", float.class
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// remove "icon." keys if radio button has custom icon
|
||||||
|
Icon icon = c.getIcon();
|
||||||
|
if( icon != null && !(icon instanceof FlatRadioButtonIcon) )
|
||||||
|
expected.keySet().removeIf( key -> key.startsWith( "icon." ) );
|
||||||
|
|
||||||
assertMapEquals( expected, ui.getStyleableInfos( c ) );
|
assertMapEquals( expected, ui.getStyleableInfos( c ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -77,6 +77,9 @@ import com.formdev.flatlaf.icons.FlatRadioButtonMenuItemIcon;
|
|||||||
import com.formdev.flatlaf.icons.FlatSearchIcon;
|
import com.formdev.flatlaf.icons.FlatSearchIcon;
|
||||||
import com.formdev.flatlaf.icons.FlatSearchWithHistoryIcon;
|
import com.formdev.flatlaf.icons.FlatSearchWithHistoryIcon;
|
||||||
import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableUI;
|
import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableUI;
|
||||||
|
import com.formdev.flatlaf.ui.TestFlatStyling.CustomCheckBoxIcon;
|
||||||
|
import com.formdev.flatlaf.ui.TestFlatStyling.CustomIcon;
|
||||||
|
import com.formdev.flatlaf.ui.TestFlatStyling.CustomRadioButtonIcon;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Karl Tauber
|
* @author Karl Tauber
|
||||||
@@ -269,11 +272,20 @@ public class TestFlatStyleableValue
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void checkBox() {
|
void checkBox() {
|
||||||
JCheckBox c = new JCheckBox();
|
checkBox( new JCheckBox() );
|
||||||
|
checkBox( new JCheckBox( new CustomCheckBoxIcon() ) );
|
||||||
|
checkBox( new JCheckBox( new CustomIcon() ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkBox( JCheckBox c ) {
|
||||||
FlatCheckBoxUI ui = (FlatCheckBoxUI) c.getUI();
|
FlatCheckBoxUI ui = (FlatCheckBoxUI) c.getUI();
|
||||||
|
|
||||||
// FlatCheckBoxUI extends FlatRadioButtonUI
|
// FlatCheckBoxUI extends FlatRadioButtonUI
|
||||||
radioButton( ui, c );
|
radioButton( ui, c );
|
||||||
|
|
||||||
|
// necessary to clear FlatRadioButtonUI.oldStyleValues because
|
||||||
|
// ui.applyStyle(...) operates on shared instance
|
||||||
|
ui.uninstallUI( c );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -536,14 +548,24 @@ public class TestFlatStyleableValue
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void radioButton() {
|
void radioButton() {
|
||||||
JRadioButton c = new JRadioButton();
|
radioButton( new JRadioButton() );
|
||||||
|
radioButton( new JRadioButton( new CustomRadioButtonIcon() ) );
|
||||||
|
radioButton( new JRadioButton( new CustomIcon() ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void radioButton( JRadioButton c ) {
|
||||||
FlatRadioButtonUI ui = (FlatRadioButtonUI) c.getUI();
|
FlatRadioButtonUI ui = (FlatRadioButtonUI) c.getUI();
|
||||||
|
|
||||||
assertTrue( ui.getDefaultIcon() instanceof FlatRadioButtonIcon );
|
assertTrue( ui.getDefaultIcon() instanceof FlatRadioButtonIcon );
|
||||||
|
|
||||||
radioButton( ui, c );
|
radioButton( ui, c );
|
||||||
|
|
||||||
testFloat( c, ui, "icon.centerDiameter", 1.23f );
|
if( !(c.getIcon() instanceof CustomIcon) )
|
||||||
|
testFloat( c, ui, "icon.centerDiameter", 1.23f );
|
||||||
|
|
||||||
|
// necessary to clear FlatRadioButtonUI.oldStyleValues because
|
||||||
|
// ui.applyStyle(...) operates on shared instance
|
||||||
|
ui.uninstallUI( c );
|
||||||
}
|
}
|
||||||
|
|
||||||
private void radioButton( FlatRadioButtonUI ui, AbstractButton b ) {
|
private void radioButton( FlatRadioButtonUI ui, AbstractButton b ) {
|
||||||
@@ -551,6 +573,11 @@ public class TestFlatStyleableValue
|
|||||||
|
|
||||||
//---- icon ----
|
//---- icon ----
|
||||||
|
|
||||||
|
if( b.getIcon() instanceof CustomIcon ) {
|
||||||
|
assertEquals( null, ui.getStyleableValue( b, "icon.focusWidth" ) );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
testFloat( b, ui, "icon.focusWidth", 1.23f );
|
testFloat( b, ui, "icon.focusWidth", 1.23f );
|
||||||
testColor( b, ui, "icon.focusColor", 0x123456 );
|
testColor( b, ui, "icon.focusColor", 0x123456 );
|
||||||
testFloat( b, ui, "icon.borderWidth", 1.23f );
|
testFloat( b, ui, "icon.borderWidth", 1.23f );
|
||||||
|
|||||||
@@ -19,7 +19,9 @@ package com.formdev.flatlaf.ui;
|
|||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
|
import java.awt.Component;
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
|
import java.awt.Graphics;
|
||||||
import java.awt.Insets;
|
import java.awt.Insets;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -294,13 +296,22 @@ public class TestFlatStyling
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void checkBox() {
|
void checkBox() {
|
||||||
JCheckBox c = new JCheckBox();
|
checkBox( new JCheckBox() );
|
||||||
|
checkBox( new JCheckBox( new CustomIcon() ) );
|
||||||
|
checkBox( new JCheckBox( new CustomCheckBoxIcon() ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkBox( JCheckBox c ) {
|
||||||
FlatCheckBoxUI ui = (FlatCheckBoxUI) c.getUI();
|
FlatCheckBoxUI ui = (FlatCheckBoxUI) c.getUI();
|
||||||
|
|
||||||
assertTrue( ui.getDefaultIcon() instanceof FlatCheckBoxIcon );
|
assertTrue( ui.getDefaultIcon() instanceof FlatCheckBoxIcon );
|
||||||
|
|
||||||
// FlatCheckBoxUI extends FlatRadioButtonUI
|
// FlatCheckBoxUI extends FlatRadioButtonUI
|
||||||
radioButton( ui, c );
|
radioButton( ui, c );
|
||||||
|
|
||||||
|
// necessary to clear FlatRadioButtonUI.oldStyleValues because
|
||||||
|
// ui.applyStyle(...) operates on shared instance
|
||||||
|
ui.uninstallUI( c );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -651,14 +662,24 @@ public class TestFlatStyling
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void radioButton() {
|
void radioButton() {
|
||||||
JRadioButton c = new JRadioButton();
|
radioButton( new JRadioButton() );
|
||||||
|
radioButton( new JRadioButton( new CustomIcon() ) );
|
||||||
|
radioButton( new JRadioButton( new CustomRadioButtonIcon() ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void radioButton( JRadioButton c ) {
|
||||||
FlatRadioButtonUI ui = (FlatRadioButtonUI) c.getUI();
|
FlatRadioButtonUI ui = (FlatRadioButtonUI) c.getUI();
|
||||||
|
|
||||||
assertTrue( ui.getDefaultIcon() instanceof FlatRadioButtonIcon );
|
assertTrue( ui.getDefaultIcon() instanceof FlatRadioButtonIcon );
|
||||||
|
|
||||||
radioButton( ui, c );
|
radioButton( ui, c );
|
||||||
|
|
||||||
ui.applyStyle( c, "icon.centerDiameter: 8" );
|
if( !(c.getIcon() instanceof CustomIcon) )
|
||||||
|
ui.applyStyle( c, "icon.centerDiameter: 8" );
|
||||||
|
|
||||||
|
// necessary to clear FlatRadioButtonUI.oldStyleValues because
|
||||||
|
// ui.applyStyle(...) operates on shared instance
|
||||||
|
ui.uninstallUI( c );
|
||||||
}
|
}
|
||||||
|
|
||||||
private void radioButton( FlatRadioButtonUI ui, AbstractButton b ) {
|
private void radioButton( FlatRadioButtonUI ui, AbstractButton b ) {
|
||||||
@@ -676,6 +697,9 @@ public class TestFlatStyling
|
|||||||
|
|
||||||
//---- icon ----
|
//---- icon ----
|
||||||
|
|
||||||
|
if( b.getIcon() instanceof CustomIcon )
|
||||||
|
return;
|
||||||
|
|
||||||
ui.applyStyle( b, "icon.focusWidth: 1.5" );
|
ui.applyStyle( b, "icon.focusWidth: 1.5" );
|
||||||
ui.applyStyle( b, "icon.focusColor: #fff" );
|
ui.applyStyle( b, "icon.focusColor: #fff" );
|
||||||
ui.applyStyle( b, "icon.borderWidth: 1.5" );
|
ui.applyStyle( b, "icon.borderWidth: 1.5" );
|
||||||
@@ -1565,4 +1589,34 @@ public class TestFlatStyling
|
|||||||
UIManager.put( "test.enum", null );
|
UIManager.put( "test.enum", null );
|
||||||
assertEquals( SomeEnum.enumValue1, FlatUIUtils.getUIEnum( "test.enum", SomeEnum.class, SomeEnum.enumValue1 ) );
|
assertEquals( SomeEnum.enumValue1, FlatUIUtils.getUIEnum( "test.enum", SomeEnum.class, SomeEnum.enumValue1 ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//---- class CustomIcon ---------------------------------------------------
|
||||||
|
|
||||||
|
static class CustomIcon
|
||||||
|
implements Icon
|
||||||
|
{
|
||||||
|
@Override public void paintIcon( Component c, Graphics g, int x, int y ) {}
|
||||||
|
@Override public int getIconWidth() { return 1; }
|
||||||
|
@Override public int getIconHeight() { return 1; }
|
||||||
|
}
|
||||||
|
|
||||||
|
//---- class CustomCheckBoxIcon ----------------------------------------
|
||||||
|
|
||||||
|
static class CustomCheckBoxIcon
|
||||||
|
extends FlatCheckBoxIcon
|
||||||
|
{
|
||||||
|
CustomCheckBoxIcon() {
|
||||||
|
background = Color.green;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//---- class CustomRadioButtonIcon ----------------------------------------
|
||||||
|
|
||||||
|
static class CustomRadioButtonIcon
|
||||||
|
extends FlatRadioButtonIcon
|
||||||
|
{
|
||||||
|
CustomRadioButtonIcon() {
|
||||||
|
background = Color.green;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user