blob: 361549d559712208b36e0ed83a9d5ba1e342eee3 (
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
package com.c2kernel.utils;
//Java
import java.net.MalformedURLException;
import java.net.URL;
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 = null;
static private URL domainBaseURL = null;
static private URL importURL = null;
static public ImageIcon nullImg = new ImageIcon(new byte[] { 0 });
static {
setKernelBaseURL("com/c2kernel/utils/resources/");
}
/*
* Kernel Resource URL section
*/
public static void setKernelBaseURL(String newBaseURL) {
baseURL = getURLorResURL(newBaseURL);
}
public static void setKernelBaseURL(URL newBaseURL) {
baseURL = newBaseURL;
}
public static URL getKernelBaseURL() {
return baseURL;
}
static public URL getKernelResourceURL(String resName) throws MalformedURLException {
return new URL(baseURL, resName);
}
/*
* Domain Resource URL section
*/
public static void setDomainBaseURL(URL newBaseURL) {
domainBaseURL = newBaseURL;
}
public static void setDomainBaseURL(String newBaseURL) {
domainBaseURL = getURLorResURL(newBaseURL);
}
public static URL getDomainBaseURL() {
return domainBaseURL;
}
static public URL getDomainResourceURL(String resName) throws MalformedURLException {
return new URL(domainBaseURL, 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 getTextResource(String resName)
// throws IOException
{
Logger.msg(8, "Resource::getTextResource() - Getting resource: " + resName);
if (txtCache.containsKey(resName)) {
return txtCache.get(resName);
}
try {
String newRes = null;
try {
newRes = FileStringUtility.url2String(getDomainResourceURL(resName));
} catch (Exception ex) { } // no domain base
if (newRes == null || newRes.length() == 0) { // not found in domain
newRes = FileStringUtility.url2String(getKernelResourceURL(resName));
}
txtCache.put(resName, newRes);
return newRes;
} catch (Exception e) {
return null;
}
}
/**
* Gets an image from the resource directories
*
* @param resName - filename after resources/images
* @return
*/
static public ImageIcon getImageResource(String resName) {
try {
return getImage(resName);
} catch (ObjectNotFoundException ex) {
Logger.error("Image "+resName+" not found. Using null icon");
return nullImg;
}
}
static public ImageIcon getImage(String resName) throws ObjectNotFoundException {
if (resName == null)
return nullImg;
if (imgCache.containsKey(resName)) {
return imgCache.get(resName);
}
URL imgLocation = null;
ImageIcon newImg = null;
// try domain resources first
if (domainBaseURL != null) {
try {
imgLocation = new URL(domainBaseURL + "images/" + resName.toLowerCase());
newImg = new ImageIcon(imgLocation);
} catch (MalformedURLException e) { }
}
// try kernel resources next
if (newImg == null || newImg.getIconHeight() == -1) {
try {
imgLocation = new URL(baseURL + "images/" + resName.toLowerCase());
newImg = new ImageIcon(imgLocation);
} catch (MalformedURLException e) { }
}
// else return null image
if (newImg == null || newImg.getIconHeight() == -1) {
throw new ObjectNotFoundException();
}
else imgCache.put(resName, newImg);
Logger.msg(7, "Loaded "+resName+" "+newImg.getIconWidth()+"x"+newImg.getIconHeight());
return newImg;
}
/**
* Retrieves the stored import URL
* @return
*/
public static URL getImportURL() {
return importURL;
}
/**
* @param url
*/
public static void setImportURL(URL url) {
importURL = url;
}
static public String getDomainVersion() {
try {
return FileStringUtility.url2String(getDomainResourceURL("version.txt"));
} catch (Exception ex) {
return "Domain application version not found";
}
}
static public String getKernelVersion() {
try {
return FileStringUtility.url2String(getKernelResourceURL("textFiles/version.txt"));
} catch (Exception ex) {
return "No version info found";
}
}
}
|