Source code: javax/ide/util/IconDescription.java
1 package javax.ide.util;
2
3 import java.net.URL;
4
5 import java.util.Locale;
6 import java.util.MissingResourceException;
7 import java.util.ResourceBundle;
8
9 /**
10 * The description of an icon. An icon is specified either as a key lookup
11 * in a resource file which resolves to a resource path relative to the resource
12 * file, or an absolute resource path.<p>
13 *
14 * Use the {@link #createPathInstance( ClassLoader, String )} or
15 * {@link #createResourceInstance( ClassLoader, String, String)} factory
16 * methods to create an instance of this class. To retrieve the URL of the
17 * icon, use {@link #getURL()}. This URL is normally a resource (i.e. classpath)
18 * URL, suitable for use when constructing an <tt>ImageIcon</tt> or other
19 * toolkit specific image type.
20 */
21 public abstract class IconDescription
22 {
23 private URL _url;
24
25 private IconDescription()
26 {
27
28 }
29
30 /**
31 * Create an instance that wraps the specified icon URL.
32 *
33 * @param url the url of an icon.
34 * @return an IconDescription instance.
35 */
36 public static IconDescription createInstance( final URL url )
37 {
38 return new IconDescription()
39 {
40 protected URL resolveURL() throws MissingIconException
41 {
42 return url;
43 }
44 };
45 }
46
47 /**
48 * Create an instance of <tt>IconDescription</tt> based on a resource key
49 * lookup.
50 *
51 * @param loader the class loader to load the icon resources from.
52 * @param bundleClass the class name of the resource bundle.
53 * @param key the key of the image path.
54 *
55 * @return a new <tt>IconDescription</tt> instance.
56 */
57 public static IconDescription createResourceInstance( final ClassLoader loader,
58 final String bundleClass, final String key )
59 {
60 return new IconDescription()
61 {
62 protected URL resolveURL()
63 throws MissingIconException
64 {
65
66 ResourceBundle bundle;
67 try
68 {
69 bundle = ResourceBundle.getBundle( bundleClass,
70 Locale.getDefault(), loader );
71 }
72 catch ( MissingResourceException mre )
73 {
74 throw new MissingIconException(
75 "Resource bundle " + bundleClass + " not found.",
76 mre
77 );
78 }
79
80 try
81 {
82 String path = bundle.getString( key );
83 if ( path == null || path.trim().length() == 0 )
84 {
85 throw new MissingIconException(
86 "Icon key "+key+" has empty value in bundle "+bundleClass
87 );
88 }
89 Class bundleClass = bundle.getClass();
90 URL result = bundleClass.getResource( path );
91 if ( result == null )
92 {
93 throw new MissingIconException(
94 "Icon path "+path+" does not exist. Specified by key "+key+
95 " in bundle "+bundleClass
96 );
97 }
98 return result;
99 }
100 catch ( MissingResourceException mre )
101 {
102 throw new MissingIconException(
103 "Icon key "+key+" not defined in bundle "+ bundleClass,
104 mre
105 );
106 }
107 }
108 };
109 }
110
111 /**
112 * Create an instance of <tt>IconDescription</tt> based on a direct icon
113 * path in the manifest file.
114 *
115 * @param loader the class loader to load the icon resource from.
116 * @param path the path of the icon.
117 * @return a new <tt>IconDescription</tt> instance.
118 */
119 public static IconDescription createPathInstance( final ClassLoader loader,
120 final String path )
121 {
122 return new IconDescription()
123 {
124 protected URL resolveURL()
125 throws MissingIconException
126 {
127 URL resource = loader.getResource( path );
128 if ( resource == null )
129 {
130 throw new MissingIconException(
131 "Icon path " + path + " not found."
132 );
133 }
134 return resource;
135 }
136 };
137 }
138
139 /**
140 * Resolve the URL for this instance of IconDescription.
141 *
142 * @return the resolved URL.
143 */
144 protected abstract URL resolveURL() throws MissingIconException;
145
146 /**
147 * Get the resolved URL of the icon.
148 *
149 * @return the URL of the icon.
150 * @throws MissingIconException if the icon could not be loaded.
151 */
152 public URL getURL()
153 throws MissingIconException
154 {
155 if ( _url == null )
156 {
157 _url = resolveURL();
158 }
159 return _url;
160 }
161 }