From 684e01bb190c7d0b95347b732aeb3cdeda4740d7 Mon Sep 17 00:00:00 2001 From: abranson Date: Tue, 18 Oct 2011 17:00:33 +0200 Subject: Module support --- source/com/c2kernel/utils/CastorArrayList.java | 2 +- source/com/c2kernel/utils/Resource.java | 148 ++++++++++++------------- 2 files changed, 75 insertions(+), 75 deletions(-) (limited to 'source/com/c2kernel/utils') diff --git a/source/com/c2kernel/utils/CastorArrayList.java b/source/com/c2kernel/utils/CastorArrayList.java index ed15c0f..ea9a090 100644 --- a/source/com/c2kernel/utils/CastorArrayList.java +++ b/source/com/c2kernel/utils/CastorArrayList.java @@ -15,7 +15,7 @@ import java.util.ArrayList; * All rights reserved. **************************************************************************/ // -abstract public class CastorArrayList implements Serializable{ +abstract public class CastorArrayList implements Serializable { public ArrayList list; public CastorArrayList() { diff --git a/source/com/c2kernel/utils/Resource.java b/source/com/c2kernel/utils/Resource.java index ba2f71a..10228da 100644 --- a/source/com/c2kernel/utils/Resource.java +++ b/source/com/c2kernel/utils/Resource.java @@ -3,7 +3,7 @@ package com.c2kernel.utils; //Java import java.net.MalformedURLException; import java.net.URL; -import java.util.ArrayList; +import java.util.HashMap; import java.util.Hashtable; import javax.swing.ImageIcon; @@ -19,42 +19,36 @@ public class Resource { static private Hashtable txtCache = new Hashtable(); static private Hashtable imgCache = new Hashtable(); static private URL baseURL = getURLorResURL("com/c2kernel/utils/resources/"); - static private ArrayList domainBaseURL = new ArrayList(); - static public ImageIcon nullImg = new ImageIcon(new byte[] { 0 }); - - /* - * Kernel Resource URL section - */ - public static void setKernelBaseURL(String newBaseURL) { - baseURL = getURLorResURL(newBaseURL); - } + static private HashMap moduleBaseURLs = new HashMap(); + 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 addDomainBaseURL(URL newBaseURL) { - domainBaseURL.add(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 addDomainBaseURL(String newBaseURL) { - addDomainBaseURL(getURLorResURL(newBaseURL)); + public static void addModuleBaseURL(String ns, String newBaseURL) { + addModuleBaseURL(ns, getURLorResURL(newBaseURL)); } - public static ArrayList getDomainBaseURL() { - return domainBaseURL; + public static URL getModuleBaseURL(String ns) { + return moduleBaseURLs.get(ns); + } + + public static HashMap 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) { @@ -71,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); } } /** @@ -102,57 +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; - } - - 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() { -- cgit v1.2.3