From 6f71e4ada0d291131d3f840d07e9987f3f5530de Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Tue, 7 Jul 2020 14:21:31 +0200 Subject: [PATCH] Theme Editor: use deferred properties loading --- .../com/formdev/flatlaf/UIDefaultsLoader.java | 15 +++++++++------ .../formdev/flatlaf/UIDefaultsLoaderAccessor.java | 5 ++--- .../themeeditor/FlatThemePropertiesSupport.java | 6 +++++- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java index b1501355..45620742 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java @@ -154,8 +154,11 @@ class UIDefaultsLoader } } + Function propertiesGetter = key -> { + return properties.getProperty( key ); + }; Function resolver = value -> { - return resolveValue( properties, value ); + return resolveValue( value, propertiesGetter ); }; // get globals, which override all other defaults that end with same suffix @@ -165,7 +168,7 @@ class UIDefaultsLoader if( !key.startsWith( GLOBAL_PREFIX ) ) continue; - String value = resolveValue( properties, (String) e.getValue() ); + String value = resolveValue( (String) e.getValue(), propertiesGetter ); try { globals.put( key.substring( GLOBAL_PREFIX.length() ), parseValue( key, value, null, resolver, addonClassLoaders ) ); @@ -191,7 +194,7 @@ class UIDefaultsLoader if( key.startsWith( VARIABLE_PREFIX ) || key.startsWith( GLOBAL_PREFIX ) ) continue; - String value = resolveValue( properties, (String) e.getValue() ); + String value = resolveValue( (String) e.getValue(), propertiesGetter ); try { defaults.put( key, parseValue( key, value, null, resolver, addonClassLoaders ) ); } catch( RuntimeException ex ) { @@ -207,7 +210,7 @@ class UIDefaultsLoader FlatLaf.LOG.log( level, "FlatLaf: Failed to parse: '" + key + '=' + value + '\'', ex ); } - static String resolveValue( Properties properties, String value ) { + static String resolveValue( String value, Function propertiesGetter ) { if( value.startsWith( PROPERTY_PREFIX ) ) value = value.substring( PROPERTY_PREFIX.length() ); else if( !value.startsWith( VARIABLE_PREFIX ) ) @@ -219,7 +222,7 @@ class UIDefaultsLoader optional = true; } - String newValue = properties.getProperty( value ); + String newValue = propertiesGetter.apply( value ); if( newValue == null ) { if( optional ) return "null"; @@ -227,7 +230,7 @@ class UIDefaultsLoader throw new IllegalArgumentException( "variable or property '" + value + "' not found" ); } - return resolveValue( properties, newValue ); + return resolveValue( newValue, propertiesGetter ); } enum ValueType { UNKNOWN, STRING, BOOLEAN, CHARACTER, INTEGER, FLOAT, BORDER, ICON, INSETS, DIMENSION, COLOR, diff --git a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/UIDefaultsLoaderAccessor.java b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/UIDefaultsLoaderAccessor.java index 3e1c1fc5..fb4698d1 100644 --- a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/UIDefaultsLoaderAccessor.java +++ b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/UIDefaultsLoaderAccessor.java @@ -17,7 +17,6 @@ package com.formdev.flatlaf; import java.util.Collections; -import java.util.Properties; import java.util.function.Function; import com.formdev.flatlaf.UIDefaultsLoader.ValueType; @@ -49,8 +48,8 @@ public class UIDefaultsLoaderAccessor public static Object NULL = ValueType.NULL; public static Object LAZY = ValueType.LAZY; - public static String resolveValue( Properties properties, String value ) { - return UIDefaultsLoader.resolveValue( properties, value ); + public static String resolveValue( String value, Function propertiesGetter ) { + return UIDefaultsLoader.resolveValue( value, propertiesGetter ); } public static Object parseValue( String key, String value, Object[] resultValueType, diff --git a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePropertiesSupport.java b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePropertiesSupport.java index 442f0ae1..0f215773 100644 --- a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePropertiesSupport.java +++ b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePropertiesSupport.java @@ -37,6 +37,7 @@ class FlatThemePropertiesSupport implements DocumentListener { private final FlatSyntaxTextArea textArea; + private final Function propertiesGetter; private final Function resolver; private Properties propertiesCache; private final Map parsedValueCache = new HashMap<>(); @@ -44,6 +45,9 @@ class FlatThemePropertiesSupport FlatThemePropertiesSupport( FlatSyntaxTextArea textArea ) { this.textArea = textArea; + propertiesGetter = key -> { + return getProperties().getProperty( key ); + }; resolver = v -> { return resolveValue( v ); }; @@ -52,7 +56,7 @@ class FlatThemePropertiesSupport } private String resolveValue( String value ) { - return UIDefaultsLoaderAccessor.resolveValue( getProperties(), value ); + return UIDefaultsLoaderAccessor.resolveValue( value, propertiesGetter ); } Object getParsedValueAtLine( int line ) {