2 * Copyright (c) 2004 Christopher Lenz and others
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * Christopher Lenz - initial implementation based on the internal Eclipse
10 * class of the same name, defined in multiple packages
12 * $Id: ImageDescriptorRegistry.java,v 1.2 2006-10-21 23:13:53 pombredanne Exp $
15 package net.sourceforge.phpeclipse.ui.views.util;
17 import java.util.HashMap;
18 import java.util.Iterator;
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.swt.widgets.Display;
26 * Registry that keeps a table of image descriptors and the images created from
29 public class ImageDescriptorRegistry {
32 * Stores all registered image descriptors as keys, and the images created
33 * from them as values.
35 private Map registry = new HashMap(10);
37 // Constructors ------------------------------------------------------------
40 * Creates a new image descriptor registry for the current or default
41 * display, respectively.
43 public ImageDescriptorRegistry() {
44 this(Display.getCurrent() != null ? Display.getCurrent() : Display
49 * Creates a new image descriptor registry for the given display. All images
50 * managed by this registry will be disposed when the display gets disposed.
53 * the display the images managed by this registry are allocated
56 public ImageDescriptorRegistry(Display display) {
57 display.disposeExec(new Runnable() {
65 * Returns the image assiciated with the given image descriptor.
68 * the image descriptor for which the registry manages an image
69 * @return the image associated with the image descriptor or <tt>null</tt>
70 * if the image descriptor can't create the requested image.
72 public Image get(ImageDescriptor descriptor) {
73 if (descriptor == null) {
74 descriptor = ImageDescriptor.getMissingImageDescriptor();
76 Image result = (Image) registry.get(descriptor);
78 result = descriptor.createImage();
80 registry.put(descriptor, result);
87 * Disposes all images managed by this registry.
89 public void dispose() {
90 for (Iterator i = registry.values().iterator(); i.hasNext();) {
91 ((Image) i.next()).dispose();