]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/ImageCache.java
Implement ImageDescriptor.getImageData(int zoom)
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / gfx / ImageCache.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.utils.ui.gfx;
13
14 import java.util.concurrent.ConcurrentHashMap;
15 import java.util.concurrent.ConcurrentMap;
16
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.jface.resource.LocalResourceManager;
19 import org.eclipse.jface.resource.ResourceManager;
20 import org.eclipse.swt.graphics.Image;
21
22 /**
23  * ImageCache caches SWT images and does clean dispose.<br>
24  * Based on "eclipse - building commercial-quality plug-ins" book's chapter
25  * 7.7's example.<br>
26  * 
27  * @author Marko Luukkainen
28  * 
29  * @deprecated Please avoid using this class if at all possible. From a resource
30  *             management point-of-view this class does not fit well into a
31  *             dynamic OSGi component world. Instead, use JFace
32  *             {@link ResourceManager} and particularly
33  *             {@link LocalResourceManager}. You can create one in your local
34  *             context (a view, editor, bundle activator, etc.) with
35  *             <code>new LocalResourceManager(JFaceResources.getResources())</code>
36  *             . Remember to dispose your LocalResourceManager when its owner is
37  *             disposed. Images can be loaded using
38  *             {@link LocalResourceManager#createImage(ImageDescriptor)}.
39  * 
40  */
41 public class ImageCache {
42
43     /** A singleton cache instance */
44     private static final ImageCache                      imageCache = new ImageCache();
45
46     /** Descriptor to image mapping */
47     private final ConcurrentMap<ImageDescriptor, Image>  images     = new ConcurrentHashMap<ImageDescriptor, Image>();
48
49     private final ConcurrentMap<ImageDescriptor, Object> locks      = new ConcurrentHashMap<ImageDescriptor, Object>();
50
51     /**
52      * Retrieves and caches the specified image.
53      * 
54      * @param imageDescriptor the ImageDescriptor of the Image
55      * @return Image valid image or <code>null</code>if the image could not be
56      *         created
57      */
58     public Image getImage(ImageDescriptor imageDescriptor) {
59         if (imageDescriptor == null)
60             return null;
61
62         Image image = images.get(imageDescriptor);
63         if (image != null)
64             return image;
65
66         Object newLock = new Object();
67         Object loadLock = locks.putIfAbsent(imageDescriptor, newLock);
68         if (loadLock == null)
69             loadLock = newLock;
70
71         // Localized locking, don't lock the whole class while actually loading the image.
72         synchronized (loadLock) {
73             image = images.get(imageDescriptor);
74             if (image == null) {
75                 boolean returnError = false;
76                 image = imageDescriptor.createImage(returnError);
77                 if (image != null) {
78                     images.put(imageDescriptor, image);
79                 }
80             }
81         }
82
83         locks.remove(imageDescriptor, loadLock);
84
85         return image;
86     }
87
88     public void dispose() {
89         // Do not synchronize while disposing image resources
90         Image[] imgs = null;
91         synchronized (images) {
92             imgs = images.values().toArray(new Image[images.size()]);
93             images.clear();
94         }
95         locks.clear();
96
97         for (Image img : imgs) {
98             img.dispose();
99         }
100     }
101
102     public static ImageCache getInstance() {
103         return imageCache;
104     }
105
106 }