Use JDK 9 MultiResolutionImage

This commit is contained in:
Reinhard Pointner 2018-03-05 18:44:24 +07:00
parent 5e0888c8c3
commit 53429d9073
2 changed files with 44 additions and 56 deletions

View File

@ -203,14 +203,14 @@ public class Main {
MacAppUtilities.initializeApplication(FileBotMenuBar.createHelp(), files -> SwingEventBus.getInstance().post(new FileTransferable(files))); MacAppUtilities.initializeApplication(FileBotMenuBar.createHelp(), files -> SwingEventBus.getInstance().post(new FileTransferable(files)));
} else if (isUbuntuApp()) { } else if (isUbuntuApp()) {
// Ubuntu/Debian specific configuration // Ubuntu/Debian specific configuration
frame.setIconImages(ResourceManager.getApplicationIcons()); frame.setIconImages(ResourceManager.getApplicationIconImages());
} else if (isWindowsApp()) { } else if (isWindowsApp()) {
// Windows specific configuration // Windows specific configuration
WinAppUtilities.initializeApplication(); WinAppUtilities.initializeApplication();
frame.setIconImages(ResourceManager.getApplicationIcons()); frame.setIconImages(ResourceManager.getApplicationIconImages());
} else { } else {
// generic Linux/FreeBSD/Solaris configuration // generic Linux/FreeBSD/Solaris configuration
frame.setIconImages(ResourceManager.getApplicationIcons()); frame.setIconImages(ResourceManager.getApplicationIconImages());
} }
// start application // start application

View File

@ -1,97 +1,85 @@
package net.filebot; package net.filebot;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static java.util.stream.Collectors.*; import static java.util.stream.Collectors.*;
import java.awt.Image; import java.awt.Image;
import java.awt.Toolkit; import java.awt.Toolkit;
import java.awt.image.BaseMultiResolutionImage;
import java.net.URL; import java.net.URL;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream; import java.util.stream.Stream;
import javax.imageio.ImageIO;
import javax.swing.Icon; import javax.swing.Icon;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
import net.filebot.util.WeakValueHashMap;
public final class ResourceManager { public final class ResourceManager {
private static final WeakValueHashMap<String, Icon> cache = new WeakValueHashMap<String, Icon>(256); private static final Map<String, Icon> cache = synchronizedMap(new HashMap<String, Icon>(256));
public static Icon getIcon(String name) { public static Icon getIcon(String... name) {
return getIcon(name, null); return getIcon(asList(name));
} }
public static Icon getIcon(String name, String def) { private static Icon getIcon(List<String> icons) {
Icon icon = null; return cache.computeIfAbsent(icons.get(0), i -> {
// load image
// try cache URL[] resource = getMultiResolutionImageResource(i);
synchronized (cache) { if (resource.length > 0) {
icon = cache.get(name); return new ImageIcon(getMultiResolutionImage(resource));
if (icon != null) { }
return icon;
} // try next image
if (icons.size() > 0) {
return getIcon(icons.subList(1, icons.size()));
} }
URL resource = getImageResource(name, def);
if (resource == null) {
return null; return null;
} });
Image image = getImage(resource);
icon = new ImageIcon(image);
// update cache
synchronized (cache) {
cache.put(name, icon);
}
return icon;
} }
public static Stream<URL> getApplicationIconResources() { public static Stream<URL> getApplicationIconResources() {
return Stream.of("window.icon.large", "window.icon.medium", "window.icon.small").map(ResourceManager::getImageResource); return Stream.of("window.icon.large", "window.icon.medium", "window.icon.small").map(ResourceManager::getImageResource);
} }
public static List<Image> getApplicationIcons() { public static List<Image> getApplicationIconImages() {
return getApplicationIconResources().map(ResourceManager::getImage).collect(toList()); return getApplicationIconResources().map(ResourceManager::getToolkitImage).collect(toList());
} }
public static Icon getFlagIcon(String languageCode) { public static Icon getFlagIcon(String languageCode) {
return getIcon("flags/" + languageCode); return getIcon("flags/" + languageCode);
} }
public static Image getImage(String name) { private static Image getToolkitImage(URL resource) {
return getImage(getImageResource(name));
}
private static Image getImage(URL resource) {
// load sun.awt.image.ToolkitImage or sun.awt.image.MultiResolutionToolkitImage (via @2x convention) // load sun.awt.image.ToolkitImage or sun.awt.image.MultiResolutionToolkitImage (via @2x convention)
return Toolkit.getDefaultToolkit().getImage(resource); return Toolkit.getDefaultToolkit().getImage(resource);
} }
/** private static Image getMultiResolutionImage(URL[] resource) {
* 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. try {
* Image[] image = new Image[resource.length];
* @param name for (int i = 0; i < image.length; i++) {
* simple name of the resource (without extension) image[i] = ImageIO.read(resource[i]);
* @return URL of the resource or null if resource does not exist }
*/ return new BaseMultiResolutionImage(image);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static URL[] getMultiResolutionImageResource(String name) {
return Stream.of(name, name + "@2x").map(ResourceManager::getImageResource).filter(Objects::nonNull).toArray(URL[]::new);
}
private static URL getImageResource(String name) { private static URL getImageResource(String name) {
return ResourceManager.class.getResource("resources/" + name + ".png"); return ResourceManager.class.getResource("resources/" + name + ".png");
} }
private static URL getImageResource(String name, String def) {
URL resource = getImageResource(name);
if (resource == null)
resource = getImageResource(def);
return resource;
}
/**
* Dummy constructor to prevent instantiation.
*/
private ResourceManager() { private ResourceManager() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }