Package com.formdev.flatlaf.ui
Class FlatStylingSupport
- java.lang.Object
-
- com.formdev.flatlaf.ui.FlatStylingSupport
-
public class FlatStylingSupport extends java.lang.Object
Support for styling components in CSS syntax.- Since:
- 2
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
FlatStylingSupport.Styleable
Indicates that a field is intended to be used by FlatLaf styling support.static interface
FlatStylingSupport.StyleableBorder
static interface
FlatStylingSupport.StyleableField
Indicates that a field in the specified (super) class is intended to be used by FlatLaf styling support.static interface
FlatStylingSupport.StyleableFields
Container annotation forFlatStylingSupport.StyleableField
.(package private) static class
FlatStylingSupport.StyleableInfosMap<K,V>
static interface
FlatStylingSupport.StyleableLookupProvider
static interface
FlatStylingSupport.StyleableUI
static class
FlatStylingSupport.UnknownStyleException
-
Constructor Summary
Constructors Constructor Description FlatStylingSupport()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static java.util.Map<java.lang.String,java.lang.Object>
applyStyle(java.util.Map<java.lang.String,java.lang.Object> style, java.util.function.BiFunction<java.lang.String,java.lang.Object,java.lang.Object> applyProperty)
static java.lang.Object
applyToAnnotatedObject(java.lang.Object obj, java.lang.String key, java.lang.Object value)
Applies the given value to an annotated field of the given object.(package private) static java.lang.Object
applyToAnnotatedObjectOrBorder(java.lang.Object obj, java.lang.String key, java.lang.Object value, javax.swing.JComponent c, java.util.concurrent.atomic.AtomicBoolean borderShared)
static java.lang.Object
applyToAnnotatedObjectOrComponent(java.lang.Object obj, java.lang.Object comp, java.lang.String key, java.lang.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 java.lang.Object
applyToField(java.lang.Object obj, java.lang.String fieldName, java.lang.String key, java.lang.Object value)
Applies the given value to a field of the given object.private static java.lang.Object
applyToField(java.lang.Object obj, java.lang.String fieldName, java.lang.String key, java.lang.Object value, java.util.function.Predicate<java.lang.reflect.Field> predicate)
private static java.lang.Object
applyToField(java.lang.reflect.Field f, java.lang.Object obj, java.lang.Object value, boolean useMethodHandles)
private static java.lang.Object
applyToProperty(java.lang.Object obj, java.lang.String name, java.lang.Object value)
Applies the given value to a property of the given object.private static java.lang.String
buildMethodName(java.lang.String prefix, java.lang.String name)
private static void
checkValidField(java.lang.reflect.Field f)
(package private) static javax.swing.border.Border
cloneBorder(javax.swing.border.Border border)
(package private) static javax.swing.Icon
cloneIcon(javax.swing.Icon icon)
static void
collectAnnotatedStyleableInfos(java.lang.Object obj, java.util.Map<java.lang.String,java.lang.Class<?>> infos)
Search for all fields annotated withFlatStylingSupport.Styleable
and add them to the given map.static void
collectStyleableInfos(javax.swing.border.Border border, java.util.Map<java.lang.String,java.lang.Class<?>> infos)
static java.lang.String
concatStyles(java.lang.String style1, java.lang.String style2)
Concatenates two styles in CSS syntax.private static java.lang.Object
convertToEnum(java.lang.Object value, java.lang.Class<?> type)
(package private) static java.beans.PropertyChangeListener
createPropertyChangeListener(javax.swing.JComponent c, java.lang.Runnable installStyle, java.beans.PropertyChangeListener superListener)
static java.util.Map<java.lang.String,java.lang.Class<?>>
getAnnotatedStyleableInfos(java.lang.Object obj)
Returns a map of all fields annotated withFlatStylingSupport.Styleable
.static java.util.Map<java.lang.String,java.lang.Class<?>>
getAnnotatedStyleableInfos(java.lang.Object obj, javax.swing.border.Border border)
static java.lang.Object
getAnnotatedStyleableValue(java.lang.Object obj, java.lang.String key)
static java.lang.Object
getAnnotatedStyleableValue(java.lang.Object obj, javax.swing.border.Border border, java.lang.String key)
private static java.lang.Object
getFieldValue(java.lang.reflect.Field f, java.lang.Object obj, boolean useMethodHandles)
static java.lang.Object
getResolvedStyle(javax.swing.JComponent c, java.lang.String type)
static java.lang.Object
getStyle(javax.swing.JComponent c)
Returns the style specified in client propertyFlatClientProperties.STYLE
.private static java.lang.reflect.Field
getStyleableField(FlatStylingSupport.StyleableField styleableField)
static java.lang.Object
getStyleClass(javax.swing.JComponent c)
Returns the style class(es) specified in client propertyFlatClientProperties.STYLE_CLASS
.private static java.lang.Object
getStyleForClass(java.lang.String styleClass, java.lang.String type)
static java.lang.Object
getStyleForClasses(java.lang.Object styleClass, java.lang.String type)
Returns the styles for the given style class(es) and the given type.(package private) static boolean
hasStyleProperty(javax.swing.JComponent c)
private static boolean
isValidField(java.lang.reflect.Field f)
static java.lang.Object
joinStyles(java.lang.Object style1, java.lang.Object style2)
Joins two styles.private static java.lang.String
keyToFieldName(java.lang.String key)
private static java.lang.IllegalArgumentException
newFieldAccessFailed(java.lang.reflect.Field f, java.lang.Throwable ex)
static java.util.Map<java.lang.String,java.lang.Object>
parse(java.lang.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 java.util.Map<java.lang.String,java.lang.Object>
parseAndApply(java.util.Map<java.lang.String,java.lang.Object> oldStyleValues, java.lang.Object style, java.util.function.BiFunction<java.lang.String,java.lang.Object,java.lang.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 java.lang.Object
parseValue(java.lang.String key, java.lang.String value)
static void
putAllPrefixKey(java.util.Map<java.lang.String,java.lang.Class<?>> infos, java.lang.String keyPrefix, java.util.Map<java.lang.String,java.lang.Class<?>> infos2)
-
-
-
Method Detail
-
getStyle
public static java.lang.Object getStyle(javax.swing.JComponent c)
Returns the style specified in client propertyFlatClientProperties.STYLE
.
-
getStyleClass
public static java.lang.Object getStyleClass(javax.swing.JComponent c)
Returns the style class(es) specified in client propertyFlatClientProperties.STYLE_CLASS
.
-
hasStyleProperty
static boolean hasStyleProperty(javax.swing.JComponent c)
-
getResolvedStyle
public static java.lang.Object getResolvedStyle(javax.swing.JComponent c, java.lang.String type) throws java.lang.IllegalArgumentException
- Throws:
java.lang.IllegalArgumentException
-
getStyleForClasses
public static java.lang.Object getStyleForClasses(java.lang.Object styleClass, java.lang.String type) throws java.lang.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:
java.lang.IllegalArgumentException
-
getStyleForClass
private static java.lang.Object getStyleForClass(java.lang.String styleClass, java.lang.String type) throws java.lang.IllegalArgumentException
- Throws:
java.lang.IllegalArgumentException
-
joinStyles
public static java.lang.Object joinStyles(java.lang.Object style1, java.lang.Object style2) throws java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException
-
concatStyles
public static java.lang.String concatStyles(java.lang.String style1, java.lang.String style2)
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 java.util.Map<java.lang.String,java.lang.Object> parseAndApply(java.util.Map<java.lang.String,java.lang.Object> oldStyleValues, java.lang.Object style, java.util.function.BiFunction<java.lang.String,java.lang.Object,java.lang.Object> applyProperty) throws FlatStylingSupport.UnknownStyleException, java.lang.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, 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 keysjava.lang.IllegalArgumentException
- on syntax errorsjava.lang.ClassCastException
- if value type does not fit to expected type
-
applyStyle
private static java.util.Map<java.lang.String,java.lang.Object> applyStyle(java.util.Map<java.lang.String,java.lang.Object> style, java.util.function.BiFunction<java.lang.String,java.lang.Object,java.lang.Object> applyProperty)
-
parse
public static java.util.Map<java.lang.String,java.lang.Object> parse(java.lang.String style) throws java.lang.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:
java.lang.IllegalArgumentException
- on syntax errors
-
parseValue
private static java.lang.Object parseValue(java.lang.String key, java.lang.String value) throws java.lang.IllegalArgumentException
- Throws:
java.lang.IllegalArgumentException
-
applyToAnnotatedObject
public static java.lang.Object applyToAnnotatedObject(java.lang.Object obj, java.lang.String key, java.lang.Object value) throws FlatStylingSupport.UnknownStyleException, java.lang.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 namejava.lang.IllegalArgumentException
- if value type does not fit to expected type
-
keyToFieldName
private static java.lang.String keyToFieldName(java.lang.String key)
-
applyToField
static java.lang.Object applyToField(java.lang.Object obj, java.lang.String fieldName, java.lang.String key, java.lang.Object value) throws FlatStylingSupport.UnknownStyleException, java.lang.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 namejava.lang.IllegalArgumentException
- if value type does not fit to expected type
-
applyToField
private static java.lang.Object applyToField(java.lang.Object obj, java.lang.String fieldName, java.lang.String key, java.lang.Object value, java.util.function.Predicate<java.lang.reflect.Field> predicate) throws FlatStylingSupport.UnknownStyleException, java.lang.IllegalArgumentException
- Throws:
FlatStylingSupport.UnknownStyleException
java.lang.IllegalArgumentException
-
applyToField
private static java.lang.Object applyToField(java.lang.reflect.Field f, java.lang.Object obj, java.lang.Object value, boolean useMethodHandles) throws java.lang.IllegalArgumentException
- Throws:
java.lang.IllegalArgumentException
-
getFieldValue
private static java.lang.Object getFieldValue(java.lang.reflect.Field f, java.lang.Object obj, boolean useMethodHandles) throws java.lang.IllegalArgumentException
- Throws:
java.lang.IllegalArgumentException
-
newFieldAccessFailed
private static java.lang.IllegalArgumentException newFieldAccessFailed(java.lang.reflect.Field f, java.lang.Throwable ex)
-
checkValidField
private static void checkValidField(java.lang.reflect.Field f) throws java.lang.IllegalArgumentException
- Throws:
java.lang.IllegalArgumentException
-
isValidField
private static boolean isValidField(java.lang.reflect.Field f)
-
getStyleableField
private static java.lang.reflect.Field getStyleableField(FlatStylingSupport.StyleableField styleableField) throws java.lang.IllegalArgumentException
- Throws:
java.lang.IllegalArgumentException
-
applyToProperty
private static java.lang.Object applyToProperty(java.lang.Object obj, java.lang.String name, java.lang.Object value) throws FlatStylingSupport.UnknownStyleException, java.lang.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 namejava.lang.IllegalArgumentException
- if value type does not fit to expected type
-
buildMethodName
private static java.lang.String buildMethodName(java.lang.String prefix, java.lang.String name)
-
convertToEnum
private static java.lang.Object convertToEnum(java.lang.Object value, java.lang.Class<?> type) throws java.lang.IllegalArgumentException
- Throws:
java.lang.IllegalArgumentException
-
applyToAnnotatedObjectOrComponent
public static java.lang.Object applyToAnnotatedObjectOrComponent(java.lang.Object obj, java.lang.Object comp, java.lang.String key, java.lang.Object value) throws FlatStylingSupport.UnknownStyleException, java.lang.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 namejava.lang.IllegalArgumentException
- if value type does not fit to expected type
-
applyToAnnotatedObjectOrBorder
static java.lang.Object applyToAnnotatedObjectOrBorder(java.lang.Object obj, java.lang.String key, java.lang.Object value, javax.swing.JComponent c, java.util.concurrent.atomic.AtomicBoolean borderShared) throws java.lang.IllegalArgumentException
- Throws:
java.lang.IllegalArgumentException
-
createPropertyChangeListener
static java.beans.PropertyChangeListener createPropertyChangeListener(javax.swing.JComponent c, java.lang.Runnable installStyle, java.beans.PropertyChangeListener superListener)
-
cloneBorder
static javax.swing.border.Border cloneBorder(javax.swing.border.Border border) throws java.lang.IllegalArgumentException
- Throws:
java.lang.IllegalArgumentException
-
cloneIcon
static javax.swing.Icon cloneIcon(javax.swing.Icon icon) throws java.lang.IllegalArgumentException
- Throws:
java.lang.IllegalArgumentException
-
getAnnotatedStyleableInfos
public static java.util.Map<java.lang.String,java.lang.Class<?>> getAnnotatedStyleableInfos(java.lang.Object obj) throws java.lang.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:
java.lang.IllegalArgumentException
-
getAnnotatedStyleableInfos
public static java.util.Map<java.lang.String,java.lang.Class<?>> getAnnotatedStyleableInfos(java.lang.Object obj, javax.swing.border.Border border) throws java.lang.IllegalArgumentException
- Throws:
java.lang.IllegalArgumentException
-
collectAnnotatedStyleableInfos
public static void collectAnnotatedStyleableInfos(java.lang.Object obj, java.util.Map<java.lang.String,java.lang.Class<?>> infos) throws java.lang.IllegalArgumentException
Search 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:
java.lang.IllegalArgumentException
-
collectStyleableInfos
public static void collectStyleableInfos(javax.swing.border.Border border, java.util.Map<java.lang.String,java.lang.Class<?>> infos)
-
putAllPrefixKey
public static void putAllPrefixKey(java.util.Map<java.lang.String,java.lang.Class<?>> infos, java.lang.String keyPrefix, java.util.Map<java.lang.String,java.lang.Class<?>> infos2)
-
getAnnotatedStyleableValue
public static java.lang.Object getAnnotatedStyleableValue(java.lang.Object obj, java.lang.String key) throws java.lang.IllegalArgumentException
- Throws:
java.lang.IllegalArgumentException
-
getAnnotatedStyleableValue
public static java.lang.Object getAnnotatedStyleableValue(java.lang.Object obj, javax.swing.border.Border border, java.lang.String key)
-
-