ae34a4c279b0f56483920af18276e1cdb7da0f62
[phpeclipse.git] / net.sourceforge.phpeclipse / 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 org.eclipse.swt.graphics.Image;
11 import org.eclipse.swt.widgets.Display;
12
13 import net.sourceforge.phpdt.internal.ui.util.SWTUtil;
14 import org.eclipse.jface.resource.ImageDescriptor;
15 import org.eclipse.jface.util.Assert;
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         private Display fDisplay;
24         
25         /**
26          * Creates a new image descriptor registry for the current or default display,
27          * respectively.
28          */
29         public ImageDescriptorRegistry() {
30                 this(SWTUtil.getStandardDisplay());
31         }
32         
33         /**
34          * Creates a new image descriptor registry for the given display. All images
35          * managed by this registry will be disposed when the display gets disposed.
36          * 
37          * @param diaplay the display the images managed by this registry are allocated for 
38          */
39         public ImageDescriptorRegistry(Display display) {
40                 fDisplay= display;
41                 Assert.isNotNull(fDisplay);
42                 hookDisplay();
43         }
44         
45         /**
46          * Returns the image assiciated with the given image descriptor.
47          * 
48          * @param descriptor the image descriptor for which the registry manages an image
49          * @return the image associated with the image descriptor or <code>null</code>
50          *  if the image descriptor can't create the requested image.
51          */
52         public Image get(ImageDescriptor descriptor) {
53                 if (descriptor == null)
54                         descriptor= ImageDescriptor.getMissingImageDescriptor();
55                         
56                 Image result= (Image)fRegistry.get(descriptor);
57                 if (result != null)
58                         return result;
59         
60                 Assert.isTrue(fDisplay == SWTUtil.getStandardDisplay(), "Allocating image for wrong display."); //$NON-NLS-1$
61                 result= descriptor.createImage();
62                 if (result != null)
63                         fRegistry.put(descriptor, result);
64                 return result;
65         }
66
67         /**
68          * Disposes all images managed by this registry.
69          */     
70         public void dispose() {
71                 for (Iterator iter= fRegistry.values().iterator(); iter.hasNext(); ) {
72                         Image image= (Image)iter.next();
73                         image.dispose();
74                 }
75                 fRegistry.clear();
76         }
77         
78         private void hookDisplay() {
79                 fDisplay.disposeExec(new Runnable() {
80                         public void run() {
81                                 dispose();
82                         }       
83                 });
84         }
85 }
86