diff --git a/core/src/main/java/com/github/weisj/darklaf/LafManager.java b/core/src/main/java/com/github/weisj/darklaf/LafManager.java index 5c16b636..f2f0e059 100644 --- a/core/src/main/java/com/github/weisj/darklaf/LafManager.java +++ b/core/src/main/java/com/github/weisj/darklaf/LafManager.java @@ -26,8 +26,7 @@ package com.github.weisj.darklaf; import com.github.weisj.darklaf.platform.DecorationsHandler; import com.github.weisj.darklaf.platform.ThemePreferencesHandler; import com.github.weisj.darklaf.task.DefaultsAdjustmentTask; -import com.github.weisj.darklaf.theme.DarculaTheme; -import com.github.weisj.darklaf.theme.Theme; +import com.github.weisj.darklaf.theme.*; import com.github.weisj.darklaf.theme.event.ThemePreferenceListener; import com.github.weisj.darklaf.theme.info.DefaultThemeProvider; import com.github.weisj.darklaf.theme.info.PreferredThemeStyle; @@ -38,8 +37,8 @@ import java.awt.*; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.InvocationTargetException; -import java.util.Collection; -import java.util.HashSet; +import java.util.List; +import java.util.*; import java.util.logging.Level; import java.util.logging.LogManager; import java.util.logging.Logger; @@ -54,10 +53,18 @@ public final class LafManager { private static ThemeProvider themeProvider; private static Theme theme; private static boolean logEnabled = false; + private static final List registeredThemes = new ArrayList<>(); private static final Collection uiDefaultsTasks = new HashSet<>(); static { enableLogging(true); + registerTheme(new IntelliJTheme(), + new DarculaTheme(), + new SolarizedLightTheme(), + new SolarizedDarkTheme(), + new OneDarkTheme(), + new HighContrastLightTheme(), + new HighContrastDarkTheme()); } /** @@ -205,6 +212,50 @@ public final class LafManager { return getThemeProvider().getTheme(style); } + /** + * Register all themes in array. Registered themes are returned in {@link #getRegisteredThemes()}. + * + * @param themes the themes to register. + */ + public static void registerTheme(final Theme... themes) { + if (themes == null) return; + for (Theme theme : themes) { + registerTheme(theme); + } + } + + /** + * Register a theme. Registered themes are returned in {@link #getRegisteredThemes()}. + * + * @param theme the theme to register. + */ + public static void registerTheme(final Theme theme) { + registeredThemes.add(theme); + } + + /** + * Remove a register a theme. Registered themes are returned in {@link #getRegisteredThemes()}. + * + * @param theme the theme to register. + */ + public static void unregisterTheme(final Theme theme) { + registeredThemes.remove(theme); + } + + /** + * Get all currently registered themes. + * + * @return all themes currently registered as array. + */ + public static Theme[] getRegisteredThemes() { + Theme[] themes = registeredThemes.toArray(new Theme[0]); + Arrays.sort(themes); + return themes; + } + + public static ComboBoxModel getThemeComboBoxModel() { + return new DefaultComboBoxModel<>(getRegisteredThemes()); + } /** * Get the current theme. This method will never return null even if the LaF isn#t currently installed. @@ -259,9 +310,9 @@ public final class LafManager { } /** - * Reloads the icon theme. Forces icons to update their colors. + * Reloads the theme. Forces all properties to be reloaded. */ - public static void reloadIconTheme() { + public static void reloadTheme() { try { setTheme(getTheme().getClass().getDeclaredConstructor().newInstance()); } catch (InstantiationException diff --git a/core/src/test/java/documentation/CreateUITable.java b/core/src/test/java/documentation/CreateUITable.java index b0ded28c..7b16225c 100644 --- a/core/src/test/java/documentation/CreateUITable.java +++ b/core/src/test/java/documentation/CreateUITable.java @@ -29,7 +29,7 @@ import com.github.weisj.darklaf.PropertyLoader; import com.github.weisj.darklaf.components.border.DropShadowBorder; import com.github.weisj.darklaf.icons.DarkSVGIcon; import com.github.weisj.darklaf.icons.EmptyIcon; -import com.github.weisj.darklaf.theme.*; +import com.github.weisj.darklaf.theme.Theme; import com.github.weisj.darklaf.util.*; import com.kitfox.svg.app.beans.SVGIcon; import defaults.SampleRenderer; @@ -61,12 +61,7 @@ public class CreateUITable { private UIDefaults currentDefaults; public static void main(final String[] args) throws IOException { - for (Theme theme : new Theme[]{ - new IntelliJTheme(), - new DarculaTheme(), - new SolarizedLightTheme(), - new SolarizedDarkTheme() - }) { + for (Theme theme : LafManager.getRegisteredThemes()) { createThemeDefaultsPage(theme); } } diff --git a/core/src/test/java/icon/IconDemo.java b/core/src/test/java/icon/IconDemo.java index e9540110..18d1d0d4 100644 --- a/core/src/test/java/icon/IconDemo.java +++ b/core/src/test/java/icon/IconDemo.java @@ -80,7 +80,7 @@ public class IconDemo implements ComponentDemo { private static void updateThemedIconColor(final Color c) { UIManager.put("TestIcon.color", c); - LafManager.reloadIconTheme(); + LafManager.reloadTheme(); } @Override diff --git a/core/src/test/java/ui/ComponentDemo.java b/core/src/test/java/ui/ComponentDemo.java index 80b5444f..ec4f96e0 100644 --- a/core/src/test/java/ui/ComponentDemo.java +++ b/core/src/test/java/ui/ComponentDemo.java @@ -24,7 +24,7 @@ package ui; import com.github.weisj.darklaf.LafManager; -import com.github.weisj.darklaf.theme.*; +import com.github.weisj.darklaf.theme.Theme; import com.github.weisj.darklaf.theme.info.ColorToneRule; import com.github.weisj.darklaf.theme.info.ContrastRule; import com.github.weisj.darklaf.theme.info.PreferredThemeStyle; @@ -75,13 +75,7 @@ public interface ComponentDemo { String currentThemeName = LafManager.getTheme().getClass().getSimpleName(); JMenu menu = new JMenu("Theme"); ButtonGroup bg = new ButtonGroup(); - for (Theme theme : new Theme[]{new DarculaTheme(), - new IntelliJTheme(), - new SolarizedLightTheme(), - new SolarizedDarkTheme(), - new OneDarkTheme(), - new HighContrastLightTheme(), - new HighContrastDarkTheme()}) { + for (Theme theme : LafManager.getRegisteredThemes()) { createThemeItem(currentThemeName, menu, bg, theme); } return menu; diff --git a/theme/src/main/java/com/github/weisj/darklaf/theme/Theme.java b/theme/src/main/java/com/github/weisj/darklaf/theme/Theme.java index 4c3ca009..53b91ae8 100644 --- a/theme/src/main/java/com/github/weisj/darklaf/theme/Theme.java +++ b/theme/src/main/java/com/github/weisj/darklaf/theme/Theme.java @@ -36,6 +36,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; +import java.util.Comparator; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; @@ -43,7 +44,7 @@ import java.util.logging.Logger; /** * @author Jannis Weis */ -public abstract class Theme { +public abstract class Theme implements Comparable, Comparator { private static final Logger LOGGER = Logger.getLogger(Theme.class.getName()); private FontSizeRule fontSizeRule; @@ -326,4 +327,20 @@ public abstract class Theme { this.fontSizeRule = fontSizeRule; } + @Override + public int compareTo(final Theme o) { + if (o == null) return 1; + int stringComp = getName().compareTo(o.getName()); + int contrastCompare = Boolean.compare(isHighContrast(this), isHighContrast(o)); + int toneCompare = Boolean.compare(isDark(this), isDark(o)); + if (contrastCompare != 0) return contrastCompare; + if (toneCompare != 0) return toneCompare; + return stringComp; + } + + @Override + public int compare(final Theme o1, final Theme o2) { + if (o1 == null) return -1; + return o1.compareTo(o2); + } }