mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-13 07:17:13 -06:00
Styling: support using simple references to UI defaults
e.g. `mySlider.putClientProperty( "JComponent.style", "thumbColor: $TextField.background; thumbBorderColor: $Component.borderColor" );`
This commit is contained in:
@@ -21,6 +21,7 @@ import java.util.Map;
|
|||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
import javax.swing.JComponent;
|
import javax.swing.JComponent;
|
||||||
|
import javax.swing.UIManager;
|
||||||
import com.formdev.flatlaf.FlatClientProperties;
|
import com.formdev.flatlaf.FlatClientProperties;
|
||||||
import com.formdev.flatlaf.FlatLaf;
|
import com.formdev.flatlaf.FlatLaf;
|
||||||
import com.formdev.flatlaf.util.StringUtils;
|
import com.formdev.flatlaf.util.StringUtils;
|
||||||
@@ -45,6 +46,7 @@ public class FlatStyleSupport
|
|||||||
* the function must return the old value
|
* the function must return the old value
|
||||||
* @return map of old values modified by the given style, or {@code null}
|
* @return map of old values modified by the given style, or {@code null}
|
||||||
* @throws IllegalArgumentException on syntax errors
|
* @throws IllegalArgumentException on syntax errors
|
||||||
|
* @throws ClassCastException if value type does not fit to expected type
|
||||||
*/
|
*/
|
||||||
public static Map<String, Object> parse( Map<String, Object> oldStyleValues,
|
public static Map<String, Object> parse( Map<String, Object> oldStyleValues,
|
||||||
String style, BiFunction<String, Object, Object> applyProperty ) throws IllegalArgumentException
|
String style, BiFunction<String, Object, Object> applyProperty ) throws IllegalArgumentException
|
||||||
@@ -82,8 +84,8 @@ public class FlatStyleSupport
|
|||||||
throw new IllegalArgumentException( "missing value in '" + part + "'" );
|
throw new IllegalArgumentException( "missing value in '" + part + "'" );
|
||||||
|
|
||||||
// parse value string and convert it into binary value
|
// parse value string and convert it into binary value
|
||||||
Object val = FlatLaf.parseDefaultsValue( key, value );
|
Object newValue = parseValue( key, value );
|
||||||
Object oldValue = applyProperty.apply( key, val );
|
Object oldValue = applyProperty.apply( key, newValue );
|
||||||
|
|
||||||
// remember previous value
|
// remember previous value
|
||||||
if( oldValues == null )
|
if( oldValues == null )
|
||||||
@@ -94,6 +96,13 @@ public class FlatStyleSupport
|
|||||||
return oldValues;
|
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 ) {
|
public static boolean hasStyle( JComponent c ) {
|
||||||
return getStyle( c ) != null;
|
return getStyle( c ) != null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user