Refactory: remove unused classes, imports, fields and methods.
[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 //incastrix
14 //import org.eclipse.jface.text.Assert;
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.swt.widgets.Display;
18
19 /**
20  * A registry that maps <code>ImageDescriptors</code> to <code>Image</code>.
21  */
22 public class ImageDescriptorRegistry {
23
24         private HashMap fRegistry = new HashMap(10);
25
26         private Display fDisplay;
27
28         /**
29          * Creates a new image descriptor registry for the current or default
30          * display, respectively.
31          */
32         public ImageDescriptorRegistry() {
33                 this(SWTUtil.getStandardDisplay());
34         }
35
36         /**
37          * Creates a new image descriptor registry for the given display. All images
38          * managed by this registry will be disposed when the display gets disposed.
39          * 
40          * @param diaplay
41          *            the display the images managed by this registry are allocated
42          *            for
43          */
44         public ImageDescriptorRegistry(Display display) {
45                 fDisplay = display;
46                 Assert.isNotNull(fDisplay);
47                 hookDisplay();
48         }
49
50         /**
51          * Returns the image assiciated with the given image descriptor.
52          * 
53          * @param descriptor
54          *            the image descriptor for which the registry manages an image
55          * @return the image associated with the image descriptor or
56          *         <code>null</code> if the image descriptor can't create the
57          *         requested image.
58          */
59         public Image get(ImageDescriptor descriptor) {
60                 if (descriptor == null)
61                         descriptor = ImageDescriptor.getMissingImageDescriptor();
62
63                 Image result = (Image) fRegistry.get(descriptor);
64                 if (result != null)
65                         return result;
66
67                 Assert.isTrue(fDisplay == SWTUtil.getStandardDisplay(),
68                                 "Allocating image for wrong display."); //$NON-NLS-1$
69                 result = descriptor.createImage();
70                 if (result != null)
71                         fRegistry.put(descriptor, result);
72                 return result;
73         }
74
75         /**
76          * Disposes all images managed by this registry.
77          */
78         public void dispose() {
79                 for (Iterator iter = fRegistry.values().iterator(); iter.hasNext();) {
80                         Image image = (Image) iter.next();
81                         image.dispose();
82                 }
83                 fRegistry.clear();
84         }
85
86         private void hookDisplay() {
87                 fDisplay.disposeExec(new Runnable() {
88                         public void run() {
89                                 dispose();
90                         }
91                 });
92         }
93 }