KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > NavigatorContentServiceLabelProvider


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.internal.navigator;
12
13
14 import java.util.Iterator JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.core.commands.common.EventManager;
18 import org.eclipse.core.runtime.SafeRunner;
19 import org.eclipse.jface.util.SafeRunnable;
20 import org.eclipse.jface.viewers.IColorProvider;
21 import org.eclipse.jface.viewers.IFontProvider;
22 import org.eclipse.jface.viewers.ILabelProvider;
23 import org.eclipse.jface.viewers.ILabelProviderListener;
24 import org.eclipse.jface.viewers.ITreePathLabelProvider;
25 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
26 import org.eclipse.jface.viewers.TreePath;
27 import org.eclipse.jface.viewers.ViewerLabel;
28 import org.eclipse.osgi.util.NLS;
29 import org.eclipse.swt.graphics.Color;
30 import org.eclipse.swt.graphics.Font;
31 import org.eclipse.swt.graphics.Image;
32 import org.eclipse.ui.internal.navigator.extensions.NavigatorContentExtension;
33 import org.eclipse.ui.navigator.CommonViewer;
34 import org.eclipse.ui.navigator.INavigatorContentDescriptor;
35 import org.eclipse.ui.navigator.INavigatorContentService;
36
37 /**
38  * <p>
39  * Provides relevant labels based on the associated
40  * {@link INavigatorContentService}for the contents of a
41  * TreeViewer .
42  * <p>
43  *
44  * <p>
45  * Except for the dependency on
46  * {@link INavigatorContentService}, this class has no
47  * dependencies on the rest of the Common Navigator framework. Tree viewers that would like to use
48  * the extensions defined by the Common Navigator, without using the actual view part or other
49  * pieces of functionality (filters, sorting, etc) may choose to use this class, in effect using an
50  * extensible label provider.
51  * </p>
52  * <p>
53  * <strong>EXPERIMENTAL</strong>. This class or interface has been added as part of a work in
54  * progress. There is a guarantee neither that this API will work nor that it will remain the same.
55  * Please do not use this API without consulting with the Platform/UI team.
56  * </p>
57  *
58  * @since 3.2
59  *
60  * @see org.eclipse.ui.internal.navigator.NavigatorContentService
61  * @see org.eclipse.ui.internal.navigator.NavigatorContentServiceContentProvider
62  */

