001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.model;
011:
012: import java.util.HashMap;
013: import java.util.Iterator;
014:
015: import org.eclipse.jface.resource.ImageDescriptor;
016: import org.eclipse.jface.viewers.ITableLabelProvider;
017: import org.eclipse.jface.viewers.LabelProvider;
018: import org.eclipse.osgi.util.NLS;
019: import org.eclipse.swt.graphics.Image;
020: import org.eclipse.ui.IPerspectiveDescriptor;
021: import org.eclipse.ui.PlatformUI;
022: import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
023: import org.eclipse.ui.internal.WorkbenchImages;
024: import org.eclipse.ui.internal.WorkbenchMessages;
025:
026: /**
027: * A table label provider implementation for showing workbench perspectives
028: * (objects of type <code>IPerspectiveDescriptor</code>) in table- and
029: * tree-structured viewers.
030: * <p>
031: * Clients may instantiate this class. It is not intended to be subclassed.
032: * </p>
033: *
034: * @since 3.0
035: */
036: public final class PerspectiveLabelProvider extends LabelProvider
037: implements ITableLabelProvider {
038:
039: /**
040: * List of all Image objects this label provider is responsible for.
041: */
042: private HashMap imageCache = new HashMap(5);
043:
044: /**
045: * Indicates whether the default perspective is visually marked.
046: */
047: private boolean markDefault;
048:
049: /**
050: * Creates a new label provider for perspectives.
051: * The default perspective is visually marked.
052: */
053: public PerspectiveLabelProvider() {
054: this (true);
055: }
056:
057: /**
058: * Creates a new label provider for perspectives.
059: *
060: * @param markDefault <code>true</code> if the default perspective is to be
061: * visually marked, and <code>false</code> if the default perspective is
062: * not treated as anything special
063: */
064: public PerspectiveLabelProvider(boolean markDefault) {
065: super ();
066: this .markDefault = markDefault;
067: }
068:
069: /* (non-Javadoc)
070: * @see org.eclipse.jface.viewers.ILabelProvider
071: */
072: public final Image getImage(Object element) {
073: if (element instanceof IPerspectiveDescriptor) {
074: IPerspectiveDescriptor desc = (IPerspectiveDescriptor) element;
075: ImageDescriptor imageDescriptor = desc.getImageDescriptor();
076: if (imageDescriptor == null) {
077: imageDescriptor = WorkbenchImages
078: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_DEF_PERSPECTIVE);
079: }
080: Image image = (Image) imageCache.get(imageDescriptor);
081: if (image == null) {
082: image = imageDescriptor.createImage();
083: imageCache.put(imageDescriptor, image);
084: }
085: return image;
086: }
087: return null;
088: }
089:
090: /* (non-Javadoc)
091: * @see org.eclipse.jface.viewers.ILabelProvider
092: */
093: public final void dispose() {
094: for (Iterator i = imageCache.values().iterator(); i.hasNext();) {
095: ((Image) i.next()).dispose();
096: }
097: imageCache.clear();
098: }
099:
100: /* (non-Javadoc)
101: * @see org.eclipse.jface.viewers.ILabelProvider
102: */
103: public final String getText(Object element) {
104: if (element instanceof IPerspectiveDescriptor) {
105: IPerspectiveDescriptor desc = (IPerspectiveDescriptor) element;
106: String label = desc.getLabel();
107: if (markDefault) {
108: String def = PlatformUI.getWorkbench()
109: .getPerspectiveRegistry()
110: .getDefaultPerspective();
111: if (desc.getId().equals(def)) {
112: label = NLS
113: .bind(
114: WorkbenchMessages.PerspectivesPreference_defaultLabel,
115: label);
116: }
117: }
118: return label;
119: }
120: return WorkbenchMessages.PerspectiveLabelProvider_unknown;
121: }
122:
123: /**
124: * @see ITableLabelProvider#getColumnImage
125: */
126: public final Image getColumnImage(Object element, int columnIndex) {
127: return getImage(element);
128: }
129:
130: /**
131: * @see ITableLabelProvider#getColumnText
132: */
133: public final String getColumnText(Object element, int columnIndex) {
134: return getText(element);
135: }
136: }
|