1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.utils.ui.gfx;
14 import java.util.concurrent.ConcurrentHashMap;
15 import java.util.concurrent.ConcurrentMap;
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;
23 * ImageCache caches SWT images and does clean dispose.<br>
24 * Based on "eclipse - building commercial-quality plug-ins" book's chapter
27 * @author Marko Luukkainen
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)}.
41 public class ImageCache {
43 /** A singleton cache instance */
44 private static final ImageCache imageCache = new ImageCache();
46 /** Descriptor to image mapping */
47 private final ConcurrentMap<ImageDescriptor, Image> images = new ConcurrentHashMap<ImageDescriptor, Image>();
49 private final ConcurrentMap<ImageDescriptor, Object> locks = new ConcurrentHashMap<ImageDescriptor, Object>();
52 * Retrieves and caches the specified image.
54 * @param imageDescriptor the ImageDescriptor of the Image
55 * @return Image valid image or <code>null</code>if the image could not be
58 public Image getImage(ImageDescriptor imageDescriptor) {
59 if (imageDescriptor == null)
62 Image image = images.get(imageDescriptor);
66 Object newLock = new Object();
67 Object loadLock = locks.putIfAbsent(imageDescriptor, newLock);
71 // Localized locking, don't lock the whole class while actually loading the image.
72 synchronized (loadLock) {
73 image = images.get(imageDescriptor);
75 boolean returnError = false;
76 image = imageDescriptor.createImage(returnError);
78 images.put(imageDescriptor, image);
83 locks.remove(imageDescriptor, loadLock);
88 public void dispose() {
89 // Do not synchronize while disposing image resources
91 synchronized (images) {
92 imgs = images.values().toArray(new Image[images.size()]);
97 for (Image img : imgs) {
102 public static ImageCache getInstance() {