mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-06 14:00:55 +03:00
Styling: support enums
This commit is contained in:
@@ -414,10 +414,10 @@ public class FlatStylingSupport
|
||||
|
||||
// get old value and set new value
|
||||
Object oldValue = f.get( obj );
|
||||
f.set( obj, value );
|
||||
f.set( obj, convertToEnum( value, f.getType() ) );
|
||||
return oldValue;
|
||||
} catch( IllegalAccessException ex ) {
|
||||
throw new IllegalArgumentException( "failed to access field '" + cls.getName() + "." + fieldName + "'" );
|
||||
throw new IllegalArgumentException( "failed to access field '" + cls.getName() + "." + fieldName + "'", ex );
|
||||
}
|
||||
}
|
||||
} catch( NoSuchFieldException ex ) {
|
||||
@@ -470,7 +470,7 @@ public class FlatStylingSupport
|
||||
}
|
||||
Method setter = cls.getMethod( setterName, getter.getReturnType() );
|
||||
Object oldValue = getter.invoke( obj );
|
||||
setter.invoke( obj, value );
|
||||
setter.invoke( obj, convertToEnum( value, getter.getReturnType() ) );
|
||||
return oldValue;
|
||||
} catch( NoSuchMethodException ex ) {
|
||||
throw new UnknownStyleException( name );
|
||||
@@ -490,6 +490,21 @@ public class FlatStylingSupport
|
||||
return new String( chars );
|
||||
}
|
||||
|
||||
@SuppressWarnings( { "unchecked", "rawtypes" } )
|
||||
private static Object convertToEnum( Object value, Class<?> type )
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
// if type is an enum, convert string to enum value
|
||||
if( Enum.class.isAssignableFrom( type ) && value instanceof String ) {
|
||||
try {
|
||||
value = Enum.valueOf( (Class<? extends Enum>) type, (String) value );
|
||||
} catch( IllegalArgumentException ex ) {
|
||||
throw new IllegalArgumentException( "unknown enum value '" + value + "' in enum '" + type.getName() + "'", ex );
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the given value to an annotated field of the given object
|
||||
* or to a property of the given component.
|
||||
@@ -575,7 +590,7 @@ public class FlatStylingSupport
|
||||
try {
|
||||
return borderClass.getDeclaredConstructor().newInstance();
|
||||
} catch( Exception ex ) {
|
||||
throw new IllegalArgumentException( "failed to clone border '" + borderClass.getName() + "'" );
|
||||
throw new IllegalArgumentException( "failed to clone border '" + borderClass.getName() + "'", ex );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -584,7 +599,7 @@ public class FlatStylingSupport
|
||||
try {
|
||||
return iconClass.getDeclaredConstructor().newInstance();
|
||||
} catch( Exception ex ) {
|
||||
throw new IllegalArgumentException( "failed to clone icon '" + iconClass.getName() + "'" );
|
||||
throw new IllegalArgumentException( "failed to clone icon '" + iconClass.getName() + "'", ex );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -146,6 +146,19 @@ public class FlatUIUtils
|
||||
return (value instanceof Number) ? ((Number)value).floatValue() : defaultValue;
|
||||
}
|
||||
|
||||
/** @since 2 */
|
||||
public static <T extends Enum<T>> T getUIEnum( String key, Class<T> enumType, T defaultValue ) {
|
||||
Object value = UIManager.get( key );
|
||||
if( value instanceof String ) {
|
||||
try {
|
||||
return Enum.valueOf( enumType, (String) value );
|
||||
} catch( IllegalArgumentException ex ) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/** @since 1.1.2 */
|
||||
public static boolean getBoolean( JComponent c, String systemPropertyKey,
|
||||
String clientPropertyKey, String uiKey, boolean defaultValue )
|
||||
|
||||
@@ -1282,4 +1282,38 @@ public class TestFlatStyling
|
||||
icon.applyStyleProperty( "searchIconHoverColor", Color.WHITE );
|
||||
icon.applyStyleProperty( "searchIconPressedColor", Color.WHITE );
|
||||
}
|
||||
|
||||
//---- enums --------------------------------------------------------------
|
||||
|
||||
enum SomeEnum { enumValue1, enumValue2 }
|
||||
|
||||
static class ClassWithEnum {
|
||||
SomeEnum enum1;
|
||||
}
|
||||
|
||||
@Test
|
||||
void enumField() {
|
||||
ClassWithEnum c = new ClassWithEnum();
|
||||
FlatStylingSupport.applyToField( c, "enum1", "enum1", "enumValue1" );
|
||||
FlatStylingSupport.applyToField( c, "enum1", "enum1", "enumValue2" );
|
||||
}
|
||||
|
||||
@Test
|
||||
void enumProperty() {
|
||||
JList<Object> c = new JList<>();
|
||||
FlatListUI ui = (FlatListUI) c.getUI();
|
||||
ui.applyStyle( "dropMode: INSERT" );
|
||||
}
|
||||
|
||||
@Test
|
||||
void enumUIDefaults() {
|
||||
UIManager.put( "test.enum", SomeEnum.enumValue1.toString() );
|
||||
assertEquals( SomeEnum.enumValue1, FlatUIUtils.getUIEnum( "test.enum", SomeEnum.class, null ) );
|
||||
|
||||
UIManager.put( "test.enum", "unknown" );
|
||||
assertEquals( null, FlatUIUtils.getUIEnum( "test.enum", SomeEnum.class, null ) );
|
||||
|
||||
UIManager.put( "test.enum", null );
|
||||
assertEquals( SomeEnum.enumValue1, FlatUIUtils.getUIEnum( "test.enum", SomeEnum.class, SomeEnum.enumValue1 ) );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user