1 package com
.swtdesigner
;
3 import java
.io
.InputStream
;
4 import java
.lang
.reflect
.Constructor
;
5 import java
.lang
.reflect
.Method
;
6 import java
.net
.MalformedURLException
;
8 import java
.util
.HashMap
;
9 import java
.util
.Iterator
;
11 import org
.eclipse
.jface
.resource
.CompositeImageDescriptor
;
12 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
13 import org
.eclipse
.swt
.graphics
.*;
16 * Utility class for managing OS resources associated with SWT/JFace controls such as
17 * colors, fonts, images, etc.
19 * !!! IMPORTANT !!! Application code must explicitly invoke the <code>dispose()</code>
20 * method to release the operating system resources managed by cached objects
21 * when those objects and OS resources are no longer needed (e.g. on
22 * application shutdown)
24 * This class may be freely distributed as part of any application or plugin.
26 * Copyright (c) 2003 - 2005, Instantiations, Inc. <br>All Rights Reserved
31 public class ResourceManager
extends SWTResourceManager
{
34 * Dispose of cached objects and their underlying OS resources. This should
35 * only be called when the cached objects are no longer needed (e.g. on
36 * application shutdown)
38 public static void dispose() {
45 //////////////////////////////
47 //////////////////////////////
50 * Maps image descriptors to images
52 private static HashMap
<ImageDescriptor
, Image
> m_DescriptorImageMap
= new HashMap
<ImageDescriptor
, Image
>();
55 * Maps images to image decorators
57 private static HashMap
<Image
, HashMap
<Image
, Image
>> m_ImageToDecoratorMap
= new HashMap
<Image
, HashMap
<Image
, Image
>>();
60 * Returns an image descriptor stored in the file at the specified path relative to the specified class
61 * @param clazz Class The class relative to which to find the image descriptor
62 * @param path String The path to the image file
63 * @return ImageDescriptor The image descriptor stored in the file at the specified path
65 public static ImageDescriptor
getImageDescriptor(Class
<?
> clazz
, String path
) {
66 return ImageDescriptor
.createFromFile(clazz
, path
);
70 * Returns an image descriptor stored in the file at the specified path
71 * @param path String The path to the image file
72 * @return ImageDescriptor The image descriptor stored in the file at the specified path
74 public static ImageDescriptor
getImageDescriptor(String path
) {
76 return ImageDescriptor
.createFromURL((new File(path
)).toURI().toURL());
77 } catch (MalformedURLException e
) {
83 * Returns an image based on the specified image descriptor
84 * @param descriptor ImageDescriptor The image descriptor for the image
85 * @return Image The image based on the specified image descriptor
87 public static Image
getImage(ImageDescriptor descriptor
) {
88 if (descriptor
== null)
90 Image image
= m_DescriptorImageMap
.get(descriptor
);
92 image
= descriptor
.createImage();
93 m_DescriptorImageMap
.put(descriptor
, image
);
99 * Returns an image composed of a base image decorated by another image
100 * @param baseImage Image The base image that should be decorated
101 * @param decorator Image The image to decorate the base image
102 * @param corner The corner to place decorator image
103 * @return Image The resulting decorated image
105 public static Image
decorateImage(final Image baseImage
, final Image decorator
, final int corner
) {
106 HashMap
<Image
, Image
> decoratedMap
= m_ImageToDecoratorMap
.get(baseImage
);
107 if (decoratedMap
== null) {
108 decoratedMap
= new HashMap
<Image
, Image
>();
109 m_ImageToDecoratorMap
.put(baseImage
, decoratedMap
);
111 Image result
= decoratedMap
.get(decorator
);
112 if (result
== null) {
113 final Rectangle bid
= baseImage
.getBounds();
114 final Rectangle did
= decorator
.getBounds();
115 final Point baseImageSize
= new Point(bid
.width
, bid
.height
);
116 CompositeImageDescriptor compositImageDesc
= new CompositeImageDescriptor() {
117 protected void drawCompositeImage(int width
, int height
) {
118 drawImage(baseImage
.getImageData(), 0, 0);
119 if (corner
== TOP_LEFT
) {
120 drawImage(decorator
.getImageData(), 0, 0);
121 } else if (corner
== TOP_RIGHT
) {
122 drawImage(decorator
.getImageData(), bid
.width
- did
.width
- 1, 0);
123 } else if (corner
== BOTTOM_LEFT
) {
124 drawImage(decorator
.getImageData(), 0, bid
.height
- did
.height
- 1);
125 } else if (corner
== BOTTOM_RIGHT
) {
126 drawImage(decorator
.getImageData(), bid
.width
- did
.width
- 1, bid
.height
- did
.height
- 1);
129 protected Point
getSize() {
130 return baseImageSize
;
133 result
= compositImageDesc
.createImage();
134 decoratedMap
.put(decorator
, result
);
140 * Dispose all of the cached images
142 public static void disposeImages() {
143 SWTResourceManager
.disposeImages();
145 for (Iterator
<Image
> I
= m_DescriptorImageMap
.values().iterator(); I
.hasNext();)
147 m_DescriptorImageMap
.clear();
150 //////////////////////////////
151 // Plugin images support
152 //////////////////////////////
157 private static HashMap
<URL
, Image
> m_URLImageMap
= new HashMap
<URL
, Image
>();
160 * Retuns an image based on a plugin and file path
161 * @param plugin Object The plugin containing the image
162 * @param name String The path to th eimage within the plugin
163 * @return Image The image stored in the file at the specified path
165 public static Image
getPluginImage(Object plugin
, String name
) {
168 URL url
= getPluginImageURL(plugin
, name
);
169 if (m_URLImageMap
.containsKey(url
))
170 return m_URLImageMap
.get(url
);
171 InputStream is
= url
.openStream();
174 image
= getImage(is
);
175 m_URLImageMap
.put(url
, image
);
180 } catch (Throwable e
) {
181 // Ignore any exceptions
183 } catch (Throwable e
) {
184 // Ignore any exceptions
190 * Retuns an image descriptor based on a plugin and file path
191 * @param plugin Object The plugin containing the image
192 * @param name String The path to th eimage within the plugin
193 * @return ImageDescriptor The image descriptor stored in the file at the specified path
195 public static ImageDescriptor
getPluginImageDescriptor(Object plugin
, String name
) {
198 URL url
= getPluginImageURL(plugin
, name
);
199 return ImageDescriptor
.createFromURL(url
);
200 } catch (Throwable e
) {
201 // Ignore any exceptions
203 } catch (Throwable e
) {
204 // Ignore any exceptions
210 * Retuns an URL based on a plugin and file path
211 * @param plugin Object The plugin containing the file path
212 * @param name String The file path
213 * @return URL The URL representing the file at the specified path
216 @SuppressWarnings("unchecked") //$NON-NLS-1$
217 private static URL
getPluginImageURL(Object plugin
, String name
) throws Exception
{
218 // try to work with 'plugin' as with OSGI BundleContext
220 Class bundleClass
= Class
.forName("org.osgi.framework.Bundle"); //$NON-NLS-1$
221 Class bundleContextClass
= Class
.forName("org.osgi.framework.BundleContext"); //$NON-NLS-1$
222 if (bundleContextClass
.isAssignableFrom(plugin
.getClass())) {
223 Method getBundleMethod
= bundleContextClass
.getMethod("getBundle", new Class
[]{}); //$NON-NLS-1$
224 Object bundle
= getBundleMethod
.invoke(plugin
, new Object
[]{});
226 Class ipathClass
= Class
.forName("org.eclipse.core.runtime.IPath"); //$NON-NLS-1$
227 Class pathClass
= Class
.forName("org.eclipse.core.runtime.Path"); //$NON-NLS-1$
228 Constructor pathConstructor
= pathClass
.getConstructor(new Class
[]{String
.class});
229 Object path
= pathConstructor
.newInstance(new Object
[]{name
});
231 Class platformClass
= Class
.forName("org.eclipse.core.runtime.Platform"); //$NON-NLS-1$
232 Method findMethod
= platformClass
.getMethod("find", new Class
[]{bundleClass
, ipathClass
}); //$NON-NLS-1$
233 return (URL
) findMethod
.invoke(null, new Object
[]{bundle
, path
});
235 } catch (Throwable e
) {
236 // Ignore any exceptions
238 // else work with 'plugin' as with usual Eclipse plugin
240 Class pluginClass
= Class
.forName("org.eclipse.core.runtime.Plugin"); //$NON-NLS-1$
241 if (pluginClass
.isAssignableFrom(plugin
.getClass())) {
243 Class ipathClass
= Class
.forName("org.eclipse.core.runtime.IPath"); //$NON-NLS-1$
244 Class pathClass
= Class
.forName("org.eclipse.core.runtime.Path"); //$NON-NLS-1$
245 Constructor pathConstructor
= pathClass
.getConstructor(new Class
[]{String
.class});
246 Object path
= pathConstructor
.newInstance(new Object
[]{name
});
248 Method findMethod
= pluginClass
.getMethod("find", new Class
[]{ipathClass
}); //$NON-NLS-1$
249 return (URL
) findMethod
.invoke(plugin
, new Object
[]{path
});