summaryrefslogtreecommitdiff
path: root/source/com/c2kernel/utils/Resource.java
blob: 10228dac409e2b4d9c7fdef81e16a7136dcbc8bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.c2kernel.utils;

//Java
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Hashtable;

import javax.swing.ImageIcon;

import com.c2kernel.common.ObjectNotFoundException;

/**************************************************************************
 *
 * @author $Author: abranson $ $Date: 2005/10/05 07:39:36 $
 * @version $Revision: 1.71 $
 **************************************************************************/
public class Resource {
    static private Hashtable<String, String> txtCache = new Hashtable<String, String>();
    static private Hashtable<String, ImageIcon> imgCache = new Hashtable<String, ImageIcon>();
    static private URL baseURL = getURLorResURL("com/c2kernel/utils/resources/");
    static private HashMap<String, URL> moduleBaseURLs = new HashMap<String, URL>();
    static public final ImageIcon nullImg = new ImageIcon(new byte[] { 0 });

    public static URL getKernelBaseURL() {
        return baseURL;
    }

    public static URL getKernelResourceURL(String resName) throws MalformedURLException {
        return new URL(baseURL, resName);
    }

    public static void addModuleBaseURL(String ns, URL newBaseURL) {
        moduleBaseURLs.put(ns, newBaseURL);
        Logger.msg("Adding resource URL for "+ns+": "+newBaseURL.toString());
    }

    public static void addModuleBaseURL(String ns, String newBaseURL) {
    	addModuleBaseURL(ns, getURLorResURL(newBaseURL));
    }

    public static URL getModuleBaseURL(String ns) {
        return moduleBaseURLs.get(ns);
    }
    
    public static HashMap<String, URL> getModuleBaseURLs() {
        return moduleBaseURLs;
    }

    static public URL getModuleResourceURL(String ns, String resName) throws MalformedURLException {
        return new URL(moduleBaseURLs.get(ns), resName);
    }

    private static URL getURLorResURL(String newURL) {
        URL result;
        try {
            result = new URL(newURL);
        } catch (java.net.MalformedURLException ex) {
            //it is assumed that a 'wrong' URL denotes a directory
            //of files resolvable from the CLASSPATH
            result = Resource.class.getClassLoader().getResource(newURL);
        }
        return result;
    }
    /**************************************************************************
     * Gets any text resource files
     **************************************************************************/
    
    static public String findTextResource(String resName) {
        try {
        	for (String ns : getModuleBaseURLs().keySet()) {
        		try {
        			return getTextResource(ns, resName);
        		} catch (ObjectNotFoundException ex) { }
			}
            return getTextResource(null, resName);
        } catch (ObjectNotFoundException ex) {
            Logger.warning("Text resource '"+resName+"' not found.");
            return null;
        }
    }
    
    static public String getTextResource(String ns, String resName) throws ObjectNotFoundException
    //        throws IOException
    {
        Logger.msg(8, "Resource::getTextResource() - Getting resource: " + resName);

        if (txtCache.containsKey(ns+'/'+resName)) {
            return txtCache.get(ns+'/'+resName);
        }

        try {

            String newRes = null;
            URL loc;
            
            if (ns == null) // kernel
                loc = getKernelResourceURL(resName);
            else
            	loc = getModuleResourceURL(ns, resName);
            newRes = FileStringUtility.url2String(loc);
            txtCache.put(ns+'/'+resName, newRes);
            return newRes;
        } catch (Exception e) {
            throw new ObjectNotFoundException(e.getMessage(),null);
        }
    }
    /**
     * Gets an image from the resource directories
     *
     * @param resName - filename after resources/images
     * @return
     */
    static public ImageIcon findImage(String resName) {
        try {
        	for (String ns : getModuleBaseURLs().keySet()) {
        		try {
        			return getImage(ns, resName);
        		} catch (ObjectNotFoundException ex) { }
			}
            return getImage(null, resName);
        } catch (ObjectNotFoundException ex) {
            Logger.warning("Image '"+resName+"' not found. Using null icon");
            return nullImg;
        }
    }

    static public ImageIcon getImage(String ns, String resName) throws ObjectNotFoundException {
        if (resName == null)
            return nullImg;

        if (imgCache.containsKey(ns+'/'+resName)) {
            return imgCache.get(ns+'/'+resName);
        }

        URL imgLocation = null;
        if (ns == null) 
        	try {
        		imgLocation = getKernelResourceURL("images/"+resName);
        	} catch (MalformedURLException ex) { }
        else
        	try {
        		imgLocation = getModuleResourceURL(ns, "images/"+resName);
        	} catch (MalformedURLException ex) { }
        
        if (imgLocation!= null) {
        	ImageIcon newImg = new ImageIcon(imgLocation);

        	if (newImg.getIconHeight() > -1) {
        		imgCache.put(ns+'/'+resName, newImg);
                Logger.msg(0, "Loaded "+resName+" "+newImg.getIconWidth()+"x"+newImg.getIconHeight());
                return newImg;
        	}
        }
        throw new ObjectNotFoundException();
    }

    static public String getKernelVersion() {
    	try {
			return FileStringUtility.url2String(getKernelResourceURL("textFiles/version.txt"));
    	} catch (Exception ex) {
    		return "No version info found";
    	}

    }
}