Gridarta Editor
ResourceIcons.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.utils;
21 
22 import java.awt.Image;
23 import java.awt.Toolkit;
24 import java.awt.image.FilteredImageSource;
25 import java.awt.image.ImageFilter;
26 import java.awt.image.ImageProducer;
27 import java.io.File;
28 import java.net.URL;
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.MissingResourceException;
32 import javax.swing.ImageIcon;
33 import org.apache.log4j.Category;
34 import org.apache.log4j.Logger;
35 import org.jetbrains.annotations.NotNull;
36 import org.jetbrains.annotations.Nullable;
37 
46 public class ResourceIcons {
47 
51  private static final String ICON_DIR = "icons/";
52 
56  public static final String SYSTEM_DIR = "system/";
57 
58  @NotNull
59  public static final String SQUARE_SELECTED_SQUARE = SYSTEM_DIR + "selected_square.png";
60 
61  @NotNull
62  public static final String SQUARE_SELECTED_SQUARE_NORTH = SYSTEM_DIR + "selected_square_n.png";
63 
64  @NotNull
65  public static final String SQUARE_SELECTED_SQUARE_EAST = SYSTEM_DIR + "selected_square_e.png";
66 
67  @NotNull
68  public static final String SQUARE_SELECTED_SQUARE_SOUTH = SYSTEM_DIR + "selected_square_s.png";
69 
70  @NotNull
71  public static final String SQUARE_SELECTED_SQUARE_WEST = SYSTEM_DIR + "selected_square_w.png";
72 
73  @NotNull
74  public static final String SQUARE_PRE_SELECTED_SQUARE = SYSTEM_DIR + "pre_selected_square.png";
75 
76  @NotNull
77  public static final String SQUARE_CURSOR = SYSTEM_DIR + "cursor.png";
78 
79  @NotNull
80  public static final String SQUARE_EMPTY = SYSTEM_DIR + "empty.png";
81 
82  @NotNull
83  public static final String SQUARE_UNKNOWN = SYSTEM_DIR + "unknown.png";
84 
85  public static final String SQUARE_WARNING = SYSTEM_DIR + "warning.png";
86 
91  private static final String SQUARE_LIGHT = SYSTEM_DIR + "light.png";
92 
93  @NotNull
94  public static final String SQUARE_NO_FACE = SYSTEM_DIR + "no_face.png";
95 
96  @NotNull
97  public static final String SQUARE_NO_ARCH = SYSTEM_DIR + "no_arch.png";
98 
99  @NotNull
100  public static final String TREASURE_LIST = SYSTEM_DIR + "treasure_list.png";
101 
102  @NotNull
103  public static final String TREASUREONE_LIST = SYSTEM_DIR + "treasureone_list.png";
104 
105  @NotNull
106  public static final String TREASURE_YES = SYSTEM_DIR + "treasure_yes.png";
107 
108  @NotNull
109  public static final String TREASURE_NO = SYSTEM_DIR + "treasure_no.png";
110 
114  @NotNull
115  public static final String DEFAULT_ICON = SYSTEM_DIR + "default_icon.png";
116 
120  @NotNull
121  public static final String DEFAULT_PREVIEW = SYSTEM_DIR + "default_preview.png";
122 
123  @NotNull
124  public static final String CLOSE_TAB_SMALL_ICON = ICON_DIR + "close_tab_small_icon.gif";
125 
126  @NotNull
127  public static final String AUTO_RUN_SMALL_ICON = ICON_DIR + "auto_run_small_icon.gif";
128 
129  @NotNull
130  public static final String FILTER_SMALL_ICON = ICON_DIR + "filter_small_icon.gif";
131 
132  @NotNull
133  public static final String RUN_PLUGIN_SMALL_ICON = ICON_DIR + "run_plugin_small_icon.gif";
134 
138  @NotNull
139  public static final String APP_ICON = ICON_DIR + "app_icon.gif";
140 
144  private static final Category LOG = Logger.getLogger(ResourceIcons.class);
145 
149  @NotNull
150  private final Map<String, ImageIcon> imageCache = new HashMap<>();
151 
152  @Nullable
153  private ImageIcon warningSquareIcon;
154 
155  @Nullable
156  private ImageIcon lightSquareIcon;
157 
167  @NotNull
168  public ImageIcon getResourceIcon(@NotNull final String iconName) throws MissingResourceException {
169  // check cache
170  final ImageIcon imageIcon = imageCache.get(iconName);
171  if (imageIcon != null) {
172  if (LOG.isDebugEnabled()) {
173  LOG.debug("getResourceIcon(" + iconName + "): return cached");
174  }
175  return imageIcon;
176  }
177 
178  @NotNull final ImageIcon icon;
179 
180  final URL iconURL = ResourceIcons.class.getClassLoader().getResource(iconName);
181  if (iconURL == null) {
182  final File iconFile = new File(iconName);
183  if (!iconFile.exists()) {
184  LOG.warn("Cannot find icon '" + iconName + "'");
185  throw new MissingResourceException("missing resource: icon " + iconName, ResourceIcons.class.getName(), iconName);
186  }
187 
188  if (LOG.isDebugEnabled()) {
189  LOG.debug("getResourceIcon(" + iconName + "): loading from file: " + iconFile);
190  }
191  icon = new ImageIcon(iconFile.getAbsolutePath());
192  } else {
193  if (LOG.isDebugEnabled()) {
194  LOG.debug("getResourceIcon(" + iconName + "): loading from resource: " + iconURL);
195  }
196  icon = new ImageIcon(iconURL);
197  }
198 
199  imageCache.put(iconName, icon);
200  return icon;
201  }
202 
208  public void addToCache(@NotNull final String name, @NotNull final ImageIcon imageIcon) {
209  imageCache.put(name, imageIcon);
210  }
211 
212  @NotNull
213  public ImageIcon getWarningSquareIcon() {
214  if (warningSquareIcon == null) {
215  final ImageFilter alphaFilter = AlphaImageFilterInstance.ALPHA_FILTER;
216  final ImageIcon sysIcon = getResourceIcon(SQUARE_WARNING);
217  final Image image = sysIcon.getImage();
218  final ImageProducer source = image.getSource();
219  final ImageProducer producer = new FilteredImageSource(source, alphaFilter);
220  final Image image2 = Toolkit.getDefaultToolkit().createImage(producer);
221  warningSquareIcon = new ImageIcon(image2);
222  }
223  return warningSquareIcon;
224  }
225 
231  @NotNull
232  public ImageIcon getLightSquareIcon() {
233  if (lightSquareIcon == null) {
234  final ImageFilter alphaFilter = AlphaImageFilterInstance.ALPHA_FILTER;
235  final ImageIcon sysIcon = getResourceIcon(SQUARE_LIGHT);
236  final Image image = sysIcon.getImage();
237  final ImageProducer source = image.getSource();
238  final ImageProducer producer = new FilteredImageSource(source, alphaFilter);
239  final Image image2 = Toolkit.getDefaultToolkit().createImage(producer);
240  lightSquareIcon = new ImageIcon(image2);
241  }
242  return lightSquareIcon;
243  }
244 
245 }
name
name
Definition: ArchetypeTypeSetParserTest-ignoreDefaultAttribute1-result.txt:2
net.sf.gridarta.utils.ResourceIcons.RUN_PLUGIN_SMALL_ICON
static final String RUN_PLUGIN_SMALL_ICON
Definition: ResourceIcons.java:133
net.sf.gridarta.utils.ResourceIcons.SQUARE_PRE_SELECTED_SQUARE
static final String SQUARE_PRE_SELECTED_SQUARE
Definition: ResourceIcons.java:74
net.sf.gridarta.utils.ResourceIcons.APP_ICON
static final String APP_ICON
Application icon definitions (icon-dir).
Definition: ResourceIcons.java:139
net.sf.gridarta.utils.ResourceIcons.SQUARE_SELECTED_SQUARE_EAST
static final String SQUARE_SELECTED_SQUARE_EAST
Definition: ResourceIcons.java:65
net.sf.gridarta.utils.ResourceIcons.ICON_DIR
static final String ICON_DIR
The directory that contains the common-use icons.
Definition: ResourceIcons.java:51
net.sf.gridarta.utils.ResourceIcons.SQUARE_NO_FACE
static final String SQUARE_NO_FACE
Definition: ResourceIcons.java:94
net.sf.gridarta.utils.ResourceIcons.AUTO_RUN_SMALL_ICON
static final String AUTO_RUN_SMALL_ICON
Definition: ResourceIcons.java:127
net.sf.gridarta.utils.ResourceIcons.imageCache
final Map< String, ImageIcon > imageCache
Caches image icons.
Definition: ResourceIcons.java:150
net.sf.gridarta.utils.ResourceIcons.SQUARE_EMPTY
static final String SQUARE_EMPTY
Definition: ResourceIcons.java:80
net.sf.gridarta.utils.ResourceIcons.SQUARE_SELECTED_SQUARE_WEST
static final String SQUARE_SELECTED_SQUARE_WEST
Definition: ResourceIcons.java:71
net.sf.gridarta.utils.ResourceIcons.warningSquareIcon
ImageIcon warningSquareIcon
Definition: ResourceIcons.java:153
net.sf.gridarta.utils.ResourceIcons.SQUARE_SELECTED_SQUARE_SOUTH
static final String SQUARE_SELECTED_SQUARE_SOUTH
Definition: ResourceIcons.java:68
net.sf.gridarta.utils.ResourceIcons.CLOSE_TAB_SMALL_ICON
static final String CLOSE_TAB_SMALL_ICON
Definition: ResourceIcons.java:124
net.sf.gridarta.utils.ResourceIcons.DEFAULT_ICON
static final String DEFAULT_ICON
The default map icon to use if no icon can be created.
Definition: ResourceIcons.java:115
net.sf.gridarta.utils.ResourceIcons.TREASURE_LIST
static final String TREASURE_LIST
Definition: ResourceIcons.java:100
net.sf.gridarta.utils.ResourceIcons.TREASURE_YES
static final String TREASURE_YES
Definition: ResourceIcons.java:106
net.sf.gridarta.utils.AlphaImageFilterInstance.ALPHA_FILTER
static final ImageFilter ALPHA_FILTER
The singleton AlphaImageFilter instance for creating transparent images.
Definition: AlphaImageFilterInstance.java:34
net.sf.gridarta.utils.ResourceIcons.FILTER_SMALL_ICON
static final String FILTER_SMALL_ICON
Definition: ResourceIcons.java:130
net.sf.gridarta.utils.ResourceIcons.SQUARE_WARNING
static final String SQUARE_WARNING
Definition: ResourceIcons.java:85
net.sf.gridarta.utils.ResourceIcons.LOG
static final Category LOG
The Logger for printing log messages.
Definition: ResourceIcons.java:144
net.sf.gridarta.utils.ResourceIcons.lightSquareIcon
ImageIcon lightSquareIcon
Definition: ResourceIcons.java:156
net.sf.gridarta.utils.ResourceIcons.SQUARE_CURSOR
static final String SQUARE_CURSOR
Definition: ResourceIcons.java:77
net.sf.gridarta.utils.ResourceIcons.SQUARE_UNKNOWN
static final String SQUARE_UNKNOWN
Definition: ResourceIcons.java:83
net.sf.gridarta.utils.ResourceIcons.getResourceIcon
ImageIcon getResourceIcon(@NotNull final String iconName)
Returns the image icon for the given icon name.
Definition: ResourceIcons.java:168
net.sf.gridarta.utils.ResourceIcons.TREASUREONE_LIST
static final String TREASUREONE_LIST
Definition: ResourceIcons.java:103
net.sf.gridarta.utils.ResourceIcons.DEFAULT_PREVIEW
static final String DEFAULT_PREVIEW
The default map preview to use if no icon can be created.
Definition: ResourceIcons.java:121
net.sf.gridarta.utils.ResourceIcons.TREASURE_NO
static final String TREASURE_NO
Definition: ResourceIcons.java:109
net.sf.gridarta.utils.ResourceIcons.addToCache
void addToCache(@NotNull final String name, @NotNull final ImageIcon imageIcon)
Add an image to the cache.
Definition: ResourceIcons.java:208
net.sf.gridarta.utils.ResourceIcons.getWarningSquareIcon
ImageIcon getWarningSquareIcon()
Definition: ResourceIcons.java:213
net.sf.gridarta.utils.ResourceIcons.SQUARE_SELECTED_SQUARE_NORTH
static final String SQUARE_SELECTED_SQUARE_NORTH
Definition: ResourceIcons.java:62
net.sf.gridarta.utils.ResourceIcons.getLightSquareIcon
ImageIcon getLightSquareIcon()
Returns the ImageIcon for highlighting map squares that are affected by nearby light emitting game ob...
Definition: ResourceIcons.java:232
net.sf.gridarta.utils.ResourceIcons
Creates ImageIcon instances from resources.
Definition: ResourceIcons.java:46
net.sf.gridarta.utils.ResourceIcons.SQUARE_LIGHT
static final String SQUARE_LIGHT
The name of the image for highlighting map squares that are affected by nearby light emitting game ob...
Definition: ResourceIcons.java:91
net.sf.gridarta.utils.ResourceIcons.SYSTEM_DIR
static final String SYSTEM_DIR
The directory that contains the system icons.
Definition: ResourceIcons.java:56
net.sf.gridarta.utils.AlphaImageFilterInstance
Utility class holding the singleton AlphaImageFilter instance.
Definition: AlphaImageFilterInstance.java:28
net.sf.gridarta.utils.ResourceIcons.SQUARE_NO_ARCH
static final String SQUARE_NO_ARCH
Definition: ResourceIcons.java:97
net.sf.gridarta.utils.ResourceIcons.SQUARE_SELECTED_SQUARE
static final String SQUARE_SELECTED_SQUARE
Definition: ResourceIcons.java:59