1 11 package org.eclipse.jdt.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.runtime.IPath; 19 20 import org.eclipse.core.resources.IStorage; 21 22 import org.eclipse.swt.graphics.Image; 23 24 import org.eclipse.jface.resource.ImageDescriptor; 25 import org.eclipse.jface.viewers.LabelProvider; 26 27 import org.eclipse.ui.IEditorRegistry; 28 import org.eclipse.ui.IFileEditorMapping; 29 import org.eclipse.ui.ISharedImages; 30 import org.eclipse.ui.PlatformUI; 31 32 import org.eclipse.jdt.core.IJarEntryResource; 33 34 38 public class StorageLabelProvider extends LabelProvider { 39 40 private IEditorRegistry fEditorRegistry= null; 41 private Map fJarImageMap= new HashMap (10); 42 private Image fDefaultImage; 43 44 private IEditorRegistry getEditorRegistry() { 45 if (fEditorRegistry == null) 46 fEditorRegistry= PlatformUI.getWorkbench().getEditorRegistry(); 47 return fEditorRegistry; 48 } 49 50 53 public Image getImage(Object element) { 54 if (element instanceof IStorage) 55 return getImageForJarEntry((IStorage)element); 56 57 return super.getImage(element); 58 } 59 60 63 public String getText(Object element) { 64 if (element instanceof IStorage) { 65 return ((IStorage)element).getName(); 66 } 67 return super.getText(element); 68 } 69 70 74 public void dispose() { 75 if (fJarImageMap != null) { 76 Iterator each= fJarImageMap.values().iterator(); 77 while (each.hasNext()) { 78 Image image= (Image)each.next(); 79 image.dispose(); 80 } 81 fJarImageMap= null; 82 } 83 fDefaultImage= null; 84 } 85 86 90 private Image getImageForJarEntry(IStorage element) { 91 if (element instanceof IJarEntryResource && !((IJarEntryResource) element).isFile()) { 92 return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER); 93 } 94 95 if (fJarImageMap == null) 96 return getDefaultImage(); 97 98 if (element == null || element.getName() == null) 99 return getDefaultImage(); 100 101 String name= element.getName(); 103 Image image= (Image)fJarImageMap.get(name); 104 if (image != null) 105 return image; 106 IFileEditorMapping[] mappings= getEditorRegistry().getFileEditorMappings(); 107 int i= 0; 108 while (i < mappings.length) { 109 if (mappings[i].getLabel().equals(name)) 110 break; 111 i++; 112 } 113 String key= name; 114 if (i == mappings.length) { 115 IPath path= element.getFullPath(); 117 if (path == null) 118 return getDefaultImage(); 119 key= path.getFileExtension(); 120 if (key == null) 121 return getDefaultImage(); 122 image= (Image)fJarImageMap.get(key); 123 if (image != null) 124 return image; 125 } 126 127 ImageDescriptor desc= getEditorRegistry().getImageDescriptor(name); 129 image= desc.createImage(); 130 131 fJarImageMap.put(key, image); 132 133 return image; 134 } 135 136 private Image getDefaultImage() { 137 if (fDefaultImage == null) 138 fDefaultImage= PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FILE); 139 return fDefaultImage; 140 } 141 } 142 | Popular Tags |