a5d86bc5cd512a85059fb207e3a302f5d9b0d258
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / viewsupport / StorageLabelProvider.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation 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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.viewsupport;
12
13 import java.util.HashMap;
14 import java.util.Iterator;
15 import java.util.Map;
16
17 import org.eclipse.core.resources.IStorage;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.jface.resource.ImageDescriptor;
20 import org.eclipse.jface.viewers.LabelProvider;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.ui.IEditorRegistry;
23 import org.eclipse.ui.IFileEditorMapping;
24 import org.eclipse.ui.PlatformUI;
25
26 /**
27  * Standard label provider for IStorage objects. Use this class when you want to
28  * present IStorage objects in a viewer.
29  */
30 public class StorageLabelProvider extends LabelProvider {
31
32         private IEditorRegistry fEditorRegistry = PlatformUI.getWorkbench()
33                         .getEditorRegistry();
34
35         private Map fJarImageMap = new HashMap(10);
36
37         private Image fDefaultImage;
38
39         /*
40          * (non-Javadoc)
41          * 
42          * @see ILabelProvider#getImage
43          */
44         public Image getImage(Object element) {
45                 if (element instanceof IStorage)
46                         return getImageForJarEntry((IStorage) element);
47
48                 return super.getImage(element);
49         }
50
51         /*
52          * (non-Javadoc)
53          * 
54          * @see ILabelProvider#getText
55          */
56         public String getText(Object element) {
57                 if (element instanceof IStorage)
58                         return ((IStorage) element).getName();
59
60                 return super.getText(element);
61         }
62
63         /*
64          * (non-Javadoc)
65          * 
66          * @see IBaseLabelProvider#dispose
67          */
68         public void dispose() {
69                 if (fJarImageMap != null) {
70                         Iterator each = fJarImageMap.values().iterator();
71                         while (each.hasNext()) {
72                                 Image image = (Image) each.next();
73                                 image.dispose();
74                         }
75                         fJarImageMap = null;
76                 }
77                 if (fDefaultImage != null)
78                         fDefaultImage.dispose();
79                 fDefaultImage = null;
80         }
81
82         /*
83          * Gets and caches an image for a JarEntryFile. The image for a JarEntryFile
84          * is retrieved from the EditorRegistry.
85          */
86         private Image getImageForJarEntry(IStorage element) {
87                 if (fJarImageMap == null)
88                         return getDefaultImage();
89
90                 if (element == null || element.getName() == null)
91                         return getDefaultImage();
92
93                 // Try to find icon for full name
94                 String name = element.getName();
95                 Image image = (Image) fJarImageMap.get(name);
96                 if (image != null)
97                         return image;
98                 IFileEditorMapping[] mappings = fEditorRegistry.getFileEditorMappings();
99                 int i = 0;
100                 while (i < mappings.length) {
101                         if (mappings[i].getLabel().equals(name))
102                                 break;
103                         i++;
104                 }
105                 String key = name;
106                 if (i == mappings.length) {
107                         // Try to find icon for extension
108                         IPath path = element.getFullPath();
109                         if (path == null)
110                                 return getDefaultImage();
111                         key = path.getFileExtension();
112                         if (key == null)
113                                 return getDefaultImage();
114                         image = (Image) fJarImageMap.get(key);
115                         if (image != null)
116                                 return image;
117                 }
118
119                 // Get the image from the editor registry
120                 ImageDescriptor desc = fEditorRegistry.getImageDescriptor(name);
121                 image = desc.createImage();
122
123                 fJarImageMap.put(key, image);
124
125                 return image;
126         }
127
128         private Image getDefaultImage() {
129                 if (fDefaultImage == null)
130                         fDefaultImage = fEditorRegistry.getImageDescriptor((String) null)
131                                         .createImage();
132                 return fDefaultImage;
133         }
134 }