KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > util > gui > IconManager


1 package net.suberic.util.gui;
2 import java.awt.Component JavaDoc;
3 import javax.swing.*;
4 import java.util.*;
5
6 import net.suberic.util.VariableBundle;
7
8 /**
9  * This manages a set of icons.
10  *
11  */

12 public class IconManager {
13
14   // the source VariableBundle
15
VariableBundle mResources = null;
16
17   // the source property.
18
String JavaDoc mProperty = null;
19
20   // the default location for icons
21
String JavaDoc mIconDirectory = null;
22
23   // the default extension for icons
24
String JavaDoc mIconExtension = null;;
25
26   // the icon manager map
27
static HashMap sManagers = new HashMap();
28
29   // the image map
30
static HashMap sImageMap = new HashMap();
31
32   /**
33    * Creates a new IconManager from the given property.
34    *
35    * @param pResources the VariableBundle used to access the icons
36    * @param pResourceBase a property in the given VariableBundle that will resolve to provide the correct property base
37    */

38   protected IconManager(VariableBundle pResources, String JavaDoc pResourceBase) {
39     mResources = pResources;
40     mProperty = pResources.getProperty(pResourceBase, "");
41     mIconDirectory = mResources.getProperty(mProperty + ".defaultDirectory", "images");
42     mIconExtension = mResources.getProperty(mProperty + ".defaultExtension", "images");
43   }
44
45   /**
46    * Gets the IconManager for the given resources and resource base. Will
47    * return a cached copy if available.
48    */

49   public static IconManager getIconManager(VariableBundle pResources, String JavaDoc pResourceBase) {
50     IconManager returnValue = (IconManager) sManagers.get(pResourceBase + pResources.getProperty(pResourceBase, "") + System.identityHashCode(pResources));
51     if (returnValue == null) {
52       synchronized(sManagers) {
53         returnValue = (IconManager) sManagers.get(pResourceBase + pResources.getProperty(pResourceBase, "") + System.identityHashCode(pResources));
54         if (returnValue == null) {
55           returnValue = new IconManager(pResources, pResourceBase);
56           sManagers.put(pResourceBase + pResources.getProperty(pResourceBase, "") + System.identityHashCode(pResources), returnValue);
57         }
58       }
59     }
60
61     return returnValue;
62   }
63
64   /**
65    * Gets the ImageIcon specified by the resource given.
66    */

67   public ImageIcon getIcon(String JavaDoc pIconString) {
68     java.net.URL JavaDoc imageURL = this.getClass().getResource(mResources.getProperty(mProperty + ".icon." + pIconString, mIconDirectory + "/" + pIconString + "." + mIconExtension));
69     if (imageURL != null) {
70       ImageIcon returnValue = (ImageIcon) sImageMap.get(imageURL);
71       if (returnValue == null) {
72         synchronized(sImageMap) {
73           returnValue = (ImageIcon) sImageMap.get(imageURL);
74           if (returnValue == null) {
75             returnValue = new ImageIcon(imageURL);
76             sImageMap.put(imageURL, returnValue);
77           }
78         }
79       }
80       return returnValue;
81     } else {
82       System.err.println("got null for iconString " + pIconString + ", file " + mResources.getProperty(mProperty + ".icon." + pIconString, mIconDirectory + "/" + pIconString + "." + mIconExtension));
83       return null;
84     }
85   }
86
87
88 }
89
90
Popular Tags