package com.digi.config.util; import java.io.*; import java.text.MessageFormat; import java.util.*; import javax.swing.ImageIcon; import java.net.URL; /** * This class provides static helper methods for accessing the various resource * bundles available in the config system. * */ public class ConfigResource { //============================================================================== //= Static attributes //============================================================================== /** Singleton reference */ static ConfigResource theInstance = null; /** * Singleton accessor method */ public static ConfigResource getInstance() { if (theInstance==null) { theInstance = new ConfigResource(); } return theInstance; } /** The default resource files */ public static final String DFT_MSG_TEXT_BUNDLE_NAME = "com.digi.config.resources.CommonResourceBundle"; public static final String DFT_UI_TEXT_BUNDLE_NAME = "com.digi.config.resources.CommonUiResourceBundle"; public static final String DFT_UI_IMAGE_BUNDLE_NAME = "com.digi.config.resources.CommonImgResourceBundle"; public static final String DFT_HELP_TEXT_BUNDLE_NAME = "com.digi.config.resources.CommonHelpResourceBundle"; /** The location of the image files */ private static String imagesDirectory="/com/digi/config/images/"; public static void setImagesDirectory(String directory) { imagesDirectory = directory; } //============================================================================== //= Instance attributes //============================================================================== // Vector of resource bundles to lookup resources in. Start looking at top of list and work way down till resource found. Vector msgTextRbList; Vector uiTextRbList; Vector uiImageRbList; Vector helpTextRbList; /** Cache of loaded images so don't load multiple times */ private static Map imageMap=new HashMap(); protected ConfigResource() { // Define the ResourceBundle stacks msgTextRbList = new Vector(); uiTextRbList = new Vector(); uiImageRbList = new Vector(); helpTextRbList = new Vector(); // populate Rb Vectors with the default bundles _loadMsgTextRb(DFT_MSG_TEXT_BUNDLE_NAME); _loadUiTextRb(DFT_UI_TEXT_BUNDLE_NAME); _loadUiImageRb(DFT_UI_IMAGE_BUNDLE_NAME); _loadHelpTextRb(DFT_HELP_TEXT_BUNDLE_NAME); } /** * load the specified resource bundle and push it onto the message text resource stack. */ public void _loadMsgTextRb(String bundleName) { try { ResourceBundle rb=ResourceBundle.getBundle(bundleName); msgTextRbList.add(0,rb); } catch (MissingResourceException exc) { SystemLog.log("MissingBundle", new Serializable[] {bundleName}, new Serializable[] {exc}); } } /** * load the specified resource bundle and push it onto the UI text resource stack. */ public void _loadUiTextRb(String bundleName) { try { ResourceBundle rb=ResourceBundle.getBundle(bundleName); uiTextRbList.add(0,rb); } catch (MissingResourceException exc) { SystemLog.log("MissingBundle", new Serializable[] {bundleName}, new Serializable[] {exc}); } } /** * load the specified resource bundle and push it onto the UI image resource stack. */ public void _loadUiImageRb(String bundleName) { try { // ImageResourceBundle rb= new ImageResourceBundle(bundleName); ResourceBundle rb=ResourceBundle.getBundle(bundleName); uiImageRbList.add(0,rb); } catch (MissingResourceException exc) { SystemLog.log("MissingBundle", new Serializable[] {bundleName}, new Serializable[] {exc}); } } /** * load the specified resource bundle and push it onto the help text resource stack. */ public void _loadHelpTextRb(String bundleName) { try { ResourceBundle rb=ResourceBundle.getBundle(bundleName); helpTextRbList.add(0,rb); } catch (MissingResourceException exc) { SystemLog.log("MissingBundle", new Serializable[] {bundleName}, new Serializable[] {exc}); } } // // Static helper methods for loading new resource files // /** * load the specified resource bundle and push it onto the message text resource stack. */ public static void loadMsgTextRb(String bundleName) { getInstance()._loadMsgTextRb(bundleName); } /** * load the specified resource bundle and push it onto the UI text resource stack. */ public static void loadUiTextRb(String bundleName) { getInstance()._loadUiTextRb(bundleName); } /** * load the specified resource bundle and push it onto the UI image resource stack. */ public static void loadUiImageRb(String bundleName) { getInstance()._loadUiImageRb(bundleName); } /** * load the specified resource bundle and push it onto the help text resource stack. */ public static void loadHelpTextRb(String bundleName) { getInstance()._loadHelpTextRb(bundleName); } /** * Fetches a string from a message text resource bundle. Search the resource bundle stack from the * top to the bottom until the message is found. * Note: If the message is not located, then the key value is returned. * * @param key to use to get text from the resource bundle * @return the requested message or the key if the text is missing */ public static String getMsgRbString(String key) { try { return getMsgRbStringForce(key); } catch (MissingResourceException e) { //SystemLog.log("MissingBundleKey", new Serializable[] { DFT_MSG_TEXT_BUNDLE_NAME, key }, new Serializable[] { exc }); } return key; } /** * Fetches a string from a message text resource bundle. Search the resource bundle stack from the * top to the bottom until the message is found. * Note: If the message is not located, then a MissingResourceException is thrown * * @param key to use to get text from the resource bundle * @return the requested message */ public static String getMsgRbStringForce(String key) throws MissingResourceException { MissingResourceException mre = null; String localizedString=null; for (Iterator i=getInstance().msgTextRbList.iterator(); (localizedString==null) && i.hasNext();) { ResourceBundle rb = (ResourceBundle)i.next(); try { localizedString = rb.getString(key); } catch (MissingResourceException exc) { mre = exc; } } if ((localizedString==null) && (mre!=null)) { // didn't find the msg in any of the resource bundles throw mre; } return localizedString; } /** * Fetches a string from a message text resource bundle. Search the resource bundle stack from the * top to the bottom until the message is found. The messeage is then formatted with the supplied * parameters. * Note: If the message is not located, then the key value is returned. * * * @param key to use to get text from the resource bundle * @param replacementValues is an array of replacement values to insert into the message. * @return the requested message or the key is the text is missing */ public static String getMsgRbString(String key, Object[] replacementValues) { String localizedString; localizedString = getMsgRbString(key); if (!localizedString.equals(key)) { localizedString=MessageFormat.format(localizedString,replacementValues); } return localizedString; } /** * Fetches a string from a ui text resource bundle. Search the resource bundle stack from the * top to the bottom until the message is found. * Note: If the message is not located, then the key value is returned. * * @param key to use to get text from the resource bundle * @return the requested message or the key if the text is missing */ public static String getUiRbString(String key) { String localizedString=null; for (Iterator i=getInstance().uiTextRbList.iterator(); (localizedString==null) && i.hasNext();) { ResourceBundle rb = (ResourceBundle)i.next(); try { localizedString = rb.getString(key); } catch (MissingResourceException exc) { } } if (localizedString==null) { localizedString=key; //SystemLog.log("MissingBundleKey", new Serializable[] { DFT_UI_TEXT_BUNDLE_NAME, key }, new Serializable[] { exc }); } return localizedString; } /** * Fetches a string from a ui text resource bundle. Search the resource bundle stack from the * top to the bottom until the message is found. The messeage is then formatted with the supplied * parameters. * Note: If the message is not located, then the key value is returned. * * @param key to use to get text from the resource bundle * @param replacementValues is an array of replacement values to insert into the message. * @return the requested message or the key is the text is missing */ public static String getUiRbString(String key, Object[] replacementValues) { String localizedString; localizedString = getUiRbString(key); if (!localizedString.equals(key)) { localizedString=MessageFormat.format(localizedString,replacementValues); } return localizedString; } /** * Fetches a image from a ui image resource bundle. Search the resource bundle stack from the * top to the bottom until the image is found. * Note: If the image is not located, then the value null is returned. * * @param key to use to get text from the resource bundle * @return the requested image or null if the image is not found */ public static ImageIcon getImageRbIcon(String key) { ImageIcon icon=null; // first check the image cache icon = (ImageIcon) imageMap.get(key); if (icon==null) { // image wasn't found in the cache, find it. String imageName=null; // Search the resource bundles for the image name that is associated with this key for (Iterator i=getInstance().uiImageRbList.iterator(); (imageName==null) && i.hasNext();) { ResourceBundle rb = (ResourceBundle)i.next(); try { imageName = rb.getString(key); } catch (MissingResourceException exc) { } } // If no name is found, use the key itself as the name if (imageName==null) { imageName = key; } //System.out.println("Locating image: "+imageName); // if no path information was provided, then add the default path String fullPathName = imageName; if (fullPathName.indexOf(File.separatorChar)==-1) { fullPathName = imagesDirectory+fullPathName; } // try locating the image through the class loader try { //System.out.println(" checking with class loader for image: "+fullPathName); URL iconUrl = ConfigResource.class.getResource(fullPathName); if (iconUrl!=null) { icon = new ImageIcon(iconUrl); //System.out.println(" found image through classloader: "+iconUrl); } } catch (MissingResourceException exc) { } // if not found, try locating the image from the web fs // finish this... re-enable after debugging web fs hang // if ((icon==null) && (PathLocater.getDocumentBase()!=null)) { // try { // System.out.println(" checking with documentbase for image: "+imageName); // URL iconUrl = new URL(PathLocater.getDocumentBase(), imageName); // if (iconUrl!=null) { // icon = new ImageIcon(iconUrl); // System.out.println(" found image through web fs: "+iconUrl); // } // } catch (Exception exc) { // } // } // If we found the image, store it in the cache. if (icon!=null) { imageMap.put(key, icon); } } if (icon==null) { //System.out.println(" image not found."); //SystemLog.log("MissingBundleKey", new Serializable[] { DFT_UI_IMAGE_BUNDLE_NAME, key }, new Serializable[] { exc }); } return icon; } /** * Fetches a string from a help text resource bundle. Search the resource bundle stack from the * top to the bottom until the message is found. * Note: If the message is not located, then the key value is returned. * * @param key to use to get text from the resource bundle * @return the requested message or the key if the text is missing */ public static String getHelpRbString(String key) { String localizedString=null; for (Iterator i=getInstance().helpTextRbList.iterator(); (localizedString==null) && i.hasNext();) { ResourceBundle rb = (ResourceBundle)i.next(); try { localizedString = rb.getString(key); } catch (MissingResourceException exc) { } } if (localizedString==null) { localizedString=key; //SystemLog.log("MissingBundleKey", new Serializable[] { DFT_HELP_TEXT_BUNDLE_NAME, key }, new Serializable[] { exc }); } return localizedString; } }