From 11e0757387e34d623ee2c27d14faf5de0b83f5b5 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Tue, 12 Mar 2024 13:36:56 +0100 Subject: [PATCH] FlatSVGIcon: - setColorFilter() now returns `this` - added method to enable/disable and clear SVGDocument cache - do not filter red color in `paintSvgError()` --- .../formdev/flatlaf/extras/FlatSVGIcon.java | 46 +++++++++++++++++-- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatSVGIcon.java b/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatSVGIcon.java index 39843369..92cafcba 100644 --- a/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatSVGIcon.java +++ b/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatSVGIcon.java @@ -63,6 +63,7 @@ public class FlatSVGIcon extends ImageIcon implements DisabledIconProvider { + private static boolean svgCacheEnabled = true; // cache that uses soft references for values, which allows freeing SVG documents if no longer used private static final SoftCache svgCache = new SoftCache<>(); private static final SVGLoader svgLoader = new SVGLoader(); @@ -450,8 +451,9 @@ public class FlatSVGIcon * @param colorFilter The color filter * @since 1.2 */ - public void setColorFilter( ColorFilter colorFilter ) { + public FlatSVGIcon setColorFilter( ColorFilter colorFilter ) { this.colorFilter = colorFilter; + return this; } private void update() { @@ -485,6 +487,9 @@ public class FlatSVGIcon } static synchronized SVGDocument loadSVG( URL url ) { + if( !svgCacheEnabled ) + return loadSVGUncached( url ); + // get from our cache String cacheKey = url.toString(); SVGDocument document = svgCache.get( cacheKey ); @@ -492,15 +497,21 @@ public class FlatSVGIcon return 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 ) { LoggingFacade.INSTANCE.logSevere( "FlatSVGIcon: failed to load '" + url + "'", null ); return null; } - svgCache.put( cacheKey, document ); - return document; } @@ -612,7 +623,10 @@ public class FlatSVGIcon } private void paintSvgError( Graphics2D g, int x, int y ) { - g.setColor( Color.red ); + if( g instanceof GraphicsFilter ) + ((GraphicsFilter)g).setColorUnfiltered( Color.red ); + else + g.setColor( Color.red ); g.fillRect( x, y, getIconWidth(), getIconHeight() ); } @@ -693,6 +707,24 @@ public class FlatSVGIcon 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 -------------------------------------------------- /** @@ -979,6 +1011,10 @@ public class FlatSVGIcon super.setColor( filterColor( c ) ); } + void setColorUnfiltered( Color c ) { + super.setColor( c ); + } + @Override public void setPaint( Paint paint ) { if( paint instanceof Color )