From 51ae12d05e3e4866656c1ff17ae4159c6930ade3 Mon Sep 17 00:00:00 2001 From: weisj Date: Fri, 18 Sep 2020 23:39:55 +0200 Subject: [PATCH] Fix error messages in DefaultThemeProvider constructor. Ensure themes in DefaultThemeProvider are non-null. Provide factory methods fro ThemeProvider. --- .../theme/info/DefaultThemeProvider.java | 29 +++++++++++++++++-- .../darklaf/theme/info/ThemeProvider.java | 9 ++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/theme/src/main/java/com/github/weisj/darklaf/theme/info/DefaultThemeProvider.java b/theme/src/main/java/com/github/weisj/darklaf/theme/info/DefaultThemeProvider.java index 87d6a552..2a709386 100644 --- a/theme/src/main/java/com/github/weisj/darklaf/theme/info/DefaultThemeProvider.java +++ b/theme/src/main/java/com/github/weisj/darklaf/theme/info/DefaultThemeProvider.java @@ -21,6 +21,8 @@ */ package com.github.weisj.darklaf.theme.info; +import java.util.Objects; + import com.github.weisj.darklaf.theme.*; public class DefaultThemeProvider implements ThemeProvider { @@ -34,8 +36,16 @@ public class DefaultThemeProvider implements ThemeProvider { this(new IntelliJTheme(), new DarculaTheme(), new HighContrastLightTheme(), new HighContrastDarkTheme()); } + public DefaultThemeProvider(final Theme lightTheme, final Theme darkTheme) { + this(lightTheme, darkTheme, new HighContrastLightTheme(), new HighContrastDarkTheme()); + } + public DefaultThemeProvider(final Theme lightTheme, final Theme darkTheme, final Theme lightHighContrastTheme, final Theme darkHighContrastTheme) { + Objects.requireNonNull(lightTheme, "Light theme is null"); + Objects.requireNonNull(darkTheme, "Dark theme is null"); + Objects.requireNonNull(lightHighContrastTheme, "Light high contrast theme is null"); + Objects.requireNonNull(darkHighContrastTheme, "Dark high contrast theme is null"); /* * Ensure the given themes actually serve the purpose they are intended for. */ @@ -47,11 +57,11 @@ public class DefaultThemeProvider implements ThemeProvider { "Given light high-contrast theme " + lightHighContrastTheme + " is declared as dark"); } if (!Theme.isDark(darkTheme)) { - throw new UnsupportedThemeException("Given dark theme " + darkTheme + "is not declared as dark"); + throw new UnsupportedThemeException("Given dark theme " + darkTheme + " is not declared as dark"); } if (!Theme.isDark(darkHighContrastTheme)) { throw new UnsupportedThemeException( - "Given dark high-contrast theme " + darkTheme + "is not declared as dark"); + "Given dark high-contrast theme " + darkHighContrastTheme + "is not declared as dark"); } /* * A high contrast theme may serve as a standard theme, but a standard theme should never be used @@ -81,4 +91,19 @@ public class DefaultThemeProvider implements ThemeProvider { return theme.derive(themeStyle.getFontSizeRule(), themeStyle.getAccentColorRule()); } + + @Override + public boolean equals(final Object o) { + if (this == o) return true; + if (!(o instanceof DefaultThemeProvider)) return false; + DefaultThemeProvider that = (DefaultThemeProvider) o; + return lightTheme.appearsEqualTo(that.lightTheme) && darkTheme.appearsEqualTo(that.darkTheme) + && lightHighContrastTheme.appearsEqualTo(that.lightHighContrastTheme) + && darkHighContrastTheme.appearsEqualTo(that.darkHighContrastTheme); + } + + @Override + public int hashCode() { + return Objects.hash(lightTheme, darkTheme, lightHighContrastTheme, darkHighContrastTheme); + } } diff --git a/theme/src/main/java/com/github/weisj/darklaf/theme/info/ThemeProvider.java b/theme/src/main/java/com/github/weisj/darklaf/theme/info/ThemeProvider.java index 5054a038..bb2df8e7 100644 --- a/theme/src/main/java/com/github/weisj/darklaf/theme/info/ThemeProvider.java +++ b/theme/src/main/java/com/github/weisj/darklaf/theme/info/ThemeProvider.java @@ -26,4 +26,13 @@ import com.github.weisj.darklaf.theme.Theme; public interface ThemeProvider { Theme getTheme(final PreferredThemeStyle themeStyle); + + static ThemeProvider fromThemes(final Theme lightTheme, final Theme darkTheme) { + return new DefaultThemeProvider(lightTheme, darkTheme); + } + + static ThemeProvider fromThemes(final Theme lightTheme, final Theme darkTheme, final Theme lightHighContrastTheme, + final Theme darkHighContrastTheme) { + return new DefaultThemeProvider(lightTheme, darkTheme, lightHighContrastTheme, darkHighContrastTheme); + } }