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;
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;
20 * A registry that maps <code>ImageDescriptors</code> to <code>Image</code>.
22 public class ImageDescriptorRegistry {
24 private HashMap fRegistry = new HashMap(10);
26 private Display fDisplay;
29 * Creates a new image descriptor registry for the current or default
30 * display, respectively.
32 public ImageDescriptorRegistry() {
33 this(SWTUtil.getStandardDisplay());
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.
41 * the display the images managed by this registry are allocated
44 public ImageDescriptorRegistry(Display display) {
46 Assert.isNotNull(fDisplay);
51 * Returns the image assiciated with the given image 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
59 public Image get(ImageDescriptor descriptor) {
60 if (descriptor == null)
61 descriptor = ImageDescriptor.getMissingImageDescriptor();
63 Image result = (Image) fRegistry.get(descriptor);
67 Assert.isTrue(fDisplay == SWTUtil.getStandardDisplay(),
68 "Allocating image for wrong display."); //$NON-NLS-1$
69 result = descriptor.createImage();
71 fRegistry.put(descriptor, result);
76 * Disposes all images managed by this registry.
78 public void dispose() {
79 for (Iterator iter = fRegistry.values().iterator(); iter.hasNext();) {
80 Image image = (Image) iter.next();
86 private void hookDisplay() {
87 fDisplay.disposeExec(new Runnable() {