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;
13 import java.util.HashMap;
14 import java.util.Iterator;
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;
27 * Standard label provider for IStorage objects. Use this class when you want to
28 * present IStorage objects in a viewer.
30 public class StorageLabelProvider extends LabelProvider {
32 private IEditorRegistry fEditorRegistry = PlatformUI.getWorkbench()
35 private Map fJarImageMap = new HashMap(10);
37 private Image fDefaultImage;
42 * @see ILabelProvider#getImage
44 public Image getImage(Object element) {
45 if (element instanceof IStorage)
46 return getImageForJarEntry((IStorage) element);
48 return super.getImage(element);
54 * @see ILabelProvider#getText
56 public String getText(Object element) {
57 if (element instanceof IStorage)
58 return ((IStorage) element).getName();
60 return super.getText(element);
66 * @see IBaseLabelProvider#dispose
68 public void dispose() {
69 if (fJarImageMap != null) {
70 Iterator each = fJarImageMap.values().iterator();
71 while (each.hasNext()) {
72 Image image = (Image) each.next();
77 if (fDefaultImage != null)
78 fDefaultImage.dispose();
83 * Gets and caches an image for a JarEntryFile. The image for a JarEntryFile
84 * is retrieved from the EditorRegistry.
86 private Image getImageForJarEntry(IStorage element) {
87 if (fJarImageMap == null)
88 return getDefaultImage();
90 if (element == null || element.getName() == null)
91 return getDefaultImage();
93 // Try to find icon for full name
94 String name = element.getName();
95 Image image = (Image) fJarImageMap.get(name);
98 IFileEditorMapping[] mappings = fEditorRegistry.getFileEditorMappings();
100 while (i < mappings.length) {
101 if (mappings[i].getLabel().equals(name))
106 if (i == mappings.length) {
107 // Try to find icon for extension
108 IPath path = element.getFullPath();
110 return getDefaultImage();
111 key = path.getFileExtension();
113 return getDefaultImage();
114 image = (Image) fJarImageMap.get(key);
119 // Get the image from the editor registry
120 ImageDescriptor desc = fEditorRegistry.getImageDescriptor(name);
121 image = desc.createImage();
123 fJarImageMap.put(key, image);
128 private Image getDefaultImage() {
129 if (fDefaultImage == null)
130 fDefaultImage = fEditorRegistry.getImageDescriptor((String) null)
132 return fDefaultImage;