* ResourceManager will now cache Icons

This commit is contained in:
Reinhard Pointner 2009-05-13 18:20:26 +00:00
parent cda76bb77a
commit 92fedf2ad1
5 changed files with 61 additions and 19 deletions

View File

@ -145,5 +145,19 @@
diskPersistent="false"
memoryStoreEvictionPolicy="LRU"
/>
<!--
Short-lived memory cache for resources like icons. This cache is used by ResourceManager.
-->
<cache name="resource"
maxElementsInMemory="100"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
memoryStoreEvictionPolicy="LRU"
/>
</ehcache>

View File

@ -7,32 +7,45 @@ import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
public final class ResourceManager {
public static ImageIcon getIcon(String name) {
private static final Cache cache = CacheManager.getInstance().getCache("resource");
public static Icon getIcon(String name) {
return getIcon(name, null);
}
public static ImageIcon getIcon(String name, String def) {
URL resource = getImageResource(name, def);
public static Icon getIcon(String name, String def) {
Icon icon = probeCache(name, Icon.class);
if (resource != null)
return new ImageIcon(resource);
if (icon == null) {
URL resource = getImageResource(name, def);
if (resource != null) {
icon = populateCache(name, Icon.class, new ImageIcon(resource));
}
}
return null;
return icon;
}
public static ImageIcon getFlagIcon(String languageCode) {
public static Icon getFlagIcon(String languageCode) {
return getIcon(String.format("flags/%s", languageCode), "flags/default");
}
public static ImageIcon getArchiveIcon(String type) {
public static Icon getArchiveIcon(String type) {
return getIcon(String.format("archives/%s", type), "archives/default");
}
@ -68,6 +81,24 @@ public final class ResourceManager {
}
private static <T> T probeCache(String name, Class<T> type) {
Element entry = cache.get(type.getName() + ":" + name);
if (entry != null) {
return type.cast(entry.getObjectValue());
}
return null;
}
private static <T> T populateCache(String name, Class<? super T> type, T value) {
cache.put(new Element(type.getName() + ":" + name, value));
return value;
}
/**
* Dummy constructor to prevent instantiation.
*/

View File

@ -22,7 +22,7 @@ import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.Icon;
import javax.swing.JToggleButton;
import net.sourceforge.filebot.ResourceManager;
@ -30,8 +30,8 @@ import net.sourceforge.filebot.ResourceManager;
public class ChecksumButton extends JToggleButton {
private static final ImageIcon contentArea = ResourceManager.getIcon("button.checksum");
private static final ImageIcon contentAreaSelected = ResourceManager.getIcon("button.checksum.selected");
private static final Icon contentArea = ResourceManager.getIcon("button.checksum");
private static final Icon contentAreaSelected = ResourceManager.getIcon("button.checksum.selected");
public ChecksumButton(Action action) {
@ -66,7 +66,7 @@ public class ChecksumButton extends JToggleButton {
g2d.setRenderingHint(KEY_TEXT_ANTIALIASING, VALUE_TEXT_ANTIALIAS_ON);
g2d.setRenderingHint(KEY_RENDERING, VALUE_RENDER_QUALITY);
// paint background image in the center
if (isSelected()) {
contentAreaSelected.paintIcon(this, g2d, (int) round((getWidth() - contentAreaSelected.getIconWidth()) / (double) 2), (int) round((getHeight() - contentAreaSelected.getIconHeight()) / (double) 2));
@ -75,7 +75,7 @@ public class ChecksumButton extends JToggleButton {
}
Rectangle2D textBounds = g2d.getFontMetrics().getStringBounds(getText(), g2d);
// draw text in the center
g2d.drawString(getText(), round((getWidth() - textBounds.getWidth()) / 2) + 1, round(getHeight() / 2 - textBounds.getY() - textBounds.getHeight() / 2));
}

View File

@ -3,7 +3,6 @@ package net.sourceforge.filebot.ui.panel.subtitle;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import net.sourceforge.filebot.ResourceManager;
import net.sourceforge.filebot.web.SubtitleDescriptor;
@ -16,7 +15,7 @@ public class SubtitlePackage {
private final ArchiveType archiveType;
private final ImageIcon archiveIcon;
private final Icon archiveIcon;
private final Language language;

View File

@ -13,11 +13,10 @@ public final class ExceptionUtilities {
}
@SuppressWarnings("unchecked")
public static <T extends Throwable> T findCause(Throwable t, Class<T> type) {
while (t != null) {
if (type.isInstance(t))
return (T) t;
return type.cast(t);
t = t.getCause();
}
@ -42,10 +41,9 @@ public final class ExceptionUtilities {
}
@SuppressWarnings("unchecked")
public static <T extends Throwable> T wrap(Throwable t, Class<T> type) {
if (type.isInstance(t)) {
return (T) t;
return type.cast(t);
}
try {