001: package net.suberic.util.gui;
002:
003: import java.awt.Component;
004: import javax.swing.*;
005: import java.util.*;
006:
007: import net.suberic.util.VariableBundle;
008:
009: /**
010: * This manages a set of icons.
011: *
012: */
013: public class IconManager {
014:
015: // the source VariableBundle
016: VariableBundle mResources = null;
017:
018: // the source property.
019: String mProperty = null;
020:
021: // the default location for icons
022: String mIconDirectory = null;
023:
024: // the default extension for icons
025: String mIconExtension = null;;
026:
027: // the icon manager map
028: static HashMap sManagers = new HashMap();
029:
030: // the image map
031: static HashMap sImageMap = new HashMap();
032:
033: /**
034: * Creates a new IconManager from the given property.
035: *
036: * @param pResources the VariableBundle used to access the icons
037: * @param pResourceBase a property in the given VariableBundle that will resolve to provide the correct property base
038: */
039: protected IconManager(VariableBundle pResources,
040: String pResourceBase) {
041: mResources = pResources;
042: mProperty = pResources.getProperty(pResourceBase, "");
043: mIconDirectory = mResources.getProperty(mProperty
044: + ".defaultDirectory", "images");
045: mIconExtension = mResources.getProperty(mProperty
046: + ".defaultExtension", "images");
047: }
048:
049: /**
050: * Gets the IconManager for the given resources and resource base. Will
051: * return a cached copy if available.
052: */
053: public static IconManager getIconManager(VariableBundle pResources,
054: String pResourceBase) {
055: IconManager returnValue = (IconManager) sManagers
056: .get(pResourceBase
057: + System.identityHashCode(pResources));
058: if (returnValue == null) {
059: synchronized (sManagers) {
060: returnValue = (IconManager) sManagers.get(pResourceBase
061: + System.identityHashCode(pResources));
062: if (returnValue == null) {
063: returnValue = new IconManager(pResources,
064: pResourceBase);
065: sManagers.put(pResourceBase
066: + System.identityHashCode(pResources),
067: returnValue);
068: }
069: }
070: }
071:
072: return returnValue;
073: }
074:
075: /**
076: * Gets the ImageIcon specified by the resource given.
077: */
078: public ImageIcon getIcon(String pIconString) {
079: java.net.URL imageURL = this .getClass().getResource(
080: mResources.getProperty(mProperty + ".icon."
081: + pIconString, mIconDirectory + "/"
082: + pIconString + "." + mIconExtension));
083: if (imageURL != null) {
084: ImageIcon returnValue = (ImageIcon) sImageMap.get(imageURL);
085: if (returnValue == null) {
086: synchronized (sImageMap) {
087: returnValue = (ImageIcon) sImageMap.get(imageURL);
088: if (returnValue == null) {
089: returnValue = new ImageIcon(imageURL);
090: sImageMap.put(imageURL, returnValue);
091: }
092: }
093: }
094: return returnValue;
095: } else {
096: System.err.println("got null for iconString "
097: + pIconString
098: + ", file "
099: + mResources.getProperty(mProperty + ".icon."
100: + pIconString, mIconDirectory + "/"
101: + pIconString + "." + mIconExtension));
102: return null;
103: }
104: }
105:
106: }
|