001 /* 002 Copyright (C) 2001 Renaud Pawlak, Laurent Martelli 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU Lesser General Public License as 006 published by the Free Software Foundation; either version 2 of the 007 License, or (at your option) any later version. 008 009 This program is distributed in the hope that it will be useful, but 010 WITHOUT ANY WARRANTY; without even the implied warranty of 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 Lesser General Public License for more details. 013 014 You should have received a copy of the GNU Lesser General Public 015 License along with this program; if not, write to the Free Software 016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 017 USA */ 018 019 package org.objectweb.jac.aspects.gui; 020 021 import java.io.InputStream; 022 import java.net.URL; 023 import java.util.Hashtable; 024 import javax.swing.ImageIcon; 025 import org.apache.log4j.Logger; 026 027 /** 028 * A class with static methods to get GUI resources as icons.<p> 029 * 030 * @author <a href="mailto:renaud@cnam.fr">Renaud Pawlak</a> 031 * @author <a href="mailto:laurent@aopsys.com">Laurent Martelli</a> 032 */ 033 034 public class ResourceManager { 035 static Logger logger = Logger.getLogger("gui.resources"); 036 037 /** Store the path where to find resources for the GUI. */ 038 public static final String RESOURCES_PATH = "org/objectweb/jac/aspects/gui/resources/"; 039 040 static Hashtable icons = new Hashtable(); 041 042 /** 043 * Creates an icon from a path string. 044 * 045 * @param path the path of the icon (absolute or accessible through 046 * the classpath) */ 047 048 public static ImageIcon createIcon(String path) { 049 logger.debug("resolving URL for "+path); 050 URL url = ResourceManager.class.getClassLoader().getResource(path); 051 if (url==null) { 052 logger.warn("Could not find resource "+path); 053 return null; 054 } else { 055 logger.debug("URL="+url); 056 return new ImageIcon(url); 057 } 058 } 059 060 static Hashtable resources = new Hashtable(); 061 062 /** 063 * Define a resource. 064 * 065 * @param name the short name of the resource 066 * @param path the full path to the resource 067 * @see #getIcon(String) 068 */ 069 public static void defineResource(String name, String path) { 070 resources.put(name,path); 071 } 072 073 public static String getResource(String name) { 074 return (String)resources.get(name); 075 } 076 077 public static InputStream getResourceAsStream(String name) { 078 return ResourceManager.class.getClassLoader().getResourceAsStream( 079 getResource(name)); 080 } 081 082 /** 083 * Build an icon using a resource as the image. If the resource is 084 * null, returns null. 085 * 086 * @param resource the resource name (full path) 087 * @see #getIconResource(String) 088 * @see #defineResource(String,String) */ 089 090 public static ImageIcon getIcon(String resource) { 091 if (resource==null) 092 return null; 093 ImageIcon result = (ImageIcon)icons.get(resource); 094 if (result==null) { 095 result = createIcon(resource); 096 if (result!=null) 097 icons.put(resource,result); 098 } 099 return result; 100 } 101 102 /** 103 * Build an icon using a named resource as the image. If name is 104 * not a known resource name, returns null. 105 * 106 * @param name the name of the resource 107 * 108 * @see #getIcon(String) 109 * @see #defineResource(String,String) */ 110 public static ImageIcon getIconResource(String name) { 111 return getIcon(getResource(name)); 112 } 113 }