X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.utils.ui%2Fsrc%2Forg%2Fsimantics%2Futils%2Fui%2Fgfx%2FImageCache.java;fp=bundles%2Forg.simantics.utils.ui%2Fsrc%2Forg%2Fsimantics%2Futils%2Fui%2Fgfx%2FImageCache.java;h=eba4b8ded2ae726dccd23e8c342ef79aba794f3a;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=f194f5f08547c830b8373b82daa771810f1637d5;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/ImageCache.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/ImageCache.java index f194f5f08..eba4b8ded 100644 --- a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/ImageCache.java +++ b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/ImageCache.java @@ -1,106 +1,106 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.utils.ui.gfx; - -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; - -import org.eclipse.jface.resource.ImageDescriptor; -import org.eclipse.jface.resource.LocalResourceManager; -import org.eclipse.jface.resource.ResourceManager; -import org.eclipse.swt.graphics.Image; - -/** - * ImageCache caches SWT images and does clean dispose.
- * Based on "eclipse - building commercial-quality plug-ins" book's chapter - * 7.7's example.
- * - * @author Marko Luukkainen - * - * @deprecated Please avoid using this class if at all possible. From a resource - * management point-of-view this class does not fit well into a - * dynamic OSGi component world. Instead, use JFace - * {@link ResourceManager} and particularly - * {@link LocalResourceManager}. You can create one in your local - * context (a view, editor, bundle activator, etc.) with - * new LocalResourceManager(JFaceResources.getResources()) - * . Remember to dispose your LocalResourceManager when its owner is - * disposed. Images can be loaded using - * {@link LocalResourceManager#createImage(ImageDescriptor)}. - * - */ -public class ImageCache { - - /** A singleton cache instance */ - private static final ImageCache imageCache = new ImageCache(); - - /** Descriptor to image mapping */ - private final ConcurrentMap images = new ConcurrentHashMap(); - - private final ConcurrentMap locks = new ConcurrentHashMap(); - - /** - * Retrieves and caches the specified image. - * - * @param imageDescriptor the ImageDescriptor of the Image - * @return Image valid image or nullif the image could not be - * created - */ - public Image getImage(ImageDescriptor imageDescriptor) { - if (imageDescriptor == null) - return null; - - Image image = images.get(imageDescriptor); - if (image != null) - return image; - - Object newLock = new Object(); - Object loadLock = locks.putIfAbsent(imageDescriptor, newLock); - if (loadLock == null) - loadLock = newLock; - - // Localized locking, don't lock the whole class while actually loading the image. - synchronized (loadLock) { - image = images.get(imageDescriptor); - if (image == null) { - boolean returnError = false; - image = imageDescriptor.createImage(returnError); - if (image != null) { - images.put(imageDescriptor, image); - } - } - } - - locks.remove(imageDescriptor, loadLock); - - return image; - } - - public void dispose() { - // Do not synchronize while disposing image resources - Image[] imgs = null; - synchronized (images) { - imgs = images.values().toArray(new Image[images.size()]); - images.clear(); - } - locks.clear(); - - for (Image img : imgs) { - img.dispose(); - } - } - - public static ImageCache getInstance() { - return imageCache; - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.utils.ui.gfx; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.jface.resource.LocalResourceManager; +import org.eclipse.jface.resource.ResourceManager; +import org.eclipse.swt.graphics.Image; + +/** + * ImageCache caches SWT images and does clean dispose.
+ * Based on "eclipse - building commercial-quality plug-ins" book's chapter + * 7.7's example.
+ * + * @author Marko Luukkainen + * + * @deprecated Please avoid using this class if at all possible. From a resource + * management point-of-view this class does not fit well into a + * dynamic OSGi component world. Instead, use JFace + * {@link ResourceManager} and particularly + * {@link LocalResourceManager}. You can create one in your local + * context (a view, editor, bundle activator, etc.) with + * new LocalResourceManager(JFaceResources.getResources()) + * . Remember to dispose your LocalResourceManager when its owner is + * disposed. Images can be loaded using + * {@link LocalResourceManager#createImage(ImageDescriptor)}. + * + */ +public class ImageCache { + + /** A singleton cache instance */ + private static final ImageCache imageCache = new ImageCache(); + + /** Descriptor to image mapping */ + private final ConcurrentMap images = new ConcurrentHashMap(); + + private final ConcurrentMap locks = new ConcurrentHashMap(); + + /** + * Retrieves and caches the specified image. + * + * @param imageDescriptor the ImageDescriptor of the Image + * @return Image valid image or nullif the image could not be + * created + */ + public Image getImage(ImageDescriptor imageDescriptor) { + if (imageDescriptor == null) + return null; + + Image image = images.get(imageDescriptor); + if (image != null) + return image; + + Object newLock = new Object(); + Object loadLock = locks.putIfAbsent(imageDescriptor, newLock); + if (loadLock == null) + loadLock = newLock; + + // Localized locking, don't lock the whole class while actually loading the image. + synchronized (loadLock) { + image = images.get(imageDescriptor); + if (image == null) { + boolean returnError = false; + image = imageDescriptor.createImage(returnError); + if (image != null) { + images.put(imageDescriptor, image); + } + } + } + + locks.remove(imageDescriptor, loadLock); + + return image; + } + + public void dispose() { + // Do not synchronize while disposing image resources + Image[] imgs = null; + synchronized (images) { + imgs = images.values().toArray(new Image[images.size()]); + images.clear(); + } + locks.clear(); + + for (Image img : imgs) { + img.dispose(); + } + } + + public static ImageCache getInstance() { + return imageCache; + } + +}