Package com.formdev.flatlaf.ui
Class FlatStylingSupport
java.lang.Object
com.formdev.flatlaf.ui.FlatStylingSupport
Support for styling components in CSS syntax.
- Since:
- 2
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic @interface
Indicates that a field is intended to be used by FlatLaf styling support.static interface
static @interface
Indicates that a field in the specified (super) class is intended to be used by FlatLaf styling support.static @interface
Container annotation forFlatStylingSupport.StyleableField
.(package private) static class
static interface
static interface
static class
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionapplyStyle
(Map<String, Object> style, BiFunction<String, Object, Object> applyProperty) static Object
applyToAnnotatedObject
(Object obj, String key, Object value) Applies the given value to an annotated field of the given object.(package private) static Object
applyToAnnotatedObjectOrBorder
(Object obj, String key, Object value, JComponent c, AtomicBoolean borderShared) static Object
applyToAnnotatedObjectOrComponent
(Object obj, Object comp, String key, Object value) Applies the given value to an annotated field of the given object or to a property of the given component.(package private) static Object
applyToField
(Object obj, String fieldName, String key, Object value) Applies the given value to a field of the given object.private static Object
private static Object
applyToField
(Field f, Object obj, Object value, boolean useMethodHandles) private static Object
applyToProperty
(Object obj, String name, Object value) Applies the given value to a property of the given object.private static String
buildMethodName
(String prefix, String name) private static void
(package private) static Border
cloneBorder
(Border border) (package private) static Icon
static void
collectAnnotatedStyleableInfos
(Object obj, Map<String, Class<?>> infos) Search for all fields annotated withFlatStylingSupport.Styleable
and add them to the given map.static void
collectStyleableInfos
(Border border, Map<String, Class<?>> infos) static String
concatStyles
(String style1, String style2) Concatenates two styles in CSS syntax.private static Object
convertToEnum
(Object value, Class<?> type) (package private) static PropertyChangeListener
createPropertyChangeListener
(JComponent c, Runnable installStyle, PropertyChangeListener superListener) Returns a map of all fields annotated withFlatStylingSupport.Styleable
.getAnnotatedStyleableInfos
(Object obj, Border border) static Object
getAnnotatedStyleableValue
(Object obj, String key) static Object
getAnnotatedStyleableValue
(Object obj, Border border, String key) private static Object
getFieldValue
(Field f, Object obj, boolean useMethodHandles) static Object
getResolvedStyle
(JComponent c, String type) static Object
Returns the style specified in client propertyFlatClientProperties.STYLE
.private static Field
getStyleableField
(FlatStylingSupport.StyleableField styleableField) static Object
Returns the style class(es) specified in client propertyFlatClientProperties.STYLE_CLASS
.private static Object
getStyleForClass
(String styleClass, String type) static Object
getStyleForClasses
(Object styleClass, String type) Returns the styles for the given style class(es) and the given type.(package private) static boolean
private static boolean
static Object
joinStyles
(Object style1, Object style2) Joins two styles.private static String
keyToFieldName
(String key) private static IllegalArgumentException
newFieldAccessFailed
(Field f, Throwable ex) Parses styles in CSS syntax ("key1: value1; key2: value2; ..."), converts the value strings into binary and returns all key/value pairs as map.parseAndApply
(Map<String, Object> oldStyleValues, Object style, BiFunction<String, Object, Object> applyProperty) Parses styles in CSS syntax ("key1: value1; key2: value2; ..."), converts the value strings into binary and invokes the given function to apply the properties.private static Object
parseValue
(String key, String value) static void
-
Constructor Details
-
FlatStylingSupport
public FlatStylingSupport()
-
-
Method Details
-
getStyle
Returns the style specified in client propertyFlatClientProperties.STYLE
. -
getStyleClass
Returns the style class(es) specified in client propertyFlatClientProperties.STYLE_CLASS
. -
hasStyleProperty
-
getResolvedStyle
- Throws:
IllegalArgumentException
-
getStyleForClasses
public static Object getStyleForClasses(Object styleClass, String type) throws IllegalArgumentException Returns the styles for the given style class(es) and the given type.The style rules must be defined in UI defaults either as strings (in CSS syntax) or as
Map
<String, Object> (with binary values). The key must be in syntax:[style]type.styleClass
, where the type is optional. E.g. in FlatLaf properties file:
or in Java code:[style]Button.primary = borderColor: #08f; background: #08f; foreground: #fff [style].secondary = borderColor: #0f8; background: #0f8
The rule "Button.primary" can be applied to buttons only. The rule ".secondary" can be applied to any component.UIManager.put( "[style]Button.primary", "borderColor: #08f; background: #08f; foreground: #fff" ); UIManager.put( "[style].secondary", "borderColor: #0f8; background: #0f8" );
To have similar behavior as in CSS, this method first gets the rule without type, then the rule with type and concatenates both rules. E.g. invoking this method with parameters styleClass="foo" and type="Button" does following:
return joinStyles( UIManager.get( "[style].foo" ), UIManager.get( "[style]Button.foo" ) );
- Parameters:
styleClass
- the style class(es) either as string (single class or multiple classes separated by space characters) or asString[]
orList
<String> (multiple classes)type
- the type of the component- Returns:
- the styles
- Throws:
IllegalArgumentException
-
getStyleForClass
private static Object getStyleForClass(String styleClass, String type) throws IllegalArgumentException - Throws:
IllegalArgumentException
-
joinStyles
Joins two styles. They can be either strings (in CSS syntax) orMap
<String, Object> (with binary values).If both styles are strings, then a joined string is returned. If both styles are maps, then a joined map is returned. If one style is a map and the other style a string, then the string is parsed (using
parse(String)
) to a map and a joined map is returned.- Parameters:
style1
- first style as string or map, ornull
style2
- second style as string or map, ornull
- Returns:
- new joined style
- Throws:
IllegalArgumentException
-
concatStyles
Concatenates two styles in CSS syntax.- Parameters:
style1
- first style, ornull
style2
- second style, ornull
- Returns:
- concatenation of the two styles separated by a semicolon
-
parseAndApply
public static Map<String,Object> parseAndApply(Map<String, Object> oldStyleValues, Object style, BiFunction<String, throws FlatStylingSupport.UnknownStyleException, IllegalArgumentExceptionObject, Object> applyProperty) Parses styles in CSS syntax ("key1: value1; key2: value2; ..."), converts the value strings into binary and invokes the given function to apply the properties.- Parameters:
oldStyleValues
- map of old values modified by the previous invocation, ornull
style
- the style in CSS syntax as string, or a Map, ornull
applyProperty
- function that is invoked to apply the properties; first parameter is the key, second the binary value; the function must return the old value- Returns:
- map of old values modified by the given style, or
null
- Throws:
FlatStylingSupport.UnknownStyleException
- on unknown style keysIllegalArgumentException
- on syntax errorsClassCastException
- if value type does not fit to expected type
-
applyStyle
-
parse
Parses styles in CSS syntax ("key1: value1; key2: value2; ..."), converts the value strings into binary and returns all key/value pairs as map.- Parameters:
style
- the style in CSS syntax, ornull
- Returns:
- map of parsed styles, or
null
- Throws:
IllegalArgumentException
- on syntax errors
-
parseValue
- Throws:
IllegalArgumentException
-
applyToAnnotatedObject
public static Object applyToAnnotatedObject(Object obj, String key, Object value) throws FlatStylingSupport.UnknownStyleException, IllegalArgumentException Applies the given value to an annotated field of the given object. The field must be annotated withFlatStylingSupport.Styleable
.- Parameters:
obj
- the objectkey
- the name of the fieldvalue
- the new value- Returns:
- the old value of the field
- Throws:
FlatStylingSupport.UnknownStyleException
- if object does not have an annotated field with given nameIllegalArgumentException
- if value type does not fit to expected type
-
keyToFieldName
-
applyToField
static Object applyToField(Object obj, String fieldName, String key, Object value) throws FlatStylingSupport.UnknownStyleException, IllegalArgumentException Applies the given value to a field of the given object.- Parameters:
obj
- the objectfieldName
- the name of the fieldkey
- the key (only used for error reporting)value
- the new value- Returns:
- the old value of the field
- Throws:
FlatStylingSupport.UnknownStyleException
- if object does not have a field with given nameIllegalArgumentException
- if value type does not fit to expected type
-
applyToField
private static Object applyToField(Object obj, String fieldName, String key, Object value, Predicate<Field> predicate) throws FlatStylingSupport.UnknownStyleException, IllegalArgumentException -
applyToField
private static Object applyToField(Field f, Object obj, Object value, boolean useMethodHandles) throws IllegalArgumentException - Throws:
IllegalArgumentException
-
getFieldValue
private static Object getFieldValue(Field f, Object obj, boolean useMethodHandles) throws IllegalArgumentException - Throws:
IllegalArgumentException
-
newFieldAccessFailed
-
checkValidField
- Throws:
IllegalArgumentException
-
isValidField
-
getStyleableField
private static Field getStyleableField(FlatStylingSupport.StyleableField styleableField) throws IllegalArgumentException - Throws:
IllegalArgumentException
-
applyToProperty
private static Object applyToProperty(Object obj, String name, Object value) throws FlatStylingSupport.UnknownStyleException, IllegalArgumentException Applies the given value to a property of the given object. Works only for properties that have public getter and setter methods. First the property getter is invoked to get the old value, then the property setter is invoked to set the new value.- Parameters:
obj
- the objectname
- the name of the propertyvalue
- the new value- Returns:
- the old value of the property
- Throws:
FlatStylingSupport.UnknownStyleException
- if object does not have a property with given nameIllegalArgumentException
- if value type does not fit to expected type
-
buildMethodName
-
convertToEnum
- Throws:
IllegalArgumentException
-
applyToAnnotatedObjectOrComponent
public static Object applyToAnnotatedObjectOrComponent(Object obj, Object comp, String key, Object value) throws FlatStylingSupport.UnknownStyleException, IllegalArgumentException Applies the given value to an annotated field of the given object or to a property of the given component. The field must be annotated withFlatStylingSupport.Styleable
. The component property must have public getter and setter methods.- Parameters:
obj
- the objectcomp
- the component, ornull
key
- the name of the fieldvalue
- the new value- Returns:
- the old value of the field
- Throws:
FlatStylingSupport.UnknownStyleException
- if object does not have an annotated field with given nameIllegalArgumentException
- if value type does not fit to expected type
-
applyToAnnotatedObjectOrBorder
static Object applyToAnnotatedObjectOrBorder(Object obj, String key, Object value, JComponent c, AtomicBoolean borderShared) throws IllegalArgumentException - Throws:
IllegalArgumentException
-
createPropertyChangeListener
static PropertyChangeListener createPropertyChangeListener(JComponent c, Runnable installStyle, PropertyChangeListener superListener) -
cloneBorder
- Throws:
IllegalArgumentException
-
cloneIcon
- Throws:
IllegalArgumentException
-
getAnnotatedStyleableInfos
public static Map<String,Class<?>> getAnnotatedStyleableInfos(Object obj) throws IllegalArgumentException Returns a map of all fields annotated withFlatStylingSupport.Styleable
. The key is the name of the field and the value the type of the field.- Throws:
IllegalArgumentException
-
getAnnotatedStyleableInfos
public static Map<String,Class<?>> getAnnotatedStyleableInfos(Object obj, Border border) throws IllegalArgumentException - Throws:
IllegalArgumentException
-
collectAnnotatedStyleableInfos
public static void collectAnnotatedStyleableInfos(Object obj, Map<String, Class<?>> infos) throws IllegalArgumentExceptionSearch for all fields annotated withFlatStylingSupport.Styleable
and add them to the given map. The key is the name of the field and the value the type of the field.- Throws:
IllegalArgumentException
-
collectStyleableInfos
-
putAllPrefixKey
-
getAnnotatedStyleableValue
public static Object getAnnotatedStyleableValue(Object obj, String key) throws IllegalArgumentException - Throws:
IllegalArgumentException
-
getAnnotatedStyleableValue
-