001/*
002 * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
003 * Copyright (C) 2000-2010 The Gridarta Developers.
004 *
005 * This program is free software; you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation; either version 2 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License along
016 * with this program; if not, write to the Free Software Foundation, Inc.,
017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
018 */
019
020package net.sf.gridarta.utils;
021
022import java.awt.Image;
023import java.awt.Toolkit;
024import java.awt.image.FilteredImageSource;
025import java.awt.image.ImageFilter;
026import java.awt.image.ImageProducer;
027import javax.swing.Icon;
028import javax.swing.ImageIcon;
029import org.jetbrains.annotations.NotNull;
030import org.jetbrains.annotations.Nullable;
031
032/**
033 * Utility class for system icons.
034 * @author Andreas Kirschbaum
035 */
036public class SystemIcons {
037
038    /**
039     * The directory that contains the common-use icons.
040     */
041    private static final String ICON_DIR = "icons/";
042
043    /**
044     * The directory that contains the system icons.
045     */
046    public static final String SYSTEM_DIR = "system/";
047
048    public static final String SQUARE_SELECTED_SQUARE = SYSTEM_DIR + "selected_square.png";
049
050    public static final String SQUARE_SELECTED_SQUARE_NORTH = SYSTEM_DIR + "selected_square_n.png";
051
052    public static final String SQUARE_SELECTED_SQUARE_EAST = SYSTEM_DIR + "selected_square_e.png";
053
054    public static final String SQUARE_SELECTED_SQUARE_SOUTH = SYSTEM_DIR + "selected_square_s.png";
055
056    public static final String SQUARE_SELECTED_SQUARE_WEST = SYSTEM_DIR + "selected_square_w.png";
057
058    public static final String SQUARE_PRE_SELECTED_SQUARE = SYSTEM_DIR + "pre_selected_square.png";
059
060    public static final String SQUARE_CURSOR = SYSTEM_DIR + "cursor.png";
061
062    public static final String SQUARE_EMPTY = SYSTEM_DIR + "empty.png";
063
064    public static final String SQUARE_UNKNOWN = SYSTEM_DIR + "unknown.png";
065
066    public static final String SQUARE_WARNING = SYSTEM_DIR + "warning.png";
067
068    /**
069     * The name of the image for highlighting map squares that are affected by
070     * nearby light emitting game objects.
071     */
072    public static final String SQUARE_LIGHT = SYSTEM_DIR + "light.png";
073
074    public static final String SQUARE_NO_FACE = SYSTEM_DIR + "no_face.png";
075
076    public static final String SQUARE_NO_ARCH = SYSTEM_DIR + "no_arch.png";
077
078    private static final String TREASURE_LIST = SYSTEM_DIR + "treasure_list.png";
079
080    private static final String TREASUREONE_LIST = SYSTEM_DIR + "treasureone_list.png";
081
082    private static final String TREASURE_YES = SYSTEM_DIR + "treasure_yes.png";
083
084    private static final String TREASURE_NO = SYSTEM_DIR + "treasure_no.png";
085
086    /**
087     * The default map icon to use if no icon can be created.
088     */
089    public static final String DEFAULT_ICON = SYSTEM_DIR + "default_icon.png";
090
091    /**
092     * The default map preview to use if no icon can be created.
093     */
094    public static final String DEFAULT_PREVIEW = SYSTEM_DIR + "default_preview.png";
095
096    private static final String CLOSE_TAB_SMALL_ICON = ICON_DIR + "close_tab_small_icon.gif";
097
098    private static final String AUTO_RUN_SMALL_ICON = ICON_DIR + "auto_run_small_icon.gif";
099
100    private static final String FILTER_SMALL_ICON = ICON_DIR + "filter_small_icon.gif";
101
102    private static final String RUN_PLUGIN_SMALL_ICON = ICON_DIR + "run_plugin_small_icon.gif";
103
104    /**
105     * Application icon definitions (icon-dir).
106     */
107    private static final String APP_ICON = ICON_DIR + "app_icon.gif";
108
109    /**
110     * The {@link GUIUtils} for creating icons.
111     */
112    @NotNull
113    private final GUIUtils guiUtils;
114
115    @Nullable
116    private ImageIcon mapSelIcon;
117
118    @Nullable
119    private ImageIcon mapSelIconNorth;
120
121    @Nullable
122    private ImageIcon mapSelIconEast;
123
124    @Nullable
125    private ImageIcon mapSelIconSouth;
126
127    @Nullable
128    private ImageIcon mapSelIconWest;
129
130    @Nullable
131    private ImageIcon mapPreSelIcon;
132
133    @Nullable
134    private ImageIcon mapCursorIcon;
135
136    @Nullable
137    private Icon emptySquareIcon;
138
139    @Nullable
140    private ImageIcon unknownSquareIcon;
141
142    @Nullable
143    private ImageIcon warningSquareIcon;
144
145    @Nullable
146    private ImageIcon lightSquareIcon;
147
148    @Nullable
149    private ImageIcon noFaceSquareIcon;
150
151    @Nullable
152    private ImageIcon noArchSquareIcon;
153
154    /**
155     * The default icon {@link Image} for map previews.
156     */
157    @Nullable
158    private Image defaultIcon;
159
160    /**
161     * The default preview {@link Image} for map icon.
162     */
163    @Nullable
164    private Image defaultPreview;
165
166    @Nullable
167    private Icon treasureListIcon;
168
169    @Nullable
170    private Icon treasureOneListIcon;
171
172    @Nullable
173    private Icon treasureYesIcon;
174
175    @Nullable
176    private Icon treasureNoIcon;
177
178    @Nullable
179    private ImageIcon closeTabSmallIcon;
180
181    @Nullable
182    private Icon autoRunSmallIcon;
183
184    @Nullable
185    private Icon filterSmallIcon;
186
187    @Nullable
188    private Icon runPluginSmallIcon;
189
190    /**
191     * The application's icon or <code>null</code> if none is available.
192     */
193    @Nullable
194    private ImageIcon appIcon;
195
196    /**
197     * Creates a new instance.
198     * @param guiUtils the gui utils for creating icons
199     */
200    public SystemIcons(@NotNull final GUIUtils guiUtils) {
201        this.guiUtils = guiUtils;
202    }
203
204    @NotNull
205    @SuppressWarnings("NullableProblems")
206    public ImageIcon getMapCursorIcon() {
207        if (mapCursorIcon == null) {
208            mapCursorIcon = guiUtils.getResourceIcon(SQUARE_CURSOR);
209        }
210        return mapCursorIcon;
211    }
212
213    @NotNull
214    @SuppressWarnings("NullableProblems")
215    public Icon getEmptySquareIcon() {
216        if (emptySquareIcon == null) {
217            emptySquareIcon = guiUtils.getResourceIcon(SQUARE_EMPTY);
218        }
219        return emptySquareIcon;
220    }
221
222    @NotNull
223    @SuppressWarnings("NullableProblems")
224    public ImageIcon getUnknownSquareIcon() {
225        if (unknownSquareIcon == null) {
226            unknownSquareIcon = guiUtils.getResourceIcon(SQUARE_UNKNOWN);
227        }
228        return unknownSquareIcon;
229    }
230
231    @NotNull
232    @SuppressWarnings("NullableProblems")
233    public ImageIcon getWarningSquareIcon() {
234        if (warningSquareIcon == null) {
235            final ImageFilter alphaFilter = AlphaImageFilterInstance.ALPHA_FILTER;
236            final ImageIcon sysIcon = guiUtils.getResourceIcon(SQUARE_WARNING);
237            final Image image = sysIcon.getImage();
238            final ImageProducer source = image.getSource();
239            final ImageProducer producer = new FilteredImageSource(source, alphaFilter);
240            final Image image2 = Toolkit.getDefaultToolkit().createImage(producer);
241            warningSquareIcon = new ImageIcon(image2);
242        }
243        return warningSquareIcon;
244    }
245
246    /**
247     * Returns the {@link ImageIcon} for highlighting map squares that are
248     * affected by nearby light emitting game objects.
249     * @return the image icon
250     */
251    @NotNull
252    @SuppressWarnings("NullableProblems")
253    public ImageIcon getLightSquareIcon() {
254        if (lightSquareIcon == null) {
255            final ImageFilter alphaFilter = AlphaImageFilterInstance.ALPHA_FILTER;
256            final ImageIcon sysIcon = guiUtils.getResourceIcon(SQUARE_LIGHT);
257            final Image image = sysIcon.getImage();
258            final ImageProducer source = image.getSource();
259            final ImageProducer producer = new FilteredImageSource(source, alphaFilter);
260            final Image image2 = Toolkit.getDefaultToolkit().createImage(producer);
261            lightSquareIcon = new ImageIcon(image2);
262        }
263        return lightSquareIcon;
264    }
265
266    @NotNull
267    @SuppressWarnings("NullableProblems")
268    public ImageIcon getNoFaceSquareIcon() {
269        if (noFaceSquareIcon == null) {
270            noFaceSquareIcon = guiUtils.getResourceIcon(SQUARE_NO_FACE);
271        }
272        return noFaceSquareIcon;
273    }
274
275    @NotNull
276    @SuppressWarnings("NullableProblems")
277    public ImageIcon getNoArchSquareIcon() {
278        if (noArchSquareIcon == null) {
279            noArchSquareIcon = guiUtils.getResourceIcon(SQUARE_NO_ARCH);
280        }
281        return noArchSquareIcon;
282    }
283
284    @NotNull
285    @SuppressWarnings("NullableProblems")
286    public ImageIcon getMapSelectedIcon() {
287        if (mapSelIcon == null) {
288            mapSelIcon = guiUtils.getResourceIcon(SQUARE_SELECTED_SQUARE);
289        }
290        return mapSelIcon;
291    }
292
293    @NotNull
294    @SuppressWarnings("NullableProblems")
295    public ImageIcon getMapSelectedIconNorth() {
296        if (mapSelIconNorth == null) {
297            mapSelIconNorth = guiUtils.getResourceIcon(SQUARE_SELECTED_SQUARE_NORTH);
298        }
299        return mapSelIconNorth;
300    }
301
302    @NotNull
303    @SuppressWarnings("NullableProblems")
304    public ImageIcon getMapSelectedIconEast() {
305        if (mapSelIconEast == null) {
306            mapSelIconEast = guiUtils.getResourceIcon(SQUARE_SELECTED_SQUARE_EAST);
307        }
308        return mapSelIconEast;
309    }
310
311    @NotNull
312    @SuppressWarnings("NullableProblems")
313    public ImageIcon getMapSelectedIconSouth() {
314        if (mapSelIconSouth == null) {
315            mapSelIconSouth = guiUtils.getResourceIcon(SQUARE_SELECTED_SQUARE_SOUTH);
316        }
317        return mapSelIconSouth;
318    }
319
320    @NotNull
321    @SuppressWarnings("NullableProblems")
322    public ImageIcon getMapSelectedIconWest() {
323        if (mapSelIconWest == null) {
324            mapSelIconWest = guiUtils.getResourceIcon(SQUARE_SELECTED_SQUARE_WEST);
325        }
326        return mapSelIconWest;
327    }
328
329    @NotNull
330    @SuppressWarnings("NullableProblems")
331    public ImageIcon getMapPreSelectedIcon() {
332        if (mapPreSelIcon == null) {
333            mapPreSelIcon = guiUtils.getResourceIcon(SQUARE_PRE_SELECTED_SQUARE);
334        }
335        return mapPreSelIcon;
336    }
337
338    /**
339     * Returns the default icon {@link Image} for map previews.
340     * @return the image
341     */
342    @NotNull
343    @SuppressWarnings("NullableProblems")
344    public Image getDefaultIcon() {
345        if (defaultIcon == null) {
346            final ImageIcon icon = guiUtils.getResourceIcon(DEFAULT_ICON);
347            defaultIcon = icon.getImage();
348        }
349        assert defaultIcon != null;
350        return defaultIcon;
351    }
352
353    /**
354     * Returns the default preview {@link Image} for map icon.
355     * @return the image
356     */
357    @NotNull
358    @SuppressWarnings("NullableProblems")
359    public Image getDefaultPreview() {
360        if (defaultPreview == null) {
361            final ImageIcon icon = guiUtils.getResourceIcon(DEFAULT_PREVIEW);
362            defaultPreview = icon.getImage();
363        }
364        assert defaultPreview != null;
365        return defaultPreview;
366    }
367
368    @NotNull
369    @SuppressWarnings("NullableProblems")
370    public Icon getTreasureListIcon() {
371        if (treasureListIcon == null) {
372            treasureListIcon = guiUtils.getResourceIcon(TREASURE_LIST);
373        }
374        return treasureListIcon;
375    }
376
377    @NotNull
378    @SuppressWarnings("NullableProblems")
379    public Icon getTreasureOneListIcon() {
380        if (treasureOneListIcon == null) {
381            treasureOneListIcon = guiUtils.getResourceIcon(TREASUREONE_LIST);
382        }
383        return treasureOneListIcon;
384    }
385
386    @NotNull
387    @SuppressWarnings("NullableProblems")
388    public Icon getTreasureYesIcon() {
389        if (treasureYesIcon == null) {
390            treasureYesIcon = guiUtils.getResourceIcon(TREASURE_YES);
391        }
392        return treasureYesIcon;
393    }
394
395    @NotNull
396    @SuppressWarnings("NullableProblems")
397    public Icon getTreasureNoIcon() {
398        if (treasureNoIcon == null) {
399            treasureNoIcon = guiUtils.getResourceIcon(TREASURE_NO);
400        }
401        return treasureNoIcon;
402    }
403
404    @NotNull
405    @SuppressWarnings("NullableProblems")
406    public ImageIcon getCloseTabSmallIcon() {
407        if (closeTabSmallIcon == null) {
408            closeTabSmallIcon = guiUtils.getResourceIcon(CLOSE_TAB_SMALL_ICON);
409        }
410        return closeTabSmallIcon;
411    }
412
413    @NotNull
414    @SuppressWarnings("NullableProblems")
415    public Icon getAutoRunSmallIcon() {
416        if (autoRunSmallIcon == null) {
417            autoRunSmallIcon = guiUtils.getResourceIcon(AUTO_RUN_SMALL_ICON);
418        }
419        return autoRunSmallIcon;
420    }
421
422    @NotNull
423    @SuppressWarnings("NullableProblems")
424    public Icon getFilterSmallIcon() {
425        if (filterSmallIcon == null) {
426            filterSmallIcon = guiUtils.getResourceIcon(FILTER_SMALL_ICON);
427        }
428        return filterSmallIcon;
429    }
430
431    @NotNull
432    @SuppressWarnings("NullableProblems")
433    public Icon getRunPluginSmallIcon() {
434        if (runPluginSmallIcon == null) {
435            runPluginSmallIcon = guiUtils.getResourceIcon(RUN_PLUGIN_SMALL_ICON);
436        }
437        return runPluginSmallIcon;
438    }
439
440    /**
441     * Returns the application's icon.
442     * @return the icon or <code>null</code> if none is available
443     */
444    @Nullable
445    @SuppressWarnings("NullableProblems")
446    public ImageIcon getAppIcon() {
447        if (appIcon == null) {
448            appIcon = guiUtils.getResourceIcon(APP_ICON);
449        }
450        return appIcon;
451    }
452
453}