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 net.sourceforge.phpdt.internal.ui.util.SWTUtil;
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;
18 * A registry that maps <code>ImageDescriptors</code> to <code>Image</code>.
20 public class ImageDescriptorRegistry {
22 private HashMap fRegistry = new HashMap(10);
24 private Display fDisplay;
27 * Creates a new image descriptor registry for the current or default
28 * display, respectively.
30 public ImageDescriptorRegistry() {
31 this(SWTUtil.getStandardDisplay());
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.
39 * the display the images managed by this registry are allocated
42 public ImageDescriptorRegistry(Display display) {
44 Assert.isNotNull(fDisplay);
49 * Returns the image assiciated with the given image 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
57 public Image get(ImageDescriptor descriptor) {
58 if (descriptor == null)
59 descriptor = ImageDescriptor.getMissingImageDescriptor();
61 Image result = (Image) fRegistry.get(descriptor);
65 Assert.isTrue(fDisplay == SWTUtil.getStandardDisplay(),
66 "Allocating image for wrong display."); //$NON-NLS-1$
67 result = descriptor.createImage();
69 fRegistry.put(descriptor, result);
74 * Disposes all images managed by this registry.
76 public void dispose() {
77 for (Iterator iter = fRegistry.values().iterator(); iter.hasNext();) {
78 Image image = (Image) iter.next();
84 private void hookDisplay() {
85 fDisplay.disposeExec(new Runnable() {