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-2015 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  if (LOG.isDebugEnabled()) {
183  LOG.debug("getResourceIcon(" + iconName + "): loading from resource: " + iconURL);
184  }
185  icon = new ImageIcon(iconURL);
186  } else {
187  final File iconFile = new File(iconName);
188  if (!iconFile.exists()) {
189  LOG.warn("Cannot find icon '" + iconName + "'");
190  throw new MissingResourceException("missing resource: icon " + iconName, ResourceIcons.class.getName(), iconName);
191  }
192 
193  if (LOG.isDebugEnabled()) {
194  LOG.debug("getResourceIcon(" + iconName + "): loading from file: " + iconFile);
195  }
196  icon = new ImageIcon(iconFile.getAbsolutePath());
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  @SuppressWarnings("NullableProblems")
214  public ImageIcon getWarningSquareIcon() {
215  if (warningSquareIcon == null) {
216  final ImageFilter alphaFilter = AlphaImageFilterInstance.ALPHA_FILTER;
217  final ImageIcon sysIcon = getResourceIcon(SQUARE_WARNING);
218  final Image image = sysIcon.getImage();
219  final ImageProducer source = image.getSource();
220  final ImageProducer producer = new FilteredImageSource(source, alphaFilter);
221  final Image image2 = Toolkit.getDefaultToolkit().createImage(producer);
222  warningSquareIcon = new ImageIcon(image2);
223  }
224  return warningSquareIcon;
225  }
226 
232  @NotNull
233  @SuppressWarnings("NullableProblems")
234  public ImageIcon getLightSquareIcon() {
235  if (lightSquareIcon == null) {
236  final ImageFilter alphaFilter = AlphaImageFilterInstance.ALPHA_FILTER;
237  final ImageIcon sysIcon = getResourceIcon(SQUARE_LIGHT);
238  final Image image = sysIcon.getImage();
239  final ImageProducer source = image.getSource();
240  final ImageProducer producer = new FilteredImageSource(source, alphaFilter);
241  final Image image2 = Toolkit.getDefaultToolkit().createImage(producer);
242  lightSquareIcon = new ImageIcon(image2);
243  }
244  return lightSquareIcon;
245  }
246 
247 }
Utility class holding the singleton AlphaImageFilter instance.
static final String RUN_PLUGIN_SMALL_ICON
static final String ICON_DIR
The directory that contains the common-use icons.
static final Category LOG
The Logger for printing log messages.
ImageIcon getLightSquareIcon()
Returns the ImageIcon for highlighting map squares that are affected by nearby light emitting game ob...
static final String SYSTEM_DIR
The directory that contains the system icons.
static final String DEFAULT_PREVIEW
The default map preview to use if no icon can be created.
static final String DEFAULT_ICON
The default map icon to use if no icon can be created.
static final ImageFilter ALPHA_FILTER
The singleton AlphaImageFilter instance for creating transparent images.
static final String SQUARE_SELECTED_SQUARE_NORTH
static final String SQUARE_SELECTED_SQUARE_SOUTH
Creates ImageIcon instances from resources.
static final String SQUARE_SELECTED_SQUARE_EAST
static final String SQUARE_PRE_SELECTED_SQUARE
static final String SQUARE_LIGHT
The name of the image for highlighting map squares that are affected by nearby light emitting game ob...
ImageIcon getResourceIcon(@NotNull final String iconName)
Returns the image icon for the given icon name.
static final String SQUARE_SELECTED_SQUARE_WEST
static final String SQUARE_SELECTED_SQUARE
static final String APP_ICON
Application icon definitions (icon-dir).
void addToCache(@NotNull final String name, @NotNull final ImageIcon imageIcon)
Add an image to the cache.
final Map< String, ImageIcon > imageCache
Caches image icons.
static final String CLOSE_TAB_SMALL_ICON