This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/src/main/java/envoy/client/ui/IconUtil.java

161 lines
5.8 KiB
Java
Raw Normal View History

2020-03-16 14:30:14 +01:00
package envoy.client.ui;
import java.util.EnumMap;
import java.util.EnumSet;
2020-07-06 22:33:04 +02:00
import java.util.logging.Level;
2020-03-16 14:30:14 +01:00
import javafx.scene.image.Image;
2020-03-16 14:30:14 +01:00
2020-07-06 22:33:04 +02:00
import envoy.client.data.Settings;
import envoy.util.EnvoyLog;
2020-03-16 14:30:14 +01:00
/**
2020-03-17 12:45:50 +01:00
* Provides static utility methods for loading icons from the resource
* folder.
* <p>
* Project: <strong>envoy-client</strong><br>
* File: <strong>IconUtil.java</strong><br>
* Created: <strong>16.03.2020</strong><br>
2020-03-16 14:30:14 +01:00
*
* @author Kai S. K. Engelbart
* @since Envoy Client v0.1-beta
2020-03-16 14:30:14 +01:00
*/
public class IconUtil {
private IconUtil() {}
2020-03-17 12:45:50 +01:00
/**
* Loads an image from the resource folder.
*
* @param path the path to the icon inside the resource folder
* @return the loaded image
* @since Envoy Client v0.1-beta
*/
2020-07-06 22:33:04 +02:00
public static Image load(String path) {
Image image = null;
2020-07-06 22:33:04 +02:00
try {
image = new Image(IconUtil.class.getResource(path).toExternalForm());
} catch (final NullPointerException e) {
EnvoyLog.getLogger(IconUtil.class).log(Level.WARNING, String.format("Could not load image at path %s: ", path), e);
}
return image;
}
/**
* Loads an image from the resource folder and scales it to the given size.
*
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
* @return the scaled image
* @since Envoy Client v0.1-beta
2020-03-17 12:45:50 +01:00
*/
public static Image load(String path, int size) {
Image image = null;
2020-07-06 22:33:04 +02:00
try {
image = new Image(IconUtil.class.getResource(path).toExternalForm(), size, size, true, true);
} catch (final NullPointerException e) {
EnvoyLog.getLogger(IconUtil.class).log(Level.WARNING, String.format("Could not load image at path %s: ", path), e);
}
return image;
}
2020-03-16 14:30:14 +01:00
2020-07-06 22:33:04 +02:00
/**
* Loads a {@code .png} image from the sub-folder {@code /icons/} of the
* resource folder.<br>
2020-07-06 22:33:04 +02:00
* The suffix {@code .png} is automatically appended.
*
* @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
* @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
*/
public static Image loadIcon(String name) { return load("/icons/" + name + ".png"); }
2020-07-06 22:33:04 +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.
*
* @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
* @apiNote let's load a sample image {@code /icons/abc.png} in size 16.<br>
2020-07-06 22:33:04 +02:00
* To do that, we only have to call
* {@code IconUtil.loadIcon("abc", 16)}
2020-07-06 22:33:04 +02:00
*/
public static Image loadIcon(String name, int size) { return load("/icons/" + name + ".png", size); }
2020-07-06 22:33:04 +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.
*
* @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
* @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
*/
public static Image loadIconThemeSensitive(String name) { return loadIcon(themeSpecificSubFolder() + name); }
2020-07-06 22:33:04 +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.
*
* @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
* @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
*/
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
/**
*
* Loads images specified by an enum. The images have to be named like the
2020-03-17 12:45:50 +01:00
* 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.
*
* @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
* @param size the size to scale the images to
* @return a map containing the loaded images with the corresponding enum
2020-03-17 12:45:50 +01:00
* constants as keys
* @since Envoy Client v0.1-beta
2020-03-17 12:45:50 +01: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))
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
/**
* This method should be called if the display of an image depends upon the
2020-07-06 22:33:04 +02:00
* 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
*
* @return the theme specific folder
2020-07-06 22:33:04 +02:00
* @since Envoy Client v0.1-beta
*/
public static String themeSpecificSubFolder() {
return Settings.getInstance().isUsingDefaultTheme() ? Settings.getInstance().getCurrentTheme() + "/" : "";
}
2020-03-16 14:30:14 +01:00
}