Package com.formdev.flatlaf.ui
Class FlatStylingSupport
- java.lang.Object
-
- com.formdev.flatlaf.ui.FlatStylingSupport
-
public class FlatStylingSupport extends Object
Support for styling components in CSS syntax.- Since:
- 2
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceFlatStylingSupport.StyleableIndicates that a field is intended to be used by FlatLaf styling support.static interfaceFlatStylingSupport.StyleableBorderstatic interfaceFlatStylingSupport.StyleableFieldIndicates that a field in the specified (super) class is intended to be used by FlatLaf styling support.static interfaceFlatStylingSupport.StyleableFieldsContainer annotation forFlatStylingSupport.StyleableField.static interfaceFlatStylingSupport.StyleableLookupProviderstatic interfaceFlatStylingSupport.StyleableUIstatic classFlatStylingSupport.UnknownStyleException
-
Constructor Summary
Constructors Constructor Description FlatStylingSupport()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static 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.static Map<String,Class<?>>getAnnotatedStyleableInfos(Object obj)Returns a map of all fields annotated withFlatStylingSupport.Styleable.static Map<String,Class<?>>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 ObjectgetStyle(JComponent c)Returns the style specified in client propertyFlatClientProperties.STYLE.static ObjectgetStyleClass(JComponent c)Returns 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.static Map<String,Object>parse(String style)Parses styles in CSS syntax ("key1: value1; key2: value2; ..."), converts the value strings into binary and returns all key/value pairs as map.static Map<String,Object>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 voidputAllPrefixKey(Map<String,Class<?>> infos, String keyPrefix, Map<String,Class<?>> infos2)
-
-
-
Method Detail
-
getStyle
public static Object getStyle(JComponent c)
Returns the style specified in client propertyFlatClientProperties.STYLE.
-
getStyleClass
public static Object getStyleClass(JComponent c)
Returns the style class(es) specified in client propertyFlatClientProperties.STYLE_CLASS.
-
getResolvedStyle
public static Object getResolvedStyle(JComponent c, String type)
-
getStyleForClasses
public static Object getStyleForClasses(Object styleClass, String type)
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
-
joinStyles
public static Object joinStyles(Object style1, Object style2)
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
-
concatStyles
public static String concatStyles(String style1, String style2)
Concatenates two styles in CSS syntax.- Parameters:
style1- first style, ornullstyle2- 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,Object,Object> applyProperty) throws FlatStylingSupport.UnknownStyleException, IllegalArgumentException
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
public static Map<String,Object> parse(String style) throws IllegalArgumentException
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)
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.
-
getAnnotatedStyleableInfos
public static Map<String,Class<?>> getAnnotatedStyleableInfos(Object obj, Border border)
-
collectAnnotatedStyleableInfos
public static void collectAnnotatedStyleableInfos(Object obj, Map<String,Class<?>> infos)
Search 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.
-
collectStyleableInfos
public static void collectStyleableInfos(Border border, Map<String,Class<?>> infos)
-
putAllPrefixKey
public static void putAllPrefixKey(Map<String,Class<?>> infos, String keyPrefix, Map<String,Class<?>> infos2)
-
-