p2izing the editor
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor.designproposal2 / src / com / swtdesigner / ResourceManager.java
1 package com.swtdesigner;
2 import java.io.File;
3 import java.io.InputStream;
4 import java.lang.reflect.Constructor;
5 import java.lang.reflect.Method;
6 import java.net.MalformedURLException;
7 import java.net.URL;
8 import java.util.HashMap;
9 import java.util.Iterator;
10
11 import org.eclipse.jface.resource.CompositeImageDescriptor;
12 import org.eclipse.jface.resource.ImageDescriptor;
13 import org.eclipse.swt.graphics.*;
14
15 /**
16 * Utility class for managing OS resources associated with SWT/JFace controls such as
17 * colors, fonts, images, etc.
18 *
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)
23 *
24 * This class may be freely distributed as part of any application or plugin.
25 * <p>
26 * Copyright (c) 2003 - 2005, Instantiations, Inc. <br>All Rights Reserved
27 *
28 * @author scheglov_ke
29 * @author Dan Rubel
30 */
31 public class ResourceManager extends SWTResourceManager {
32
33 /**
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)
37 */
38 public static void dispose() {
39 disposeColors();
40 disposeFonts();
41 disposeImages();
42 disposeCursors();
43 }
44
45 //////////////////////////////
46 // Image support
47 //////////////////////////////
48
49 /**
50 * Maps image descriptors to images
51 */
52 private static HashMap<ImageDescriptor, Image> m_DescriptorImageMap = new HashMap<ImageDescriptor, Image>();
53
54 /**
55 * Maps images to image decorators
56 */
57 private static HashMap<Image, HashMap<Image, Image>> m_ImageToDecoratorMap = new HashMap<Image, HashMap<Image, Image>>();
58
59 /**
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
64 */
65 public static ImageDescriptor getImageDescriptor(Class<?> clazz, String path) {
66 return ImageDescriptor.createFromFile(clazz, path);
67 }
68
69 /**
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
73 */
74 public static ImageDescriptor getImageDescriptor(String path) {
75 try {
76 return ImageDescriptor.createFromURL((new File(path)).toURI().toURL());
77 } catch (MalformedURLException e) {
78 return null;
79 }
80 }
81
82 /**
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
86 */
87 public static Image getImage(ImageDescriptor descriptor) {
88 if (descriptor == null)
89 return null;
90 Image image = m_DescriptorImageMap.get(descriptor);
91 if (image == null) {
92 image = descriptor.createImage();
93 m_DescriptorImageMap.put(descriptor, image);
94 }
95 return image;
96 }
97
98 /**
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
104 */
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);
110 }
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);
127 }
128 }
129 protected Point getSize() {
130 return baseImageSize;
131 }
132 };
133 result = compositImageDesc.createImage();
134 decoratedMap.put(decorator, result);
135 }
136 return result;
137 }
138
139 /**
140 * Dispose all of the cached images
141 */
142 public static void disposeImages() {
143 SWTResourceManager.disposeImages();
144 //
145 for (Iterator<Image> I = m_DescriptorImageMap.values().iterator(); I.hasNext();)
146 I.next().dispose();
147 m_DescriptorImageMap.clear();
148 }
149
150 //////////////////////////////
151 // Plugin images support
152 //////////////////////////////
153
154 /**
155 * Maps URL to images
156 */
157 private static HashMap<URL, Image> m_URLImageMap = new HashMap<URL, Image>();
158
159 /**
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
164 */
165 public static Image getPluginImage(Object plugin, String name) {
166 try {
167 try {
168 URL url = getPluginImageURL(plugin, name);
169 if (m_URLImageMap.containsKey(url))
170 return m_URLImageMap.get(url);
171 InputStream is = url.openStream();
172 Image image;
173 try {
174 image = getImage(is);
175 m_URLImageMap.put(url, image);
176 } finally {
177 is.close();
178 }
179 return image;
180 } catch (Throwable e) {
181 // Ignore any exceptions
182 }
183 } catch (Throwable e) {
184 // Ignore any exceptions
185 }
186 return null;
187 }
188
189 /**
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
194 */
195 public static ImageDescriptor getPluginImageDescriptor(Object plugin, String name) {
196 try {
197 try {
198 URL url = getPluginImageURL(plugin, name);
199 return ImageDescriptor.createFromURL(url);
200 } catch (Throwable e) {
201 // Ignore any exceptions
202 }
203 } catch (Throwable e) {
204 // Ignore any exceptions
205 }
206 return null;
207 }
208
209 /**
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
214 * @throws Exception
215 */
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
219 try {
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[]{});
225 //
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});
230 //
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});
234 }
235 } catch (Throwable e) {
236 // Ignore any exceptions
237 }
238 // else work with 'plugin' as with usual Eclipse plugin
239 {
240 Class pluginClass = Class.forName("org.eclipse.core.runtime.Plugin"); //$NON-NLS-1$
241 if (pluginClass.isAssignableFrom(plugin.getClass())) {
242 //
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});
247 //
248 Method findMethod = pluginClass.getMethod("find", new Class[]{ipathClass}); //$NON-NLS-1$
249 return (URL) findMethod.invoke(plugin, new Object[]{path});
250 }
251 }
252 return null;
253 }
254 }