2007-12-23 14:28:04 -05:00
|
|
|
|
|
|
|
package net.sourceforge.filebot.resources;
|
|
|
|
|
|
|
|
|
|
|
|
import java.awt.Image;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.URL;
|
2008-05-03 16:43:15 -04:00
|
|
|
import java.util.Collections;
|
2007-12-23 14:28:04 -05:00
|
|
|
import java.util.HashMap;
|
2008-05-03 16:43:15 -04:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.WeakHashMap;
|
2007-12-23 14:28:04 -05:00
|
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
import javax.swing.ImageIcon;
|
|
|
|
|
|
|
|
|
|
|
|
public class ResourceManager {
|
|
|
|
|
2008-05-03 16:43:15 -04:00
|
|
|
private ResourceManager() {
|
|
|
|
// hide constructor
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final Map<String, String> aliasMap = new HashMap<String, String>();
|
2007-12-23 14:28:04 -05:00
|
|
|
|
|
|
|
static {
|
2008-02-09 12:53:08 -05:00
|
|
|
aliasMap.put("tab.loading", "tab.loading.gif");
|
|
|
|
aliasMap.put("tab.history", "action.find.png");
|
|
|
|
aliasMap.put("loading", "loading.gif");
|
2007-12-23 14:28:04 -05:00
|
|
|
}
|
|
|
|
|
2008-06-02 15:12:28 -04:00
|
|
|
private static final Map<String, ImageIcon> iconCache = Collections.synchronizedMap(new WeakHashMap<String, ImageIcon>());
|
2007-12-23 14:28:04 -05:00
|
|
|
|
2008-05-03 16:43:15 -04:00
|
|
|
|
|
|
|
public static ImageIcon getIcon(String name) {
|
2008-06-02 15:12:28 -04:00
|
|
|
return getIcon(name, null);
|
2008-05-03 16:43:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-02 15:12:28 -04:00
|
|
|
public static ImageIcon getIcon(String name, String def) {
|
|
|
|
ImageIcon icon = iconCache.get(name);
|
|
|
|
|
|
|
|
if (icon == null) {
|
|
|
|
// load image if not in cache
|
|
|
|
URL resource = getResource(name, def);
|
|
|
|
|
|
|
|
if (resource != null) {
|
|
|
|
icon = new ImageIcon(resource);
|
|
|
|
iconCache.put(name, icon);
|
|
|
|
}
|
|
|
|
}
|
2008-02-28 20:16:52 -05:00
|
|
|
|
2008-06-02 15:12:28 -04:00
|
|
|
return icon;
|
2007-12-23 14:28:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-02 15:12:28 -04:00
|
|
|
public static ImageIcon getFlagIcon(String languageCode) {
|
|
|
|
return getIcon(String.format("flags/%s", languageCode), "flags/default");
|
2007-12-23 14:28:04 -05:00
|
|
|
}
|
|
|
|
|
2008-02-09 12:53:08 -05:00
|
|
|
|
2008-06-02 15:12:28 -04:00
|
|
|
public static ImageIcon getArchiveIcon(String type) {
|
|
|
|
return getIcon(String.format("archives/%s", type), "archives/default");
|
2008-03-02 12:02:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-02 15:12:28 -04:00
|
|
|
public static Image getImage(String name) {
|
|
|
|
try {
|
|
|
|
return ImageIO.read(getResource(name));
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
2008-02-28 20:16:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-09 12:53:08 -05:00
|
|
|
private static URL getResource(String name) {
|
|
|
|
String resource = null;
|
|
|
|
|
|
|
|
if (aliasMap.containsKey(name))
|
|
|
|
resource = aliasMap.get(name);
|
|
|
|
else
|
|
|
|
resource = name + ".png";
|
|
|
|
|
|
|
|
return ResourceManager.class.getResource(resource);
|
|
|
|
}
|
2008-05-03 16:43:15 -04:00
|
|
|
|
2008-06-02 15:12:28 -04:00
|
|
|
|
|
|
|
private static URL getResource(String name, String def) {
|
|
|
|
URL resource = getResource(name);
|
|
|
|
|
|
|
|
if (resource == null)
|
|
|
|
resource = getResource(def);
|
|
|
|
|
|
|
|
return resource;
|
|
|
|
}
|
|
|
|
|
2007-12-23 14:28:04 -05:00
|
|
|
}
|