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
extends BasicLookAndFeel
{
private static final String VARIABLE_PREFIX = "@";
private static final String GLOBAL_PREFIX = "*.";
private BasicLookAndFeel base;
@Override
@@ -143,14 +146,14 @@ public abstract class FlatLaf
}
// get globals, which override all other defaults that end with same suffix
String globalPrefix = "*.";
HashMap<String, Object> globals = new HashMap<>();
for( Object okey : properties.keySet() ) {
String key = (String) okey;
if( key.startsWith( globalPrefix ) ) {
Object value = parseValue( key, properties.getProperty( key ) );
globals.put( key.substring( globalPrefix.length() ), value );
}
for( Map.Entry<Object, Object> e : properties.entrySet() ) {
String key = (String) e.getKey();
if( !key.startsWith( GLOBAL_PREFIX ) )
continue;
String value = resolveVariable( properties, (String) e.getValue() );
globals.put( key.substring( GLOBAL_PREFIX.length() ), parseValue( key, value ) );
}
// override UI defaults with globals
@@ -167,7 +170,10 @@ public abstract class FlatLaf
// add non-global properties to UI defaults
for( Map.Entry<Object, Object> e : properties.entrySet() ) {
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 ) );
}
} 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 ) {
value = value.trim();

View File

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

View File

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