001    package resource.util;
002    
003    import java.net.*;
004    import java.util.*;
005    
006    /**
007     * The global manager for all resources. The ResourceManager is responsible for managing all binary resources
008     * of the framework. This can be images, videos, text ...<br>
009     * <br>
010     * Resources are managed as follows:<br>
011     * <br>
012     * The root directory is a directory ending with the name "resource". Here it is just the directory
013     * "resource". Starting from this directory all resources are given back relative to this path.<br>
014     * <br>
015     * There can be multiple resouces to manage: gif-images, jpeg-images, text and others.<br>
016     * For all of these resources there should exist one resource module.<br>
017     * There is already one module for {@link #RESOURCE_GIF gif-resources}.<br>
018     * </br>
019     * Resources are requested as follows:<br>
020     * <CODE>URL url = ResourceManager.getInstance().getResource (String type, String path);</CODE></br>
021     * The "type" is the name of the resource module. The path string is the path relative to the directory
022     * of the requested type and is separated with a dot.
023     * </br>
024     * Example:</br>
025     * </br>
026     * An icon of the size 16x16 should be requested. It is located in the directory "resource/gif/icon".<br>
027     * The icon's name is "document.gif".
028     * So the call is:<br>
029     * <CODE>URL url = ResourceManager.getInstance().getResource (RESOURCE_GIF, "icon.document");</CODE>
030     *
031     * @author Thomas Medack
032     * @version 1.0
033     */
034    public class ResourceManager {
035    
036        /**
037         * Interface for a resource module.
038         */
039        public static interface ResourceModule {
040            /**
041             * Returns the path relative to the current resource path.
042             *
043             * @return the path.
044             */
045            public String getPath();
046    
047            /**
048             * Returns the file extension of the resource, e.g. "gif".
049             *
050             * @return the file extension
051             */
052            public String getExtension();
053    
054            /**
055             * Returns the type name of the module. This is the name by which the resources are requested.
056             *
057             * @return the type name
058             */
059            public String getTypeName();
060        }
061    
062        /**
063         * Map of all resource modules.
064         */
065        private HashMap<String, ResourceModule> moduls = new HashMap<String, ResourceModule>();
066    
067        /**
068         * The root path of all resources.
069         */
070        protected String resourceRootPath = "";
071    
072        /**
073         * The singleton instance of the resource manager.
074         */
075        private static ResourceManager instance = null;
076    
077        /**
078         * Creates a new instance of ResourceManager.
079         */
080        protected ResourceManager() {
081            // add resource module
082            addResourceModule(new ResourceModuleGIF());
083    
084            // root path is package "resource"
085            resourceRootPath = "resource";
086        }
087    
088        /**
089         * Returns the singleton instance of the ResourceManager.
090         *
091         * @return the singleton instance.
092         */
093        public static ResourceManager getInstance() {
094            if (instance == null) {
095                instance = new ResourceManager();
096    
097            }
098            return instance;
099        }
100    
101        /**
102         * Adds a resource module to the ResourceManager.
103         *
104         * @param rm the resource module to be added.
105         */
106        public void addResourceModule(ResourceModule rm) {
107            moduls.put(rm.getTypeName(), rm);
108        }
109    
110        /**
111         * Removes a resource module from the ResourceManager.
112         *
113         * @param rm the resource module to be removed.
114         */
115        public void removeResourceModule(ResourceModule rm) {
116            moduls.remove(rm.getTypeName());
117        }
118    
119        /**
120         * Returns a resource as URL. Therefor the logical name of the resource module and the relative path
121         * starting from it have to be specified.<br>
122         * </br>
123         * Example:</br>
124         * </br>
125         * logical name of the gif-module: "RESOURCE_GIF". The absolute path of this module is
126         * "org/eit/ui/resource/gif". The requested resource is "document.gif".<br>
127         * Therefore the call is: <CODE>getResource ("RESOURCE_GIF", "icon.16x16.document)</CODE>
128         * (without file extension).
129         *
130         * @param type the logical type name
131         * @param path der relative path + file name without extension
132         * @return the resource's URL.
133         */
134        public URL getResource(String type, String path) {
135            //changed from ClassLoader.getSystemClassLoader() to this.getClass().getClassLoader()
136            //because the first version does not work with Java Web Start
137            //return ClassLoader.getSystemClassLoader().getResource(getFullPath(type, path));
138            return this.getClass().getClassLoader().getResource(getFullPath(type, path));
139        }
140    
141        /**
142         * Returns the full path of a resource..
143         *
144         * @param type the logical type name
145         * @param path der relative path + file name without extension
146         * @return the resource's full path as String.
147         */
148        public String getFullPath(String type, String path) {
149            ResourceModule rm = (ResourceModule)moduls.get(type);
150            String p = resourceRootPath + "/" + rm.getPath();
151    
152            if (rm != null) {
153                // construct path
154                StringTokenizer st = new StringTokenizer(path, ".");
155                while (st.hasMoreTokens()) {
156                    p = p + "/" + st.nextToken();
157                }
158                // append extension
159                p = p + "." + rm.getExtension();
160    
161                return p;
162            }
163    
164            return "";
165        }
166    
167        /**
168         * Type name for resource module: GIF
169         */
170        public static final String RESOURCE_GIF = "gif";
171    
172        /**
173         * Predefined module for .gif-resources.
174         */
175        private static final class ResourceModuleGIF implements ResourceModule {
176    
177            /**
178             * Returns the resource's file extension: "gif".
179             *
180             * @return the file extension
181             */
182            public String getExtension() {
183                return "gif";
184            }
185    
186            /**
187             * Returns the path relative to the current resource path.
188             *
189             * @return the path.
190             */
191            public String getPath() {
192                return "gif";
193            }
194    
195            /**
196             * Returns the type name of the module. This is the name by which the resources are requested.
197             *
198             * @return the type name
199             */
200            public String getTypeName() {
201                return RESOURCE_GIF;
202            }
203        }
204    }