KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > viewsupport > StorageLabelProvider


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
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.runtime.IPath;
19
20 import org.eclipse.core.resources.IStorage;
21
22 import org.eclipse.swt.graphics.Image;
23
24 import org.eclipse.jface.resource.ImageDescriptor;
25 import org.eclipse.jface.viewers.LabelProvider;
26
27 import org.eclipse.ui.IEditorRegistry;
28 import org.eclipse.ui.IFileEditorMapping;
29 import org.eclipse.ui.ISharedImages;
30 import org.eclipse.ui.PlatformUI;
31
32 import org.eclipse.jdt.core.IJarEntryResource;
33
34 /**
35  * Standard label provider for IStorage objects.
36  * Use this class when you want to present IStorage objects in a viewer.
37  */

38 public class StorageLabelProvider extends LabelProvider {
39
40     private IEditorRegistry fEditorRegistry= null;
41     private Map JavaDoc fJarImageMap= new HashMap JavaDoc(10);
42     private Image fDefaultImage;
43
44     private IEditorRegistry getEditorRegistry() {
45         if (fEditorRegistry == null)
46             fEditorRegistry= PlatformUI.getWorkbench().getEditorRegistry();
47         return fEditorRegistry;
48     }
49
50     /* (non-Javadoc)
51      * @see ILabelProvider#getImage
52      */

53     public Image getImage(Object JavaDoc element) {
54         if (element instanceof IStorage)
55             return getImageForJarEntry((IStorage)element);
56
57         return super.getImage(element);
58     }
59
60     /* (non-Javadoc)
61      * @see ILabelProvider#getText
62      */

63     public String JavaDoc getText(Object JavaDoc element) {
64         if (element instanceof IStorage) {
65             return ((IStorage)element).getName();
66         }
67         return super.getText(element);
68     }
69
70     /* (non-Javadoc)
71      *
72      * @see IBaseLabelProvider#dispose
73      */

74     public void dispose() {
75         if (fJarImageMap != null) {
76             Iterator JavaDoc each= fJarImageMap.values().iterator();
77             while (each.hasNext()) {
78                 Image image= (Image)each.next();
79                 image.dispose();
80             }
81             fJarImageMap= null;
82         }
83         fDefaultImage= null;
84     }
85     
86     /*
87      * Gets and caches an image for a JarEntryFile.
88      * The image for a JarEntryFile is retrieved from the EditorRegistry.
89      */

90     private Image getImageForJarEntry(IStorage element) {
91         if (element instanceof IJarEntryResource && !((IJarEntryResource) element).isFile()) {
92             return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER);
93         }
94         
95         if (fJarImageMap == null)
96             return getDefaultImage();
97
98         if (element == null || element.getName() == null)
99             return getDefaultImage();
100
101         // Try to find icon for full name
102
String JavaDoc name= element.getName();
103         Image image= (Image)fJarImageMap.get(name);
104         if (image != null)
105             return image;
106         IFileEditorMapping[] mappings= getEditorRegistry().getFileEditorMappings();
107         int i= 0;
108         while (i < mappings.length) {
109             if (mappings[i].getLabel().equals(name))
110                 break;
111             i++;
112         }
113         String JavaDoc key= name;
114         if (i == mappings.length) {
115             // Try to find icon for extension
116
IPath path= element.getFullPath();
117             if (path == null)
118                 return getDefaultImage();
119             key= path.getFileExtension();
120             if (key == null)
121                 return getDefaultImage();
122             image= (Image)fJarImageMap.get(key);
123             if (image != null)
124                 return image;
125         }
126
127         // Get the image from the editor registry
128
ImageDescriptor desc= getEditorRegistry().getImageDescriptor(name);
129         image= desc.createImage();
130
131         fJarImageMap.put(key, image);
132
133         return image;
134     }
135     
136     private Image getDefaultImage() {
137         if (fDefaultImage == null)
138             fDefaultImage= PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FILE);
139         return fDefaultImage;
140     }
141 }
142
Popular Tags