Skip to content

Commit 6a8d4b3

Browse files
martingrswltr
authored andcommitted
Add support for customizable window icons
Merged-by: Stefan Walter <stefan.walter@iml.fraunhofer.de>
1 parent c45044d commit 6a8d4b3

File tree

4 files changed

+97
-10
lines changed

4 files changed

+97
-10
lines changed

opentcs-common/src/main/java/org/opentcs/util/gui/Icons.java

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
import java.awt.Image;
66
import java.io.IOException;
7+
import java.net.URL;
78
import java.util.ArrayList;
89
import java.util.List;
10+
import java.util.Objects;
911
import javax.imageio.ImageIO;
1012
import org.slf4j.Logger;
1113
import org.slf4j.LoggerFactory;
@@ -24,13 +26,25 @@ public final class Icons {
2426
*/
2527
private static final String ICON_PATH = "/org/opentcs/util/gui/res/icons/";
2628
/**
27-
* File names of the openTCS window icons.
29+
* File names of the default openTCS window icons.
2830
*/
29-
private static final String[] ICON_FILES = {"opentcs_icon_016.png",
31+
private static final List<String> DEFAULT_ICON_FILES = List.of(
32+
"opentcs_icon_016.png",
3033
"opentcs_icon_032.png",
3134
"opentcs_icon_064.png",
3235
"opentcs_icon_128.png",
33-
"opentcs_icon_256.png"};
36+
"opentcs_icon_256.png"
37+
);
38+
/**
39+
* File names of custom window icons that can be provided by a user.
40+
*/
41+
private static final List<String> CUSTOM_ICON_FILES = List.of(
42+
"custom_icon_016.png",
43+
"custom_icon_032.png",
44+
"custom_icon_064.png",
45+
"custom_icon_128.png",
46+
"custom_icon_256.png"
47+
);
3448

3549
/**
3650
* Prevents instantiation.
@@ -40,23 +54,62 @@ private Icons() {
4054
}
4155

4256
/**
43-
* Get the icon for OpenTCS windows in different resolutions.
57+
* Get the icon for openTCS windows in different resolutions.
4458
*
4559
* @return List of icons
4660
*/
4761
public static List<Image> getOpenTCSIcons() {
62+
List<Image> customIcons = loadCustomIcons();
63+
return customIcons.isEmpty() ? loadDefaultIcons() : customIcons;
64+
}
65+
66+
private static List<Image> loadCustomIcons() {
67+
List<Image> icons = loadIcons(findFiles(ICON_PATH, CUSTOM_ICON_FILES));
68+
69+
if (icons.isEmpty()) {
70+
LOG.debug(
71+
"Couldn't find any custom icon files at '{}' (file names: {}). Using default icon files.",
72+
ICON_PATH,
73+
CUSTOM_ICON_FILES
74+
);
75+
return List.of();
76+
}
77+
78+
if (icons.size() < CUSTOM_ICON_FILES.size()) {
79+
LOG.warn(
80+
"Couldn't find all custom icon files at '{}' (file names: {}). Using default icon files.",
81+
ICON_PATH,
82+
CUSTOM_ICON_FILES
83+
);
84+
return List.of();
85+
}
86+
87+
return icons;
88+
}
89+
90+
private static List<Image> loadDefaultIcons() {
91+
return loadIcons(findFiles(ICON_PATH, DEFAULT_ICON_FILES));
92+
}
93+
94+
private static List<URL> findFiles(String path, List<String> fileNames) {
95+
return fileNames.stream()
96+
.map(fileName -> Icons.class.getResource(path + fileName))
97+
.filter(Objects::nonNull)
98+
.toList();
99+
}
100+
101+
private static List<Image> loadIcons(List<URL> files) {
48102
try {
49103
List<Image> icons = new ArrayList<>();
50-
for (String iconFile : ICON_FILES) {
51-
String iconURL = ICON_PATH + iconFile;
52-
final Image icon = ImageIO.read(Icons.class.getResource(iconURL));
104+
for (URL iconFile : files) {
105+
final Image icon = ImageIO.read(iconFile);
53106
icons.add(icon);
54107
}
55108
return icons;
56109
}
57-
catch (IOException | IllegalArgumentException exc) {
58-
LOG.warn("Couldn't load icon images from path {}", ICON_PATH, exc);
59-
return new ArrayList<>();
110+
catch (IOException exc) {
111+
LOG.warn("Couldn't load icon images from paths: {}", files, exc);
112+
return List.of();
60113
}
61114
}
62115
}

opentcs-documentation/src/docs/developers-guide/05_extending-control-center.adoc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,19 @@ See the JDK's API documentation for more information about how this mechanism wo
4646
(The openTCS start scripts include all JAR files in that directory in the application's classpath.)
4747

4848
Panels meeting these requirements are found automatically when you start the kernel control center application.
49+
50+
=== How to change the window icon
51+
52+
To change the image that is used as the icon for the application window:
53+
54+
. Prepare custom icon files with the following names (the number at the end of the file name indicates the respective image's expected width and height in pixels):
55+
** `custom_icon_016.png`
56+
** `custom_icon_032.png`
57+
** `custom_icon_064.png`
58+
** `custom_icon_128.png`
59+
** `custom_icon_256.png`
60+
. Create a JAR file (with any name, e.g. `my-custom-icons.jar`) that contains the custom icon files in a folder named `/org/opentcs/util/gui/res/icons/`.
61+
* Place the JAR file in the subdirectory `lib/` of the kernel control center application's installation directory _before_ the application is started.
62+
(The openTCS start scripts include all JAR files in that directory in the application's classpath.)
63+
64+
Note that the default set of icons will be used if _any_ of the custom icons (as described above) could not be found.

opentcs-documentation/src/docs/developers-guide/06_extending-model-editor-and-operations-desk.adoc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,19 @@ To customize the appearance of locations and vehicles, new theme implementations
100100
. Set the `locationThemeClass` or `vehicleThemeClass` in the Operations Desk application's configuration file.
101101

102102
Vehicles or locations in plant models are then rendered using your custom theme.
103+
104+
=== How to change the window icon
105+
106+
To change the image that is used as the icon for the application window:
107+
108+
. Prepare custom icon files with the following names (the number at the end of the file name indicates the respective image's expected width and height in pixels):
109+
** `custom_icon_016.png`
110+
** `custom_icon_032.png`
111+
** `custom_icon_064.png`
112+
** `custom_icon_128.png`
113+
** `custom_icon_256.png`
114+
. Create a JAR file (with any name, e.g. `my-custom-icons.jar`) that contains the custom icon files in a folder named `/org/opentcs/util/gui/res/icons/`.
115+
* Place the JAR file in the subdirectory `lib/` of the Model Editor application's and/or Operations Desk application's installation directory _before_ the application is started.
116+
(The openTCS start scripts include all JAR files in that directory in the respective application's classpath.)
117+
118+
Note that the default set of icons will be used if _any_ of the custom icons (as described above) could not be found.

opentcs-documentation/src/docs/release-notes/changelog.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ This change log lists the most relevant changes for past releases in reverse chr
2222
** Allow order sequences to contain multiple types of transport orders.
2323
Instead of a single type, order sequences now define a set of order types.
2424
Additionally, transport orders whose type is included in this set of order types can be added to the order sequence.
25+
** Add support for customizable window icons.
26+
For more information, please refer to the developer's guide.
2527
** Update web API specification and implementation to version 1.14.0:
2628
*** Extend the request body of the `POST /orderSequences/{NAME}` endpoint so that it also excepts a list of order types.
2729
*** Extend the response bodies where order sequences are returned to also include the list of order types.

0 commit comments

Comments
 (0)