From 0830c78728c5e00ec299ffc5a9aa36c017f9d2fa Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Tue, 15 Jun 2021 19:44:55 +0200 Subject: [PATCH] Styling: support using simple references to UI defaults e.g. `mySlider.putClientProperty( "JComponent.style", "thumbColor: $TextField.background; thumbBorderColor: $Component.borderColor" );` --- .../com/formdev/flatlaf/ui/FlatStyleSupport.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatStyleSupport.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatStyleSupport.java index 0c94dccf..6ae189e4 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatStyleSupport.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatStyleSupport.java @@ -21,6 +21,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.function.BiFunction; import javax.swing.JComponent; +import javax.swing.UIManager; import com.formdev.flatlaf.FlatClientProperties; import com.formdev.flatlaf.FlatLaf; import com.formdev.flatlaf.util.StringUtils; @@ -45,6 +46,7 @@ public class FlatStyleSupport * the function must return the old value * @return map of old values modified by the given style, or {@code null} * @throws IllegalArgumentException on syntax errors + * @throws ClassCastException if value type does not fit to expected type  */ public static Map parse( Map oldStyleValues, String style, BiFunction applyProperty ) throws IllegalArgumentException @@ -82,8 +84,8 @@ public class FlatStyleSupport throw new IllegalArgumentException( "missing value in '" + part + "'" ); // parse value string and convert it into binary value - Object val = FlatLaf.parseDefaultsValue( key, value ); - Object oldValue = applyProperty.apply( key, val ); + Object newValue = parseValue( key, value ); + Object oldValue = applyProperty.apply( key, newValue ); // remember previous value if( oldValues == null ) @@ -94,6 +96,13 @@ public class FlatStyleSupport return oldValues; } + private static Object parseValue( String key, String value ) { + if( value.startsWith( "$" ) ) + return UIManager.get( value.substring( 1 ) ); + + return FlatLaf.parseDefaultsValue( key, value ); + } + public static boolean hasStyle( JComponent c ) { return getStyle( c ) != null; }