KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 org.eclipse.jface.resource.ImageDescriptor;
14 import org.eclipse.jface.viewers.DecoratingLabelProvider;
15 import org.eclipse.jface.viewers.IColorProvider;
16 import org.eclipse.jface.viewers.IFontProvider;
17 import org.eclipse.jface.viewers.ILabelProvider;
18 import org.eclipse.jface.viewers.LabelProvider;
19 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
20 import org.eclipse.swt.graphics.Color;
21 import org.eclipse.swt.graphics.Font;
22 import org.eclipse.swt.graphics.FontData;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.swt.graphics.RGB;
25 import org.eclipse.swt.widgets.Display;
26 import org.eclipse.ui.IEditorRegistry;
27 import org.eclipse.ui.IPropertyListener;
28 import org.eclipse.ui.PlatformUI;
29 import org.eclipse.ui.internal.util.SWTResourceUtil;
30 import org.eclipse.ui.internal.util.Util;
31
32 /**
33  * Provides basic labels for adaptable objects that have the
34  * <code>IWorkbenchAdapter</code> adapter associated with them. All dispensed
35  * images are cached until the label provider is explicitly disposed.
36  * This class provides a facility for subclasses to define annotations
37  * on the labels and icons of adaptable objects.
38  */

39 public class WorkbenchLabelProvider extends LabelProvider implements
40         IColorProvider, IFontProvider {
41
42     /**
43      * Returns a workbench label provider that is hooked up to the decorator
44      * mechanism.
45      *
46      * @return a new <code>DecoratingLabelProvider</code> which wraps a <code>
47      * new <code>WorkbenchLabelProvider</code>
48      */

49     public static ILabelProvider getDecoratingWorkbenchLabelProvider() {
50         return new DecoratingLabelProvider(new WorkbenchLabelProvider(),
51                 PlatformUI.getWorkbench().getDecoratorManager()
52                         .getLabelDecorator());
53     }
54     
55     /**
56      * Listener that tracks changes to the editor registry and does a full update
57      * when it changes, since many workbench adapters derive their icon from the file
58      * associations in the registry.
59      */

60     private IPropertyListener editorRegistryListener = new IPropertyListener() {
61         public void propertyChanged(Object JavaDoc source, int propId) {
62             if (propId == IEditorRegistry.PROP_CONTENTS) {
63                 fireLabelProviderChanged(new LabelProviderChangedEvent(WorkbenchLabelProvider.this));
64             }
65         }
66     };
67
68     /**
69      * Creates a new workbench label provider.
70      */

71     public WorkbenchLabelProvider() {
72         PlatformUI.getWorkbench().getEditorRegistry().addPropertyListener(editorRegistryListener);
73     }
74
75     /**
76      * Returns an image descriptor that is based on the given descriptor,
77      * but decorated with additional information relating to the state
78      * of the provided object.
79      *
80      * Subclasses may reimplement this method to decorate an object's
81      * image.
82      *
83      * @param input The base image to decorate.
84      * @param element The element used to look up decorations.
85      * @return the resuling ImageDescriptor.
86      * @see org.eclipse.jface.resource.CompositeImageDescriptor
87      */

88     protected ImageDescriptor decorateImage(ImageDescriptor input,
89             Object JavaDoc element) {
90         return input;
91     }
92
93     /**
94      * Returns a label that is based on the given label,
95      * but decorated with additional information relating to the state
96      * of the provided object.
97      *
98      * Subclasses may implement this method to decorate an object's
99      * label.
100      * @param input The base text to decorate.
101      * @param element The element used to look up decorations.
102      * @return the resulting text
103      */

104     protected String JavaDoc decorateText(String JavaDoc input, Object JavaDoc element) {
105         return input;
106     }
107
108     /* (non-Javadoc)
109      * Method declared on ILabelProvider
110      */

111     public void dispose() {
112         PlatformUI.getWorkbench().getEditorRegistry().removePropertyListener(editorRegistryListener);
113         super.dispose();
114     }
115     
116     /**
117      * Returns the implementation of IWorkbenchAdapter for the given
118      * object.
119      * @param o the object to look up.
120      * @return IWorkbenchAdapter or<code>null</code> if the adapter is not defined or the
121      * object is not adaptable.
122      */

123     protected final IWorkbenchAdapter getAdapter(Object JavaDoc o) {
124         return (IWorkbenchAdapter)Util.getAdapter(o, IWorkbenchAdapter.class);
125     }
126
127     /**
128      * Returns the implementation of IWorkbenchAdapter2 for the given
129      * object.
130      * @param o the object to look up.
131      * @return IWorkbenchAdapter2 or<code>null</code> if the adapter is not defined or the
132      * object is not adaptable.
133      */

134     protected final IWorkbenchAdapter2 getAdapter2(Object JavaDoc o) {
135         return (IWorkbenchAdapter2)Util.getAdapter(o, IWorkbenchAdapter2.class);
136     }
137
138     /* (non-Javadoc)
139      * Method declared on ILabelProvider
140      */

141     public final Image getImage(Object JavaDoc element) {
142         //obtain the base image by querying the element
143
IWorkbenchAdapter adapter = getAdapter(element);
144         if (adapter == null) {
145             return null;
146         }
147         ImageDescriptor descriptor = adapter.getImageDescriptor(element);
148         if (descriptor == null) {
149             return null;
150         }
151
152         //add any annotations to the image descriptor
153
descriptor = decorateImage(descriptor, element);
154
155         Image image = (Image) SWTResourceUtil.getImageTable().get(descriptor);
156         if (image == null) {
157             image = descriptor.createImage();
158             SWTResourceUtil.getImageTable().put(descriptor, image);
159         }
160         return image;
161     }
162
163     /* (non-Javadoc)
164      * Method declared on ILabelProvider
165      */

166     public final String JavaDoc getText(Object JavaDoc element) {
167         //query the element for its label
168
IWorkbenchAdapter adapter = getAdapter(element);
169         if (adapter == null) {
170             return ""; //$NON-NLS-1$
171
}
172         String JavaDoc label = adapter.getLabel(element);
173
174         //return the decorated label
175
return decorateText(label, element);
176     }
177
178     /* (non-Javadoc)
179      * @see org.eclipse.jface.viewers.IColorProvider#getForeground(java.lang.Object)
180      */

181     public Color getForeground(Object JavaDoc element) {
182         return getColor(element, true);
183     }
184
185     /* (non-Javadoc)
186      * @see org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object)
187      */

188     public Color getBackground(Object JavaDoc element) {
189         return getColor(element, false);
190     }
191
192     /* (non-Javadoc)
193      * @see org.eclipse.jface.viewers.IFontProvider#getFont(java.lang.Object)
194      */

195     public Font getFont(Object JavaDoc element) {
196         IWorkbenchAdapter2 adapter = getAdapter2(element);
197         if (adapter == null) {
198             return null;
199         }
200
201         FontData descriptor = adapter.getFont(element);
202         if (descriptor == null) {
203             return null;
204         }
205
206         Font font = (Font) SWTResourceUtil.getFontTable().get(descriptor);
207         if (font == null) {
208             font = new Font(Display.getCurrent(), descriptor);
209             SWTResourceUtil.getFontTable().put(descriptor, font);
210         }
211         return font;
212     }
213
214     private Color getColor(Object JavaDoc element, boolean forground) {
215         IWorkbenchAdapter2 adapter = getAdapter2(element);
216         if (adapter == null) {
217             return null;
218         }
219         RGB descriptor = forground ? adapter.getForeground(element) : adapter
220                 .getBackground(element);
221         if (descriptor == null) {
222             return null;
223         }
224
225         Color color = (Color) SWTResourceUtil.getColorTable().get(descriptor);
226         if (color == null) {
227             color = new Color(Display.getCurrent(), descriptor);
228             SWTResourceUtil.getColorTable().put(descriptor, color);
229         }
230         return color;
231     }
232 }
233
Popular Tags