mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-13 07:17:13 -06: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 )
|
||||
|
||||
Reference in New Issue
Block a user