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();
144 // dispose ImageDescriptor images
146 for (Iterator
<Image
> I
= m_DescriptorImageMap
.values().iterator(); I
.hasNext();)
148 m_DescriptorImageMap
.clear();
150 // dispose plugin images
152 for (Iterator
<Image
> I
= m_URLImageMap
.values().iterator(); I
.hasNext();)
154 m_URLImageMap
.clear();
156 // dispose decorated images
157 for (Iterator
<HashMap
<Image
, Image
>> I
= m_ImageToDecoratorMap
.values().iterator(); I
.hasNext();) {
158 HashMap
<Image
, Image
> decoratedMap
= I
.next();
159 for (Iterator
<Image
> J
= decoratedMap
.values().iterator(); J
.hasNext();) {
160 Image decoratedImage
= J
.next();
161 decoratedImage
.dispose();
166 //////////////////////////////
167 // Plugin images support
168 //////////////////////////////
173 private static HashMap
<URL
, Image
> m_URLImageMap
= new HashMap
<URL
, Image
>();
176 * Retuns an image based on a plugin and file path
177 * @param plugin Object The plugin containing the image
178 * @param name String The path to th eimage within the plugin
179 * @return Image The image stored in the file at the specified path
181 public static Image
getPluginImage(Object plugin
, String name
) {
184 URL url
= getPluginImageURL(plugin
, name
);
185 if (m_URLImageMap
.containsKey(url
))
186 return m_URLImageMap
.get(url
);
187 InputStream is
= url
.openStream();
190 image
= getImage(is
);
191 m_URLImageMap
.put(url
, image
);
196 } catch (Throwable e
) {
197 // Ignore any exceptions
199 } catch (Throwable e
) {
200 // Ignore any exceptions
206 * Retuns an image descriptor based on a plugin and file path
207 * @param plugin Object The plugin containing the image
208 * @param name String The path to th eimage within the plugin
209 * @return ImageDescriptor The image descriptor stored in the file at the specified path
211 public static ImageDescriptor
getPluginImageDescriptor(Object plugin
, String name
) {
214 URL url
= getPluginImageURL(plugin
, name
);
215 return ImageDescriptor
.createFromURL(url
);
216 } catch (Throwable e
) {
217 // Ignore any exceptions
219 } catch (Throwable e
) {
220 // Ignore any exceptions
226 * Retuns an URL based on a plugin and file path
227 * @param plugin Object The plugin containing the file path
228 * @param name String The file path
229 * @return URL The URL representing the file at the specified path
232 private static URL
getPluginImageURL(Object plugin
, String name
) throws Exception
{
233 // try to work with 'plugin' as with OSGI BundleContext
235 Class
<?
> bundleClass
= Class
.forName("org.osgi.framework.Bundle"); //$NON-NLS-1$
236 Class
<?
> bundleContextClass
= Class
.forName("org.osgi.framework.BundleContext"); //$NON-NLS-1$
237 if (bundleContextClass
.isAssignableFrom(plugin
.getClass())) {
238 Method getBundleMethod
= bundleContextClass
.getMethod("getBundle", new Class
[0]); //$NON-NLS-1$
239 Object bundle
= getBundleMethod
.invoke(plugin
, new Object
[0]);
241 Class
<?
> ipathClass
= Class
.forName("org.eclipse.core.runtime.IPath"); //$NON-NLS-1$
242 Class
<?
> pathClass
= Class
.forName("org.eclipse.core.runtime.Path"); //$NON-NLS-1$
243 Constructor
<?
> pathConstructor
= pathClass
.getConstructor(new Class
[]{String
.class});
244 Object path
= pathConstructor
.newInstance(new Object
[]{name
});
246 Class
<?
> platformClass
= Class
.forName("org.eclipse.core.runtime.Platform"); //$NON-NLS-1$
247 Method findMethod
= platformClass
.getMethod("find", new Class
[]{bundleClass
, ipathClass
}); //$NON-NLS-1$
248 return (URL
) findMethod
.invoke(null, new Object
[]{bundle
, path
});
250 } catch (Throwable e
) {
251 // Ignore any exceptions
253 // else work with 'plugin' as with usual Eclipse plugin
255 Class
<?
> pluginClass
= Class
.forName("org.eclipse.core.runtime.Plugin"); //$NON-NLS-1$
256 if (pluginClass
.isAssignableFrom(plugin
.getClass())) {
258 Class
<?
> ipathClass
= Class
.forName("org.eclipse.core.runtime.IPath"); //$NON-NLS-1$
259 Class
<?
> pathClass
= Class
.forName("org.eclipse.core.runtime.Path"); //$NON-NLS-1$
260 Constructor
<?
> pathConstructor
= pathClass
.getConstructor(new Class
[]{String
.class});
261 Object path
= pathConstructor
.newInstance(new Object
[]{name
});
263 Method findMethod
= pluginClass
.getMethod("find", new Class
[]{ipathClass
}); //$NON-NLS-1$
264 return (URL
) findMethod
.invoke(plugin
, new Object
[]{path
});