diff --git a/core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelCMYK.java b/core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelCMYK.java index ac02f47b..cb473154 100644 --- a/core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelCMYK.java +++ b/core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelCMYK.java @@ -26,6 +26,8 @@ package com.github.weisj.darklaf.color; import java.awt.*; +import com.github.weisj.darklaf.util.LazyValue; + /** * @author Jannis Weis */ @@ -34,11 +36,10 @@ public class DarkColorModelCMYK extends DarkColorModel { private static final int[] cmyk = new int[4]; private static final int[] rgb = new int[3]; - private static DarkColorModelCMYK instance; + private static final LazyValue instance = new LazyValue<>(DarkColorModelCMYK::new); public static DarkColorModelCMYK getInstance() { - if (instance == null) instance = new DarkColorModelCMYK(); - return instance; + return instance.get(); } public DarkColorModelCMYK() { diff --git a/core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelHSB.java b/core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelHSB.java index fe57fed7..920918cc 100644 --- a/core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelHSB.java +++ b/core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelHSB.java @@ -26,6 +26,8 @@ package com.github.weisj.darklaf.color; import java.awt.*; +import com.github.weisj.darklaf.util.LazyValue; + /** * @author Jannis Weis */ @@ -34,11 +36,10 @@ public class DarkColorModelHSB extends DarkColorModel { private static final float[] hsvVals = new float[3]; private static final int[] hsb = new int[3]; - private static DarkColorModelHSB instance; + private static final LazyValue instance = new LazyValue<>(DarkColorModelHSB::new); public static DarkColorModelHSB getInstance() { - if (instance == null) instance = new DarkColorModelHSB(); - return instance; + return instance.get(); } public DarkColorModelHSB() { diff --git a/core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelHSL.java b/core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelHSL.java index 0484b026..43f7e5b3 100644 --- a/core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelHSL.java +++ b/core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelHSL.java @@ -26,6 +26,8 @@ package com.github.weisj.darklaf.color; import java.awt.*; +import com.github.weisj.darklaf.util.LazyValue; + /** * @author Jannis Weis */ @@ -34,11 +36,10 @@ public class DarkColorModelHSL extends DarkColorModel { private static final int[] hsl = new int[3]; private static final int[] rgb = new int[3]; - private static DarkColorModelHSL instance; + private static final LazyValue instance = new LazyValue<>(DarkColorModelHSL::new); public static DarkColorModelHSL getInstance() { - if (instance == null) instance = new DarkColorModelHSL(); - return instance; + return instance.get(); } public DarkColorModelHSL() { diff --git a/core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelRGB.java b/core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelRGB.java index 970303c8..635fe7d4 100644 --- a/core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelRGB.java +++ b/core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelRGB.java @@ -26,13 +26,14 @@ package com.github.weisj.darklaf.color; import java.awt.*; +import com.github.weisj.darklaf.util.LazyValue; + public class DarkColorModelRGB extends DarkColorModel { - private static DarkColorModelRGB instance; + private static final LazyValue instance = new LazyValue<>(DarkColorModelRGB::new); public static DarkColorModelRGB getInstance() { - if (instance == null) instance = new DarkColorModelRGB(); - return instance; + return instance.get(); } public DarkColorModelRGB() { diff --git a/core/src/main/java/com/github/weisj/darklaf/components/tooltip/TooltipAwareButton.java b/core/src/main/java/com/github/weisj/darklaf/components/tooltip/TooltipAwareButton.java index e9d287a1..6fb25bc4 100644 --- a/core/src/main/java/com/github/weisj/darklaf/components/tooltip/TooltipAwareButton.java +++ b/core/src/main/java/com/github/weisj/darklaf/components/tooltip/TooltipAwareButton.java @@ -28,10 +28,14 @@ import javax.swing.*; import com.github.weisj.darklaf.ui.tooltip.DarkTooltipUI; import com.github.weisj.darklaf.util.Alignment; +import com.github.weisj.darklaf.util.LazyValue; public class TooltipAwareButton extends JButton implements ToolTipAware { - private ToolTipContext context; + private final LazyValue context = new LazyValue<>(() -> new ToolTipContext().setAlignment(Alignment.SOUTH) + .setCenterAlignment(Alignment.SOUTH) + .setAlignInside(false) + .setIgnoreBorder(true)); public TooltipAwareButton() { this(null, null); @@ -62,12 +66,6 @@ public class TooltipAwareButton extends JButton implements ToolTipAware { @Override public ToolTipContext getToolTipContext() { - if (context == null) { - context = new ToolTipContext().setAlignment(Alignment.SOUTH) - .setCenterAlignment(Alignment.SOUTH) - .setAlignInside(false) - .setIgnoreBorder(true); - } - return context; + return context.get(); } } diff --git a/core/src/main/java/com/github/weisj/darklaf/components/tooltip/TooltipAwareToggleButton.java b/core/src/main/java/com/github/weisj/darklaf/components/tooltip/TooltipAwareToggleButton.java index 21b9b9b5..13fa6606 100644 --- a/core/src/main/java/com/github/weisj/darklaf/components/tooltip/TooltipAwareToggleButton.java +++ b/core/src/main/java/com/github/weisj/darklaf/components/tooltip/TooltipAwareToggleButton.java @@ -28,10 +28,14 @@ import javax.swing.*; import com.github.weisj.darklaf.ui.tooltip.DarkTooltipUI; import com.github.weisj.darklaf.util.Alignment; +import com.github.weisj.darklaf.util.LazyValue; public class TooltipAwareToggleButton extends JToggleButton implements ToolTipAware { - private ToolTipContext context; + private final LazyValue context = new LazyValue<>(() -> new ToolTipContext().setAlignment(Alignment.SOUTH) + .setCenterAlignment(Alignment.SOUTH) + .setAlignInside(false) + .setIgnoreBorder(true)); public TooltipAwareToggleButton() { this(null, null); @@ -62,12 +66,6 @@ public class TooltipAwareToggleButton extends JToggleButton implements ToolTipAw @Override public ToolTipContext getToolTipContext() { - if (context == null) { - context = new ToolTipContext().setAlignment(Alignment.SOUTH) - .setCenterAlignment(Alignment.SOUTH) - .setAlignInside(false) - .setIgnoreBorder(true); - } - return context; + return context.get(); } } diff --git a/core/src/main/java/com/github/weisj/darklaf/settings/ThemeSettings.java b/core/src/main/java/com/github/weisj/darklaf/settings/ThemeSettings.java index e10842ab..99843324 100644 --- a/core/src/main/java/com/github/weisj/darklaf/settings/ThemeSettings.java +++ b/core/src/main/java/com/github/weisj/darklaf/settings/ThemeSettings.java @@ -39,22 +39,22 @@ import com.github.weisj.darklaf.theme.event.ThemePreferenceListener; import com.github.weisj.darklaf.theme.info.AccentColorRule; import com.github.weisj.darklaf.theme.info.FontSizeRule; import com.github.weisj.darklaf.util.DarkUIUtil; +import com.github.weisj.darklaf.util.LazyValue; public class ThemeSettings implements ThemePreferenceListener { - private static ThemeSettings instance; + private static final LazyValue instance = new LazyValue<>(ThemeSettings::new); private final JPanel contentPane; private final ThemeSettingsPanel settingsPanel; private final ResourceBundle resourceBundle = ResourceBundle.getBundle("theme_settings"); private JDialog dialog; public static ThemeSettings getInstance() { - if (!isInitialized()) instance = new ThemeSettings(); - return instance; + return instance.get(); } public static boolean isInitialized() { - return instance != null; + return instance.isInitialized(); } /** diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/filechooser/DarkFileChooserUIBridge.java b/core/src/main/java/com/github/weisj/darklaf/ui/filechooser/DarkFileChooserUIBridge.java index 1c31c862..6a185a37 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/filechooser/DarkFileChooserUIBridge.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/filechooser/DarkFileChooserUIBridge.java @@ -172,7 +172,7 @@ public abstract class DarkFileChooserUIBridge extends BasicFileChooserUI { Locale l = fc.getLocale(); - UIDefaults defaults = MetalUIDefaults.DEFAULTS; + UIDefaults defaults = MetalUIDefaults.get(); lookInLabelMnemonic = getMnemonic(defaults, "FileChooser.lookInLabelMnemonic", l); lookInLabelText = defaults.getString("FileChooser.lookInLabelText", l); diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/filechooser/DarkFilePaneUIBridge.java b/core/src/main/java/com/github/weisj/darklaf/ui/filechooser/DarkFilePaneUIBridge.java index 1745c63b..49b4f366 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/filechooser/DarkFilePaneUIBridge.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/filechooser/DarkFilePaneUIBridge.java @@ -457,7 +457,7 @@ public abstract class DarkFilePaneUIBridge extends JPanel implements PropertyCha readOnly = UIManager.getBoolean("FileChooser.readOnly"); // TUDU: On windows, get the following localized strings from the OS - UIDefaults metal = MetalUIDefaults.DEFAULTS; + UIDefaults metal = MetalUIDefaults.get(); viewMenuLabelText = metal.getString("FileChooser.viewMenuLabelText", l); refreshActionLabelText = metal.getString("FileChooser.refreshActionLabelText", l); @@ -600,7 +600,7 @@ public abstract class DarkFilePaneUIBridge extends JPanel implements PropertyCha String title = dataItem.getTitle(); if (title != null && title.startsWith("FileChooser.") && title.endsWith("HeaderText")) { // the column must have a string resource that we try to get - String uiTitle = MetalUIDefaults.DEFAULTS.getString(title, table.getLocale()); + String uiTitle = MetalUIDefaults.get().getString(title, table.getLocale()); if (uiTitle != null) { title = uiTitle; } diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/filechooser/MetalUIDefaults.java b/core/src/main/java/com/github/weisj/darklaf/ui/filechooser/MetalUIDefaults.java index 33900cc4..aee52b4e 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/filechooser/MetalUIDefaults.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/filechooser/MetalUIDefaults.java @@ -27,5 +27,13 @@ package com.github.weisj.darklaf.ui.filechooser; import javax.swing.*; import javax.swing.plaf.metal.MetalLookAndFeel; +import com.github.weisj.darklaf.util.LazyValue; + // Extra class is for lazy initialization -class MetalUIDefaults { static final UIDefaults DEFAULTS = new MetalLookAndFeel().getDefaults(); } +class MetalUIDefaults { + private static final LazyValue DEFAULTS = new LazyValue<>(() -> new MetalLookAndFeel().getDefaults()); + + static UIDefaults get() { + return DEFAULTS.get(); + } +} diff --git a/property-loader/src/main/java/com/github/weisj/darklaf/icons/IconLoader.java b/property-loader/src/main/java/com/github/weisj/darklaf/icons/IconLoader.java index f58865ad..de9140a9 100644 --- a/property-loader/src/main/java/com/github/weisj/darklaf/icons/IconLoader.java +++ b/property-loader/src/main/java/com/github/weisj/darklaf/icons/IconLoader.java @@ -35,13 +35,15 @@ import java.util.logging.Logger; import javax.swing.*; +import com.github.weisj.darklaf.util.LazyValue; + /** * @author Jannis Weis */ public final class IconLoader { private static final Logger LOGGER = Logger.getLogger(IconLoader.class.getName()); private static final Map, IconLoader> iconLoaderMap = new HashMap<>(); - private static IconLoader instance; + private static final LazyValue instance = new LazyValue<>(() -> get(null)); private static Object currentThemeKey; private static AwareIconStyle currentAwareStyle; @@ -63,8 +65,7 @@ public final class IconLoader { * @return the default icon loader. */ public static IconLoader get() { - if (instance == null) instance = get(null); - return instance; + return instance.get(); } /** diff --git a/utils/src/main/java/com/github/weisj/darklaf/util/LazyValue.java b/utils/src/main/java/com/github/weisj/darklaf/util/LazyValue.java new file mode 100644 index 00000000..9a366a3b --- /dev/null +++ b/utils/src/main/java/com/github/weisj/darklaf/util/LazyValue.java @@ -0,0 +1,50 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jannis Weis + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ +package com.github.weisj.darklaf.util; + +import java.util.function.Supplier; + +public class LazyValue { + + private Supplier supplier; + private T value; + + public LazyValue(final Supplier supplier) { + + this.supplier = supplier; + } + + public boolean isInitialized() { + return supplier == null; + } + + public T get() { + if (value == null && supplier != null) { + value = supplier.get(); + supplier = null; + } + return value; + } +} diff --git a/utils/src/main/java/com/github/weisj/darklaf/util/PropertyUtil.java b/utils/src/main/java/com/github/weisj/darklaf/util/PropertyUtil.java index 39e61005..9a068707 100644 --- a/utils/src/main/java/com/github/weisj/darklaf/util/PropertyUtil.java +++ b/utils/src/main/java/com/github/weisj/darklaf/util/PropertyUtil.java @@ -20,6 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.github.weisj.darklaf.util;