View Javadoc

1   /*
2    * $Id: IconFactory.java,v 1.5 2005/04/03 22:26:25 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 javax.swing.Icon;
21  import javax.swing.ImageIcon;
22  import org.apache.commons.logging.LogFactory;
23  
24  /***
25   * a cached factory of images stored off this package, for things such as
26   * button icons etc
27   *
28   * @author Martin Hill
29   *
30   */
31  
32  
33  public class IconFactory extends ImageFactory
34  {
35     public static final int SMALL = 0;   //for lines, status bars, etc
36     public static final int MEDIUM = 1; //size suitable for toolbar buttons
37     public static final int LARGE = 2;   //for dialog boxes, etc
38  
39     //pixel sizes for above
40     private static final int[] PIXELS = new int[] { 16, 32, 64 };
41  
42     //cached icons - lookups for above
43     private static final Hashtable[] cache = new Hashtable[] { new Hashtable(), new  Hashtable(), new Hashtable() };
44  
45     /*** Get an icon suitable for message boxes, etc.  Looks first
46      * for file, then for icons used by option pane in UI Manager
47      */
48     public static Icon getIcon(String iconName)
49     {
50        return getIcon(iconName, LARGE);
51     }
52  
53     /*** Get an icon suitable for display on lines, status bars, etc.
54      * Looks first in hashtable, then for image file, then makes one
55      * out of larger icon if necessary
56      */
57     public static Icon getSmallIcon(String iconName)
58     {
59        return getIcon(iconName, SMALL);
60     }
61  
62     /*** Get an icon with the size given
63      * Looks first in hashtable, then for image file, then makes one
64      * out of larger icon if necessary
65      * @todo - this only deals with ImageIcons but it ought to not fail if there are other types
66      */
67     public static Icon getIcon(String iconName, int size)
68     {
69        assert (size >= SMALL) && (size <= LARGE) : "Size must be SMALL, MEDIUM or LARGE";
70        
71        //if it's in the hashtable, return that.
72        ImageIcon icon = (ImageIcon) cache[size].get(iconName);
73        if (icon != null)
74           return icon;
75  
76        //if it's in the cache at this or a larger size, get that
77        int trySize = size+1;
78        while ((trySize<=LARGE) && (icon == null))
79        {
80           icon = (ImageIcon) cache[trySize].get(iconName);
81           trySize++;
82        }
83  
84        //if its not there, look for a file
85        if (icon == null) {
86           //look for file
87           icon = loadIcon(iconName.trim());
88        }
89  
90        //make sure correct size - shrink if not
91        if (icon != null) {
92           if (icon.getIconWidth() > PIXELS[size]) {
93              icon = new ImageIcon(icon.getImage().getScaledInstance(PIXELS[size], PIXELS[size],Image.SCALE_REPLICATE));
94           }
95           cache[size].put(iconName, icon);
96        }
97  
98        return icon;
99     }
100 
101    /***
102     * Loads Icon from file in the images subdirectory of this class
103     */
104    private static ImageIcon loadIcon(String filename)
105    {
106       Image image = loadImage(filename+".gif");
107       if (image != null) {
108          return new ImageIcon(image);
109       }
110       return null;
111    }
112 }
113 /*
114 $Log: IconFactory.java,v $
115 Revision 1.5  2005/04/03 22:26:25  mch
116 Added images
117 
118 Revision 1.4  2005/04/03 12:51:31  mch
119 split imagefactory from iconfactory
120 
121 
122  */
123