1 12 package org.eclipse.ui.internal.dialogs; 13 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 17 import org.eclipse.jface.resource.ImageDescriptor; 18 import org.eclipse.jface.viewers.IColorProvider; 19 import org.eclipse.jface.viewers.LabelProvider; 20 import org.eclipse.swt.graphics.Color; 21 import org.eclipse.swt.graphics.Image; 22 import org.eclipse.ui.ISharedImages; 23 import org.eclipse.ui.IWorkbenchPage; 24 import org.eclipse.ui.IWorkbenchWindow; 25 import org.eclipse.ui.internal.WorkbenchImages; 26 import org.eclipse.ui.internal.WorkbenchMessages; 27 import org.eclipse.ui.views.IViewCategory; 28 import org.eclipse.ui.views.IViewDescriptor; 29 30 33 public class ViewLabelProvider extends LabelProvider implements IColorProvider { 34 private HashMap images; 35 private final IWorkbenchWindow window; 36 private final Color dimmedForeground; 37 38 42 public ViewLabelProvider(IWorkbenchWindow window, Color dimmedForeground) { 43 this.window = window; 44 this.dimmedForeground = dimmedForeground; 45 } 46 47 Image cacheImage(ImageDescriptor desc) { 48 if (images == null) { 49 images = new HashMap (21); 50 } 51 Image image = (Image) images.get(desc); 52 if (image == null) { 53 image = desc.createImage(); 54 images.put(desc, image); 55 } 56 return image; 57 } 58 59 62 public void dispose() { 63 if (images != null) { 64 for (Iterator i = images.values().iterator(); i.hasNext();) { 65 ((Image) i.next()).dispose(); 66 } 67 images = null; 68 } 69 super.dispose(); 70 } 71 72 75 public Image getImage(Object element) { 76 if (element instanceof IViewDescriptor) { 77 ImageDescriptor desc = ((IViewDescriptor) element) 78 .getImageDescriptor(); 79 if (desc != null) { 80 return cacheImage(desc); 81 } 82 } else if (element instanceof IViewCategory) { 83 ImageDescriptor desc = WorkbenchImages 84 .getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER); 85 return cacheImage(desc); 86 } 87 return null; 88 } 89 90 93 public String getText(Object element) { 94 String label = WorkbenchMessages.ViewLabel_unknown; 95 if (element instanceof IViewCategory) { 96 label = ((IViewCategory) element).getLabel(); 97 } else if (element instanceof IViewDescriptor) { 98 label = ((IViewDescriptor) element).getLabel(); 99 } 100 return DialogUtil.removeAccel(label); 101 } 102 103 public Color getBackground(Object element) { 104 return null; 105 } 106 107 public Color getForeground(Object element) { 108 if (element instanceof IViewDescriptor) { 109 IWorkbenchPage activePage = window.getActivePage(); 110 if (activePage != null) { 111 if (activePage 112 .findView(((IViewDescriptor) element).getId()) != null) { 113 return dimmedForeground; 114 } 115 } 116 } 117 return null; 118 } 119 } 120 | Popular Tags |