A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / views / util / ImageDescriptorRegistry.java
1 /*
2  * Copyright (c) 2004 Christopher Lenz and others
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     Christopher Lenz - initial implementation based on the internal Eclipse
10  *                        class of the same name, defined in multiple packages
11  * 
12  * $Id: ImageDescriptorRegistry.java,v 1.2 2006-10-21 23:13:53 pombredanne Exp $
13  */
14
15 package net.sourceforge.phpeclipse.ui.views.util;
16
17 import java.util.HashMap;
18 import java.util.Iterator;
19 import java.util.Map;
20
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.swt.widgets.Display;
24
25 /**
26  * Registry that keeps a table of image descriptors and the images created from
27  * those descriptors.
28  */
29 public class ImageDescriptorRegistry {
30
31         /**
32          * Stores all registered image descriptors as keys, and the images created
33          * from them as values.
34          */
35         private Map registry = new HashMap(10);
36
37         // Constructors ------------------------------------------------------------
38
39         /**
40          * Creates a new image descriptor registry for the current or default
41          * display, respectively.
42          */
43         public ImageDescriptorRegistry() {
44                 this(Display.getCurrent() != null ? Display.getCurrent() : Display
45                                 .getDefault());
46         }
47
48         /**
49          * Creates a new image descriptor registry for the given display. All images
50          * managed by this registry will be disposed when the display gets disposed.
51          * 
52          * @param display
53          *            the display the images managed by this registry are allocated
54          *            for
55          */
56         public ImageDescriptorRegistry(Display display) {
57                 display.disposeExec(new Runnable() {
58                         public void run() {
59                                 dispose();
60                         }
61                 });
62         }
63
64         /**
65          * Returns the image assiciated with the given image descriptor.
66          * 
67          * @param descriptor
68          *            the image descriptor for which the registry manages an image
69          * @return the image associated with the image descriptor or <tt>null</tt>
70          *         if the image descriptor can't create the requested image.
71          */
72         public Image get(ImageDescriptor descriptor) {
73                 if (descriptor == null) {
74                         descriptor = ImageDescriptor.getMissingImageDescriptor();
75                 }
76                 Image result = (Image) registry.get(descriptor);
77                 if (result == null) {
78                         result = descriptor.createImage();
79                         if (result != null) {
80                                 registry.put(descriptor, result);
81                         }
82                 }
83                 return result;
84         }
85
86         /**
87          * Disposes all images managed by this registry.
88          */
89         public void dispose() {
90                 for (Iterator i = registry.values().iterator(); i.hasNext();) {
91                         ((Image) i.next()).dispose();
92                 }
93                 registry.clear();
94         }
95
96 }