View Javadoc

1   /*
2    * $Id: ImageFactory.java,v 1.1 2005/04/03 12:51:31 mch Exp $
3    *
4    * Copyright 2003 AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid Software License,
7    * a copy of which has been included with this distribution in the LICENSE.txt file.
8    *
9    */
10  
11  package org.astrogrid.ui;
12  
13  import java.awt.Image;
14  import java.awt.Toolkit;
15  import java.io.IOException;
16  import java.io.InputStream;
17  import java.net.URL;
18  import java.util.Hashtable;
19  import javax.imageio.ImageIO;
20  import org.apache.commons.logging.LogFactory;
21  
22  /***
23   * a cached factory of images stored off this package, for things such as
24   * button icons etc
25   *
26   * @author Martin Hill
27   *
28   */
29  
30  
31  public class ImageFactory
32  {
33     //cached images
34     private static final Hashtable imageCache = new Hashtable();
35  
36     /*** Get an image suitable for message boxes, etc.  Does a typecast of getIcon()
37      * for file, then for icons used by option pane in UI Manager
38      */
39     public static Image getImage(String imageName)
40     {
41        //if it's in the hashtable, return that.
42        Image image = (Image) imageCache.get(imageName);
43        if (image != null) {
44           return image;
45        }
46  
47        image = loadImage(imageName);
48        
49        if (image == null) {
50           image = loadImage(imageName+".gif");
51        }
52        
53        return image;
54     }
55  
56     /***
57      * Loads Image from file in the images subdirectory of this class
58      */
59     public static Image loadImage(String filename)
60     {
61        //path is subdirectory of this package, called images.  This doesn't work for JAR files
62        String path = "./images/"+filename;
63        URL url = IconFactory.class.getResource(path);
64        LogFactory.getLog(IconFactory.class).debug("Looking for image "+path+" got "+url);
65        if (url != null) {
66           return Toolkit.getDefaultToolkit().getImage(url);
67        }
68  
69  //      String path = "org/astrogrid/ui/images/"+filename+".gif";
70        
71        InputStream in = IconFactory.class.getResourceAsStream(path);
72        LogFactory.getLog(IconFactory.class).debug("getResourceAsStream returns "+in);
73        if (in != null) {
74           try {
75              return ImageIO.read(in);
76           }
77           catch (IOException ioe) {
78              //ignore, carry on looking some other way
79           }
80        }
81        return null;
82     }
83  
84     /***
85      * Convenience routine for Loading Image from file in the images subdirectory of the given class
86      */
87     public static Image loadClassImage(Class givenClass, String filename)
88     {
89        String path = "./images/"+filename;
90  
91        //path is subdirectory of this package, called images
92        URL url = givenClass.getResource(path);
93        if (url != null) {
94           return Toolkit.getDefaultToolkit().getImage(url);
95        }
96  
97        InputStream in = givenClass.getResourceAsStream(path);
98        LogFactory.getLog(IconFactory.class).debug("getResourceAsStream returns "+in);
99        if (in != null) {
100          try {
101             return ImageIO.read(in);
102          }
103          catch (IOException ioe) {
104             //ignore, carry on looking some other way
105          }
106       }
107       return null;
108    }
109 
110 
111 }
112 /*
113 $Log: ImageFactory.java,v $
114 Revision 1.1  2005/04/03 12:51:31  mch
115 split imagefactory from iconfactory
116 
117  */
118