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