2014-04-19 02:30:29 -04:00
|
|
|
package net.filebot;
|
2007-12-23 14:28:04 -05:00
|
|
|
|
2018-03-05 06:44:24 -05:00
|
|
|
import static java.util.Collections.*;
|
2016-09-26 04:17:00 -04:00
|
|
|
import static java.util.stream.Collectors.*;
|
2007-12-23 14:28:04 -05:00
|
|
|
|
2019-05-16 09:45:00 -04:00
|
|
|
import java.awt.GraphicsEnvironment;
|
2007-12-23 14:28:04 -05:00
|
|
|
import java.awt.Image;
|
2018-03-05 06:44:24 -05:00
|
|
|
import java.awt.image.BaseMultiResolutionImage;
|
2018-07-11 07:06:18 -04:00
|
|
|
import java.awt.image.BufferedImage;
|
2007-12-23 14:28:04 -05:00
|
|
|
import java.net.URL;
|
2018-07-11 07:06:18 -04:00
|
|
|
import java.util.ArrayList;
|
2018-03-05 06:44:24 -05:00
|
|
|
import java.util.HashMap;
|
2015-05-10 16:33:21 -04:00
|
|
|
import java.util.List;
|
2018-03-05 06:44:24 -05:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Objects;
|
2016-09-26 04:17:00 -04:00
|
|
|
import java.util.stream.Stream;
|
2007-12-23 14:28:04 -05:00
|
|
|
|
2018-03-05 06:44:24 -05:00
|
|
|
import javax.imageio.ImageIO;
|
2009-05-13 14:20:26 -04:00
|
|
|
import javax.swing.Icon;
|
2007-12-23 14:28:04 -05:00
|
|
|
import javax.swing.ImageIcon;
|
|
|
|
|
2018-07-11 09:30:40 -04:00
|
|
|
import org.imgscalr.Scalr;
|
|
|
|
import org.imgscalr.Scalr.Method;
|
|
|
|
import org.imgscalr.Scalr.Mode;
|
|
|
|
|
2008-10-10 15:20:37 -04:00
|
|
|
public final class ResourceManager {
|
2015-05-10 16:33:21 -04:00
|
|
|
|
2018-03-05 06:44:24 -05:00
|
|
|
private static final Map<String, Icon> cache = synchronizedMap(new HashMap<String, Icon>(256));
|
2015-11-20 03:26:57 -05:00
|
|
|
|
2018-03-06 02:04:24 -05:00
|
|
|
public static Icon getIcon(String name) {
|
|
|
|
return cache.computeIfAbsent(name, i -> {
|
2018-03-05 06:44:24 -05:00
|
|
|
// load image
|
|
|
|
URL[] resource = getMultiResolutionImageResource(i);
|
|
|
|
if (resource.length > 0) {
|
2018-07-11 09:30:40 -04:00
|
|
|
return new ImageIcon(getMultiResolutionImage(resource));
|
2018-03-05 06:44:24 -05:00
|
|
|
}
|
2015-11-20 03:26:57 -05:00
|
|
|
|
2018-03-06 02:04:24 -05:00
|
|
|
// default image
|
|
|
|
return null;
|
2018-03-05 06:44:24 -05:00
|
|
|
});
|
2007-12-23 14:28:04 -05:00
|
|
|
}
|
2015-05-10 16:33:21 -04:00
|
|
|
|
2017-04-01 15:45:02 -04:00
|
|
|
public static Stream<URL> getApplicationIconResources() {
|
2018-03-06 02:36:01 -05:00
|
|
|
return Stream.of("window.icon16", "window.icon64").map(ResourceManager::getImageResource);
|
2017-04-01 15:45:02 -04:00
|
|
|
}
|
|
|
|
|
2018-03-05 06:44:24 -05:00
|
|
|
public static List<Image> getApplicationIconImages() {
|
2018-03-06 02:36:01 -05:00
|
|
|
return Stream.of("window.icon16", "window.icon64").map(ResourceManager::getMultiResolutionImageResource).map(ResourceManager::getMultiResolutionImage).collect(toList());
|
2015-05-10 16:33:21 -04:00
|
|
|
}
|
|
|
|
|
2009-05-13 14:20:26 -04:00
|
|
|
public static Icon getFlagIcon(String languageCode) {
|
2015-11-20 09:04:49 -05:00
|
|
|
return getIcon("flags/" + languageCode);
|
|
|
|
}
|
|
|
|
|
2018-03-05 06:44:24 -05:00
|
|
|
private static Image getMultiResolutionImage(URL[] resource) {
|
|
|
|
try {
|
2019-05-16 09:45:00 -04:00
|
|
|
// Load multi-resolution images only if necessary
|
|
|
|
if (PRIMARY_SCALE_FACTOR == 1) {
|
|
|
|
return ImageIO.read(resource[0]);
|
|
|
|
}
|
|
|
|
|
2018-07-11 07:06:18 -04:00
|
|
|
List<BufferedImage> image = new ArrayList<BufferedImage>(resource.length);
|
|
|
|
for (URL r : resource) {
|
|
|
|
image.add(ImageIO.read(r));
|
2018-03-05 06:44:24 -05:00
|
|
|
}
|
2018-07-11 07:06:18 -04:00
|
|
|
|
2018-07-11 09:30:40 -04:00
|
|
|
// Windows 10: use @2x image for non-integer scale factors 1.25 / 1.5 / 1.75
|
|
|
|
if (PRIMARY_SCALE_FACTOR != 1 && PRIMARY_SCALE_FACTOR != 2) {
|
|
|
|
BufferedImage hidpi = image.get(image.size() - 1);
|
|
|
|
if (PRIMARY_SCALE_FACTOR < 2) {
|
|
|
|
image.add(1, scale(PRIMARY_SCALE_FACTOR, hidpi));
|
|
|
|
} else {
|
|
|
|
image.add(scale(PRIMARY_SCALE_FACTOR, hidpi));
|
|
|
|
}
|
2018-07-11 07:06:18 -04:00
|
|
|
}
|
2018-07-11 09:30:40 -04:00
|
|
|
|
|
|
|
return new BaseMultiResolutionImage(image.toArray(new Image[0]));
|
2018-03-05 06:44:24 -05:00
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
2008-02-09 12:53:08 -05:00
|
|
|
}
|
2015-05-10 16:33:21 -04:00
|
|
|
|
2018-03-05 06:44:24 -05:00
|
|
|
private static URL[] getMultiResolutionImageResource(String name) {
|
|
|
|
return Stream.of(name, name + "@2x").map(ResourceManager::getImageResource).filter(Objects::nonNull).toArray(URL[]::new);
|
|
|
|
}
|
2015-05-10 16:33:21 -04:00
|
|
|
|
2018-03-05 06:44:24 -05:00
|
|
|
private static URL getImageResource(String name) {
|
|
|
|
return ResourceManager.class.getResource("resources/" + name + ".png");
|
2008-06-02 15:12:28 -04:00
|
|
|
}
|
2015-05-10 16:33:21 -04:00
|
|
|
|
2019-05-16 09:45:00 -04:00
|
|
|
public static final double PRIMARY_SCALE_FACTOR = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().getDefaultTransform().getScaleX();
|
2018-07-11 07:06:18 -04:00
|
|
|
|
2019-05-16 09:45:00 -04:00
|
|
|
private static BufferedImage scale(double scale, BufferedImage image) {
|
2018-07-11 07:06:18 -04:00
|
|
|
int w = (int) (scale * image.getWidth());
|
|
|
|
int h = (int) (scale * image.getHeight());
|
2018-07-11 09:30:40 -04:00
|
|
|
return Scalr.resize(image, Method.ULTRA_QUALITY, Mode.FIT_TO_WIDTH, w, h, Scalr.OP_ANTIALIAS);
|
2008-07-30 18:37:01 -04:00
|
|
|
}
|
2015-05-10 16:33:21 -04:00
|
|
|
|
2007-12-23 14:28:04 -05:00
|
|
|
}
|