Styling: support enums

This commit is contained in:
Karl Tauber
2021-09-30 10:40:43 +02:00
parent d93dde0a03
commit d66e35fdde
3 changed files with 67 additions and 5 deletions

View File

@@ -414,10 +414,10 @@ public class FlatStylingSupport
// get old value and set new value // get old value and set new value
Object oldValue = f.get( obj ); Object oldValue = f.get( obj );
f.set( obj, value ); f.set( obj, convertToEnum( value, f.getType() ) );
return oldValue; return oldValue;
} catch( IllegalAccessException ex ) { } 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 ) { } catch( NoSuchFieldException ex ) {
@@ -470,7 +470,7 @@ public class FlatStylingSupport
} }
Method setter = cls.getMethod( setterName, getter.getReturnType() ); Method setter = cls.getMethod( setterName, getter.getReturnType() );
Object oldValue = getter.invoke( obj ); Object oldValue = getter.invoke( obj );
setter.invoke( obj, value ); setter.invoke( obj, convertToEnum( value, getter.getReturnType() ) );
return oldValue; return oldValue;
} catch( NoSuchMethodException ex ) { } catch( NoSuchMethodException ex ) {
throw new UnknownStyleException( name ); throw new UnknownStyleException( name );
@@ -490,6 +490,21 @@ public class FlatStylingSupport
return new String( chars ); 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 * Applies the given value to an annotated field of the given object
* or to a property of the given component. * or to a property of the given component.
@@ -575,7 +590,7 @@ public class FlatStylingSupport
try { try {
return borderClass.getDeclaredConstructor().newInstance(); return borderClass.getDeclaredConstructor().newInstance();
} catch( Exception ex ) { } 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 { try {
return iconClass.getDeclaredConstructor().newInstance(); return iconClass.getDeclaredConstructor().newInstance();
} catch( Exception ex ) { } catch( Exception ex ) {
throw new IllegalArgumentException( "failed to clone icon '" + iconClass.getName() + "'" ); throw new IllegalArgumentException( "failed to clone icon '" + iconClass.getName() + "'", ex );
} }
} }

View File

@@ -146,6 +146,19 @@ public class FlatUIUtils
return (value instanceof Number) ? ((Number)value).floatValue() : defaultValue; 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 */ /** @since 1.1.2 */
public static boolean getBoolean( JComponent c, String systemPropertyKey, public static boolean getBoolean( JComponent c, String systemPropertyKey,
String clientPropertyKey, String uiKey, boolean defaultValue ) String clientPropertyKey, String uiKey, boolean defaultValue )

View File

@@ -1282,4 +1282,38 @@ public class TestFlatStyling
icon.applyStyleProperty( "searchIconHoverColor", Color.WHITE ); icon.applyStyleProperty( "searchIconHoverColor", Color.WHITE );
icon.applyStyleProperty( "searchIconPressedColor", 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 ) );
}
} }