* cache loaded icons
This commit is contained in:
parent
5b25895808
commit
cd38ec17e2
|
@ -4,42 +4,58 @@ import static java.util.Arrays.*;
|
|||
|
||||
import java.awt.Image;
|
||||
import java.awt.Toolkit;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.Icon;
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
import net.filebot.util.WeakValueHashMap;
|
||||
|
||||
public final class ResourceManager {
|
||||
|
||||
private static final WeakValueHashMap<String, Icon> cache = new WeakValueHashMap<String, Icon>(256);
|
||||
|
||||
public static Icon getIcon(String name) {
|
||||
return getIcon(name, null);
|
||||
}
|
||||
|
||||
public static Icon getIcon(String name, String def) {
|
||||
URL resource = getImageResource(name, def);
|
||||
Icon icon = null;
|
||||
|
||||
// try cache
|
||||
synchronized (cache) {
|
||||
icon = cache.get(name);
|
||||
if (icon != null) {
|
||||
return icon;
|
||||
}
|
||||
}
|
||||
|
||||
URL resource = getImageResource(name, def);
|
||||
if (resource == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (Settings.isMacApp()) {
|
||||
// load sun.awt.image.ToolkitImage or sun.awt.image.MultiResolutionToolkitImage (via @2x convention)
|
||||
Image image = Toolkit.getDefaultToolkit().getImage(resource);
|
||||
return new ImageIcon(image);
|
||||
icon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(resource));
|
||||
} else {
|
||||
return new ImageIcon(resource);
|
||||
icon = new ImageIcon(resource);
|
||||
}
|
||||
|
||||
// update cache
|
||||
synchronized (cache) {
|
||||
cache.put(name, icon);
|
||||
}
|
||||
|
||||
return icon;
|
||||
}
|
||||
|
||||
public static List<Image> getApplicationIcons() {
|
||||
Image[] images = new Image[3];
|
||||
images[0] = ResourceManager.getImage("window.icon.small");
|
||||
images[1] = ResourceManager.getImage("window.icon.medium");
|
||||
images[2] = ResourceManager.getImage("window.icon.large");
|
||||
return asList(images);
|
||||
return getApplicationIconURLs().stream().map(it -> {
|
||||
return Toolkit.getDefaultToolkit().getImage(it);
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static List<URL> getApplicationIconURLs() {
|
||||
|
@ -54,14 +70,6 @@ public final class ResourceManager {
|
|||
return getIcon(String.format("flags/%s", languageCode));
|
||||
}
|
||||
|
||||
public static Image getImage(String name) {
|
||||
try {
|
||||
return ImageIO.read(getImageResource(name));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the URL of an image resource in this jar. Image must be located in <code>resources/</code> and the file type is assumed to be png.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue