From 7ab5c8c2a4e3b6220cfe34bf383cd65397d4135f Mon Sep 17 00:00:00 2001 From: weisj Date: Tue, 14 Apr 2020 18:01:11 +0200 Subject: [PATCH] Refactored property replacements. --- .../darklaf/task/FontDefaultsInitTask.java | 13 ++------ .../darklaf/task/ThemeDefaultsInitTask.java | 16 +++++----- .../github/weisj/darklaf/PropertyLoader.java | 31 +++++++++++++++++++ 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/core/src/main/java/com/github/weisj/darklaf/task/FontDefaultsInitTask.java b/core/src/main/java/com/github/weisj/darklaf/task/FontDefaultsInitTask.java index 2e288ea1..772a3d92 100644 --- a/core/src/main/java/com/github/weisj/darklaf/task/FontDefaultsInitTask.java +++ b/core/src/main/java/com/github/weisj/darklaf/task/FontDefaultsInitTask.java @@ -75,12 +75,7 @@ public class FontDefaultsInitTask implements DefaultsInitTask { } private void patchMacOSFonts(final UIDefaults defaults) { - for (Map.Entry entry : defaults.entrySet()) { - if (entry.getValue() instanceof Font) { - Font font = (Font) entry.getValue(); - entry.setValue(macOSFontFromFont(font)); - } - } + PropertyLoader.replacePropertiesOfType(Font.class, defaults, this::macOSFontFromFont); } private Font macOSFontFromFont(final Font font) { @@ -95,11 +90,7 @@ public class FontDefaultsInitTask implements DefaultsInitTask { private void applyFontRule(final Theme currentTheme, final UIDefaults defaults) { FontSizeRule rule = currentTheme.getFontSizeRule(); if (rule == null || rule.getType() == FontSizeRule.AdjustmentType.NO_ADJUSTMENT) return; - for (Map.Entry entry : defaults.entrySet()) { - if (entry != null && entry.getValue() instanceof Font) { - entry.setValue(fontWithRule((Font) entry.getValue(), rule, defaults)); - } - } + PropertyLoader.replacePropertiesOfType(Font.class, defaults, f -> fontWithRule(f, rule, defaults)); } private Font fontWithRule(final Font font, final FontSizeRule rule, final UIDefaults defaults) { diff --git a/core/src/main/java/com/github/weisj/darklaf/task/ThemeDefaultsInitTask.java b/core/src/main/java/com/github/weisj/darklaf/task/ThemeDefaultsInitTask.java index 64b66524..9e179efa 100644 --- a/core/src/main/java/com/github/weisj/darklaf/task/ThemeDefaultsInitTask.java +++ b/core/src/main/java/com/github/weisj/darklaf/task/ThemeDefaultsInitTask.java @@ -92,16 +92,14 @@ public class ThemeDefaultsInitTask implements DefaultsInitTask { globalSettings.put(((String) key).substring(GLOBAL_PREFIX.length()), uiProps.get(key)); } } - - for (final Object key : defaults.keySet()) { - if (key instanceof String && ((String) key).contains(".")) { - final String s = (String) key; + PropertyLoader.replaceProperties( + defaults, + e -> e.getKey() instanceof String && ((String) e.getKey()).contains("."), + e -> { + final String s = (String) e.getKey(); final String globalKey = s.substring(s.lastIndexOf('.') + 1); - if (globalSettings.containsKey(globalKey)) { - defaults.put(key, globalSettings.get(globalKey)); - } - } - } + return globalSettings.get(globalKey); + }); } private void initUIProperties(final Theme currentTheme, final UIDefaults defaults, final Properties uiProps) { diff --git a/property-loader/src/main/java/com/github/weisj/darklaf/PropertyLoader.java b/property-loader/src/main/java/com/github/weisj/darklaf/PropertyLoader.java index 6bbecadd..c4a1d938 100644 --- a/property-loader/src/main/java/com/github/weisj/darklaf/PropertyLoader.java +++ b/property-loader/src/main/java/com/github/weisj/darklaf/PropertyLoader.java @@ -44,6 +44,8 @@ import java.io.InputStream; import java.text.AttributedCharacterIterator; import java.util.List; import java.util.*; +import java.util.function.Function; +import java.util.function.Predicate; import java.util.logging.Level; import java.util.logging.Logger; @@ -130,6 +132,35 @@ public final class PropertyLoader { } } + public static void replaceProperties(final Map properties, + final Predicate> predicate, + final Function, Object> mapper) { + replacePropertiesOfType(Object.class, properties, predicate, mapper); + } + + public static void replacePropertiesOfType(final Class type, + final Map properties, + final Function mapper) { + replacePropertiesOfType(type, properties, e -> true, e -> mapper.apply(e.getValue())); + } + + @SuppressWarnings("unchecked") + public static void replacePropertiesOfType(final Class type, + final Map properties, + final Predicate> predicate, + final Function, T> mapper) { + for (Map.Entry entry : properties.entrySet()) { + if (type == Object.class || type.isAssignableFrom(entry.getValue().getClass())) { + if (predicate.test((Map.Entry) entry)) { + T newValue = mapper.apply((Map.Entry) entry); + if (newValue != null) { + entry.setValue(newValue); + } + } + } + } + } + private static String parseKey(final String key) { if (addReferenceInfo) return key; return key.startsWith(String.valueOf(REFERENCE_PREFIX)) ? key.substring(1) : key;