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 @interfaceIndicates that a field is intended to be used by FlatLaf styling support.static @interfaceIndicates that a field in the specified (super) class is intended to be used by FlatLaf styling support.static @interfaceContainer annotation forFlatStylingSupport.StyleableField.static interfacestatic interfaceAn object that implements this interface is intended to support FlatLaf styling.static interfacestatic class -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ObjectapplyToAnnotatedObject(Object obj, String key, Object value) Applies the given value to an annotated field of the given object.static ObjectapplyToAnnotatedObjectOrComponent(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.static voidcollectAnnotatedStyleableInfos(Object obj, Map<String, Class<?>> infos) Search for all fields annotated withFlatStylingSupport.Styleableand add them to the given map.static voidcollectStyleableInfos(Border border, Map<String, Class<?>> infos) static StringconcatStyles(String style1, String style2) Concatenates two styles in CSS syntax.Returns a map of all fields annotated withFlatStylingSupport.Styleable.getAnnotatedStyleableInfos(Object obj, Border border) static ObjectgetAnnotatedStyleableValue(Object obj, String key) static ObjectgetAnnotatedStyleableValue(Object obj, Border border, String key) static ObjectgetResolvedStyle(JComponent c, String type) static ObjectReturns the style specified in client propertyFlatClientProperties.STYLE.static ObjectReturns the style class(es) specified in client propertyFlatClientProperties.STYLE_CLASS.static ObjectgetStyleForClasses(Object styleClass, String type) Returns the styles for the given style class(es) and the given type.static ObjectjoinStyles(Object style1, Object style2) Joins two styles.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.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. -
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
-
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, ornullstyle2- second style as string or map, ornull- Returns:
- new joined style
- Throws:
IllegalArgumentException
-
concatStyles
-
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, ornullstyle- the style in CSS syntax as string, or a Map, ornullapplyProperty- 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
-
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
-
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
-
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, ornullkey- 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
-
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.Styleableand 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
-