Browse Source

REPORT-127437 feat:调整代码,增加 mapSet

fbp/research
lemon 5 months ago
parent
commit
c62885a704
  1. 54
      designer-base/src/main/java/com/fine/theme/icon/plugin/PluginIconManager.java
  2. 25
      designer-base/src/main/java/com/fine/theme/icon/plugin/PluginJsonIconSet.java
  3. 4
      designer-base/src/main/java/com/fine/theme/icon/plugin/PluginLazyIcon.java
  4. 54
      designer-base/src/main/java/com/fine/theme/icon/plugin/PluginMapIconSet.java
  5. 4
      designer-base/src/main/java/com/fine/theme/light/ui/laf/FineDarkLaf.java
  6. 25
      designer-base/src/main/java/com/fine/theme/light/ui/laf/FineLaf.java
  7. 2
      designer-base/src/main/java/com/fine/theme/light/ui/laf/FineLightLaf.java

54
designer-base/src/main/java/com/fine/theme/icon/plugin/PluginIconManager.java

@ -8,6 +8,7 @@ import com.fine.theme.icon.LazyIcon;
import com.fr.base.extension.FileExtension;
import com.fr.general.IOUtils;
import com.fr.log.FineLoggerFactory;
import com.fr.nx.app.web.out.widget.utils.CollectionUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -15,7 +16,9 @@ import javax.swing.Icon;
import java.awt.Dimension;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* 管理插件图标集
@ -28,8 +31,8 @@ public class PluginIconManager {
public static final String ICON_DISABLE_SUFFIX = "_disable";
public static final Dimension DEFAULT_DIMENSION = new Dimension(16, 16);
private static final Map<String, IconSet> SOURCE_ICON_MAPS = new HashMap<>(2);
private static final HashMap<String, HashMap<String, WeakReference<Icon>>> CACHE = new HashMap<>();
private static final Map<String, List<IconSet>> SOURCE_ICON_MAPS = new ConcurrentHashMap<>(2);
private static final Map<String, HashMap<String, WeakReference<Icon>>> CACHE = new ConcurrentHashMap<>();
/**
@ -39,10 +42,12 @@ public class PluginIconManager {
* @return 图标集
*/
public static IconSet getSet(String source, String id) {
IconSet set = SOURCE_ICON_MAPS.get(source);
List<IconSet> sets = SOURCE_ICON_MAPS.get(source);
if (set != null && set.getId().equals(id)) {
return set;
for (IconSet set : sets) {
if (set != null && set.getId().equals(id)) {
return set;
}
}
throw new IconException("[PluginIconManager] Can not find icon set by id: " + id);
}
@ -51,25 +56,26 @@ public class PluginIconManager {
* 添加图标集
*
* @param source 插件
* @param set 图标集
* @param sets 图标集
*/
public static void addSet(@NotNull String source, @NotNull IconSet set) {
public static void addSet(@NotNull String source, @NotNull List<IconSet> sets) {
if (SOURCE_ICON_MAPS.containsKey(source)) {
FineLoggerFactory.getLogger().warn("[PluginIconManager] plugin:{} icon set already exists: " + source);
}
SOURCE_ICON_MAPS.put(source, set);
SOURCE_ICON_MAPS.put(source, sets);
clearCacheBySource(source);
}
/**
* 更新指定插件图标集
*
* @param source 插件
* @param set 图标集
* @param sets 图标集
*/
public static void updateSet(@NotNull String source, @NotNull IconSet set) {
SOURCE_ICON_MAPS.put(source, set);
clearCacheBySource(source);
public static void updateSet(@NotNull String source, @NotNull List<IconSet> sets) {
removeSet(source);
addSet(source, sets);
}
/**
@ -120,15 +126,17 @@ public class PluginIconManager {
final WeakReference<Icon> reference = sourceCache.get(cacheKey);
I icon = reference != null ? (I) reference.get() : null;
if (icon == null) {
IconSet set = SOURCE_ICON_MAPS.get(source);
if (set == null) {
List<IconSet> sets = SOURCE_ICON_MAPS.get(source);
if (CollectionUtils.isEmpty(sets)) {
return icon;
}
Icon f = set.findIcon(id, dimension, type);
if (f != null) {
icon = (I) f;
sourceCache.put(cacheKey, new WeakReference<>(icon));
CACHE.put(source, sourceCache);
for (IconSet set : sets) {
Icon f = set.findIcon(id, dimension, type);
if (f != null) {
icon = (I) f;
sourceCache.put(cacheKey, new WeakReference<>(icon));
CACHE.put(source, sourceCache);
}
}
}
return icon;
@ -178,9 +186,11 @@ public class PluginIconManager {
* @return 是否存在
*/
public static boolean existIcon(String id, String source) {
IconSet set = SOURCE_ICON_MAPS.get(source);
if (set != null && set.getIds().contains(id)) {
return true;
List<IconSet> sets = SOURCE_ICON_MAPS.get(source);
for (IconSet set : sets) {
if (set != null && set.getIds().contains(id)) {
return true;
}
}
return false;
}

25
designer-base/src/main/java/com/fine/theme/icon/plugin/PluginIconSet.java → designer-base/src/main/java/com/fine/theme/icon/plugin/PluginJsonIconSet.java

@ -18,20 +18,17 @@ import java.util.Map;
import java.util.Objects;
/**
* 插件图标集
* 插件 json 图标集
*
* @author lemon
* @since
* Created on 2024/08/20
*/
public class PluginIconSet extends AbstractIconSet {
public class PluginJsonIconSet extends AbstractIconSet {
private String base;
public PluginIconSet(UrlIconResource resource, Map<String, String> iconId2Path) {
addIconWithMap(iconId2Path);
public PluginJsonIconSet(UrlIconResource resource) {
if (resource.getPath() == null) {
return;
}
@ -105,20 +102,6 @@ public class PluginIconSet extends AbstractIconSet {
}
}
/**
* 根据 map 注册图标
* @param iconId2Path key: id, value: icon path
*/
public void addIconWithMap(Map<String, String> iconId2Path) {
for (Map.Entry<String, String> entry: iconId2Path.entrySet()) {
if (PluginIconManager.isSvgIcon(entry.getValue())) {
addIcon(new SvgIconSource(entry.getKey(), entry.getValue()));
} else if (PluginIconManager.isImageIcon(entry.getValue())) {
addIcon(new ImageIconSource(entry.getKey(), entry.getValue()));
}
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
@ -127,7 +110,7 @@ public class PluginIconSet extends AbstractIconSet {
if (o == null || getClass() != o.getClass()) {
return false;
}
PluginIconSet that = (PluginIconSet) o;
PluginJsonIconSet that = (PluginJsonIconSet) o;
return Objects.equals(name, that.name);
}

4
designer-base/src/main/java/com/fine/theme/icon/plugin/PluginLazyIcon.java

@ -127,11 +127,11 @@ public class PluginLazyIcon implements Identifiable, DisabledIcon, WhiteIcon, Ic
@Override
public String toString() {
return AssistUtils.toString(new StringJoiner(", ", PluginLazyIcon.class.getSimpleName() + "[", "]")
return new StringJoiner(", ", PluginLazyIcon.class.getSimpleName() + "[", "]")
.add("source='" + source + "'")
.add("id='" + id + "'")
.add("size=" + "[w=" + scale(dimension.width) + ",h=" + scale(dimension.height) + "]")
.add("type=" + type)
.toString());
.toString();
}
}

54
designer-base/src/main/java/com/fine/theme/icon/plugin/PluginMapIconSet.java

@ -0,0 +1,54 @@
package com.fine.theme.icon.plugin;
import com.fine.theme.icon.AbstractIconSet;
import com.fine.theme.icon.img.ImageIconSource;
import com.fine.theme.icon.svg.SvgIconSource;
import java.util.Map;
import java.util.Objects;
/**
* 插件 map 图标集
*
* @author lemon
* @since
* Created on 2024/08/20
*/
public class PluginMapIconSet extends AbstractIconSet {
public PluginMapIconSet(Map<String, String> iconId2Path) {
addIconWithMap(iconId2Path);
}
/**
* 根据 map 注册图标
* @param iconId2Path key: id, value: icon path
*/
public void addIconWithMap(Map<String, String> iconId2Path) {
for (Map.Entry<String, String> entry: iconId2Path.entrySet()) {
if (PluginIconManager.isSvgIcon(entry.getValue())) {
addIcon(new SvgIconSource(entry.getKey(), entry.getValue()));
} else if (PluginIconManager.isImageIcon(entry.getValue())) {
addIcon(new ImageIconSource(entry.getKey(), entry.getValue()));
}
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
PluginMapIconSet that = (PluginMapIconSet) o;
return Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hashCode(name);
}
}

4
designer-base/src/main/java/com/fine/theme/light/ui/laf/FineDarkLaf.java

@ -27,14 +27,14 @@ public class FineDarkLaf extends FineLaf {
*/
public static boolean setup() {
IconManager.addSet(new FineLightIconSet());
// dark_icon 目前还没适配,先使用 light_icon
insertIconProvider(LazyIconProvider.THEME.LIGHT_ICON);
Layouts.setScaleFactor(UIScale.getUserScaleFactor());
UIScale.addPropertyChangeListener(evt -> {
if (StringUtils.equals(evt.getPropertyName(), USER_SCALE_FACTOR)) {
Layouts.setScaleFactor((float) evt.getNewValue());
}
});
// dark_icon 目前还没适配,先使用 light_icon
insertIconProvider(LazyIconProvider.THEME.LIGHT_ICON);
return setup(new FineDarkLaf());
}

25
designer-base/src/main/java/com/fine/theme/light/ui/laf/FineLaf.java

@ -1,8 +1,10 @@
package com.fine.theme.light.ui.laf;
import com.fine.theme.icon.IconSet;
import com.fine.theme.icon.UrlIconResource;
import com.fine.theme.icon.plugin.PluginIconManager;
import com.fine.theme.icon.plugin.PluginIconSet;
import com.fine.theme.icon.plugin.PluginJsonIconSet;
import com.fine.theme.icon.plugin.PluginMapIconSet;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.util.SystemInfo;
import com.fr.design.ExtraDesignClassManager;
@ -15,6 +17,8 @@ import com.fr.plugin.observer.PluginEventListener;
import com.fr.plugin.observer.PluginEventType;
import javax.swing.PopupFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.BiConsumer;
@ -32,18 +36,23 @@ public abstract class FineLaf extends FlatLaf {
private static final String NAME = "FineLaf";
private static void handlePluginEvent(PluginEvent event, LazyIconProvider.THEME category,
BiConsumer<LazyIconProvider, PluginIconSet> operation) {
BiConsumer<LazyIconProvider, List<IconSet>> operation) {
Set<LazyIconProvider> set = event.getContext().getRuntime().get(LazyIconProvider.MARK_STRING);
dealWithPluginIcon(set, category, operation);
}
private static void dealWithPluginIcon(Set<LazyIconProvider> set, LazyIconProvider.THEME category,
BiConsumer<LazyIconProvider, PluginIconSet> operation) {
BiConsumer<LazyIconProvider, List<IconSet>> operation) {
for (LazyIconProvider provider : set) {
if (!ComparatorUtils.equals(provider.themeCategory(), category)) {
continue;
}
operation.accept(provider, new PluginIconSet(new UrlIconResource(provider.jsonPath()), provider.iconId2Path()));
List<IconSet> iconSets = new ArrayList<>();
PluginJsonIconSet jsonIconSet = new PluginJsonIconSet(new UrlIconResource(provider.jsonPath()));
iconSets.add(jsonIconSet);
PluginMapIconSet mapIconSet = new PluginMapIconSet(provider.iconId2Path());
iconSets.add(mapIconSet);
operation.accept(provider, iconSets);
}
}
@ -88,7 +97,7 @@ public abstract class FineLaf extends FlatLaf {
*/
public static void insertIconProvider(LazyIconProvider.THEME category) {
Set<LazyIconProvider> set = ExtraDesignClassManager.getInstance().getArray(LazyIconProvider.MARK_STRING);
dealWithPluginIcon(set, category, (provider, iconSet) -> PluginIconManager.addSet(provider.pluginId(), iconSet));
dealWithPluginIcon(set, category, (provider, iconSets) -> PluginIconManager.addSet(provider.pluginId(), iconSets));
listenerPlugins(category);
}
@ -99,21 +108,21 @@ public abstract class FineLaf extends FlatLaf {
PluginEventListener insert = new PluginEventListener() {
@Override
public void on(PluginEvent event) {
handlePluginEvent(event, category, (provider, iconSet) -> PluginIconManager.addSet(provider.pluginId(), iconSet));
handlePluginEvent(event, category, (provider, iconSets) -> PluginIconManager.addSet(provider.pluginId(), iconSets));
}
};
PluginEventListener update = new PluginEventListener() {
@Override
public void on(PluginEvent event) {
handlePluginEvent(event, category, (provider, iconSet) -> PluginIconManager.updateSet(provider.pluginId(), iconSet));
handlePluginEvent(event, category, (provider, iconSets) -> PluginIconManager.updateSet(provider.pluginId(), iconSets));
}
};
PluginEventListener remove = new PluginEventListener() {
@Override
public void on(PluginEvent event) {
handlePluginEvent(event, category, (provider, iconSet) -> PluginIconManager.removeSet(provider.pluginId()));
handlePluginEvent(event, category, (provider, iconSets) -> PluginIconManager.removeSet(provider.pluginId()));
}
};

2
designer-base/src/main/java/com/fine/theme/light/ui/laf/FineLightLaf.java

@ -27,13 +27,13 @@ public class FineLightLaf extends FineLaf {
*/
public static boolean setup() {
IconManager.addSet(new FineLightIconSet());
insertIconProvider(LazyIconProvider.THEME.LIGHT_ICON);
Layouts.setScaleFactor(UIScale.getUserScaleFactor());
UIScale.addPropertyChangeListener(evt -> {
if (StringUtils.equals(evt.getPropertyName(), USER_SCALE_FACTOR)) {
Layouts.setScaleFactor((float) evt.getNewValue());
}
});
insertIconProvider(LazyIconProvider.THEME.LIGHT_ICON);
return setup(new FineLightLaf());
}

Loading…
Cancel
Save