diff options
Diffstat (limited to 'source/com/c2kernel/utils/Resource.java')
| -rw-r--r-- | source/com/c2kernel/utils/Resource.java | 171 |
1 files changed, 75 insertions, 96 deletions
diff --git a/source/com/c2kernel/utils/Resource.java b/source/com/c2kernel/utils/Resource.java index 361549d..10228da 100644 --- a/source/com/c2kernel/utils/Resource.java +++ b/source/com/c2kernel/utils/Resource.java @@ -3,6 +3,7 @@ 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;
@@ -17,50 +18,37 @@ import com.c2kernel.common.ObjectNotFoundException; 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;
- }
+ 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;
}
- static public URL getKernelResourceURL(String resName) throws MalformedURLException {
+ public static 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 addModuleBaseURL(String ns, URL newBaseURL) {
+ moduleBaseURLs.put(ns, newBaseURL);
+ Logger.msg("Adding resource URL for "+ns+": "+newBaseURL.toString());
}
- public static void setDomainBaseURL(String newBaseURL) {
- domainBaseURL = getURLorResURL(newBaseURL);
+ public static void addModuleBaseURL(String ns, String newBaseURL) {
+ addModuleBaseURL(ns, getURLorResURL(newBaseURL));
}
- public static URL getDomainBaseURL() {
- return domainBaseURL;
+ public static URL getModuleBaseURL(String ns) {
+ return moduleBaseURLs.get(ns);
+ }
+
+ public static HashMap<String, URL> getModuleBaseURLs() {
+ return moduleBaseURLs;
}
- static public URL getDomainResourceURL(String resName) throws MalformedURLException {
- return new URL(domainBaseURL, resName);
+ static public URL getModuleResourceURL(String ns, String resName) throws MalformedURLException {
+ return new URL(moduleBaseURLs.get(ns), resName);
}
private static URL getURLorResURL(String newURL) {
@@ -77,29 +65,44 @@ public class Resource { /**************************************************************************
* Gets any text resource files
**************************************************************************/
- static public String getTextResource(String resName)
+
+ 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(resName)) {
- return txtCache.get(resName);
+ if (txtCache.containsKey(ns+'/'+resName)) {
+ return txtCache.get(ns+'/'+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);
+ 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) {
- return null;
+ throw new ObjectNotFoundException(e.getMessage(),null);
}
}
/**
@@ -108,72 +111,48 @@ public class Resource { * @param resName - filename after resources/images
* @return
*/
- static public ImageIcon getImageResource(String resName) {
+ static public ImageIcon findImage(String resName) {
try {
- return getImage(resName);
+ for (String ns : getModuleBaseURLs().keySet()) {
+ try {
+ return getImage(ns, resName);
+ } catch (ObjectNotFoundException ex) { }
+ }
+ return getImage(null, resName);
} catch (ObjectNotFoundException ex) {
- Logger.error("Image "+resName+" not found. Using null icon");
+ Logger.warning("Image '"+resName+"' not found. Using null icon");
return nullImg;
}
}
- static public ImageIcon getImage(String resName) throws ObjectNotFoundException {
+ static public ImageIcon getImage(String ns, String resName) throws ObjectNotFoundException {
if (resName == null)
return nullImg;
- if (imgCache.containsKey(resName)) {
- return imgCache.get(resName);
+ if (imgCache.containsKey(ns+'/'+resName)) {
+ return imgCache.get(ns+'/'+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";
- }
+ 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() {
|
