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.1 2004-09-02 18:26:28 jsurfer 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 ?
45 Display.getCurrent() : Display.getDefault());
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.
52 * @param display the display the images managed by this registry are
55 public ImageDescriptorRegistry(Display display) {
56 display.disposeExec(new Runnable() {
64 * Returns the image assiciated with the given image descriptor.
66 * @param descriptor the image descriptor for which the registry manages an
68 * @return the image associated with the image descriptor or <tt>null</tt>
69 * if the image descriptor can't create the requested image.
71 public Image get(ImageDescriptor descriptor) {
72 if (descriptor == null) {
73 descriptor = ImageDescriptor.getMissingImageDescriptor();
75 Image result = (Image) registry.get(descriptor);
77 result = descriptor.createImage();
79 registry.put(descriptor, result);
86 * Disposes all images managed by this registry.
88 public void dispose() {
89 for (Iterator i = registry.values().iterator(); i.hasNext(); ) {
90 ((Image) i.next()).dispose();