2020-09-25 14:29:23 +02:00
|
|
|
package envoy.client.util;
|
2020-03-16 14:30:14 +01:00
|
|
|
|
2020-07-20 12:57:34 +02:00
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.io.IOException;
|
2020-09-25 14:29:23 +02:00
|
|
|
import java.util.*;
|
2020-07-06 22:33:04 +02:00
|
|
|
import java.util.logging.Level;
|
2020-03-16 14:30:14 +01:00
|
|
|
|
2020-07-20 12:57:34 +02:00
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
2020-06-06 10:50:23 +02:00
|
|
|
import javafx.scene.image.Image;
|
2020-03-16 14:30:14 +01:00
|
|
|
|
2020-07-06 22:33:04 +02:00
|
|
|
import envoy.util.EnvoyLog;
|
|
|
|
|
2020-10-19 18:17:51 +02:00
|
|
|
import envoy.client.data.Settings;
|
|
|
|
|
2020-03-16 14:30:14 +01:00
|
|
|
/**
|
2020-10-19 18:17:51 +02:00
|
|
|
* Provides static utility methods for loading icons from the resource folder.
|
2020-03-16 14:30:14 +01:00
|
|
|
*
|
|
|
|
* @author Kai S. K. Engelbart
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.1-beta
|
2020-03-16 14:30:14 +01:00
|
|
|
*/
|
2020-08-22 13:37:07 +02:00
|
|
|
public final class IconUtil {
|
2020-03-16 14:30:14 +01:00
|
|
|
|
2020-10-13 11:30:19 +02:00
|
|
|
private static final HashMap<String, Image> cache = new HashMap<>();
|
|
|
|
private static final HashMap<String, Image> scaledCache = new HashMap<>();
|
|
|
|
private static final HashMap<String, BufferedImage> awtCache = new HashMap<>();
|
|
|
|
|
2020-03-16 14:30:14 +01:00
|
|
|
private IconUtil() {}
|
|
|
|
|
2020-03-17 12:45:50 +01:00
|
|
|
/**
|
2020-07-07 21:00:45 +02:00
|
|
|
* Loads an image from the resource folder.
|
2020-06-27 11:03:30 +02:00
|
|
|
*
|
2020-06-06 10:50:23 +02:00
|
|
|
* @param path the path to the icon inside the resource folder
|
2020-07-07 21:00:45 +02:00
|
|
|
* @return the loaded image
|
2020-06-06 10:50:23 +02:00
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
public static Image load(String path) {
|
|
|
|
return cache.computeIfAbsent(path,
|
|
|
|
p -> new Image(IconUtil.class.getResource(p).toExternalForm()));
|
|
|
|
}
|
2020-06-06 10:50:23 +02:00
|
|
|
|
|
|
|
/**
|
2020-07-07 21:00:45 +02:00
|
|
|
* Loads an image from the resource folder and scales it to the given size.
|
2020-06-27 11:03:30 +02:00
|
|
|
*
|
2020-03-17 12:45:50 +01:00
|
|
|
* @param path the path to the icon inside the resource folder
|
|
|
|
* @param size the size to scale the icon to
|
2020-07-07 21:00:45 +02:00
|
|
|
* @return the scaled image
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.1-beta
|
2020-03-17 12:45:50 +01:00
|
|
|
*/
|
2020-06-27 11:03:30 +02:00
|
|
|
public static Image load(String path, int size) {
|
2020-10-19 18:17:51 +02:00
|
|
|
return scaledCache.computeIfAbsent(path + size,
|
|
|
|
p -> new Image(IconUtil.class.getResource(path).toExternalForm(), size, size, true,
|
|
|
|
true));
|
2020-03-16 19:08:26 +01:00
|
|
|
}
|
2020-03-16 14:30:14 +01:00
|
|
|
|
2020-07-06 22:33:04 +02:00
|
|
|
/**
|
2020-10-19 18:17:51 +02:00
|
|
|
* Loads a {@code .png} image from the sub-folder {@code /icons/} of the resource folder.
|
2020-10-13 11:30:19 +02:00
|
|
|
* <p>
|
2020-07-06 22:33:04 +02:00
|
|
|
* The suffix {@code .png} is automatically appended.
|
|
|
|
*
|
2020-07-07 17:02:39 +02:00
|
|
|
* @param name the image name without the .png suffix
|
2020-07-06 22:33:04 +02:00
|
|
|
* @return the loaded image
|
|
|
|
* @since Envoy Client v0.1-beta
|
2020-07-07 21:00:45 +02:00
|
|
|
* @apiNote let's load a sample image {@code /icons/abc.png}.<br>
|
|
|
|
* To do that, we only have to call {@code IconUtil.loadIcon("abc")}
|
2020-07-06 22:33:04 +02:00
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
public static Image loadIcon(String name) {
|
|
|
|
return load("/icons/" + name + ".png");
|
|
|
|
}
|
2020-07-06 22:33:04 +02:00
|
|
|
|
|
|
|
/**
|
2020-10-19 18:17:51 +02:00
|
|
|
* Loads a {@code .png} image from the sub-folder {@code /icons/} of the resource folder and
|
|
|
|
* scales it to the given size.<br>
|
2020-07-06 22:33:04 +02:00
|
|
|
* The suffix {@code .png} is automatically appended.
|
|
|
|
*
|
2020-07-07 17:02:39 +02:00
|
|
|
* @param name the image name without the .png suffix
|
2020-07-06 22:33:04 +02:00
|
|
|
* @param size the size of the image to scale to
|
|
|
|
* @return the loaded image
|
|
|
|
* @since Envoy Client v0.1-beta
|
2020-07-07 21:00:45 +02:00
|
|
|
* @apiNote let's load a sample image {@code /icons/abc.png} in size 16.<br>
|
2020-10-19 18:17:51 +02:00
|
|
|
* To do that, we only have to call {@code IconUtil.loadIcon("abc", 16)}
|
2020-07-06 22:33:04 +02:00
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
public static Image loadIcon(String name, int size) {
|
|
|
|
return load("/icons/" + name + ".png", size);
|
|
|
|
}
|
2020-07-06 22:33:04 +02:00
|
|
|
|
|
|
|
/**
|
2020-10-19 18:17:51 +02:00
|
|
|
* Loads a {@code .png} image whose design depends on the currently active theme from the
|
|
|
|
* sub-folder {@code /icons/dark/} or {@code /icons/light/} of the resource folder.
|
2020-07-06 22:33:04 +02:00
|
|
|
* <p>
|
|
|
|
* The suffix {@code .png} is automatically appended.
|
|
|
|
*
|
2020-10-19 18:17:51 +02:00
|
|
|
* @param name the image name without the "black" or "white" suffix and without the .png suffix
|
2020-07-06 22:33:04 +02:00
|
|
|
* @return the loaded image
|
|
|
|
* @since Envoy Client v0.1-beta
|
2020-07-07 21:00:45 +02:00
|
|
|
* @apiNote let's take two sample images {@code /icons/dark/abc.png} and
|
|
|
|
* {@code /icons/light/abc.png}, and load one of them.<br>
|
|
|
|
* To do that theme sensitive, we only have to call
|
|
|
|
* {@code IconUtil.loadIconThemeSensitive("abc")}
|
2020-07-06 22:33:04 +02:00
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
public static Image loadIconThemeSensitive(String name) {
|
|
|
|
return loadIcon(themeSpecificSubFolder() + name);
|
|
|
|
}
|
2020-07-06 22:33:04 +02:00
|
|
|
|
|
|
|
/**
|
2020-10-19 18:17:51 +02:00
|
|
|
* Loads a {@code .png} image whose design depends on the currently active theme from the
|
|
|
|
* sub-folder {@code /icons/dark/} or {@code /icons/light/} of the resource folder and scales it
|
|
|
|
* to the given size.
|
2020-07-06 22:33:04 +02:00
|
|
|
* <p>
|
|
|
|
* The suffix {@code .png} is automatically appended.
|
|
|
|
*
|
2020-07-07 17:02:39 +02:00
|
|
|
* @param name the image name without the .png suffix
|
2020-07-06 22:33:04 +02:00
|
|
|
* @param size the size of the image to scale to
|
|
|
|
* @return the loaded image
|
|
|
|
* @since Envoy Client v0.1-beta
|
2020-07-07 21:00:45 +02:00
|
|
|
* @apiNote let's take two sample images {@code /icons/dark/abc.png} and
|
|
|
|
* {@code /icons/light/abc.png}, and load one of them in size 16.<br>
|
|
|
|
* To do that theme sensitive, we only have to call
|
|
|
|
* {@code IconUtil.loadIconThemeSensitive("abc", 16)}
|
2020-07-06 22:33:04 +02:00
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
public static Image loadIconThemeSensitive(String name, int size) {
|
|
|
|
return loadIcon(themeSpecificSubFolder() + name, size);
|
|
|
|
}
|
2020-07-06 22:33:04 +02:00
|
|
|
|
2020-03-17 12:45:50 +01:00
|
|
|
/**
|
2020-10-19 18:17:51 +02:00
|
|
|
* Loads images specified by an enum. The images have to be named like the lowercase enum
|
|
|
|
* constants with {@code .png} extension and be located inside a folder with the lowercase name
|
|
|
|
* of the enum, which must be contained inside the {@code /icons/} folder.
|
2020-06-27 11:03:30 +02:00
|
|
|
*
|
2020-07-07 21:00:45 +02:00
|
|
|
* @param <T> the enum that specifies the images to load
|
2020-03-17 12:45:50 +01:00
|
|
|
* @param enumClass the class of the enum
|
2020-07-07 21:00:45 +02:00
|
|
|
* @param size the size to scale the images to
|
2020-10-19 18:17:51 +02:00
|
|
|
* @return a map containing the loaded images with the corresponding enum constants as keys
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.1-beta
|
2020-03-17 12:45:50 +01:00
|
|
|
*/
|
2020-06-27 11:03:30 +02:00
|
|
|
public static <T extends Enum<T>> EnumMap<T, Image> loadByEnum(Class<T> enumClass, int size) {
|
|
|
|
final var icons = new EnumMap<T, Image>(enumClass);
|
|
|
|
final var path = "/icons/" + enumClass.getSimpleName().toLowerCase() + "/";
|
|
|
|
for (final var e : EnumSet.allOf(enumClass))
|
2020-03-16 19:08:26 +01:00
|
|
|
icons.put(e, load(path + e.toString().toLowerCase() + ".png", size));
|
2020-03-16 14:30:14 +01:00
|
|
|
return icons;
|
|
|
|
}
|
2020-07-06 22:33:04 +02:00
|
|
|
|
2020-07-20 12:57:34 +02:00
|
|
|
/**
|
|
|
|
* Loads a buffered image from the resource folder which is compatible with AWT.
|
|
|
|
*
|
|
|
|
* @param path the path to the icon inside the resource folder
|
|
|
|
* @return the loaded image
|
|
|
|
* @since Envoy Client v0.2-beta
|
|
|
|
*/
|
|
|
|
public static BufferedImage loadAWTCompatible(String path) {
|
2020-10-13 11:30:19 +02:00
|
|
|
return awtCache.computeIfAbsent(path, p -> {
|
|
|
|
try {
|
|
|
|
return ImageIO.read(IconUtil.class.getResource(path));
|
|
|
|
} catch (IOException e) {
|
2020-10-19 18:17:51 +02:00
|
|
|
EnvoyLog.getLogger(IconUtil.class).log(Level.WARNING,
|
|
|
|
String.format("Could not load image at path %s: ", path), e);
|
2020-10-13 11:30:19 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
2020-07-20 12:57:34 +02:00
|
|
|
}
|
|
|
|
|
2020-07-06 22:33:04 +02:00
|
|
|
/**
|
2020-10-19 18:17:51 +02:00
|
|
|
* This method should be called if the display of an image depends upon the currently active
|
|
|
|
* theme.<br>
|
|
|
|
* In case of a default theme, the string returned will be ({@code dark/} or {@code light/}),
|
|
|
|
* otherwise it will be empty.
|
2020-07-06 22:33:04 +02:00
|
|
|
*
|
2020-07-07 17:02:39 +02:00
|
|
|
* @return the theme specific folder
|
2020-07-06 22:33:04 +02:00
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
2020-07-20 12:57:34 +02:00
|
|
|
private static String themeSpecificSubFolder() {
|
2020-10-19 18:17:51 +02:00
|
|
|
return Settings.getInstance().isUsingDefaultTheme()
|
|
|
|
? Settings.getInstance().getCurrentTheme() + "/"
|
|
|
|
: "";
|
2020-07-07 17:02:39 +02:00
|
|
|
}
|
2020-03-16 14:30:14 +01:00
|
|
|
}
|