@ -2,9 +2,15 @@ package com.fr.base.svg;
import com.fr.general.IOUtils ;
import com.fr.general.IOUtils ;
import com.fr.log.FineLoggerFactory ;
import com.fr.log.FineLoggerFactory ;
import com.fr.stable.bridge.StableFactory ;
import com.fr.stable.fun.ResourcePathTransformer ;
import com.fr.stable.plugin.ExtraClassManagerProvider ;
import javax.swing.Icon ;
import javax.swing.Icon ;
import javax.swing.ImageIcon ;
import javax.swing.ImageIcon ;
import java.util.Arrays ;
import java.util.HashSet ;
import java.util.Set ;
/ * *
/ * *
* 主要是用来读取svgIcon的工具类
* 主要是用来读取svgIcon的工具类
@ -36,7 +42,7 @@ public class IconUtils {
// 判断是否以.svg结尾
// 判断是否以.svg结尾
if ( resource . endsWith ( ICON_SUFFIX_SVG ) ) {
if ( resource . endsWith ( ICON_SUFFIX_SVG ) ) {
if ( IOUtils . readResource ( resource ) ! = null ) {
if ( IOUtils . readResource ( resource ) ! = null ) {
return SVGIcon . readSVGIcon ( resource ) ;
return SVGIcon . readSVGIcon ( transformPath ( resource ) ) ;
}
}
// 适配插件
// 适配插件
return adjustPluginsPng ( resource ) ;
return adjustPluginsPng ( resource ) ;
@ -68,15 +74,15 @@ public class IconUtils {
private static Icon readNoSuffixResource ( String resource , String svgIconType ) {
private static Icon readNoSuffixResource ( String resource , String svgIconType ) {
String svgPath = resource + svgIconType ;
String svgPath = resource + svgIconType ;
if ( IOUtils . readResource ( svgPath ) ! = null ) {
if ( IOUtils . readResource ( svgPath ) ! = null ) {
return SVGIcon . readSVGIcon ( svgPath ) ;
return SVGIcon . readSVGIcon ( transformPath ( svgPath ) ) ;
}
}
String pngPath = resource + ICON_SUFFIX_PNG ;
String pngPath = resource + ICON_SUFFIX_PNG ;
if ( IOUtils . readResource ( pngPath ) ! = null ) {
if ( IOUtils . readResource ( pngPath ) ! = null ) {
return IOUtils . readIcon ( pngPath ) ;
return IOUtils . readIcon ( transformPath ( pngPath ) ) ;
}
}
String gifPath = resource + ICON_SUFFIX_GIF ;
String gifPath = resource + ICON_SUFFIX_GIF ;
if ( IOUtils . readResource ( gifPath ) ! = null ) {
if ( IOUtils . readResource ( gifPath ) ! = null ) {
return IOUtils . readIcon ( gifPath ) ;
return IOUtils . readIcon ( transformPath ( gifPath ) ) ;
}
}
FineLoggerFactory . getLogger ( ) . error ( "File not exists:{}" , resource ) ;
FineLoggerFactory . getLogger ( ) . error ( "File not exists:{}" , resource ) ;
return new ImageIcon ( ) ;
return new ImageIcon ( ) ;
@ -113,8 +119,30 @@ public class IconUtils {
private static Icon readSpecifiedTypeIcon ( String resource , String oldSuffix , String newSuffix ) {
private static Icon readSpecifiedTypeIcon ( String resource , String oldSuffix , String newSuffix ) {
String iconPath = resource . replace ( oldSuffix , newSuffix ) ;
String iconPath = resource . replace ( oldSuffix , newSuffix ) ;
if ( IOUtils . readResource ( iconPath ) ! = null ) {
if ( IOUtils . readResource ( iconPath ) ! = null ) {
return SVGIcon . readSVGIcon ( iconPath ) ;
return SVGIcon . readSVGIcon ( transformPath ( iconPath ) ) ;
}
}
return readIcon ( resource ) ;
return readIcon ( resource ) ;
}
}
private static String transformPath ( String path ) {
Set < ResourcePathTransformer > set = getResourcePathTransformers ( ) ;
for ( ResourcePathTransformer transformer : set ) {
if ( transformer . accept ( path ) ) {
return transformer . transform ( path ) ;
}
}
return path ;
}
private static Set < ResourcePathTransformer > getResourcePathTransformers ( ) {
Set < ResourcePathTransformer > set = new HashSet < ResourcePathTransformer > ( ) ;
ExtraClassManagerProvider provider = StableFactory . getExtraClassManager ( ) ;
if ( provider ! = null ) {
Set < ResourcePathTransformer > pluginProvided = provider . getArray ( ResourcePathTransformer . MARK_STRING ) ;
set . addAll ( pluginProvided ) ;
}
ResourcePathTransformer [ ] oemSet = StableFactory . getMarkedObjectsFromCollection ( ResourcePathTransformer . MARK_STRING , ResourcePathTransformer . class ) ;
set . addAll ( Arrays . asList ( oemSet ) ) ;
return set ;
}
}
}