Skip to content

Package: ResourceManager$1

ResourceManager$1

nameinstructionbranchcomplexitylinemethod
drawCompositeImage(int, int)
M: 79 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
getSize()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
{...}
M: 21 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /**
2: * Copyright (c) 2011 Google, Inc.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Google, Inc. - initial API and implementation
13: */
14: package org.eclipse.emf.ecp.cdo.internal.ui;
15:
16: import java.io.File;
17: import java.io.InputStream;
18: import java.lang.reflect.Constructor;
19: import java.lang.reflect.Method;
20: import java.net.MalformedURLException;
21: import java.net.URL;
22: import java.util.HashMap;
23: import java.util.Iterator;
24: import java.util.Map;
25:
26: import org.eclipse.core.runtime.Platform;
27: import org.eclipse.jface.resource.CompositeImageDescriptor;
28: import org.eclipse.jface.resource.ImageDescriptor;
29: import org.eclipse.swt.graphics.Image;
30: import org.eclipse.swt.graphics.Point;
31: import org.eclipse.swt.graphics.Rectangle;
32: import org.osgi.framework.Bundle;
33:
34: /**
35: * Utility class for managing OS resources associated with SWT/JFace controls such as colors, fonts, images, etc. !!!
36: * IMPORTANT !!! Application code must explicitly invoke the <code>dispose()</code> method to release the operating
37: * system resources managed by cached objects when those objects and OS resources are no longer needed (e.g. on
38: * application shutdown) This class may be freely distributed as part of any application or plugin.
39: * <p>
40: *
41: * @author scheglov_ke
42: * @author Dan Rubel
43: * @generated
44: */
45: public class ResourceManager extends SWTResourceManager {
46:         // //////////////////////////////////////////////////////////////////////////
47:         //
48:         // Image
49:         //
50:         // //////////////////////////////////////////////////////////////////////////
51:         private static Map<ImageDescriptor, Image> m_descriptorImageMap = new HashMap<ImageDescriptor, Image>();
52:
53:         /**
54:          * Returns an {@link ImageDescriptor} stored in the file at the specified path relative to the specified class.
55:          *
56:          * @param clazz
57:          * the {@link Class} relative to which to find the image descriptor.
58:          * @param path
59:          * the path to the image file.
60:          * @return the {@link ImageDescriptor} stored in the file at the specified path.
61:          */
62:         public static ImageDescriptor getImageDescriptor(Class<?> clazz, String path) {
63:                 return ImageDescriptor.createFromFile(clazz, path);
64:         }
65:
66:         /**
67:          * Returns an {@link ImageDescriptor} stored in the file at the specified path.
68:          *
69:          * @param path
70:          * the path to the image file.
71:          * @return the {@link ImageDescriptor} stored in the file at the specified path.
72:          */
73:         public static ImageDescriptor getImageDescriptor(String path) {
74:                 try {
75:                         return ImageDescriptor.createFromURL(new File(path).toURI().toURL());
76:                 } catch (final MalformedURLException e) {
77:                         return null;
78:                 }
79:         }
80:
81:         /**
82:          * Returns an {@link Image} based on the specified {@link ImageDescriptor}.
83:          *
84:          * @param descriptor
85:          * the {@link ImageDescriptor} for the {@link Image}.
86:          * @return the {@link Image} based on the specified {@link ImageDescriptor}.
87:          */
88:         public static Image getImage(ImageDescriptor descriptor) {
89:                 if (descriptor == null) {
90:                         return null;
91:                 }
92:                 Image image = m_descriptorImageMap.get(descriptor);
93:                 if (image == null) {
94:                         image = descriptor.createImage();
95:                         m_descriptorImageMap.put(descriptor, image);
96:                 }
97:                 return image;
98:         }
99:
100:         /**
101:          * Maps images to decorated images.
102:          */
103:         @SuppressWarnings("unchecked")
104:         private static Map<Image, Map<Image, Image>>[] m_decoratedImageMap = new Map[LAST_CORNER_KEY];
105:
106:         /**
107:          * Returns an {@link Image} composed of a base image decorated by another image.
108:          *
109:          * @param baseImage
110:          * the base {@link Image} that should be decorated.
111:          * @param decorator
112:          * the {@link Image} to decorate the base image.
113:          * @return {@link Image} The resulting decorated image.
114:          */
115:         public static Image decorateImage(Image baseImage, Image decorator) {
116:                 return decorateImage(baseImage, decorator, BOTTOM_RIGHT);
117:         }
118:
119:         /**
120:          * Returns an {@link Image} composed of a base image decorated by another image.
121:          *
122:          * @param baseImage
123:          * the base {@link Image} that should be decorated.
124:          * @param decorator
125:          * the {@link Image} to decorate the base image.
126:          * @param corner
127:          * the corner to place decorator image.
128:          * @return the resulting decorated {@link Image}.
129:          */
130:         public static Image decorateImage(final Image baseImage, final Image decorator, final int corner) {
131:                 if (corner <= 0 || corner >= LAST_CORNER_KEY) {
132:                         throw new IllegalArgumentException("Wrong decorate corner"); //$NON-NLS-1$
133:                 }
134:                 Map<Image, Map<Image, Image>> cornerDecoratedImageMap = m_decoratedImageMap[corner];
135:                 if (cornerDecoratedImageMap == null) {
136:                         cornerDecoratedImageMap = new HashMap<Image, Map<Image, Image>>();
137:                         m_decoratedImageMap[corner] = cornerDecoratedImageMap;
138:                 }
139:                 Map<Image, Image> decoratedMap = cornerDecoratedImageMap.get(baseImage);
140:                 if (decoratedMap == null) {
141:                         decoratedMap = new HashMap<Image, Image>();
142:                         cornerDecoratedImageMap.put(baseImage, decoratedMap);
143:                 }
144:                 //
145:                 Image result = decoratedMap.get(decorator);
146:                 if (result == null) {
147:                         final Rectangle bib = baseImage.getBounds();
148:                         final Rectangle dib = decorator.getBounds();
149:                         final Point baseImageSize = new Point(bib.width, bib.height);
150:                         final CompositeImageDescriptor compositImageDesc = new CompositeImageDescriptor() {
151:                                 @Override
152:                                 protected void drawCompositeImage(int width, int height) {
153:                                         drawImage(baseImage.getImageData(), 0, 0);
154:•                                        if (corner == TOP_LEFT) {
155:                                                 drawImage(decorator.getImageData(), 0, 0);
156:•                                        } else if (corner == TOP_RIGHT) {
157:                                                 drawImage(decorator.getImageData(), bib.width - dib.width, 0);
158:•                                        } else if (corner == BOTTOM_LEFT) {
159:                                                 drawImage(decorator.getImageData(), 0, bib.height - dib.height);
160:•                                        } else if (corner == BOTTOM_RIGHT) {
161:                                                 drawImage(decorator.getImageData(), bib.width - dib.width, bib.height - dib.height);
162:                                         }
163:                                 }
164:
165:                                 @Override
166:                                 protected Point getSize() {
167:                                         return baseImageSize;
168:                                 }
169:                         };
170:                         //
171:                         result = compositImageDesc.createImage();
172:                         decoratedMap.put(decorator, result);
173:                 }
174:                 return result;
175:         }
176:
177:         /**
178:          * Dispose all of the cached images.
179:          */
180:         public static void disposeImages() {
181:                 SWTResourceManager.disposeImages();
182:                 // dispose ImageDescriptor images
183:                 {
184:                         for (final Iterator<Image> I = m_descriptorImageMap.values().iterator(); I.hasNext();) {
185:                                 I.next().dispose();
186:                         }
187:                         m_descriptorImageMap.clear();
188:                 }
189:                 // dispose decorated images
190:                 for (int i = 0; i < m_decoratedImageMap.length; i++) {
191:                         final Map<Image, Map<Image, Image>> cornerDecoratedImageMap = m_decoratedImageMap[i];
192:                         if (cornerDecoratedImageMap != null) {
193:                                 for (final Map<Image, Image> decoratedMap : cornerDecoratedImageMap.values()) {
194:                                         for (final Image image : decoratedMap.values()) {
195:                                                 image.dispose();
196:                                         }
197:                                         decoratedMap.clear();
198:                                 }
199:                                 cornerDecoratedImageMap.clear();
200:                         }
201:                 }
202:                 // dispose plugin images
203:                 {
204:                         for (final Iterator<Image> I = m_URLImageMap.values().iterator(); I.hasNext();) {
205:                                 I.next().dispose();
206:                         }
207:                         m_URLImageMap.clear();
208:                 }
209:         }
210:
211:         // //////////////////////////////////////////////////////////////////////////
212:         //
213:         // Plugin images support
214:         //
215:         // //////////////////////////////////////////////////////////////////////////
216:         /**
217:          * Maps URL to images.
218:          */
219:         private static Map<String, Image> m_URLImageMap = new HashMap<String, Image>();
220:
221:         /**
222:          * Provider for plugin resources, used by WindowBuilder at design time.
223:          */
224:         public interface PluginResourceProvider {
225:                 URL getEntry(String symbolicName, String path);
226:         }
227:
228:         /**
229:          * Instance of {@link PluginResourceProvider}, used by WindowBuilder at design time.
230:          */
231:         private static PluginResourceProvider m_designTimePluginResourceProvider;
232:
233:         /**
234:          * Returns an {@link Image} based on a plugin and file path.
235:          *
236:          * @param plugin
237:          * the plugin {@link Object} containing the image
238:          * @param name
239:          * the path to the image within the plugin
240:          * @return the {@link Image} stored in the file at the specified path
241:          * @deprecated Use {@link #getPluginImage(String, String)} instead.
242:          */
243:         @Deprecated
244:         public static Image getPluginImage(Object plugin, String name) {
245:                 try {
246:                         final URL url = getPluginImageURL(plugin, name);
247:                         if (url != null) {
248:                                 return getPluginImageFromUrl(url);
249:                         }
250:                 } catch (final Throwable e) {
251:                         // Ignore any exceptions
252:                 }
253:                 return null;
254:         }
255:
256:         /**
257:          * Returns an {@link Image} based on a {@link Bundle} and resource entry path.
258:          *
259:          * @param symbolicName
260:          * the symbolic name of the {@link Bundle}.
261:          * @param path
262:          * the path of the resource entry.
263:          * @return the {@link Image} stored in the file at the specified path.
264:          */
265:         public static Image getPluginImage(String symbolicName, String path) {
266:                 try {
267:                         final URL url = getPluginImageURL(symbolicName, path);
268:                         if (url != null) {
269:                                 return getPluginImageFromUrl(url);
270:                         }
271:                 } catch (final Throwable e) {
272:                         // Ignore any exceptions
273:                 }
274:                 return null;
275:         }
276:
277:         /**
278:          * Returns an {@link Image} based on given {@link URL}.
279:          */
280:         private static Image getPluginImageFromUrl(URL url) {
281:                 try {
282:                         try {
283:                                 final String key = url.toExternalForm();
284:                                 Image image = m_URLImageMap.get(key);
285:                                 if (image == null) {
286:                                         final InputStream stream = url.openStream();
287:                                         try {
288:                                                 image = getImage(stream);
289:                                                 m_URLImageMap.put(key, image);
290:                                         } finally {
291:                                                 stream.close();
292:                                         }
293:                                 }
294:                                 return image;
295:                         } catch (final Throwable e) {
296:                                 // Ignore any exceptions
297:                         }
298:                 } catch (final Throwable e) {
299:                         // Ignore any exceptions
300:                 }
301:                 return null;
302:         }
303:
304:         /**
305:          * Returns an {@link ImageDescriptor} based on a plugin and file path.
306:          *
307:          * @param plugin
308:          * the plugin {@link Object} containing the image.
309:          * @param name
310:          * the path to th eimage within the plugin.
311:          * @return the {@link ImageDescriptor} stored in the file at the specified path.
312:          * @deprecated Use {@link #getPluginImageDescriptor(String, String)} instead.
313:          */
314:         @Deprecated
315:         public static ImageDescriptor getPluginImageDescriptor(Object plugin, String name) {
316:                 try {
317:                         try {
318:                                 final URL url = getPluginImageURL(plugin, name);
319:                                 return ImageDescriptor.createFromURL(url);
320:                         } catch (final Throwable e) {
321:                                 // Ignore any exceptions
322:                         }
323:                 } catch (final Throwable e) {
324:                         // Ignore any exceptions
325:                 }
326:                 return null;
327:         }
328:
329:         /**
330:          * Returns an {@link ImageDescriptor} based on a {@link Bundle} and resource entry path.
331:          *
332:          * @param symbolicName
333:          * the symbolic name of the {@link Bundle}.
334:          * @param path
335:          * the path of the resource entry.
336:          * @return the {@link ImageDescriptor} based on a {@link Bundle} and resource entry path.
337:          */
338:         public static ImageDescriptor getPluginImageDescriptor(String symbolicName, String path) {
339:                 try {
340:                         final URL url = getPluginImageURL(symbolicName, path);
341:                         if (url != null) {
342:                                 return ImageDescriptor.createFromURL(url);
343:                         }
344:                 } catch (final Throwable e) {
345:                         // Ignore any exceptions
346:                 }
347:                 return null;
348:         }
349:
350:         /**
351:          * Returns an {@link URL} based on a {@link Bundle} and resource entry path.
352:          */
353:         private static URL getPluginImageURL(String symbolicName, String path) {
354:                 // try runtime plugins
355:                 {
356:                         final Bundle bundle = Platform.getBundle(symbolicName);
357:                         if (bundle != null) {
358:                                 return bundle.getEntry(path);
359:                         }
360:                 }
361:                 // try design time provider
362:                 if (m_designTimePluginResourceProvider != null) {
363:                         return m_designTimePluginResourceProvider.getEntry(symbolicName, path);
364:                 }
365:                 // no such resource
366:                 return null;
367:         }
368:
369:         /**
370:          * Returns an {@link URL} based on a plugin and file path.
371:          *
372:          * @param plugin
373:          * the plugin {@link Object} containing the file path.
374:          * @param name
375:          * the file path.
376:          * @return the {@link URL} representing the file at the specified path.
377:          * @throws Exception
378:          */
379:         private static URL getPluginImageURL(Object plugin, String name) throws Exception {
380:                 // try to work with 'plugin' as with OSGI BundleContext
381:                 try {
382:                         final Class<?> BundleClass = Class.forName("org.osgi.framework.Bundle"); //$NON-NLS-1$
383:                         final Class<?> BundleContextClass = Class.forName("org.osgi.framework.BundleContext"); //$NON-NLS-1$
384:                         if (BundleContextClass.isAssignableFrom(plugin.getClass())) {
385:                                 final Method getBundleMethod = BundleContextClass.getMethod("getBundle", new Class[0]); //$NON-NLS-1$
386:                                 final Object bundle = getBundleMethod.invoke(plugin, new Object[0]);
387:                                 //
388:                                 final Class<?> PathClass = Class.forName("org.eclipse.core.runtime.Path"); //$NON-NLS-1$
389:                                 final Constructor<?> pathConstructor = PathClass.getConstructor(new Class[] { String.class });
390:                                 final Object path = pathConstructor.newInstance(new Object[] { name });
391:                                 //
392:                                 final Class<?> IPathClass = Class.forName("org.eclipse.core.runtime.IPath"); //$NON-NLS-1$
393:                                 final Class<?> PlatformClass = Class.forName("org.eclipse.core.runtime.Platform"); //$NON-NLS-1$
394:                                 final Method findMethod = PlatformClass.getMethod("find", new Class[] { BundleClass, IPathClass }); //$NON-NLS-1$
395:                                 return (URL) findMethod.invoke(null, new Object[] { bundle, path });
396:                         }
397:                 } catch (final Throwable e) {
398:                         // Ignore any exceptions
399:                 }
400:                 // else work with 'plugin' as with usual Eclipse plugin
401:                 {
402:                         final Class<?> PluginClass = Class.forName("org.eclipse.core.runtime.Plugin"); //$NON-NLS-1$
403:                         if (PluginClass.isAssignableFrom(plugin.getClass())) {
404:                                 //
405:                                 final Class<?> PathClass = Class.forName("org.eclipse.core.runtime.Path"); //$NON-NLS-1$
406:                                 final Constructor<?> pathConstructor = PathClass.getConstructor(new Class[] { String.class });
407:                                 final Object path = pathConstructor.newInstance(new Object[] { name });
408:                                 //
409:                                 final Class<?> IPathClass = Class.forName("org.eclipse.core.runtime.IPath"); //$NON-NLS-1$
410:                                 final Method findMethod = PluginClass.getMethod("find", new Class[] { IPathClass }); //$NON-NLS-1$
411:                                 return (URL) findMethod.invoke(plugin, new Object[] { path });
412:                         }
413:                 }
414:                 return null;
415:         }
416:
417:         // //////////////////////////////////////////////////////////////////////////
418:         //
419:         // General
420:         //
421:         // //////////////////////////////////////////////////////////////////////////
422:         /**
423:          * Dispose of cached objects and their underlying OS resources. This should only be called when the cached objects
424:          * are
425:          * no longer needed (e.g. on application shutdown).
426:          */
427:         public static void dispose() {
428:                 disposeColors();
429:                 disposeFonts();
430:                 disposeImages();
431:         }
432: }