2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.ui.viewsupport;
7 import java.util.HashMap;
8 import java.util.Iterator;
10 import org.eclipse.swt.graphics.Image;
11 import org.eclipse.swt.widgets.Display;
13 import net.sourceforge.phpdt.internal.ui.util.SWTUtil;
14 import org.eclipse.jface.resource.ImageDescriptor;
15 import org.eclipse.jface.util.Assert;
18 * A registry that maps <code>ImageDescriptors</code> to <code>Image</code>.
20 public class ImageDescriptorRegistry {
22 private HashMap fRegistry= new HashMap(10);
23 private Display fDisplay;
26 * Creates a new image descriptor registry for the current or default display,
29 public ImageDescriptorRegistry() {
30 this(SWTUtil.getStandardDisplay());
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.
37 * @param diaplay the display the images managed by this registry are allocated for
39 public ImageDescriptorRegistry(Display display) {
41 Assert.isNotNull(fDisplay);
46 * Returns the image assiciated with the given image descriptor.
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.
52 public Image get(ImageDescriptor descriptor) {
53 if (descriptor == null)
54 descriptor= ImageDescriptor.getMissingImageDescriptor();
56 Image result= (Image)fRegistry.get(descriptor);
60 Assert.isTrue(fDisplay == SWTUtil.getStandardDisplay(), "Allocating image for wrong display."); //$NON-NLS-1$
61 result= descriptor.createImage();
63 fRegistry.put(descriptor, result);
68 * Disposes all images managed by this registry.
70 public void dispose() {
71 for (Iterator iter= fRegistry.values().iterator(); iter.hasNext(); ) {
72 Image image= (Image)iter.next();
78 private void hookDisplay() {
79 fDisplay.disposeExec(new Runnable() {