|
|
|
package com.fr.base.svg;
|
|
|
|
|
|
|
|
import com.fr.general.IOUtils;
|
|
|
|
import com.fr.log.FineLoggerFactory;
|
|
|
|
|
|
|
|
import javax.swing.Icon;
|
|
|
|
import javax.swing.ImageIcon;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 主要是用来读取svgIcon的工具类
|
|
|
|
* @author Yvan
|
|
|
|
* @version 10.0
|
|
|
|
* Created by Yvan on 2020/12/23
|
|
|
|
*/
|
|
|
|
public class IconUtils {
|
|
|
|
|
|
|
|
private static final String ICON_SUFFIX_SVG = ".svg";
|
|
|
|
private static final String ICON_SUFFIX_PNG = ".png";
|
|
|
|
private static final String ICON_SUFFIX_GIF = ".gif";
|
|
|
|
private static final String SUFFIX_SEPARATOR = ".";
|
|
|
|
|
|
|
|
public static final String ICON_TYPE_NORMAL= "_normal.svg";
|
|
|
|
public static final String ICON_TYPE_DISABLED= "_disabled.svg";
|
|
|
|
public static final String ICON_TYPE_PRESSED= "_pressed.svg";
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 可以读取SVG图标或者普通图标,并且可以读取不带扩展名的文件
|
|
|
|
* 不带扩展名时以svg优先、其次png,最后gif
|
|
|
|
* @param resource 图片路径
|
|
|
|
* @return 图标
|
|
|
|
*/
|
|
|
|
public static Icon readIcon(String resource) {
|
|
|
|
// 判断是否有.XXX文件后缀
|
|
|
|
if (resource.contains(SUFFIX_SEPARATOR)) {
|
|
|
|
// 判断是否以.svg结尾
|
|
|
|
if (resource.endsWith(ICON_SUFFIX_SVG)) {
|
|
|
|
return SVGIcon.readSVGIcon(resource);
|
|
|
|
}
|
|
|
|
return IOUtils.readIcon(resource);
|
|
|
|
}
|
|
|
|
// 文件无后缀时
|
|
|
|
return readNoSuffixResource(resource, ICON_TYPE_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 尝试读取不带扩展名的图标,svg优先,其次png,最后gif,都没读到就打印错误日志,返回空白Icon
|
|
|
|
* @param resource 图片路径
|
|
|
|
* @param svgIconType 针对svg来说的图标类型
|
|
|
|
* 取值为:ICON_TYPE_NORMAL、ICON_TYPE_DISABLED、ICON_TYPE_PRESSED
|
|
|
|
* @return 图标
|
|
|
|
*/
|
|
|
|
private static Icon readNoSuffixResource(String resource, String svgIconType) {
|
|
|
|
String svgPath = resource + svgIconType;
|
|
|
|
if (IOUtils.readResource(svgPath) != null) {
|
|
|
|
return SVGIcon.readSVGIcon(svgPath);
|
|
|
|
}
|
|
|
|
String pngPath = resource + ICON_SUFFIX_PNG;
|
|
|
|
if (IOUtils.readResource(pngPath) != null) {
|
|
|
|
return IOUtils.readIcon(pngPath);
|
|
|
|
}
|
|
|
|
String gifPath = resource + ICON_SUFFIX_GIF;
|
|
|
|
if (IOUtils.readResource(gifPath) != null) {
|
|
|
|
return IOUtils.readIcon(gifPath);
|
|
|
|
}
|
|
|
|
FineLoggerFactory.getLogger().error(resource + "对应文件不存在");
|
|
|
|
return new ImageIcon();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 读取指定类型的svgIcon
|
|
|
|
* @param resource
|
|
|
|
* @param svgIconType
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static Icon readSVGIcon(String resource, String svgIconType) {
|
|
|
|
// 判断下是否有后缀
|
|
|
|
if (!resource.contains(SUFFIX_SEPARATOR)) {
|
|
|
|
return readNoSuffixResource(resource, svgIconType);
|
|
|
|
}
|
|
|
|
// 如果是".png"后缀,就替换为传入的svgIconType,然后读取图标
|
|
|
|
if (resource.endsWith(ICON_SUFFIX_PNG)) {
|
|
|
|
return readSpecifiedTypeIcon(resource, ICON_SUFFIX_PNG, svgIconType);
|
|
|
|
}
|
|
|
|
// 如果是"_XXXXXX.svg"后缀
|
|
|
|
if (resource.endsWith(ICON_TYPE_NORMAL)) {
|
|
|
|
return readSpecifiedTypeIcon(resource, ICON_TYPE_NORMAL, svgIconType);
|
|
|
|
}
|
|
|
|
if (resource.endsWith(ICON_TYPE_DISABLED)) {
|
|
|
|
return readSpecifiedTypeIcon(resource, ICON_TYPE_DISABLED, svgIconType);
|
|
|
|
}
|
|
|
|
if (resource.endsWith(ICON_TYPE_PRESSED)) {
|
|
|
|
return readSpecifiedTypeIcon(resource, ICON_TYPE_PRESSED, svgIconType);
|
|
|
|
}
|
|
|
|
return readIcon(resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Icon readSpecifiedTypeIcon(String resource, String oldSuffix, String newSuffix) {
|
|
|
|
String iconPath = resource.replace(oldSuffix, newSuffix);
|
|
|
|
if (IOUtils.readResource(iconPath) != null) {
|
|
|
|
return SVGIcon.readSVGIcon(iconPath);
|
|
|
|
}
|
|
|
|
return readIcon(resource);
|
|
|
|
}
|
|
|
|
}
|