001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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:
014: import org.eclipse.jface.resource.DeviceResourceException;
015: import org.eclipse.jface.resource.ImageDescriptor;
016: import org.eclipse.jface.resource.JFaceResources;
017: import org.eclipse.jface.resource.LocalResourceManager;
018: import org.eclipse.jface.resource.ResourceManager;
019: import org.eclipse.jface.viewers.ITableLabelProvider;
020: import org.eclipse.jface.viewers.LabelProvider;
021: import org.eclipse.swt.graphics.Image;
022: import org.eclipse.ui.IWorkbenchPart;
023: import org.eclipse.ui.Saveable;
024: import org.eclipse.ui.internal.WorkbenchPlugin;
025:
026: /**
027: * A table label provider implementation for showing workbench views and
028: * editors (objects of type <code>IWorkbenchPart</code>) in tree- and
029: * table-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 WorkbenchPartLabelProvider extends LabelProvider
037: implements ITableLabelProvider {
038:
039: private ResourceManager resourceManager = new LocalResourceManager(
040: JFaceResources.getResources());
041: private HashMap images = new HashMap();
042:
043: /**
044: * Creates a new label provider for workbench parts.
045: */
046: public WorkbenchPartLabelProvider() {
047: super ();
048: }
049:
050: /* (non-Javadoc)
051: * @see org.eclipse.jface.viewers.ILabelProvider
052: */
053: public final Image getImage(Object element) {
054: if (element instanceof IWorkbenchPart) {
055: return ((IWorkbenchPart) element).getTitleImage();
056: }
057: if (element instanceof Saveable) {
058: Saveable model = (Saveable) element;
059: ImageDescriptor imageDesc = model.getImageDescriptor();
060: // convert from ImageDescriptor to Image
061: if (imageDesc == null) {
062: return null;
063: }
064: Image image = (Image) images.get(imageDesc);
065: if (image == null) {
066: try {
067: image = resourceManager.createImage(imageDesc);
068: images.put(imageDesc, image);
069: } catch (DeviceResourceException e) {
070: WorkbenchPlugin.log(getClass(), "getImage", e); //$NON-NLS-1$
071: }
072: }
073: return image;
074: }
075: return null;
076: }
077:
078: /* (non-Javadoc)
079: * @see org.eclipse.jface.viewers.ILabelProvider
080: */
081: public final String getText(Object element) {
082: if (element instanceof IWorkbenchPart) {
083: IWorkbenchPart part = (IWorkbenchPart) element;
084: String path = part.getTitleToolTip();
085: if (path == null || path.trim().length() == 0) {
086: return part.getTitle();
087: }
088: return part.getTitle() + " [" + path + "]"; //$NON-NLS-1$ //$NON-NLS-2$
089: }
090: if (element instanceof Saveable) {
091: Saveable model = (Saveable) element;
092: String path = model.getToolTipText();
093: if (path == null || path.trim().length() == 0) {
094: return model.getName();
095: }
096: return model.getName() + " [" + path + "]"; //$NON-NLS-1$ //$NON-NLS-2$
097:
098: }
099: return null;
100: }
101:
102: /**
103: * @see ITableLabelProvider#getColumnImage
104: */
105: public final Image getColumnImage(Object element, int columnIndex) {
106: return getImage(element);
107: }
108:
109: /**
110: * @see ITableLabelProvider#getColumnText
111: */
112: public final String getColumnText(Object element, int columnIndex) {
113: return getText(element);
114: }
115:
116: /* (non-Javadoc)
117: * @see org.eclipse.jface.viewers.LabelProvider#dispose()
118: *
119: * @since 3.2
120: */
121: public void dispose() {
122: resourceManager.dispose();
123: super.dispose();
124: }
125: }
|