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
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<String, SVGDocument> 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 )