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;
21 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.jface.resource.ImageDescriptor;
24 import org.eclipse.jface.viewers.LabelProvider;
26 import org.eclipse.ui.IEditorRegistry;
27 import org.eclipse.ui.IFileEditorMapping;
28 import org.eclipse.ui.PlatformUI;
31 * Standard label provider for IStorage objects.
32 * Use this class when you want to present IStorage objects in a viewer.
34 public class StorageLabelProvider extends LabelProvider {
36 private IEditorRegistry fEditorRegistry= PlatformUI.getWorkbench().getEditorRegistry();
37 private Map fJarImageMap= new HashMap(10);
38 private Image fDefaultImage;
41 * @see ILabelProvider#getImage
43 public Image getImage(Object element) {
44 if (element instanceof IStorage)
45 return getImageForJarEntry((IStorage)element);
47 return super.getImage(element);
51 * @see ILabelProvider#getText
53 public String getText(Object element) {
54 if (element instanceof IStorage)
55 return ((IStorage)element).getName();
57 return super.getText(element);
62 * @see IBaseLabelProvider#dispose
64 public void dispose() {
65 if (fJarImageMap != null) {
66 Iterator each= fJarImageMap.values().iterator();
67 while (each.hasNext()) {
68 Image image= (Image)each.next();
73 if (fDefaultImage != null)
74 fDefaultImage.dispose();
79 * Gets and caches an image for a JarEntryFile.
80 * The image for a JarEntryFile is retrieved from the EditorRegistry.
82 private Image getImageForJarEntry(IStorage element) {
83 if (fJarImageMap == null)
84 return getDefaultImage();
86 if (element == null || element.getName() == null)
87 return getDefaultImage();
89 // Try to find icon for full name
90 String name= element.getName();
91 Image image= (Image)fJarImageMap.get(name);
94 IFileEditorMapping[] mappings= fEditorRegistry.getFileEditorMappings();
96 while (i < mappings.length) {
97 if (mappings[i].getLabel().equals(name))
102 if (i == mappings.length) {
103 // Try to find icon for extension
104 IPath path= element.getFullPath();
106 return getDefaultImage();
107 key= path.getFileExtension();
109 return getDefaultImage();
110 image= (Image)fJarImageMap.get(key);
115 // Get the image from the editor registry
116 ImageDescriptor desc= fEditorRegistry.getImageDescriptor(name);
117 image= desc.createImage();
119 fJarImageMap.put(key, image);
124 private Image getDefaultImage() {
125 if (fDefaultImage == null)
126 fDefaultImage= fEditorRegistry.getImageDescriptor((String)null).createImage();
127 return fDefaultImage;