Back to index

moin  1.9.0~rc2
Iconkit.java
Go to the documentation of this file.
00001 /*
00002  * @(#)Iconkit.java 5.1
00003  *
00004  */
00005 
00006 package CH.ifa.draw.util;
00007 
00008 import java.awt.*;
00009 import java.awt.image.ImageProducer;
00010 import java.util.*;
00011 import java.io.*;
00012 
00027 public class Iconkit {
00028     private Hashtable           fMap;
00029     private Vector              fRegisteredImages;
00030     private Component           fComponent;
00031     private final static int    ID = 123;
00032     private static Iconkit      fgIconkit = null;
00033     private static boolean      fgDebug = false;
00034 
00039     public Iconkit(Component component) {
00040         fMap = new Hashtable(53);
00041         fRegisteredImages = new Vector(10);
00042         fComponent = component;
00043         fgIconkit = this;
00044     }
00045 
00049     public static Iconkit instance() {
00050         return fgIconkit;
00051     }
00052 
00057     public void loadRegisteredImages(Component component) {
00058         if (fRegisteredImages.size() == 0)
00059             return;
00060 
00061         MediaTracker tracker = new MediaTracker(component);
00062 
00063         // register images with MediaTracker
00064         Enumeration k = fRegisteredImages.elements();
00065         while (k.hasMoreElements()) {
00066             String fileName = (String) k.nextElement();
00067             if (basicGetImage(fileName) == null) {
00068                 tracker.addImage(loadImage(fileName), ID);
00069             }
00070         }
00071         fRegisteredImages.removeAllElements();
00072 
00073         // block until all images are loaded
00074         try {
00075             tracker.waitForAll();
00076         } catch (Exception e) {  }
00077     }
00078 
00084     public void registerImage(String fileName) {
00085         fRegisteredImages.addElement(fileName);
00086     }
00087 
00091     public Image registerAndLoadImage(Component component, String fileName) {
00092         registerImage(fileName);
00093         loadRegisteredImages(component);
00094         return getImage(fileName);
00095     }
00096 
00100     public Image loadImage(String filename) {
00101         if (fMap.containsKey(filename))
00102             return (Image) fMap.get(filename);
00103         Image image = loadImageResource(filename);
00104         if (image != null)
00105             fMap.put(filename, image);
00106         return image;
00107     }
00108 
00109     public Image loadImageResource(String resourcename) {
00110         Toolkit toolkit = Toolkit.getDefaultToolkit();
00111         try {
00112             if (fgDebug)
00113                 System.out.println(resourcename);
00114            byte[] image =
00115               CH.ifa.draw.images.StaticImages.getImage(resourcename);
00116            return toolkit.createImage(image);
00117         } catch (Exception ex) {
00118             return null;
00119         }
00120     }
00121 
00127     public Image getImage(String filename) {
00128         Image image = basicGetImage(filename);
00129         if (image != null)
00130             return image;
00131         // load registered images and try again
00132         loadRegisteredImages(fComponent);
00133         // try again
00134         return basicGetImage(filename);
00135     }
00136 
00137     private Image basicGetImage(String filename) {
00138         if (fMap.containsKey(filename))
00139             return (Image) fMap.get(filename);
00140         return null;
00141     }
00142 }