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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.viewsupport;
14 import java.util.HashMap;
15 import java.util.Iterator;
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;
28 * Standard label provider for IStorage objects.
29 * Use this class when you want to present IStorage objects in a viewer.
31 public class StorageLabelProvider extends LabelProvider {
33 private IEditorRegistry fEditorRegistry= PlatformUI.getWorkbench().getEditorRegistry();
34 private Map fJarImageMap= new HashMap(10);
35 private Image fDefaultImage;
38 * @see ILabelProvider#getImage
40 public Image getImage(Object element) {
41 if (element instanceof IStorage)
42 return getImageForJarEntry((IStorage)element);
44 return super.getImage(element);
48 * @see ILabelProvider#getText
50 public String getText(Object element) {
51 if (element instanceof IStorage)
52 return ((IStorage)element).getName();
54 return super.getText(element);
59 * @see IBaseLabelProvider#dispose
61 public void dispose() {
62 if (fJarImageMap != null) {
63 Iterator each= fJarImageMap.values().iterator();
64 while (each.hasNext()) {
65 Image image= (Image)each.next();
70 if (fDefaultImage != null)
71 fDefaultImage.dispose();
76 * Gets and caches an image for a JarEntryFile.
77 * The image for a JarEntryFile is retrieved from the EditorRegistry.
79 private Image getImageForJarEntry(IStorage element) {
80 if (fJarImageMap == null)
81 return getDefaultImage();
83 if (element == null || element.getName() == null)
84 return getDefaultImage();
86 // Try to find icon for full name
87 String name= element.getName();
88 Image image= (Image)fJarImageMap.get(name);
91 IFileEditorMapping[] mappings= fEditorRegistry.getFileEditorMappings();
93 while (i < mappings.length) {
94 if (mappings[i].getLabel().equals(name))
99 if (i == mappings.length) {
100 // Try to find icon for extension
101 IPath path= element.getFullPath();
103 return getDefaultImage();
104 key= path.getFileExtension();
106 return getDefaultImage();
107 image= (Image)fJarImageMap.get(key);
112 // Get the image from the editor registry
113 ImageDescriptor desc= fEditorRegistry.getImageDescriptor(name);
114 image= desc.createImage();
116 fJarImageMap.put(key, image);
121 private Image getDefaultImage() {
122 if (fDefaultImage == null)
123 fDefaultImage= fEditorRegistry.getImageDescriptor((String)null).createImage();
124 return fDefaultImage;