001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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 org.eclipse.jface.resource.ImageDescriptor;
013: import org.eclipse.jface.viewers.DecoratingLabelProvider;
014: import org.eclipse.jface.viewers.IColorProvider;
015: import org.eclipse.jface.viewers.IFontProvider;
016: import org.eclipse.jface.viewers.ILabelProvider;
017: import org.eclipse.jface.viewers.LabelProvider;
018: import org.eclipse.jface.viewers.LabelProviderChangedEvent;
019: import org.eclipse.swt.graphics.Color;
020: import org.eclipse.swt.graphics.Font;
021: import org.eclipse.swt.graphics.FontData;
022: import org.eclipse.swt.graphics.Image;
023: import org.eclipse.swt.graphics.RGB;
024: import org.eclipse.swt.widgets.Display;
025: import org.eclipse.ui.IEditorRegistry;
026: import org.eclipse.ui.IPropertyListener;
027: import org.eclipse.ui.PlatformUI;
028: import org.eclipse.ui.internal.util.SWTResourceUtil;
029: import org.eclipse.ui.internal.util.Util;
030:
031: /**
032: * Provides basic labels for adaptable objects that have the
033: * <code>IWorkbenchAdapter</code> adapter associated with them. All dispensed
034: * images are cached until the label provider is explicitly disposed.
035: * This class provides a facility for subclasses to define annotations
036: * on the labels and icons of adaptable objects.
037: */
038: public class WorkbenchLabelProvider extends LabelProvider implements
039: IColorProvider, IFontProvider {
040:
041: /**
042: * Returns a workbench label provider that is hooked up to the decorator
043: * mechanism.
044: *
045: * @return a new <code>DecoratingLabelProvider</code> which wraps a <code>
046: * new <code>WorkbenchLabelProvider</code>
047: */
048: public static ILabelProvider getDecoratingWorkbenchLabelProvider() {
049: return new DecoratingLabelProvider(
050: new WorkbenchLabelProvider(), PlatformUI.getWorkbench()
051: .getDecoratorManager().getLabelDecorator());
052: }
053:
054: /**
055: * Listener that tracks changes to the editor registry and does a full update
056: * when it changes, since many workbench adapters derive their icon from the file
057: * associations in the registry.
058: */
059: private IPropertyListener editorRegistryListener = new IPropertyListener() {
060: public void propertyChanged(Object source, int propId) {
061: if (propId == IEditorRegistry.PROP_CONTENTS) {
062: fireLabelProviderChanged(new LabelProviderChangedEvent(
063: WorkbenchLabelProvider.this ));
064: }
065: }
066: };
067:
068: /**
069: * Creates a new workbench label provider.
070: */
071: public WorkbenchLabelProvider() {
072: PlatformUI.getWorkbench().getEditorRegistry()
073: .addPropertyListener(editorRegistryListener);
074: }
075:
076: /**
077: * Returns an image descriptor that is based on the given descriptor,
078: * but decorated with additional information relating to the state
079: * of the provided object.
080: *
081: * Subclasses may reimplement this method to decorate an object's
082: * image.
083: *
084: * @param input The base image to decorate.
085: * @param element The element used to look up decorations.
086: * @return the resuling ImageDescriptor.
087: * @see org.eclipse.jface.resource.CompositeImageDescriptor
088: */
089: protected ImageDescriptor decorateImage(ImageDescriptor input,
090: Object element) {
091: return input;
092: }
093:
094: /**
095: * Returns a label that is based on the given label,
096: * but decorated with additional information relating to the state
097: * of the provided object.
098: *
099: * Subclasses may implement this method to decorate an object's
100: * label.
101: * @param input The base text to decorate.
102: * @param element The element used to look up decorations.
103: * @return the resulting text
104: */
105: protected String decorateText(String input, Object element) {
106: return input;
107: }
108:
109: /* (non-Javadoc)
110: * Method declared on ILabelProvider
111: */
112: public void dispose() {
113: PlatformUI.getWorkbench().getEditorRegistry()
114: .removePropertyListener(editorRegistryListener);
115: super .dispose();
116: }
117:
118: /**
119: * Returns the implementation of IWorkbenchAdapter for the given
120: * object.
121: * @param o the object to look up.
122: * @return IWorkbenchAdapter or<code>null</code> if the adapter is not defined or the
123: * object is not adaptable.
124: */
125: protected final IWorkbenchAdapter getAdapter(Object o) {
126: return (IWorkbenchAdapter) Util.getAdapter(o,
127: IWorkbenchAdapter.class);
128: }
129:
130: /**
131: * Returns the implementation of IWorkbenchAdapter2 for the given
132: * object.
133: * @param o the object to look up.
134: * @return IWorkbenchAdapter2 or<code>null</code> if the adapter is not defined or the
135: * object is not adaptable.
136: */
137: protected final IWorkbenchAdapter2 getAdapter2(Object o) {
138: return (IWorkbenchAdapter2) Util.getAdapter(o,
139: IWorkbenchAdapter2.class);
140: }
141:
142: /* (non-Javadoc)
143: * Method declared on ILabelProvider
144: */
145: public final Image getImage(Object element) {
146: //obtain the base image by querying the element
147: IWorkbenchAdapter adapter = getAdapter(element);
148: if (adapter == null) {
149: return null;
150: }
151: ImageDescriptor descriptor = adapter
152: .getImageDescriptor(element);
153: if (descriptor == null) {
154: return null;
155: }
156:
157: //add any annotations to the image descriptor
158: descriptor = decorateImage(descriptor, element);
159:
160: Image image = (Image) SWTResourceUtil.getImageTable().get(
161: descriptor);
162: if (image == null) {
163: image = descriptor.createImage();
164: SWTResourceUtil.getImageTable().put(descriptor, image);
165: }
166: return image;
167: }
168:
169: /* (non-Javadoc)
170: * Method declared on ILabelProvider
171: */
172: public final String getText(Object element) {
173: //query the element for its label
174: IWorkbenchAdapter adapter = getAdapter(element);
175: if (adapter == null) {
176: return ""; //$NON-NLS-1$
177: }
178: String label = adapter.getLabel(element);
179:
180: //return the decorated label
181: return decorateText(label, element);
182: }
183:
184: /* (non-Javadoc)
185: * @see org.eclipse.jface.viewers.IColorProvider#getForeground(java.lang.Object)
186: */
187: public Color getForeground(Object element) {
188: return getColor(element, true);
189: }
190:
191: /* (non-Javadoc)
192: * @see org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object)
193: */
194: public Color getBackground(Object element) {
195: return getColor(element, false);
196: }
197:
198: /* (non-Javadoc)
199: * @see org.eclipse.jface.viewers.IFontProvider#getFont(java.lang.Object)
200: */
201: public Font getFont(Object element) {
202: IWorkbenchAdapter2 adapter = getAdapter2(element);
203: if (adapter == null) {
204: return null;
205: }
206:
207: FontData descriptor = adapter.getFont(element);
208: if (descriptor == null) {
209: return null;
210: }
211:
212: Font font = (Font) SWTResourceUtil.getFontTable().get(
213: descriptor);
214: if (font == null) {
215: font = new Font(Display.getCurrent(), descriptor);
216: SWTResourceUtil.getFontTable().put(descriptor, font);
217: }
218: return font;
219: }
220:
221: private Color getColor(Object element, boolean forground) {
222: IWorkbenchAdapter2 adapter = getAdapter2(element);
223: if (adapter == null) {
224: return null;
225: }
226: RGB descriptor = forground ? adapter.getForeground(element)
227: : adapter.getBackground(element);
228: if (descriptor == null) {
229: return null;
230: }
231:
232: Color color = (Color) SWTResourceUtil.getColorTable().get(
233: descriptor);
234: if (color == null) {
235: color = new Color(Display.getCurrent(), descriptor);
236: SWTResourceUtil.getColorTable().put(descriptor, color);
237: }
238: return color;
239: }
240: }
|