KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > viewsupport > 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.ui.viewsupport;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17
18 import org.eclipse.swt.graphics.Image;
19 import org.eclipse.swt.widgets.Display;
20
21 import org.eclipse.jdt.internal.ui.util.SWTUtil;
22 import org.eclipse.jface.resource.ImageDescriptor;
23
24 /**
25  * A registry that maps <code>ImageDescriptors</code> to <code>Image</code>.
26  */

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

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

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

60     public Image get(ImageDescriptor descriptor) {
61         if (descriptor == null)
62             descriptor= ImageDescriptor.getMissingImageDescriptor();
63             
64         Image result= (Image)fRegistry.get(descriptor);
65         if (result != null)
66             return result;
67     
68         Assert.isTrue(fDisplay == SWTUtil.getStandardDisplay(), "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 JavaDoc iter= fRegistry.values().iterator(); iter.hasNext(); ) {
80             Image image= (Image)iter.next();
81             image.dispose();
82         }
83         fRegistry.clear();
84     }
85     
86     private void hookDisplay() {
87         fDisplay.disposeExec(new Runnable JavaDoc() {
88             public void run() {
89                 dispose();
90             }
91         });
92     }
93 }
94
Popular Tags