refactory: added UI removed from core plugin.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / viewsupport / ImageDescriptorRegistry.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.ui.viewsupport;
6
7 import java.util.HashMap;
8 import java.util.Iterator;
9
10 import net.sourceforge.phpdt.internal.ui.util.SWTUtil;
11
12 import org.eclipse.jface.resource.ImageDescriptor;
13 import org.eclipse.jface.util.Assert;
14 import org.eclipse.swt.graphics.Image;
15 import org.eclipse.swt.widgets.Display;
16
17 /**
18  * A registry that maps <code>ImageDescriptors</code> to <code>Image</code>.
19  */
20 public class ImageDescriptorRegistry {
21
22         private HashMap fRegistry = new HashMap(10);
23
24         private Display fDisplay;
25
26         /**
27          * Creates a new image descriptor registry for the current or default
28          * display, respectively.
29          */
30         public ImageDescriptorRegistry() {
31                 this(SWTUtil.getStandardDisplay());
32         }
33
34         /**
35          * Creates a new image descriptor registry for the given display. All images
36          * managed by this registry will be disposed when the display gets disposed.
37          * 
38          * @param diaplay
39          *            the display the images managed by this registry are allocated
40          *            for
41          */
42         public ImageDescriptorRegistry(Display display) {
43                 fDisplay = display;
44                 Assert.isNotNull(fDisplay);
45                 hookDisplay();
46         }
47
48         /**
49          * Returns the image assiciated with the given image descriptor.
50          * 
51          * @param descriptor
52          *            the image descriptor for which the registry manages an image
53          * @return the image associated with the image descriptor or
54          *         <code>null</code> if the image descriptor can't create the
55          *         requested image.
56          */
57         public Image get(ImageDescriptor descriptor) {
58                 if (descriptor == null)
59                         descriptor = ImageDescriptor.getMissingImageDescriptor();
60
61                 Image result = (Image) fRegistry.get(descriptor);
62                 if (result != null)
63                         return result;
64
65                 Assert.isTrue(fDisplay == SWTUtil.getStandardDisplay(),
66                                 "Allocating image for wrong display."); //$NON-NLS-1$
67                 result = descriptor.createImage();
68                 if (result != null)
69                         fRegistry.put(descriptor, result);
70                 return result;
71         }
72
73         /**
74          * Disposes all images managed by this registry.
75          */
76         public void dispose() {
77                 for (Iterator iter = fRegistry.values().iterator(); iter.hasNext();) {
78                         Image image = (Image) iter.next();
79                         image.dispose();
80                 }
81                 fRegistry.clear();
82         }
83
84         private void hookDisplay() {
85                 fDisplay.disposeExec(new Runnable() {
86                         public void run() {
87                                 dispose();
88                         }
89                 });
90         }
91 }