package com.fine.theme.icon; import com.fine.theme.icon.img.ImageIconSource; import com.fine.theme.icon.svg.SvgIconSource; import com.formdev.flatlaf.json.Json; import com.formdev.flatlaf.json.ParseException; import com.fr.stable.StringUtils; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.nio.charset.StandardCharsets; import java.util.Map; import java.util.Objects; /** * json 格式图标集 * * @author vito * @since 11.0 * Created on 2023/11/17 */ public class JsonIconSet extends AbstractIconSet { private String base; public JsonIconSet(UrlIconResource resource) { addIcon(new SvgIconSource("default", "com/fine/theme/icon/default.svg")); Map json; try (InputStream in = resource.getInputStream()) { try (Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) { json = (Map) Json.parse(reader); } } catch (ParseException | IOException ex) { throw new RuntimeException(ex.getMessage(), ex); } name = (String) json.get("name"); dark = Boolean.parseBoolean((String) json.get("dark")); base = (String) json.get("base"); if (base == null) { base = StringUtils.EMPTY; } Map icons = (Map) json.get("icons"); for (Map.Entry icon : icons.entrySet()) { applyIcon(icon.getKey(), icon.getValue()); } } /** * 从配置文件中添加icon,只处理认识的格式和结构 */ private void applyIcon(String key, Object value) { if (value instanceof String) { String path = (String) value; if (IconManager.isSvgIcon(path)) { // 默认字符串提供正常图和灰化图 addIcon(new SvgIconSource(key, base + path, IconManager.findDisablePath(base + path), null )); } else if (IconManager.isImageIcon(path)) { addIcon(new ImageIconSource(key, base + path)); } // 其他无法识别格式不处理 } else if (value instanceof Map) { Map iconObj = (Map) value; String normalPath = (String) iconObj.get(IconType.normal.name()); String disablePath = (String) iconObj.get(IconType.disable.name()); String whitePath = (String) iconObj.get(IconType.white.name()); // 暂不支持混合格式,每个id的格式需要保持一致 if (IconManager.isSvgIcon(normalPath)) { addIcon(new SvgIconSource(key, base + normalPath, StringUtils.isNotBlank(disablePath) ? base + disablePath : null, StringUtils.isNotBlank(whitePath) ? base + whitePath : null )); } else if (IconManager.isImageIcon(normalPath)) { addIcon(new ImageIconSource(key, base + normalPath, StringUtils.isNotBlank(disablePath) ? base + disablePath : null, StringUtils.isNotBlank(whitePath) ? base + whitePath : null )); } } } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } JsonIconSet that = (JsonIconSet) o; return Objects.equals(name, that.name); } @Override public int hashCode() { return Objects.hashCode(name); } }