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