FlatSVGIcon:

- setColorFilter() now returns `this`
- added method to enable/disable and clear SVGDocument cache
- do not filter red color in `paintSvgError()`
This commit is contained in:
Karl Tauber
2024-03-12 13:36:56 +01:00
parent 1f1ebc3c44
commit 11e0757387

View File

@@ -63,6 +63,7 @@ public class FlatSVGIcon
extends ImageIcon extends ImageIcon
implements DisabledIconProvider implements DisabledIconProvider
{ {
private static boolean svgCacheEnabled = true;
// cache that uses soft references for values, which allows freeing SVG documents if no longer used // cache that uses soft references for values, which allows freeing SVG documents if no longer used
private static final SoftCache<String, SVGDocument> svgCache = new SoftCache<>(); private static final SoftCache<String, SVGDocument> svgCache = new SoftCache<>();
private static final SVGLoader svgLoader = new SVGLoader(); private static final SVGLoader svgLoader = new SVGLoader();
@@ -450,8 +451,9 @@ public class FlatSVGIcon
* @param colorFilter The color filter * @param colorFilter The color filter
* @since 1.2 * @since 1.2
*/ */
public void setColorFilter( ColorFilter colorFilter ) { public FlatSVGIcon setColorFilter( ColorFilter colorFilter ) {
this.colorFilter = colorFilter; this.colorFilter = colorFilter;
return this;
} }
private void update() { private void update() {
@@ -485,6 +487,9 @@ public class FlatSVGIcon
} }
static synchronized SVGDocument loadSVG( URL url ) { static synchronized SVGDocument loadSVG( URL url ) {
if( !svgCacheEnabled )
return loadSVGUncached( url );
// get from our cache // get from our cache
String cacheKey = url.toString(); String cacheKey = url.toString();
SVGDocument document = svgCache.get( cacheKey ); SVGDocument document = svgCache.get( cacheKey );
@@ -492,15 +497,21 @@ public class FlatSVGIcon
return document; return document;
// load SVG document // load SVG document
document = svgLoader.load( url ); document = loadSVGUncached( url );
svgCache.put( cacheKey, document );
return document;
}
private static SVGDocument loadSVGUncached( URL url ) {
SVGDocument document = svgLoader.load( url );
if( document == null ) { if( document == null ) {
LoggingFacade.INSTANCE.logSevere( "FlatSVGIcon: failed to load '" + url + "'", null ); LoggingFacade.INSTANCE.logSevere( "FlatSVGIcon: failed to load '" + url + "'", null );
return null; return null;
} }
svgCache.put( cacheKey, document );
return document; return document;
} }
@@ -612,6 +623,9 @@ public class FlatSVGIcon
} }
private void paintSvgError( Graphics2D g, int x, int y ) { private void paintSvgError( Graphics2D g, int x, int y ) {
if( g instanceof GraphicsFilter )
((GraphicsFilter)g).setColorUnfiltered( Color.red );
else
g.setColor( Color.red ); g.setColor( Color.red );
g.fillRect( x, y, getIconWidth(), getIconHeight() ); g.fillRect( x, y, getIconWidth(), getIconHeight() );
} }
@@ -693,6 +707,24 @@ public class FlatSVGIcon
darkLaf = FlatLaf.isLafDark(); darkLaf = FlatLaf.isLafDark();
} }
/** @since 3.5 */
public static boolean isSVGDocumentEnabled() {
return svgCacheEnabled;
}
/** @since 3.5 */
public static void setSVGDocumentEnabled( boolean svgCacheEnabled ) {
FlatSVGIcon.svgCacheEnabled = svgCacheEnabled;
if( !svgCacheEnabled )
clearSVGDocumentCache();
}
/** @since 3.5 */
public static void clearSVGDocumentCache() {
svgCache.clear();
}
//---- class ColorFilter -------------------------------------------------- //---- class ColorFilter --------------------------------------------------
/** /**
@@ -979,6 +1011,10 @@ public class FlatSVGIcon
super.setColor( filterColor( c ) ); super.setColor( filterColor( c ) );
} }
void setColorUnfiltered( Color c ) {
super.setColor( c );
}
@Override @Override
public void setPaint( Paint paint ) { public void setPaint( Paint paint ) {
if( paint instanceof Color ) if( paint instanceof Color )