use "variables" in .properties files

This commit is contained in:
Karl Tauber
2019-08-20 09:56:47 +02:00
parent 647bde2e5e
commit 85cfd9750c
3 changed files with 58 additions and 31 deletions

View File

@@ -40,6 +40,9 @@ import com.formdev.flatlaf.util.SystemInfo;
public abstract class FlatLaf public abstract class FlatLaf
extends BasicLookAndFeel extends BasicLookAndFeel
{ {
private static final String VARIABLE_PREFIX = "@";
private static final String GLOBAL_PREFIX = "*.";
private BasicLookAndFeel base; private BasicLookAndFeel base;
@Override @Override
@@ -143,14 +146,14 @@ public abstract class FlatLaf
} }
// get globals, which override all other defaults that end with same suffix // get globals, which override all other defaults that end with same suffix
String globalPrefix = "*.";
HashMap<String, Object> globals = new HashMap<>(); HashMap<String, Object> globals = new HashMap<>();
for( Object okey : properties.keySet() ) { for( Map.Entry<Object, Object> e : properties.entrySet() ) {
String key = (String) okey; String key = (String) e.getKey();
if( key.startsWith( globalPrefix ) ) { if( !key.startsWith( GLOBAL_PREFIX ) )
Object value = parseValue( key, properties.getProperty( key ) ); continue;
globals.put( key.substring( globalPrefix.length() ), value );
} String value = resolveVariable( properties, (String) e.getValue() );
globals.put( key.substring( GLOBAL_PREFIX.length() ), parseValue( key, value ) );
} }
// override UI defaults with globals // override UI defaults with globals
@@ -167,7 +170,10 @@ public abstract class FlatLaf
// add non-global properties to UI defaults // add non-global properties to UI defaults
for( Map.Entry<Object, Object> e : properties.entrySet() ) { for( Map.Entry<Object, Object> e : properties.entrySet() ) {
String key = (String) e.getKey(); String key = (String) e.getKey();
String value = (String) e.getValue(); if( key.startsWith( VARIABLE_PREFIX ) || key.startsWith( GLOBAL_PREFIX ) )
continue;
String value = resolveVariable( properties, (String) e.getValue() );
defaults.put( key, parseValue( key, value ) ); defaults.put( key, parseValue( key, value ) );
} }
} catch( IOException ex ) { } catch( IOException ex ) {
@@ -175,6 +181,17 @@ public abstract class FlatLaf
} }
} }
private String resolveVariable( Properties properties, String value ) {
if( !value.startsWith( VARIABLE_PREFIX ) )
return value;
String newValue = properties.getProperty( value );
if( newValue == null )
System.err.println( "variable '" + value + "' not found" );
return newValue;
}
private Object parseValue( String key, String value ) { private Object parseValue( String key, String value ) {
value = value.trim(); value = value.trim();

View File

@@ -14,27 +14,32 @@
# limitations under the License. # limitations under the License.
# #
#---- variables ----
@background=3c3f41
@foreground=bbbbbb
#---- globals ---- #---- globals ----
*.background=3c3f41 *.background=@background
*.foreground=bbbbbb *.foreground=@foreground
*.textBackground=3c3f41 *.textBackground=@background
*.textForeground=bbbbbb *.textForeground=@foreground
*.caretForeground=bbbbbb *.caretForeground=@foreground
*.inactiveBackground=3C3F41 *.inactiveBackground=@background
*.inactiveForeground=bbbbbb *.inactiveForeground=@foreground
*.selectionBackground=4B6EAF *.selectionBackground=4B6EAF
*.selectionForeground=bbbbbb *.selectionForeground=@foreground
#---- system ---- #---- system ----
control=3c3f41 control=@background
controlText=bbbbbb controlText=@foreground
infoText=bbbbbb infoText=@foreground
text=bbbbbb text=@foreground
textText=bbbbbb textText=@foreground
window=3c3f41 window=@background
#---- Label ---- #---- Label ----

View File

@@ -14,13 +14,18 @@
# limitations under the License. # limitations under the License.
# #
#---- variables ----
@background=f2f2f2
@foreground=000000
#---- globals ---- #---- globals ----
*.background=f2f2f2 *.background=@background
*.foreground=000000 *.foreground=@foreground
*.textBackground=cccccc *.textBackground=cccccc
*.textForeground=000000 *.textForeground=@foreground
*.caretForeground=000000 *.caretForeground=@foreground
*.inactiveBackground=ffffff *.inactiveBackground=ffffff
*.inactiveForeground=777777 *.inactiveForeground=777777
*.selectionBackground=4A6EB7 *.selectionBackground=4A6EB7
@@ -30,11 +35,11 @@
#---- system ---- #---- system ----
control=e0e0e0 control=e0e0e0
controlText=000000 controlText=@foreground
infoText=000000 infoText=@foreground
text=000000 text=@foreground
textText=000000 textText=@foreground
window=f2f2f2 window=@background
#---- Label ---- #---- Label ----