package com.digi.config.util;
import java.io.File;
import java.io.Serializable;
import java.net.URL;
import java.util.*;
import javax.swing.ImageIcon;
/**
* This is a special purpose resource bundle that allows the Config UI classes
* to easily access imageIcons. Use this resource bundle similar to any other
* resource bundle. For example, the following code segment shows how to access
* and use the bundle to get imageIcons:
*
* ImageResourceBundle irb = ResourceBundle.
* getBundle("com.digi.util.ImageResourceBundle");
* ImageIcon icon = irb.getImageIcon("PrintThis");
*
*
* The lookup is actually performed in a two step process. First, the name of
* the image file is looked up in the property file
* "ConfigImgResourceBundle.properties". This file contains a collection of
* imageName->ImageFileName pairs. Once the file name has been located, an
* IconImage is created for that file and is returned to the caller.
*
* Note: Since the image file names are located in a resource file, it is easy
* to provide locale specific icons by providing a property file for each locale
* and changing the file name mappings to the appropriate locale icons.
*
*
*/
public class ImageResourceBundle extends ResourceBundle {
//==========================================================================
//= Object attributes
//==========================================================================
/** The property file containing the mapping from image name to image file */
private String imageResourceBundleName;
/** The resource bundle containing the mapping from image name to image file */
private ResourceBundle imageResourceBundle;
/** The location of the image files */
private static String imagesDirectory="/com/digi/config/images/";
/** Cache the images so don't load multiple times */
private Map imageMap=new HashMap();
/**
* Construct a new ImageResourceBundle.
*
* The constructor ensures that the image resource bundle has been located so that
* subsequent lookups can use it.
*/
public ImageResourceBundle(String imageResourceBundleName) throws MissingResourceException {
try {
if (imageResourceBundle==null) {
imageResourceBundle=ResourceBundle.getBundle(imageResourceBundleName);
this.imageResourceBundleName = imageResourceBundleName;
}
}
catch (MissingResourceException exc) {
// If this happens, it means we cant find the resource bundle and
// therefore we won't find
// any image icons. This is obviously bad so log an error and
// throw the exception.
SystemLog.log("MissingBundle",
new Serializable[] { imageResourceBundleName}, exc);
throw exc;
}
}
/**
* Returns all the available image keys in the image resource bundle.
*
* @return Enumeration of image keys
*/
public Enumeration getKeys() {
return imageResourceBundle.getKeys();
}
/**
* Fetches the proper image icon for the specified image name.
* This method accomplishes its work by first looking up the image file
* name in the image resource bundle and then locates and creates an icon
* for the specified file. If the image is not found, it will log a message
* and return a default image.
*
* @return ImageIcon object associated with the key
*/
public Object handleGetObject(String key) {
// Check for a cached image
Object returnValue=imageMap.get(key);
if (returnValue!=null) {
return returnValue;
}
// Get the image file name
String imageName=null;
try {
imageName=imageResourceBundle.getString(key);
}
catch (MissingResourceException exc) {
imageName=key;
//return null;
// decided that its best to not load a default image when requested image is not found
// SystemLog.log("MissingBundleKey",
// new Serializable[] { imageResourceBundleName, key},
// new Serializable[] { exc});
// // try a default image instead
// try {
// imageName=imageResourceBundle.getString("NotFoundImage");
// }
// catch (MissingResourceException exc2) {
// SystemLog.log("MissingBundleKey",
// new Serializable[] {imageResourceBundleName, "NotFoundImage"},
// exc2 );
// return null;
// }
}
// Create an ImageIcon from the image file
if (imageName!=null) {
//System.out.println("Found image key in bundle... looking for image file "+imageName);
String imageFileName=imagesDirectory+imageName;
URL icon=this.getClass().getResource(imageFileName);
if (icon==null) {
//SystemLog.log("MissingIcon", new Serializable[] { imageName});
return null;
}
returnValue=new ImageIcon(icon);
imageMap.put(key, returnValue);
}
return returnValue;
}
/**
* Fetches the proper image icon for the specified image name.
* This method accomplishes its work by first looking up the image file
* name in the image resource bundle and then locates and creates an icon
* for the specified file. If the image is not found, it will log a message
* and return a default image.
*
* @return ImageIcon object associated with the key
*/
public ImageIcon getImageIcon(String key) {
return(ImageIcon)handleGetObject(key);
}
}