Skip to content

Commit

Permalink
FlatSVGIcon:
Browse files Browse the repository at this point in the history
- setColorFilter() now returns `this`
- added method to enable/disable and clear SVGDocument cache
- do not filter red color in `paintSvgError()`
  • Loading branch information
DevCharly committed Mar 12, 2024
1 parent 1f1ebc3 commit 11e0757
Showing 1 changed file with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -450,8 +451,9 @@ public ColorFilter getColorFilter() {
* @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() {
Expand Down Expand Up @@ -485,22 +487,31 @@ private void update() {
}

static synchronized SVGDocument loadSVG( URL url ) {
if( !svgCacheEnabled )
return loadSVGUncached( url );

// get from our cache
String cacheKey = url.toString();
SVGDocument document = svgCache.get( cacheKey );
if( document != null )
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;
}

Expand Down Expand Up @@ -612,7 +623,10 @@ private void paintSvg( Graphics2D g, int x, int y ) {
}

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() );
}

Expand Down Expand Up @@ -693,6 +707,24 @@ private static void lafChanged() {
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 --------------------------------------------------

/**
Expand Down Expand Up @@ -979,6 +1011,10 @@ public void setColor( Color c ) {
super.setColor( filterColor( c ) );
}

void setColorUnfiltered( Color c ) {
super.setColor( c );
}

@Override
public void setPaint( Paint paint ) {
if( paint instanceof Color )
Expand Down

0 comments on commit 11e0757

Please sign in to comment.