Browse Source

Simplified initialization of lazy singletons.

pull/154/head
weisj 5 years ago
parent
commit
f23a523525
  1. 7
      core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelCMYK.java
  2. 7
      core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelHSB.java
  3. 7
      core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelHSL.java
  4. 7
      core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelRGB.java
  5. 14
      core/src/main/java/com/github/weisj/darklaf/components/tooltip/TooltipAwareButton.java
  6. 14
      core/src/main/java/com/github/weisj/darklaf/components/tooltip/TooltipAwareToggleButton.java
  7. 8
      core/src/main/java/com/github/weisj/darklaf/settings/ThemeSettings.java
  8. 2
      core/src/main/java/com/github/weisj/darklaf/ui/filechooser/DarkFileChooserUIBridge.java
  9. 4
      core/src/main/java/com/github/weisj/darklaf/ui/filechooser/DarkFilePaneUIBridge.java
  10. 10
      core/src/main/java/com/github/weisj/darklaf/ui/filechooser/MetalUIDefaults.java
  11. 7
      property-loader/src/main/java/com/github/weisj/darklaf/icons/IconLoader.java
  12. 50
      utils/src/main/java/com/github/weisj/darklaf/util/LazyValue.java
  13. 1
      utils/src/main/java/com/github/weisj/darklaf/util/PropertyUtil.java

7
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<DarkColorModelCMYK> instance = new LazyValue<>(DarkColorModelCMYK::new);
public static DarkColorModelCMYK getInstance() {
if (instance == null) instance = new DarkColorModelCMYK();
return instance;
return instance.get();
}
public DarkColorModelCMYK() {

7
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<DarkColorModelHSB> instance = new LazyValue<>(DarkColorModelHSB::new);
public static DarkColorModelHSB getInstance() {
if (instance == null) instance = new DarkColorModelHSB();
return instance;
return instance.get();
}
public DarkColorModelHSB() {

7
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<DarkColorModelHSL> instance = new LazyValue<>(DarkColorModelHSL::new);
public static DarkColorModelHSL getInstance() {
if (instance == null) instance = new DarkColorModelHSL();
return instance;
return instance.get();
}
public DarkColorModelHSL() {

7
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<DarkColorModelRGB> instance = new LazyValue<>(DarkColorModelRGB::new);
public static DarkColorModelRGB getInstance() {
if (instance == null) instance = new DarkColorModelRGB();
return instance;
return instance.get();
}
public DarkColorModelRGB() {

14
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<ToolTipContext> 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();
}
}

14
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<ToolTipContext> 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();
}
}

8
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<ThemeSettings> 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();
}
/**

2
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);

4
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;
}

10
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<UIDefaults> DEFAULTS = new LazyValue<>(() -> new MetalLookAndFeel().getDefaults());
static UIDefaults get() {
return DEFAULTS.get();
}
}

7
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<Class<?>, IconLoader> iconLoaderMap = new HashMap<>();
private static IconLoader instance;
private static final LazyValue<IconLoader> 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();
}
/**

50
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<T> {
private Supplier<T> supplier;
private T value;
public LazyValue(final Supplier<T> 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;
}
}

1
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;

Loading…
Cancel
Save