package envoy.client.ui; import java.io.IOException; import java.util.EnumMap; import java.util.EnumSet; import javafx.scene.image.Image; /** * Provides static utility methods for loading icons from the resource * folder.
*
* Project: envoy-client * File: IconUtil.java * Created: 16.03.2020 * * @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 * @throws IOException if the loading process failed * @since Envoy Client v0.1-beta */ public static Image load(String path) throws IOException { return new Image(IconUtil.class.getResource(path).toExternalForm()); } /** * 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 * @throws IOException if the loading process failed * @since Envoy Client v0.1-beta */ public static Image load(String path, int size) throws IOException { return new Image(IconUtil.class.getResource(path).toExternalForm(), size, size, true, true); } /** * * 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 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 * @throws IOException if the loading process failed * @since Envoy Client v0.1-beta */ public static > EnumMap loadByEnum(Class enumClass, int size) throws IOException { var icons = new EnumMap(enumClass); var path = "/icons/" + enumClass.getSimpleName().toLowerCase() + "/"; for (var e : EnumSet.allOf(enumClass)) icons.put(e, load(path + e.toString().toLowerCase() + ".png", size)); return icons; } }