KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > ResourceManager


1 /*
2   Copyright (C) 2001 Renaud Pawlak, Laurent Martelli
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.aspects.gui;
20
21 import java.io.InputStream JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import javax.swing.ImageIcon JavaDoc;
25 import org.apache.log4j.Logger;
26
27 /**
28  * A class with static methods to get GUI resources as icons.<p>
29  *
30  * @author <a HREF="mailto:renaud@cnam.fr">Renaud Pawlak</a>
31  * @author <a HREF="mailto:laurent@aopsys.com">Laurent Martelli</a>
32  */

33
34 public class ResourceManager {
35     static Logger logger = Logger.getLogger("gui.resources");
36
37     /** Store the path where to find resources for the GUI. */
38     public static final String JavaDoc RESOURCES_PATH = "org/objectweb/jac/aspects/gui/resources/";
39
40     static Hashtable JavaDoc icons = new Hashtable JavaDoc();
41    
42     /**
43      * Creates an icon from a path string.
44      *
45      * @param path the path of the icon (absolute or accessible through
46      * the classpath) */

47
48     public static ImageIcon JavaDoc createIcon(String JavaDoc path) {
49         logger.debug("resolving URL for "+path);
50         URL JavaDoc url = ResourceManager.class.getClassLoader().getResource(path);
51         if (url==null) {
52             logger.warn("Could not find resource "+path);
53             return null;
54         } else {
55             logger.debug("URL="+url);
56             return new ImageIcon JavaDoc(url);
57         }
58     }
59
60     static Hashtable JavaDoc resources = new Hashtable JavaDoc();
61
62     /**
63      * Define a resource.
64      *
65      * @param name the short name of the resource
66      * @param path the full path to the resource
67      * @see #getIcon(String)
68      */

69     public static void defineResource(String JavaDoc name, String JavaDoc path) {
70         resources.put(name,path);
71     }
72
73     public static String JavaDoc getResource(String JavaDoc name) {
74         return (String JavaDoc)resources.get(name);
75     }
76
77     public static InputStream JavaDoc getResourceAsStream(String JavaDoc name) {
78         return ResourceManager.class.getClassLoader().getResourceAsStream(
79             getResource(name));
80     }
81
82     /**
83      * Build an icon using a resource as the image. If the resource is
84      * null, returns null.
85      *
86      * @param resource the resource name (full path)
87      * @see #getIconResource(String)
88      * @see #defineResource(String,String) */

89
90     public static ImageIcon JavaDoc getIcon(String JavaDoc resource) {
91         if (resource==null)
92             return null;
93         ImageIcon JavaDoc result = (ImageIcon JavaDoc)icons.get(resource);
94         if (result==null) {
95             result = createIcon(resource);
96             if (result!=null)
97                 icons.put(resource,result);
98         }
99         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 JavaDoc getIconResource(String JavaDoc name) {
111         return getIcon(getResource(name));
112     }
113 }
114
Popular Tags