11 package org.eclipse.wb.swt;
14 import java.io.InputStream;
15 import java.lang.reflect.Constructor;
16 import java.lang.reflect.Method;
17 import java.net.MalformedURLException;
19 import java.util.HashMap;
20 import java.util.Iterator;
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.jface.resource.CompositeImageDescriptor;
25 import org.eclipse.jface.resource.ImageDescriptor;
26 import org.eclipse.swt.graphics.Image;
27 import org.eclipse.swt.graphics.Point;
28 import org.eclipse.swt.graphics.Rectangle;
29 import org.osgi.framework.Bundle;
51 private static Map<ImageDescriptor, Image> m_descriptorImageMap =
new HashMap<ImageDescriptor, Image>();
63 return ImageDescriptor.createFromFile(clazz, path);
74 return ImageDescriptor.createFromURL(
new File(path).toURI().toURL());
75 }
catch (MalformedURLException e) {
86 public static Image
getImage(ImageDescriptor descriptor) {
87 if (descriptor ==
null) {
90 Image image = m_descriptorImageMap.get(descriptor);
92 image = descriptor.createImage();
93 m_descriptorImageMap.put(descriptor, image);
100 @SuppressWarnings(
"unchecked")
101 private static Map<Image, Map<Image, Image>>[] m_decoratedImageMap = new Map[
LAST_CORNER_KEY];
125 public static Image
decorateImage(
final Image baseImage,
final Image decorator,
final int corner) {
127 throw new IllegalArgumentException(
"Wrong decorate corner");
129 Map<Image, Map<Image, Image>> cornerDecoratedImageMap = m_decoratedImageMap[corner];
130 if (cornerDecoratedImageMap ==
null) {
131 cornerDecoratedImageMap =
new HashMap<Image, Map<Image, Image>>();
132 m_decoratedImageMap[corner] = cornerDecoratedImageMap;
134 Map<Image, Image> decoratedMap = cornerDecoratedImageMap.get(baseImage);
135 if (decoratedMap ==
null) {
136 decoratedMap =
new HashMap<Image, Image>();
137 cornerDecoratedImageMap.put(baseImage, decoratedMap);
140 Image result = decoratedMap.get(decorator);
141 if (result ==
null) {
142 final Rectangle bib = baseImage.getBounds();
143 final Rectangle dib = decorator.getBounds();
144 final Point baseImageSize =
new Point(bib.width, bib.height);
145 CompositeImageDescriptor compositImageDesc =
new CompositeImageDescriptor() {
147 protected void drawCompositeImage(
int width,
int height) {
148 drawImage(baseImage.getImageData(), 0, 0);
150 drawImage(decorator.getImageData(), 0, 0);
152 drawImage(decorator.getImageData(), bib.width - dib.width, 0);
154 drawImage(decorator.getImageData(), 0, bib.height - dib.height);
156 drawImage(decorator.getImageData(), bib.width - dib.width, bib.height - dib.height);
160 protected Point getSize() {
161 return baseImageSize;
165 result = compositImageDesc.createImage();
166 decoratedMap.put(decorator, result);
177 for (Iterator<Image> I = m_descriptorImageMap.values().iterator(); I.hasNext();) {
180 m_descriptorImageMap.clear();
183 for (
int i = 0; i < m_decoratedImageMap.length; i++) {
184 Map<Image, Map<Image, Image>> cornerDecoratedImageMap = m_decoratedImageMap[i];
185 if (cornerDecoratedImageMap !=
null) {
186 for (Map<Image, Image> decoratedMap : cornerDecoratedImageMap.values()) {
187 for (Image image : decoratedMap.values()) {
190 decoratedMap.clear();
192 cornerDecoratedImageMap.clear();
197 for (Iterator<Image> I = m_URLImageMap.values().iterator(); I.hasNext();) {
200 m_URLImageMap.clear();
211 private static Map<String, Image> m_URLImageMap =
new HashMap<String, Image>();
216 URL getEntry(String symbolicName, String path);
236 URL url = getPluginImageURL(plugin, name);
238 return getPluginImageFromUrl(url);
240 }
catch (Throwable e) {
256 URL url = getPluginImageURL(symbolicName, path);
258 return getPluginImageFromUrl(url);
260 }
catch (Throwable e) {
268 private static Image getPluginImageFromUrl(URL url) {
271 String key = url.toExternalForm();
272 Image image = m_URLImageMap.get(key);
274 InputStream stream = url.openStream();
277 m_URLImageMap.put(key, image);
283 }
catch (Throwable e) {
286 }
catch (Throwable e) {
306 URL url = getPluginImageURL(plugin, name);
307 return ImageDescriptor.createFromURL(url);
308 }
catch (Throwable e) {
311 }
catch (Throwable e) {
327 URL url = getPluginImageURL(symbolicName, path);
329 return ImageDescriptor.createFromURL(url);
331 }
catch (Throwable e) {
339 private static URL getPluginImageURL(String symbolicName, String path) {
342 Bundle bundle = Platform.getBundle(symbolicName);
343 if (bundle !=
null) {
344 return bundle.getEntry(path);
348 if (m_designTimePluginResourceProvider !=
null) {
349 return m_designTimePluginResourceProvider.getEntry(symbolicName, path);
364 private static URL getPluginImageURL(Object plugin, String name)
throws Exception {
367 Class<?> BundleClass = Class.forName(
"org.osgi.framework.Bundle");
368 Class<?> BundleContextClass = Class.forName(
"org.osgi.framework.BundleContext");
369 if (BundleContextClass.isAssignableFrom(plugin.getClass())) {
370 Method getBundleMethod = BundleContextClass.getMethod(
"getBundle",
new Class[0]);
371 Object bundle = getBundleMethod.invoke(plugin,
new Object[0]);
373 Class<?> PathClass = Class.forName(
"org.eclipse.core.runtime.Path");
374 Constructor<?> pathConstructor = PathClass.getConstructor(
new Class[]{String.class});
375 Object path = pathConstructor.newInstance(
new Object[]{name});
377 Class<?> IPathClass = Class.forName(
"org.eclipse.core.runtime.IPath");
378 Class<?> PlatformClass = Class.forName(
"org.eclipse.core.runtime.Platform");
379 Method findMethod = PlatformClass.getMethod(
"find",
new Class[]{BundleClass, IPathClass});
380 return (URL) findMethod.invoke(
null,
new Object[]{bundle, path});
382 }
catch (Throwable e) {
387 Class<?> PluginClass = Class.forName(
"org.eclipse.core.runtime.Plugin");
388 if (PluginClass.isAssignableFrom(plugin.getClass())) {
390 Class<?> PathClass = Class.forName(
"org.eclipse.core.runtime.Path");
391 Constructor<?> pathConstructor = PathClass.getConstructor(
new Class[]{String.class});
392 Object path = pathConstructor.newInstance(
new Object[]{name});
394 Class<?> IPathClass = Class.forName(
"org.eclipse.core.runtime.IPath");
395 Method findMethod = PluginClass.getMethod(
"find",
new Class[]{IPathClass});
396 return (URL) findMethod.invoke(plugin,
new Object[]{path});
static void disposeImages()
static Image getPluginImage(String symbolicName, String path)
static Image getImage(ImageDescriptor descriptor)
static Image decorateImage(Image baseImage, Image decorator)
static ImageDescriptor getPluginImageDescriptor(String symbolicName, String path)
static Image getPluginImage(Object plugin, String name)
static ImageDescriptor getImageDescriptor(Class<?> clazz, String path)
static ImageDescriptor getPluginImageDescriptor(Object plugin, String name)
static ImageDescriptor getImageDescriptor(String path)
static Image decorateImage(final Image baseImage, final Image decorator, final int corner)
static final int TOP_RIGHT
static void disposeImages()
static final int BOTTOM_LEFT
static final int LAST_CORNER_KEY
static void disposeFonts()
static void disposeColors()
static final int BOTTOM_RIGHT
static final int TOP_LEFT