1
2
3
4
5
6
7
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;
36 public static final int MEDIUM = 1;
37 public static final int LARGE = 2;
38
39
40 private static final int[] PIXELS = new int[] { 16, 32, 64 };
41
42
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
72 ImageIcon icon = (ImageIcon) cache[size].get(iconName);
73 if (icon != null)
74 return icon;
75
76
77 int trySize = size+1;
78 while ((trySize<=LARGE) && (icon == null))
79 {
80 icon = (ImageIcon) cache[trySize].get(iconName);
81 trySize++;
82 }
83
84
85 if (icon == null) {
86
87 icon = loadIcon(iconName.trim());
88 }
89
90
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
115
116
117
118
119
120
121
122
123