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