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 txtCache = new Hashtable(); static private Hashtable imgCache = new Hashtable(); 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 (String)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 (ImageIcon)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"; } } }