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
|
|
|
|
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.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
|
2020-06-26 23:36:14 +02:00
|
|
|
* 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
|
2020-03-23 21:52:33 +01:00
|
|
|
* @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
|
|
|
/**
|
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-07-06 22:33:04 +02:00
|
|
|
public static Image load(String path) {
|
2020-07-07 21:00:45 +02:00
|
|
|
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;
|
|
|
|
}
|
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-07-07 21:00:45 +02:00
|
|
|
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 19:08:26 +01:00
|
|
|
}
|
2020-03-16 14:30:14 +01:00
|
|
|
|
2020-07-06 22:33:04 +02:00
|
|
|
/**
|
2020-07-07 21:00:45 +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.
|
|
|
|
*
|
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-07-07 17:02:39 +02:00
|
|
|
public static Image loadIcon(String name) { return load("/icons/" + name + ".png"); }
|
2020-07-06 22:33:04 +02:00
|
|
|
|
|
|
|
/**
|
2020-07-07 21:00:45 +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-07-06 22:33:04 +02:00
|
|
|
* To do that, we only have to call
|
2020-07-07 17:02:39 +02:00
|
|
|
* {@code IconUtil.loadIcon("abc", 16)}
|
2020-07-06 22:33:04 +02:00
|
|
|
*/
|
2020-07-07 17:02:39 +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-07-07 21:00:45 +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
|
2020-07-07 17:02:39 +02:00
|
|
|
* 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-07-07 17:02:39 +02:00
|
|
|
public static Image loadIconThemeSensitive(String name) { return loadIcon(themeSpecificSubFolder() + name); }
|
2020-07-06 22:33:04 +02:00
|
|
|
|
|
|
|
/**
|
2020-07-07 21:00:45 +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-07-07 17:02:39 +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-06-27 11:03:30 +02:00
|
|
|
*
|
2020-07-07 21:00:45 +02: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
|
2020-07-07 21:00:45 +02:00
|
|
|
* 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
|
|
|
|
* @return a map containing the loaded images with the corresponding enum
|
2020-03-17 12:45:50 +01:00
|
|
|
* 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-07 21:00:45 +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>
|
2020-07-07 17:02:39 +02:00
|
|
|
* In case of a default theme, the string returned will be
|
2020-07-07 21:00:45 +02:00
|
|
|
* ({@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-07 17:02:39 +02:00
|
|
|
public static String themeSpecificSubFolder() {
|
|
|
|
return Settings.getInstance().isUsingDefaultTheme() ? Settings.getInstance().getCurrentTheme() + "/" : "";
|
|
|
|
}
|
2020-03-16 14:30:14 +01:00
|
|
|
}
|