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

70 lines
2.3 KiB
Java
Raw Normal View History

2020-03-16 14:30:14 +01:00
package envoy.client.ui;
import java.io.IOException;
import java.util.EnumMap;
import java.util.EnumSet;
import javafx.scene.image.Image;
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.<br>
* <br>
2020-03-16 14:30:14 +01:00
* Project: <strong>envoy-client</strong>
* File: <strong>IconUtil.java</strong>
* Created: <strong>16.03.2020</strong>
*
* @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 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.
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 icon
* @throws IOException if the loading process failed
* @since Envoy Client v0.1-beta
2020-03-17 12:45:50 +01:00
*/
public static Image load(String path, int size) throws IOException {
return new Image(IconUtil.class.getResource(path).toExternalForm(), size, size, true, true);
}
2020-03-16 14:30:14 +01:00
2020-03-17 12:45:50 +01:00
/**
*
* 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
* @throws IOException if the loading process failed
* @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) throws IOException {
var icons = new EnumMap<T, Image>(enumClass);
2020-03-16 14:30:14 +01:00
var path = "/icons/" + enumClass.getSimpleName().toLowerCase() + "/";
for (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;
}
}