diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatStylingSupport.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatStylingSupport.java index 6cb36be5..f32e10bb 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatStylingSupport.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatStylingSupport.java @@ -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) 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 ); } } diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java index fc3777a7..990a995f 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java @@ -146,6 +146,19 @@ public class FlatUIUtils return (value instanceof Number) ? ((Number)value).floatValue() : defaultValue; } + /** @since 2 */ + public static > T getUIEnum( String key, Class 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 ) diff --git a/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyling.java b/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyling.java index 3f143a96..ab0432ad 100644 --- a/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyling.java +++ b/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyling.java @@ -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 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 ) ); + } }