63 public class NavigatorContentServiceLabelProvider extends EventManager
64         implements ILabelProvider, IColorProvider, IFontProvider, ITreePathLabelProvider, ILabelProviderListener {
65  
66     private final NavigatorContentService contentService;
67     private final boolean isContentServiceSelfManaged;
68     private final ReusableViewerLabel reusableLabel = new ReusableViewerLabel();
69
70   
71     /**
72      * <p>
73      * Uses the supplied content service to acquire the available extensions.
74      * </p>
75      *
76      * @param aContentService
77      * The associated NavigatorContentService that should be used to acquire information.
78      */

79     public NavigatorContentServiceLabelProvider(NavigatorContentService aContentService) {
80         contentService = aContentService;
81         isContentServiceSelfManaged = false;
82     }
83
84     /**
85      * <p>
86      * Return the appropriate image for anElement. The image will be used as the icon for anElement
87      * when displayed in the tree viewer. This method uses information from its contentService to
88      * know what extensions to use to supply the correct label.
89      * </p>
90      * {@inheritDoc}
91      *
92      * @param anElement
93      * An element from the Tree Viewer
94      * @return The Image that will be used as the icon when anElement is displayed in the viewer.
95      * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
96      */

97     public Image getImage(Object JavaDoc anElement) {
98
99         Set JavaDoc contentExtensions = contentService.findContentExtensionsWithPossibleChild(anElement);
100         Image image = null;
101         for (Iterator JavaDoc itr = contentExtensions.iterator(); itr.hasNext() && image == null; ) {
102             image = findImage((NavigatorContentExtension) itr.next(), anElement);
103         }
104         return image;
105     }
106
107     /**
108      * <p>
109      * Return a String representation of anElement to be used as the display name in the tree
110      * viewer.
111      * </p>
112      * {@inheritDoc}
113      *
114      * @param anElement
115      * An element from the Tree Viewer
116      * @return The String label to display for the object when represented in the viewer.
117      * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
118      */

119     public String JavaDoc getText(Object JavaDoc anElement) {
120         Set JavaDoc contentExtensions = contentService.findContentExtensionsWithPossibleChild(anElement);
121         String JavaDoc text = null;
122         for (Iterator JavaDoc itr = contentExtensions.iterator(); itr.hasNext() && text == null; ) {
123             text = findText((NavigatorContentExtension) itr.next(), anElement);
124         }
125         // decorate the element
126
return text == null ? (NLS.bind(CommonNavigatorMessages.NavigatorContentServiceLabelProvider_Error_no_label_provider_for_0_, anElement)) : text;
127     }
128     
129     /**
130      * Search for text label and take overrides into account.
131      * Uses only simple ITreeContentProvider.getParent() style semantics.
132      */

133     private String JavaDoc findText(NavigatorContentExtension foundExtension, Object JavaDoc anElement) {
134         String JavaDoc text = null;
135         INavigatorContentDescriptor foundDescriptor;
136         text = foundExtension.getLabelProvider().getText(anElement);
137         if(text == null && (foundDescriptor = foundExtension.getDescriptor()).getOverriddenDescriptor() != null) {
138             return findText(contentService.getExtension(foundDescriptor.getOverriddenDescriptor()), anElement);
139         }
140         return text;
141     }
142     
143     /**
144      * Search for image and take overrides into account.
145      * Uses only simple ITreeContentProvider.getParent() style semantics.
146      */

147     private Image findImage(NavigatorContentExtension foundExtension, Object JavaDoc anElement) {
148         Image image = null;
149         INavigatorContentDescriptor foundDescriptor;
150         image = foundExtension.getLabelProvider().getImage(anElement);
151         if(image == null && (foundDescriptor = foundExtension.getDescriptor()).getOverriddenDescriptor() != null) {
152             return findImage(contentService.getExtension(foundDescriptor.getOverriddenDescriptor()), anElement);
153         }
154         return image;
155     }
156     
157     /* (non-Javadoc)
158      * @see org.eclipse.jface.viewers.IFontProvider#getFont(java.lang.Object)
159      */

160     public Font getFont(Object JavaDoc anElement) {
161         ILabelProvider[] labelProviders = contentService.findRelevantLabelProviders(anElement);
162         for (int i = 0; i < labelProviders.length; i++) {
163             ILabelProvider provider = labelProviders[i];
164             if (provider instanceof IFontProvider) {
165                 IFontProvider fontProvider = (IFontProvider) provider;
166                 Font font = fontProvider.getFont(anElement);
167                 if (font != null) {
168                     return font;
169                 }
170             }
171         }
172         return null;
173     }
174
175     /* (non-Javadoc)
176      * @see org.eclipse.jface.viewers.IColorProvider#getForeground(java.lang.Object)
177      */

178     public Color getForeground(Object JavaDoc anElement) {
179         ILabelProvider[] labelProviders = contentService.findRelevantLabelProviders(anElement);
180         for (int i = 0; i < labelProviders.length; i++) {
181             ILabelProvider provider = labelProviders[i];
182             if (provider instanceof IColorProvider) {
183                 IColorProvider colorProvider = (IColorProvider) provider;
184                 Color color = colorProvider.getForeground(anElement);
185                 if (color != null) {
186                     return color;
187                 }
188             }
189         }
190         return null;
191     }
192
193     /* (non-Javadoc)
194      * @see org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object)
195      */

196     public Color getBackground(Object JavaDoc anElement) {
197         ILabelProvider[] labelProviders = contentService.findRelevantLabelProviders(anElement);
198         for (int i = 0; i < labelProviders.length; i++) {
199             ILabelProvider provider = labelProviders[i];
200             if (provider instanceof IColorProvider) {
201                 IColorProvider colorProvider = (IColorProvider) provider;
202                 Color color = colorProvider.getBackground(anElement);
203                 if (color != null) {
204                     return color;
205                 }
206             }
207         }
208         return null;
209     }
210
211     /**
212      * <p>
213      * Indicates whether anElelement has aProperty that affects the display of the label.
214      * </p>
215      * {@inheritDoc}
216      *
217      * @param anElement
218      * An element from the Tree Viewer
219      * @param aProperty
220      * A property of the given element that could be a label provider
221      * @return True if any of the extensions enabled on anElement consider aProperty a
222      * label-changing property.
223      * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object,
224      * java.lang.String)
225      */

226     public boolean isLabelProperty(Object JavaDoc anElement, String JavaDoc aProperty) {
227         boolean result = false;
228         ILabelProvider[] labelProviders = contentService.findRelevantLabelProviders(anElement);
229         for (int i = 0; i < labelProviders.length && !result; i++) {
230             result = labelProviders[i].isLabelProperty(anElement, aProperty);
231         }
232         return result;
233     }
234
235
236     /**
237      * <p>
238      * Label provider listeners are currently supported.
239      * </p>
240      *
241      * {@inheritDoc}
242      * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener)
243      */

244     public void addListener(ILabelProviderListener aListener) {
245         addListenerObject(aListener);
246     }
247
248     /**
249      * <p>
250      * Label provider listeners are currently supported.
251      * </p>
252      *
253      * {@inheritDoc}
254      * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener)
255      */

256     public void removeListener(ILabelProviderListener aListener) {
257         removeListenerObject(aListener);
258     }
259
260     /**
261      * <p>
262      * Dispose of the content service, if it was created and not supplied.
263      * </p>
264      * <p>
265      * <b>If a client uses this class outside of the framework of {@link CommonViewer}, this method
266      * must be called when finished. </b>
267      * </p>
268      *
269      * {@inheritDoc}
270      * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
271      */

272     public void dispose() {
273         if (isContentServiceSelfManaged) {
274             contentService.dispose();
275         }
276
277     }
278     
279     /**
280      * Fires a label provider changed event to all registered listeners
281      * Only listeners registered at the time this method is called are notified.
282      *
283      * @param event a label provider changed event
284      *
285      * @see ILabelProviderListener#labelProviderChanged
286      */

287     protected void fireLabelProviderChanged(
288             final LabelProviderChangedEvent event) {
289         Object JavaDoc[] theListeners = getListeners();
290         for (int i = 0; i < theListeners.length; ++i) {
291             final ILabelProviderListener l = (ILabelProviderListener) theListeners[i];
292             SafeRunner.run(new SafeRunnable() {
293                 public void run() {
294                     l.labelProviderChanged(event);
295                 }
296             });
297
298         }
299     }
300
301     /* (non-Javadoc)
302      * @see org.eclipse.jface.viewers.ITreePathLabelProvider#updateLabel(org.eclipse.jface.viewers.ViewerLabel, org.eclipse.jface.viewers.TreePath)
303      */

304     public void updateLabel(ViewerLabel label, TreePath elementPath) {
305          
306         Set JavaDoc contentExtensions = contentService.findContentExtensionsWithPossibleChild(elementPath.getLastSegment());
307         reusableLabel.reset(label);
308         for (Iterator JavaDoc itr = contentExtensions.iterator(); itr.hasNext() && !(reusableLabel.isValid() && reusableLabel.hasChanged()); ) {
309             findUpdateLabel((NavigatorContentExtension)itr.next(), reusableLabel, elementPath);
310         }
311         reusableLabel.fill(label);
312     }
313
314
315     /**
316      * Search for text label and take overrides into account.
317      * Uses only simple ITreeContentProvider.getParent() style semantics.
318      */

319     private void findUpdateLabel(NavigatorContentExtension foundExtension, ReusableViewerLabel label, TreePath elementPath) {
320         
321         ILabelProvider labelProvider = foundExtension.getLabelProvider();
322         if (labelProvider instanceof ITreePathLabelProvider) {
323             ITreePathLabelProvider tplp = (ITreePathLabelProvider) labelProvider;
324             tplp.updateLabel(label, elementPath);
325         } else {
326             label.setImage(labelProvider.getImage(elementPath.getLastSegment()));
327             label.setText(labelProvider.getText(elementPath.getLastSegment()));
328         }
329          
330         if(shouldContinue(label, foundExtension)) {
331             findUpdateLabel(contentService.getExtension(foundExtension.getDescriptor().getOverriddenDescriptor()), label, elementPath);
332         }
333     }
334  
335     private boolean shouldContinue(ReusableViewerLabel label, NavigatorContentExtension foundExtension) {
336
337         if(foundExtension.getDescriptor().getOverriddenDescriptor() != null) {
338             return !(label.isValid() && label.hasChanged());
339         }
340         return false;
341     }
342
343     /* (non-Javadoc)
344      * @see org.eclipse.jface.viewers.ILabelProviderListener#labelProviderChanged(org.eclipse.jface.viewers.LabelProviderChangedEvent)
345      */

346     public void labelProviderChanged(LabelProviderChangedEvent event) {
347         fireLabelProviderChanged(event);
348     }
349
350 }
351
Popular Tags