001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: Message.java 3512 2006-12-08 05:52:03Z yling $
023: */
024: package com.bostechcorp.cbesb.ui.util.resource;
025:
026: import java.io.File;
027: import java.io.InputStream;
028: import java.lang.reflect.Constructor;
029: import java.lang.reflect.Method;
030: import java.net.MalformedURLException;
031: import java.net.URL;
032: import java.util.HashMap;
033: import java.util.Iterator;
034:
035: import org.eclipse.jface.resource.ImageDescriptor;
036: import org.eclipse.swt.graphics.Image;
037:
038: import com.bostechcorp.cbesb.ui.util.resource.SWTResourceManager;
039:
040: /**
041: * Utility class for managing OS resources associated with SWT/JFace controls
042: * such as colors, fonts, images, etc.
043: *
044: * !!! IMPORTANT !!! Application code must explicitly invoke the
045: * <code>dispose()</code> method to release the operating system resources
046: * managed by cached objects when those objects and OS resources are no longer
047: * needed (e.g. on application shutdown)
048: *
049: * This class may be freely distributed as part of any application or plugin.
050: * <p>
051: * Copyright (c) 2003 - 2005, Instantiations, Inc. <br>
052: * All Rights Reserved
053: *
054: * @author scheglov_ke
055: * @author Dan Rubel
056: */
057: public class ESBResourceManager extends SWTResourceManager {
058:
059: /**
060: * Dispose of cached objects and their underlying OS resources. This should
061: * only be called when the cached objects are no longer needed (e.g. on
062: * application shutdown)
063: */
064: public static void dispose() {
065: disposeColors();
066: disposeFonts();
067: disposeImages();
068: disposeCursors();
069: }
070:
071: // ////////////////////////////
072: // Image support
073: // ////////////////////////////
074:
075: /**
076: * Maps image descriptors to images
077: */
078: private static HashMap<ImageDescriptor, Image> m_DescriptorImageMap = new HashMap<ImageDescriptor, Image>();
079:
080: /**
081: * Returns an image descriptor stored in the file at the specified path
082: * relative to the specified class
083: *
084: * @param clazz
085: * Class The class relative to which to find the image descriptor
086: * @param path
087: * String The path to the image file
088: * @return ImageDescriptor The image descriptor stored in the file at the
089: * specified path
090: */
091: public static ImageDescriptor getImageDescriptor(Class clazz,
092: String path) {
093: return ImageDescriptor.createFromFile(clazz, path);
094: }
095:
096: /**
097: * Returns an image descriptor stored in the file at the specified path
098: *
099: * @param path
100: * String The path to the image file
101: * @return ImageDescriptor The image descriptor stored in the file at the
102: * specified path
103: */
104: public static ImageDescriptor getImageDescriptor(String path) {
105: try {
106: return ImageDescriptor.createFromURL((new File(path))
107: .toURL());
108: } catch (MalformedURLException e) {
109: //TODO
110: return null;
111: }
112: }
113:
114: /**
115: * Returns an image based on the specified image descriptor
116: *
117: * @param descriptor
118: * ImageDescriptor The image descriptor for the image
119: * @return Image The image based on the specified image descriptor
120: */
121: public static Image getImage(ImageDescriptor descriptor) {
122: if (descriptor == null)
123: return null;
124: Image image = m_DescriptorImageMap.get(descriptor);
125: if (image == null) {
126: image = descriptor.createImage();
127: m_DescriptorImageMap.put(descriptor, image);
128: }
129: return image;
130: }
131:
132: /**
133: * Dispose all of the cached images
134: */
135: public static void disposeImages() {
136: SWTResourceManager.disposeImages();
137: //
138: for (Iterator I = m_DescriptorImageMap.values().iterator(); I
139: .hasNext();)
140: ((Image) I.next()).dispose();
141: m_DescriptorImageMap.clear();
142: }
143:
144: // ////////////////////////////
145: // Plugin images support
146: // ////////////////////////////
147:
148: /**
149: * Maps URL to images
150: */
151: private static HashMap<URL, Image> m_URLImageMap = new HashMap<URL, Image>();
152:
153: /**
154: * Retuns an image based on a plugin and file path
155: *
156: * @param plugin
157: * Object The plugin containing the image
158: * @param name
159: * String The path to th eimage within the plugin
160: * @return Image The image stored in the file at the specified path
161: */
162: public static Image getPluginImage(Object plugin, String name) {
163: try {
164: try {
165: URL url = getPluginImageURL(plugin, name);
166: if (m_URLImageMap.containsKey(url))
167: return m_URLImageMap.get(url);
168: InputStream is = url.openStream();
169: Image image;
170: try {
171: image = getImage(is);
172: m_URLImageMap.put(url, image);
173: } finally {
174: is.close();
175: }
176: return image;
177: } catch (Throwable e) {
178: // Ignore any exceptions
179: }
180: } catch (Throwable e) {
181: // Ignore any exceptions
182: }
183: return null;
184: }
185:
186: /**
187: * Retuns an image descriptor based on a plugin and file path
188: *
189: * @param plugin
190: * Object The plugin containing the image
191: * @param name
192: * String The path to th eimage within the plugin
193: * @return ImageDescriptor The image descriptor stored in the file at the
194: * specified path
195: */
196: public static ImageDescriptor getPluginImageDescriptor(
197: Object plugin, String name) {
198: try {
199: try {
200: URL url = getPluginImageURL(plugin, name);
201: return ImageDescriptor.createFromURL(url);
202: } catch (Throwable e) {
203: // Ignore any exceptions
204: }
205: } catch (Throwable e) {
206: // Ignore any exceptions
207: }
208: return null;
209: }
210:
211: /**
212: * Retuns an URL based on a plugin and file path
213: *
214: * @param plugin
215: * Object The plugin containing the file path
216: * @param name
217: * String The file path
218: * @return URL The URL representing the file at the specified path
219: * @throws Exception
220: */
221: @SuppressWarnings("unchecked")//$NON-NLS-1$
222: private static URL getPluginImageURL(Object plugin, String name)
223: throws Exception {
224: // try to work with 'plugin' as with OSGI BundleContext
225: try {
226: Class bundleClass = Class
227: .forName("org.osgi.framework.Bundle"); //$NON-NLS-1$
228: Class bundleContextClass = Class
229: .forName("org.osgi.framework.BundleContext"); //$NON-NLS-1$
230: if (bundleContextClass.isAssignableFrom(plugin.getClass())) {
231: Method getBundleMethod = bundleContextClass.getMethod(
232: "getBundle", new Class[] {}); //$NON-NLS-1$
233: Object bundle = getBundleMethod.invoke(plugin,
234: new Object[] {});
235: //
236: Class ipathClass = Class
237: .forName("org.eclipse.core.runtime.IPath"); //$NON-NLS-1$
238: Class pathClass = Class
239: .forName("org.eclipse.core.runtime.Path"); //$NON-NLS-1$
240: Constructor pathConstructor = pathClass
241: .getConstructor(new Class[] { String.class });
242: Object path = pathConstructor
243: .newInstance(new Object[] { name });
244: //
245: Class platformClass = Class
246: .forName("org.eclipse.core.runtime.Platform"); //$NON-NLS-1$
247: Method findMethod = platformClass
248: .getMethod(
249: "find", new Class[] { bundleClass, ipathClass }); //$NON-NLS-1$
250: return (URL) findMethod.invoke(null, new Object[] {
251: bundle, path });
252: }
253: } catch (Throwable e) {
254: // Ignore any exceptions
255: }
256: // else work with 'plugin' as with usual Eclipse plugin
257: {
258: Class pluginClass = Class
259: .forName("org.eclipse.core.runtime.Plugin"); //$NON-NLS-1$
260: if (pluginClass.isAssignableFrom(plugin.getClass())) {
261: //
262: Class ipathClass = Class
263: .forName("org.eclipse.core.runtime.IPath"); //$NON-NLS-1$
264: Class pathClass = Class
265: .forName("org.eclipse.core.runtime.Path"); //$NON-NLS-1$
266: Constructor pathConstructor = pathClass
267: .getConstructor(new Class[] { String.class });
268: Object path = pathConstructor
269: .newInstance(new Object[] { name });
270: //
271: Method findMethod = pluginClass.getMethod(
272: "find", new Class[] { ipathClass }); //$NON-NLS-1$
273: return (URL) findMethod.invoke(plugin,
274: new Object[] { path });
275: }
276: }
277: return null;
278: }
279: }
|