KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > model > WorkbenchPartLabelProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.model;
12
13 import java.util.HashMap JavaDoc;
14
15 import org.eclipse.jface.resource.DeviceResourceException;
16 import org.eclipse.jface.resource.ImageDescriptor;
17 import org.eclipse.jface.resource.JFaceResources;
18 import org.eclipse.jface.resource.LocalResourceManager;
19 import org.eclipse.jface.resource.ResourceManager;
20 import org.eclipse.jface.viewers.ITableLabelProvider;
21 import org.eclipse.jface.viewers.LabelProvider;
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.ui.IWorkbenchPart;
24 import org.eclipse.ui.Saveable;
25 import org.eclipse.ui.internal.WorkbenchPlugin;
26
27 /**
28  * A table label provider implementation for showing workbench views and
29  * editors (objects of type <code>IWorkbenchPart</code>) in tree- and
30  * table-structured viewers.
31  * <p>
32  * Clients may instantiate this class. It is not intended to be subclassed.
33  * </p>
34  *
35  * @since 3.0
36  */

37 public final class WorkbenchPartLabelProvider extends LabelProvider implements
38         ITableLabelProvider {
39
40     private ResourceManager resourceManager = new LocalResourceManager(JFaceResources.getResources());
41     private HashMap JavaDoc images = new HashMap JavaDoc();
42     
43     /**
44      * Creates a new label provider for workbench parts.
45      */

46     public WorkbenchPartLabelProvider() {
47         super();
48     }
49
50     /* (non-Javadoc)
51      * @see org.eclipse.jface.viewers.ILabelProvider
52      */

53     public final Image getImage(Object JavaDoc element) {
54         if (element instanceof IWorkbenchPart) {
55             return ((IWorkbenchPart) element).getTitleImage();
56         }
57         if (element instanceof Saveable) {
58             Saveable model = (Saveable) element;
59             ImageDescriptor imageDesc = model.getImageDescriptor();
60             // convert from ImageDescriptor to Image
61
if (imageDesc == null) {
62                 return null;
63             }
64             Image image = (Image) images.get(imageDesc);
65             if (image == null) {
66                 try {
67                     image = resourceManager.createImage(imageDesc);
68                     images.put(imageDesc, image);
69                 }
70                 catch (DeviceResourceException e) {
71                     WorkbenchPlugin.log(getClass(), "getImage", e); //$NON-NLS-1$
72
}
73             }
74             return image;
75         }
76         return null;
77     }
78
79     /* (non-Javadoc)
80      * @see org.eclipse.jface.viewers.ILabelProvider
81      */

82     public final String JavaDoc getText(Object JavaDoc element) {
83         if (element instanceof IWorkbenchPart) {
84             IWorkbenchPart part = (IWorkbenchPart) element;
85             String JavaDoc path = part.getTitleToolTip();
86             if (path == null || path.trim().length() == 0) {
87                 return part.getTitle();
88             }
89             return part.getTitle() + " [" + path + "]"; //$NON-NLS-1$ //$NON-NLS-2$
90
}
91         if (element instanceof Saveable) {
92             Saveable model = (Saveable) element;
93             String JavaDoc path = model.getToolTipText();
94             if (path == null || path.trim().length() == 0) {
95                 return model.getName();
96             }
97             return model.getName() + " [" + path + "]"; //$NON-NLS-1$ //$NON-NLS-2$
98

99         }
100         return null;
101     }
102
103     /**
104      * @see ITableLabelProvider#getColumnImage
105      */

106     public final Image getColumnImage(Object JavaDoc element, int columnIndex) {
107         return getImage(element);
108     }
109
110     /**
111      * @see ITableLabelProvider#getColumnText
112      */

113     public final String JavaDoc getColumnText(Object JavaDoc element, int columnIndex) {
114         return getText(element);
115     }
116     
117     /* (non-Javadoc)
118      * @see org.eclipse.jface.viewers.LabelProvider#dispose()
119      *
120      * @since 3.2
121      */

122     public void dispose() {
123         resourceManager.dispose();
124         super.dispose();
125     }
126 }
127
Popular Tags