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 addResourceModule(new ResourceModulePNG()); 084 085 // root path is package "resource" 086 resourceRootPath = "resource"; 087 } 088 089 /** 090 * Returns the singleton instance of the ResourceManager. 091 * 092 * @return the singleton instance. 093 */ 094 public static ResourceManager getInstance() { 095 if (instance == null) { 096 instance = new ResourceManager(); 097 098 } 099 return instance; 100 } 101 102 /** 103 * Adds a resource module to the ResourceManager. 104 * 105 * @param rm the resource module to be added. 106 */ 107 public void addResourceModule(ResourceModule rm) { 108 moduls.put(rm.getTypeName(), rm); 109 } 110 111 /** 112 * Removes a resource module from the ResourceManager. 113 * 114 * @param rm the resource module to be removed. 115 */ 116 public void removeResourceModule(ResourceModule rm) { 117 moduls.remove(rm.getTypeName()); 118 } 119 120 /** 121 * Returns a resource as URL. Therefor the logical name of the resource module and the relative path 122 * starting from it have to be specified.<br> 123 * </br> 124 * Example:</br> 125 * </br> 126 * logical name of the gif-module: "RESOURCE_GIF". The absolute path of this module is 127 * "org/eit/ui/resource/gif". The requested resource is "document.gif".<br> 128 * Therefore the call is: <CODE>getResource ("RESOURCE_GIF", "icon.16x16.document)</CODE> 129 * (without file extension). 130 * 131 * @param type the logical type name 132 * @param path der relative path + file name without extension 133 * @return the resource's URL. 134 */ 135 public URL getResource(String type, String path) { 136 //changed from ClassLoader.getSystemClassLoader() to this.getClass().getClassLoader() 137 //because the first version does not work with Java Web Start 138 //return ClassLoader.getSystemClassLoader().getResource(getFullPath(type, path)); 139 return this.getClass().getClassLoader().getResource(getFullPath(type, path)); 140 } 141 142 /** 143 * Returns the full path of a resource.. 144 * 145 * @param type the logical type name 146 * @param path der relative path + file name without extension 147 * @return the resource's full path as String. 148 */ 149 public String getFullPath(String type, String path) { 150 ResourceModule rm = (ResourceModule)moduls.get(type); 151 String p = resourceRootPath + "/" + rm.getPath(); 152 153 if (rm != null) { 154 // construct path 155 StringTokenizer st = new StringTokenizer(path, "."); 156 while (st.hasMoreTokens()) { 157 p = p + "/" + st.nextToken(); 158 } 159 // append extension 160 p = p + "." + rm.getExtension(); 161 162 return p; 163 } 164 165 return ""; 166 } 167 168 /** 169 * Type name for resource module: GIF 170 */ 171 public static final String RESOURCE_GIF = "gif"; 172 173 /** 174 * Predefined module for .gif-resources. 175 */ 176 private static final class ResourceModuleGIF implements ResourceModule { 177 178 /** 179 * Returns the resource's file extension: "gif". 180 * 181 * @return the file extension 182 */ 183 public String getExtension() { 184 return "gif"; 185 } 186 187 /** 188 * Returns the path relative to the current resource path. 189 * 190 * @return the path. 191 */ 192 public String getPath() { 193 return "gif"; 194 } 195 196 /** 197 * Returns the type name of the module. This is the name by which the resources are requested. 198 * 199 * @return the type name 200 */ 201 public String getTypeName() { 202 return RESOURCE_GIF; 203 } 204 } 205 206 public static final String RESOURCE_PNG = "png"; 207 208 /** 209 * Predefined module for .png-resources. 210 */ 211 private static final class ResourceModulePNG implements ResourceModule { 212 213 /** 214 * Returns the resource's file extension: "png". 215 * 216 * @return the file extension 217 */ 218 public String getExtension() { 219 return "png"; 220 } 221 222 /** 223 * Returns the path relative to the current resource path. 224 * 225 * @return the path. 226 */ 227 public String getPath() { 228 return "png"; 229 } 230 231 /** 232 * Returns the type name of the module. This is the name by which the resources are requested. 233 * 234 * @return the type name 235 */ 236 public String getTypeName() { 237 return RESOURCE_PNG; 238 } 239 } 240 241 }