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

174 lines
6.2 KiB
Java

package envoy.client.ui;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.logging.Level;
import javafx.scene.image.Image;
import envoy.client.data.Settings;
import envoy.util.EnvoyLog;
/**
* 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>
*
* @author Kai S. K. Engelbart
* @since Envoy Client v0.1-beta
*/
public class IconUtil {
private IconUtil() {}
/**
* Loads an icon from the resource folder.
*
* @param path the path to the icon inside the resource folder
* @return the icon
* @since Envoy Client v0.1-beta
*/
public static Image load(String path) {
Image image;
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);
throw e;
}
return image;
}
/**
* Loads an icon from the resource folder and scales it to a given size.
*
* @param path the path to the icon inside the resource folder
* @param size the size to scale the icon to
* @return the scaled icon
* @since Envoy Client v0.1-beta
*/
public static Image load(String path, int size) {
Image image;
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);
throw e;
}
return image;
}
/**
* Loads a {@code .png} icon from the sub-folder {@code /icons/} of the resource
* folder and scales it to 16px.<br>
* The suffix {@code .png} is automatically appended.
*
* @param name the image name without the .png - suffix
* @return the loaded image
* @since Envoy Client v0.1-beta
* @apiNote let's take a sample image {@code abc.png} in the folder
* {@code /icons/}.
* <br>
* To do that, we only have to call
* {@code IconUtil.loadDefault("abc")}
*/
public static Image loadDefault(String name) { return load("/icons/" + name + ".png"); }
/**
* Loads a {@code .png} icon from the sub-folder {@code /icons/} of the resource
* folder and scales it to a given size.<br>
* The suffix {@code .png} is automatically appended.
*
* @param name the image name without the .png - suffix
* @param size the size of the image to scale to
* @return the loaded image
* @since Envoy Client v0.1-beta
* @apiNote let's take a sample image {@code abc.png} in the folder
* {@code /icons/} and load it in size 16.
* <br>
* To do that, we only have to call
* {@code IconUtil.loadDefault("abc", 16)}
*/
public static Image loadDefault(String name, int size) { return load("/icons/" + name + ".png", size); }
/**
* Loads a {@code .png} icon whose design depends on the currently active theme
* from the sub-folder {@code /icons/} of the resource folder.
* <p>
* In case of the dark theme, the suffix {@code "white"} will be appended, else
* the suffix {@code "black"} will be appended.
* <p>
* The suffix {@code .png} is automatically appended.
*
* @param name the image name without the "black" or "white" suffix and without
* the .png - suffix
* @return the loaded image
* @since Envoy Client v0.1-beta
* @apiNote let's take two sample images {@code abc_black.png} and
* {@code abc_white.png} in the folder {@code /icons/} and load them.
* <br>
* To do that theme sensitve, we only have to call
* {@code IconUtil.loadDefaultThemeSensitive("abc_")}
*/
public static Image loadDefaultThemeSensitive(String name) { return loadDefault(name + themeSpecificSuffix()); }
/**
* Loads a {@code .png} icon whose design depends on the currently active theme
* from the sub-folder {@code /icons/} of the resource
* folder and scales it to the given size.
* <p>
* In case of the dark theme, the suffix {@code "white"} will be appended, else
* the suffix {@code "black"} will be appended.
* <p>
* The suffix {@code .png} is automatically appended.
*
* @param name the image name without the .png - suffix
* @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 abc_black.png} and
* {@code abc_white.png} in the folder {@code /icons/} and load it in
* size 16.
* <br>
* To do that theme sensitve, we only have to call
* {@code IconUtil.loadDefaultThemeSensitive("abc_", 16)}
*/
public static Image loadDefaultThemeSensitive(String name, int size) { return loadDefault(name + themeSpecificSuffix(), size); }
/**
*
* Loads icons 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.
*
* @param <T> the enum that specifies the icons to load
* @param enumClass the class of the enum
* @param size the size to scale the icons to
* @return a map containing the loaded icons with the corresponding enum
* constants as keys
* @since Envoy Client v0.1-beta
*/
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));
return icons;
}
/**
* This method should be called if the display of an icon depends upon the
* currently active theme.<br>
* In case of the dark theme, the suffix {@code "white"} will be appended, else
* the suffix {@code "black"} will be appended.
*
* @return the theme specific suffix
* @since Envoy Client v0.1-beta
*/
public static String themeSpecificSuffix() { return Settings.getInstance().getCurrentTheme().equals("dark") ? "white" : "black"; }
}