intial source from http://www.sf.net/projects/wdte
[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.1 2004-09-02 18:26:28 jsurfer 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 ?
45                                 Display.getCurrent() : Display.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 the display the images managed by this registry are
53          *        allocated for 
54          */
55         public ImageDescriptorRegistry(Display display) {
56                 display.disposeExec(new Runnable() {
57                         public void run() {
58                                 dispose();
59                         }       
60                 });
61         }
62         
63         /**
64          * Returns the image assiciated with the given image descriptor.
65          * 
66          * @param descriptor the image descriptor for which the registry manages an
67          *        image
68          * @return the image associated with the image descriptor or <tt>null</tt>
69          *         if the image descriptor can't create the requested image.
70          */
71         public Image get(ImageDescriptor descriptor) {
72                 if (descriptor == null) {
73                         descriptor = ImageDescriptor.getMissingImageDescriptor();
74                 }
75                 Image result = (Image) registry.get(descriptor);
76                 if (result == null) {
77                         result = descriptor.createImage();
78                         if (result != null) {
79                                 registry.put(descriptor, result);
80                         }
81                 }
82                 return result;
83         }
84
85         /**
86          * Disposes all images managed by this registry.
87          */     
88         public void dispose() {
89                 for (Iterator i = registry.values().iterator(); i.hasNext(); ) {
90                         ((Image) i.next()).dispose();
91                 }
92                 registry.clear();
93         }
94         
95 }