01: /*******************************************************************************
02: * Copyright (c) 2000, 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.ui.viewsupport;
11:
12: import java.util.HashMap;
13: import java.util.Iterator;
14:
15: import org.eclipse.core.runtime.Assert;
16:
17: import org.eclipse.swt.graphics.Image;
18: import org.eclipse.swt.widgets.Display;
19:
20: import org.eclipse.jdt.internal.ui.util.SWTUtil;
21: import org.eclipse.jface.resource.ImageDescriptor;
22:
23: /**
24: * A registry that maps <code>ImageDescriptors</code> to <code>Image</code>.
25: */
26: public class ImageDescriptorRegistry {
27:
28: private HashMap fRegistry = new HashMap(10);
29: private Display fDisplay;
30:
31: /**
32: * Creates a new image descriptor registry for the current or default display,
33: * respectively.
34: */
35: public ImageDescriptorRegistry() {
36: this (SWTUtil.getStandardDisplay());
37: }
38:
39: /**
40: * Creates a new image descriptor registry for the given display. All images
41: * managed by this registry will be disposed when the display gets disposed.
42: *
43: * @param display the display the images managed by this registry are allocated for
44: */
45: public ImageDescriptorRegistry(Display display) {
46: fDisplay = display;
47: Assert.isNotNull(fDisplay);
48: hookDisplay();
49: }
50:
51: /**
52: * Returns the image associated with the given image descriptor.
53: *
54: * @param descriptor the image descriptor for which the registry manages an image,
55: * or <code>null</code> for a missing image descriptor
56: * @return the image associated with the image descriptor or <code>null</code>
57: * if the image descriptor can't create the requested image.
58: */
59: public Image get(ImageDescriptor descriptor) {
60: if (descriptor == null)
61: descriptor = ImageDescriptor.getMissingImageDescriptor();
62:
63: Image result = (Image) fRegistry.get(descriptor);
64: if (result != null)
65: return result;
66:
67: Assert.isTrue(fDisplay == SWTUtil.getStandardDisplay(),
68: "Allocating image for wrong display."); //$NON-NLS-1$
69: result = descriptor.createImage();
70: if (result != null)
71: fRegistry.put(descriptor, result);
72: return result;
73: }
74:
75: /**
76: * Disposes all images managed by this registry.
77: */
78: public void dispose() {
79: for (Iterator iter = fRegistry.values().iterator(); iter
80: .hasNext();) {
81: Image image = (Image) iter.next();
82: image.dispose();
83: }
84: fRegistry.clear();
85: }
86:
87: private void hookDisplay() {
88: fDisplay.disposeExec(new Runnable() {
89: public void run() {
90: dispose();
91: }
92: });
93: }
94: }
|