diff --git a/source/ehcache.xml b/source/ehcache.xml index 634522ed..0028ab21 100644 --- a/source/ehcache.xml +++ b/source/ehcache.xml @@ -145,5 +145,19 @@ diskPersistent="false" memoryStoreEvictionPolicy="LRU" /> + + + + diff --git a/source/net/sourceforge/filebot/ResourceManager.java b/source/net/sourceforge/filebot/ResourceManager.java index 146e0c11..7ac4ccd0 100644 --- a/source/net/sourceforge/filebot/ResourceManager.java +++ b/source/net/sourceforge/filebot/ResourceManager.java @@ -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 probeCache(String name, Class type) { + Element entry = cache.get(type.getName() + ":" + name); + + if (entry != null) { + return type.cast(entry.getObjectValue()); + } + + return null; + } + + + private static T populateCache(String name, Class type, T value) { + cache.put(new Element(type.getName() + ":" + name, value)); + + return value; + } + + /** * Dummy constructor to prevent instantiation. */ diff --git a/source/net/sourceforge/filebot/ui/panel/sfv/ChecksumButton.java b/source/net/sourceforge/filebot/ui/panel/sfv/ChecksumButton.java index c1792ae3..fa007d60 100644 --- a/source/net/sourceforge/filebot/ui/panel/sfv/ChecksumButton.java +++ b/source/net/sourceforge/filebot/ui/panel/sfv/ChecksumButton.java @@ -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)); } diff --git a/source/net/sourceforge/filebot/ui/panel/subtitle/SubtitlePackage.java b/source/net/sourceforge/filebot/ui/panel/subtitle/SubtitlePackage.java index ea92b04e..4964d9d7 100644 --- a/source/net/sourceforge/filebot/ui/panel/subtitle/SubtitlePackage.java +++ b/source/net/sourceforge/filebot/ui/panel/subtitle/SubtitlePackage.java @@ -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; diff --git a/source/net/sourceforge/tuned/ExceptionUtilities.java b/source/net/sourceforge/tuned/ExceptionUtilities.java index d3686f4e..c0049249 100644 --- a/source/net/sourceforge/tuned/ExceptionUtilities.java +++ b/source/net/sourceforge/tuned/ExceptionUtilities.java @@ -13,11 +13,10 @@ public final class ExceptionUtilities { } - @SuppressWarnings("unchecked") public static T findCause(Throwable t, Class 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 wrap(Throwable t, Class type) { if (type.isInstance(t)) { - return (T) t; + return type.cast(t); } try {