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.io.File; 023import java.net.URL; 024import java.util.HashMap; 025import java.util.Map; 026import java.util.MissingResourceException; 027import javax.swing.ImageIcon; 028import org.apache.log4j.Category; 029import org.apache.log4j.Logger; 030import org.jetbrains.annotations.NotNull; 031 032/** 033 * <code>CGUtils</code> is a collection of GUI utility methods. Mainly focusing 034 * on resource management. 035 * @author <a href="mailto:michael.toennies@nord-com.net">Michael Toennies</a> 036 * @author <a href="mailto:andi.vogl@gmx.net">Andreas Vogl</a> 037 * @author <a href="mailto:cher@riedquat.de">Christian Hujer</a> 038 */ 039public class GUIUtils { 040 041 /** 042 * The Logger for printing log messages. 043 */ 044 private static final Category log = Logger.getLogger(GUIUtils.class); 045 046 /** 047 * Caches image icons. Maps icon name to image icon. 048 */ 049 @NotNull 050 private final Map<String, ImageIcon> imageCache = new HashMap<String, ImageIcon>(); 051 052 /** 053 * Returns the image icon for the given icon name. Loads every icon only 054 * once and uses hash table to return the same instance if same icon name is 055 * given. Note: There must not be conflicting icon names from different 056 * directories. 057 * @param iconName the icon name 058 * @return the image icon for the given icon name 059 * @throws MissingResourceException if the icon cannot be loaded 060 */ 061 @NotNull 062 public ImageIcon getResourceIcon(@NotNull final String iconName) throws MissingResourceException { 063 // check cache 064 if (imageCache.containsKey(iconName)) { 065 if (log.isDebugEnabled()) { 066 log.debug("getResourceIcon(" + iconName + "): return cached"); 067 } 068 return imageCache.get(iconName); 069 } 070 071 @NotNull final ImageIcon icon; 072 073 final URL iconURL = GUIUtils.class.getClassLoader().getResource(iconName); 074 if (iconURL != null) { 075 if (log.isDebugEnabled()) { 076 log.debug("getResourceIcon(" + iconName + "): loading from resource: " + iconURL); 077 } 078 icon = new ImageIcon(iconURL); 079 } else { 080 final File iconFile = new File(iconName); 081 if (iconFile.exists()) { 082 if (log.isDebugEnabled()) { 083 log.debug("getResourceIcon(" + iconName + "): loading from file: " + iconFile); 084 } 085 icon = new ImageIcon(iconFile.getAbsolutePath()); 086 } else { 087 log.warn("Cannot find icon '" + iconName + "'"); 088 throw new MissingResourceException("missing resource: icon " + iconName, GUIUtils.class.getName(), iconName); 089 } 090 } 091 092 imageCache.put(iconName, icon); 093 return icon; 094 } 095 096 /** 097 * Add an image to the cache. 098 * @param name the name 099 * @param imageIcon the image icon 100 */ 101 public void addToCache(@NotNull final String name, @NotNull final ImageIcon imageIcon) { 102 imageCache.put(name, imageIcon); 103 } 104 105}