From 77f17eaa3ee4e7d6c89dc4ad00ed44019e58380c Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Tue, 5 May 2020 15:13:21 +0200 Subject: [PATCH] FlatPropertiesLaf class added that allows creating FlatLaf theme from properties (issue #97) --- .../formdev/flatlaf/FlatPropertiesLaf.java | 122 ++++++++++++++++++ .../com/formdev/flatlaf/demo/DemoPrefs.java | 6 +- .../demo/intellijthemes/IJThemesPanel.java | 81 +----------- 3 files changed, 128 insertions(+), 81 deletions(-) create mode 100644 flatlaf-core/src/main/java/com/formdev/flatlaf/FlatPropertiesLaf.java diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatPropertiesLaf.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatPropertiesLaf.java new file mode 100644 index 00000000..534f2115 --- /dev/null +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatPropertiesLaf.java @@ -0,0 +1,122 @@ +/* + * Copyright 2020 FormDev Software GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.formdev.flatlaf; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Properties; + +/** + * A Flat LaF that is able to load UI defaults from properties passed to the constructor. + *

+ * Specify the base theme in the properties with {@code @baseTheme=}. + * Allowed values for {@code } are {@code light} (the default), {@code dark}, + * {@code intellij} or {@code darcula}. + *

+ * The properties are applied after loading the base theme and may overwrite base properties. + * All features of FlatLaf properties files are available. + * + * @author Karl Tauber + */ +public class FlatPropertiesLaf + extends FlatLaf +{ + private final String name; + private final String baseTheme; + private final boolean dark; + private final Properties properties; + + public FlatPropertiesLaf( String name, File propertiesFile ) + throws IOException + { + this( name, new FileInputStream( propertiesFile ) ); + } + + public FlatPropertiesLaf( String name, InputStream in ) + throws IOException + { + this( name, loadProperties( in ) ); + } + + private static Properties loadProperties( InputStream in ) + throws IOException + { + Properties properties = new Properties(); + try( InputStream in2 = in ) { + properties.load( in2 ); + } + return properties; + } + + public FlatPropertiesLaf( String name, Properties properties ) { + this.name = name; + this.properties = properties; + + baseTheme = properties.getProperty( "@baseTheme", "light" ); + dark = "dark".equalsIgnoreCase( baseTheme ) || "darcula".equalsIgnoreCase( baseTheme ); + } + + @Override + public String getName() { + return name; + } + + @Override + public String getDescription() { + return name; + } + + @Override + public boolean isDark() { + return dark; + } + + @Override + protected ArrayList> getLafClassesForDefaultsLoading() { + ArrayList> lafClasses = new ArrayList<>(); + lafClasses.add( FlatLaf.class ); + switch( baseTheme.toLowerCase() ) { + default: + case "light": + lafClasses.add( FlatLightLaf.class ); + break; + + case "dark": + lafClasses.add( FlatDarkLaf.class ); + break; + + case "intellij": + lafClasses.add( FlatLightLaf.class ); + lafClasses.add( FlatIntelliJLaf.class ); + break; + + case "darcula": + lafClasses.add( FlatDarkLaf.class ); + lafClasses.add( FlatDarculaLaf.class ); + break; + } + return lafClasses; + } + + @Override + protected Properties getAdditionalDefaults() { + return properties; + } +} diff --git a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/DemoPrefs.java b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/DemoPrefs.java index 5203817f..dc706e5f 100644 --- a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/DemoPrefs.java +++ b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/DemoPrefs.java @@ -22,9 +22,9 @@ import java.util.prefs.Preferences; import javax.swing.UIManager; import com.formdev.flatlaf.FlatLaf; import com.formdev.flatlaf.FlatLightLaf; +import com.formdev.flatlaf.FlatPropertiesLaf; import com.formdev.flatlaf.IntelliJTheme; import com.formdev.flatlaf.demo.intellijthemes.IJThemesPanel; -import com.formdev.flatlaf.demo.intellijthemes.IJThemesPanel.PropertiesLaf; import com.formdev.flatlaf.util.StringUtils; /** @@ -68,12 +68,12 @@ public class DemoPrefs if( !theme.isEmpty() ) UIManager.getLookAndFeelDefaults().put( THEME_UI_KEY, theme ); - } else if( IJThemesPanel.PropertiesLaf.class.getName().equals( lafClassName ) ) { + } else if( FlatPropertiesLaf.class.getName().equals( lafClassName ) ) { String theme = state.get( KEY_LAF_THEME, "" ); if( theme.startsWith( FILE_PREFIX ) ) { File themeFile = new File( theme.substring( FILE_PREFIX.length() ) ); String themeName = StringUtils.removeTrailing( themeFile.getName(), ".properties" ); - FlatLaf.install( new PropertiesLaf( themeName, themeFile ) ); + FlatLaf.install( new FlatPropertiesLaf( themeName, themeFile ) ); } else FlatLightLaf.install(); diff --git a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/intellijthemes/IJThemesPanel.java b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/intellijthemes/IJThemesPanel.java index 45cf3742..f247b3d0 100644 --- a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/intellijthemes/IJThemesPanel.java +++ b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/intellijthemes/IJThemesPanel.java @@ -28,7 +28,6 @@ import java.beans.PropertyChangeListener; import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStream; import java.net.URI; import java.net.URISyntaxException; import java.nio.file.Files; @@ -38,7 +37,6 @@ import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Objects; -import java.util.Properties; import java.util.function.Predicate; import javax.swing.*; import javax.swing.border.CompoundBorder; @@ -48,6 +46,7 @@ import com.formdev.flatlaf.FlatDarkLaf; import com.formdev.flatlaf.FlatIntelliJLaf; import com.formdev.flatlaf.FlatLaf; import com.formdev.flatlaf.FlatLightLaf; +import com.formdev.flatlaf.FlatPropertiesLaf; import com.formdev.flatlaf.IntelliJTheme; import com.formdev.flatlaf.demo.DemoPrefs; import com.formdev.flatlaf.extras.FlatSVGIcon; @@ -229,7 +228,7 @@ public class IJThemesPanel } else if( themeInfo.themeFile != null ) { try { if( themeInfo.themeFile.getName().endsWith( ".properties" ) ) { - FlatLaf.install( new PropertiesLaf( themeInfo.name, themeInfo.themeFile ) ); + FlatLaf.install( new FlatPropertiesLaf( themeInfo.name, themeInfo.themeFile ) ); } else FlatLaf.install( IntelliJTheme.createLaf( new FileInputStream( themeInfo.themeFile ) ) ); @@ -342,7 +341,7 @@ public class IJThemesPanel LookAndFeel lookAndFeel = UIManager.getLookAndFeel(); String theme = UIManager.getLookAndFeelDefaults().getString( DemoPrefs.THEME_UI_KEY ); - if( theme == null && (lookAndFeel instanceof IntelliJTheme.ThemeLaf || lookAndFeel instanceof PropertiesLaf) ) + if( theme == null && (lookAndFeel instanceof IntelliJTheme.ThemeLaf || lookAndFeel instanceof FlatPropertiesLaf) ) return; Predicate test; @@ -431,78 +430,4 @@ public class IJThemesPanel private JScrollPane themesScrollPane; private JList themesList; // JFormDesigner - End of variables declaration //GEN-END:variables - - //---- class PropertiesLaf ------------------------------------------------ - - public static class PropertiesLaf - extends FlatLaf - { - private final String name; - private final String baseTheme; - private final boolean dark; - private final Properties properties; - - public PropertiesLaf( String name, File propertiesFile ) - throws IOException - { - this.name = name; - - properties = new Properties(); - try( InputStream in = new FileInputStream( propertiesFile ) ) { - if( in != null ) - properties.load( in ); - } - - baseTheme = properties.getProperty( "@baseTheme", "light" ); - dark = "dark".equalsIgnoreCase( baseTheme ) || "darcula".equalsIgnoreCase( baseTheme ); - } - - @Override - public String getName() { - return name; - } - - @Override - public String getDescription() { - return name; - } - - @Override - public boolean isDark() { - return dark; - } - - @Override - protected ArrayList> getLafClassesForDefaultsLoading() { - ArrayList> lafClasses = new ArrayList<>(); - lafClasses.add( FlatLaf.class ); - switch( baseTheme.toLowerCase() ) { - default: - case "light": - lafClasses.add( FlatLightLaf.class ); - break; - - case "dark": - lafClasses.add( FlatDarkLaf.class ); - break; - - case "intellij": - lafClasses.add( FlatLightLaf.class ); - lafClasses.add( FlatIntelliJLaf.class ); - break; - - case "darcula": - lafClasses.add( FlatDarkLaf.class ); - lafClasses.add( FlatDarculaLaf.class ); - break; - } - lafClasses.add( PropertiesLaf.class ); - return lafClasses; - } - - @Override - protected Properties getAdditionalDefaults() { - return properties; - } - } }