KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > ImageDescriptorRegistry


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.ui;
12
13  
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.widgets.Display;
21
22 /**
23  * A registry that maps <code>ImageDescriptors</code> to <code>Image</code>.
24  */

25 public class ImageDescriptorRegistry {
26
27     private HashMap JavaDoc fRegistry= new HashMap JavaDoc(10);
28     private Display fDisplay;
29     
30     /**
31      * Creates a new image descriptor registry for the current or default display,
32      * respectively.
33      */

34     public ImageDescriptorRegistry() {
35         this(JDIDebugUIPlugin.getStandardDisplay());
36     }
37     
38     /**
39      * Creates a new image descriptor registry for the given display. All images
40      * managed by this registry will be disposed when the display gets disposed.
41      *
42      * @param display the display the images managed by this registry are allocated for
43      */

44     public ImageDescriptorRegistry(Display display) {
45         fDisplay= display;
46         Assert.isNotNull(fDisplay);
47         hookDisplay();
48     }
49     
50     /**
51      * Returns the image associated with the given image descriptor.
52      *
53      * @param descriptor the image descriptor for which the registry manages an image
54      * @return the image associated with the image descriptor or <code>null</code>
55      * if the image descriptor can't create the requested image.
56      */

57     public Image get(ImageDescriptor descriptor) {
58         if (descriptor == null)
59             descriptor= ImageDescriptor.getMissingImageDescriptor();
60             
61         Image result= (Image)fRegistry.get(descriptor);
62         if (result != null)
63             return result;
64     
65         Assert.isTrue(fDisplay == JDIDebugUIPlugin.getStandardDisplay(), DebugUIMessages.ImageDescriptorRegistry_Allocating_image_for_wrong_display_1);
66         result= descriptor.createImage();
67         if (result != null)
68             fRegistry.put(descriptor, result);
69         return result;
70     }
71
72     /**
73      * Disposes all images managed by this registry.
74      */

75     public void dispose() {
76         for (Iterator JavaDoc iter= fRegistry.values().iterator(); iter.hasNext(); ) {
77             Image image= (Image)iter.next();
78             image.dispose();
79         }
80         fRegistry.clear();
81     }
82     
83     private void hookDisplay() {
84         fDisplay.asyncExec(new Runnable JavaDoc() {
85             public void run() {
86             fDisplay.disposeExec(new Runnable JavaDoc() {
87                 public void run() {
88                     dispose();
89                 }
90             });
91             }
92         });
93
94     }
95 }
96
97
Popular Tags