1 11 package org.eclipse.ui.model; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 16 import org.eclipse.jface.resource.ImageDescriptor; 17 import org.eclipse.jface.viewers.ITableLabelProvider; 18 import org.eclipse.jface.viewers.LabelProvider; 19 import org.eclipse.osgi.util.NLS; 20 import org.eclipse.swt.graphics.Image; 21 import org.eclipse.ui.IPerspectiveDescriptor; 22 import org.eclipse.ui.PlatformUI; 23 import org.eclipse.ui.internal.IWorkbenchGraphicConstants; 24 import org.eclipse.ui.internal.WorkbenchImages; 25 import org.eclipse.ui.internal.WorkbenchMessages; 26 27 37 public final class PerspectiveLabelProvider extends LabelProvider implements 38 ITableLabelProvider { 39 40 43 private HashMap imageCache = new HashMap (5); 44 45 48 private boolean markDefault; 49 50 54 public PerspectiveLabelProvider() { 55 this(true); 56 } 57 58 65 public PerspectiveLabelProvider(boolean markDefault) { 66 super(); 67 this.markDefault = markDefault; 68 } 69 70 73 public final Image getImage(Object element) { 74 if (element instanceof IPerspectiveDescriptor) { 75 IPerspectiveDescriptor desc = (IPerspectiveDescriptor) element; 76 ImageDescriptor imageDescriptor = desc.getImageDescriptor(); 77 if (imageDescriptor == null) { 78 imageDescriptor = WorkbenchImages 79 .getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_DEF_PERSPECTIVE); 80 } 81 Image image = (Image) imageCache.get(imageDescriptor); 82 if (image == null) { 83 image = imageDescriptor.createImage(); 84 imageCache.put(imageDescriptor, image); 85 } 86 return image; 87 } 88 return null; 89 } 90 91 94 public final void dispose() { 95 for (Iterator i = imageCache.values().iterator(); i.hasNext();) { 96 ((Image) i.next()).dispose(); 97 } 98 imageCache.clear(); 99 } 100 101 104 public final String getText(Object element) { 105 if (element instanceof IPerspectiveDescriptor) { 106 IPerspectiveDescriptor desc = (IPerspectiveDescriptor) element; 107 String label = desc.getLabel(); 108 if (markDefault) { 109 String def = PlatformUI.getWorkbench().getPerspectiveRegistry() 110 .getDefaultPerspective(); 111 if (desc.getId().equals(def)) { 112 label = NLS.bind(WorkbenchMessages.PerspectivesPreference_defaultLabel, label ); 113 } 114 } 115 return label; 116 } 117 return WorkbenchMessages.PerspectiveLabelProvider_unknown; 118 } 119 120 123 public final Image getColumnImage(Object element, int columnIndex) { 124 return getImage(element); 125 } 126 127 130 public final String getColumnText(Object element, int columnIndex) { 131 return getText(element); 132 } 133 } 134 | Popular Tags |