KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > DecoratingLabelProvider


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.jface.viewers;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.ListenerList;
15 import org.eclipse.swt.graphics.Color;
16 import org.eclipse.swt.graphics.Font;
17 import org.eclipse.swt.graphics.Image;
18
19 /**
20  * A decorating label provider is a label provider which combines
21  * a nested label provider and an optional decorator.
22  * The decorator decorates the label text, image, font and colors provided by
23  * the nested label provider.
24  */

25 public class DecoratingLabelProvider extends LabelProvider implements
26         ILabelProvider, IViewerLabelProvider, IColorProvider, IFontProvider, ITreePathLabelProvider {
27         
28     private ILabelProvider provider;
29
30     private ILabelDecorator decorator;
31
32     // Need to keep our own list of listeners
33
private ListenerList listeners = new ListenerList();
34
35     private IDecorationContext decorationContext = DecorationContext.DEFAULT_CONTEXT;
36
37     /**
38      * Creates a decorating label provider which uses the given label decorator
39      * to decorate labels provided by the given label provider.
40      *
41      * @param provider the nested label provider
42      * @param decorator the label decorator, or <code>null</code> if no decorator is to be used initially
43      */

44     public DecoratingLabelProvider(ILabelProvider provider,
45             ILabelDecorator decorator) {
46         Assert.isNotNull(provider);
47         this.provider = provider;
48         this.decorator = decorator;
49     }
50
51     /**
52      * The <code>DecoratingLabelProvider</code> implementation of this <code>IBaseLabelProvider</code> method
53      * adds the listener to both the nested label provider and the label decorator.
54      *
55      * @param listener a label provider listener
56      */

57     public void addListener(ILabelProviderListener listener) {
58         super.addListener(listener);
59         provider.addListener(listener);
60         if (decorator != null) {
61             decorator.addListener(listener);
62         }
63         listeners.add(listener);
64     }
65
66     /**
67      * The <code>DecoratingLabelProvider</code> implementation of this <code>IBaseLabelProvider</code> method
68      * disposes both the nested label provider and the label decorator.
69      */

70     public void dispose() {
71         provider.dispose();
72         if (decorator != null) {
73             decorator.dispose();
74         }
75     }
76
77     /**
78      * The <code>DecoratingLabelProvider</code> implementation of this
79      * <code>ILabelProvider</code> method returns the image provided
80      * by the nested label provider's <code>getImage</code> method,
81      * decorated with the decoration provided by the label decorator's
82      * <code>decorateImage</code> method.
83      */

84     public Image getImage(Object JavaDoc element) {
85         Image image = provider.getImage(element);
86         if (decorator != null) {
87             if (decorator instanceof LabelDecorator) {
88                 LabelDecorator ld2 = (LabelDecorator) decorator;
89                 Image decorated = ld2.decorateImage(image, element, getDecorationContext());
90                 if (decorated != null) {
91                     return decorated;
92                 }
93             } else {
94                 Image decorated = decorator.decorateImage(image, element);
95                 if (decorated != null) {
96                     return decorated;
97                 }
98             }
99         }
100         return image;
101     }
102
103     /**
104      * Returns the label decorator, or <code>null</code> if none has been set.
105      *
106      * @return the label decorator, or <code>null</code> if none has been set.
107      */

108     public ILabelDecorator getLabelDecorator() {
109         return decorator;
110     }
111
112     /**
113      * Returns the nested label provider.
114      *
115      * @return the nested label provider
116      */

117     public ILabelProvider getLabelProvider() {
118         return provider;
119     }
120
121     /**
122      * The <code>DecoratingLabelProvider</code> implementation of this
123      * <code>ILabelProvider</code> method returns the text label provided
124      * by the nested label provider's <code>getText</code> method,
125      * decorated with the decoration provided by the label decorator's
126      * <code>decorateText</code> method.
127      */

128     public String JavaDoc getText(Object JavaDoc element) {
129         String JavaDoc text = provider.getText(element);
130         if (decorator != null) {
131             if (decorator instanceof LabelDecorator) {
132                 LabelDecorator ld2 = (LabelDecorator) decorator;
133                 String JavaDoc decorated = ld2.decorateText(text, element, getDecorationContext());
134                 if (decorated != null) {
135                     return decorated;
136                 }
137             } else {
138                 String JavaDoc decorated = decorator.decorateText(text, element);
139                 if (decorated != null) {
140                     return decorated;
141                 }
142             }
143         }
144         return text;
145     }
146
147     /**
148      * The <code>DecoratingLabelProvider</code> implementation of this
149      * <code>IBaseLabelProvider</code> method returns <code>true</code> if the corresponding method
150      * on the nested label provider returns <code>true</code> or if the corresponding method on the
151      * decorator returns <code>true</code>.
152      */

153     public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {
154         if (provider.isLabelProperty(element, property)) {
155             return true;
156         }
157         if (decorator != null && decorator.isLabelProperty(element, property)) {
158             return true;
159         }
160         return false;
161     }
162
163     /**
164      * The <code>DecoratingLabelProvider</code> implementation of this <code>IBaseLabelProvider</code> method
165      * removes the listener from both the nested label provider and the label decorator.
166      *
167      * @param listener a label provider listener
168      */

169     public void removeListener(ILabelProviderListener listener) {
170         super.removeListener(listener);
171         provider.removeListener(listener);
172         if (decorator != null) {
173             decorator.removeListener(listener);
174         }
175         listeners.remove(listener);
176     }
177
178     /**
179      * Sets the label decorator.
180      * Removes all known listeners from the old decorator, and adds all known listeners to the new decorator.
181      * The old decorator is not disposed.
182      * Fires a label provider changed event indicating that all labels should be updated.
183      * Has no effect if the given decorator is identical to the current one.
184      *
185      * @param decorator the label decorator, or <code>null</code> if no decorations are to be applied
186      */

187     public void setLabelDecorator(ILabelDecorator decorator) {
188         ILabelDecorator oldDecorator = this.decorator;
189         if (oldDecorator != decorator) {
190             Object JavaDoc[] listenerList = this.listeners.getListeners();
191             if (oldDecorator != null) {
192                 for (int i = 0; i < listenerList.length; ++i) {
193                     oldDecorator
194                             .removeListener((ILabelProviderListener) listenerList[i]);
195                 }
196             }
197             this.decorator = decorator;
198             if (decorator != null) {
199                 for (int i = 0; i < listenerList.length; ++i) {
200                     decorator
201                             .addListener((ILabelProviderListener) listenerList[i]);
202                 }
203             }
204             fireLabelProviderChanged(new LabelProviderChangedEvent(this));
205         }
206     }
207
208
209     /*
210      * (non-Javadoc)
211      * @see org.eclipse.jface.viewers.IViewerLabelProvider#updateLabel(org.eclipse.jface.viewers.ViewerLabel, java.lang.Object)
212      */

213     public void updateLabel(ViewerLabel settings, Object JavaDoc element) {
214
215         ILabelDecorator currentDecorator = getLabelDecorator();
216         String JavaDoc oldText = settings.getText();
217         boolean decorationReady = true;
218         if (currentDecorator instanceof IDelayedLabelDecorator) {
219             IDelayedLabelDecorator delayedDecorator = (IDelayedLabelDecorator) currentDecorator;
220             if (!delayedDecorator.prepareDecoration(element, oldText)) {
221                 // The decoration is not ready but has been queued for processing
222
decorationReady = false;
223             }
224         }
225         // update icon and label
226

227         if (decorationReady || oldText == null
228                 || settings.getText().length() == 0) {
229             settings.setText(getText(element));
230         }
231
232         Image oldImage = settings.getImage();
233         if (decorationReady || oldImage == null) {
234             settings.setImage(getImage(element));
235         }
236  
237         if(decorationReady) {
238             updateForDecorationReady(settings,element);
239         }
240
241     }
242
243     /**
244      * Decoration is ready. Update anything else for the settings.
245      * @param settings The object collecting the settings.
246      * @param element The Object being decorated.
247      * @since 3.1
248      */

249     protected void updateForDecorationReady(ViewerLabel settings, Object JavaDoc element) {
250         
251         if(decorator instanceof IColorDecorator){
252             IColorDecorator colorDecorator = (IColorDecorator) decorator;
253             settings.setBackground(colorDecorator.decorateBackground(element));
254             settings.setForeground(colorDecorator.decorateForeground(element));
255         }
256         
257         if(decorator instanceof IFontDecorator) {
258             settings.setFont(((IFontDecorator) decorator).decorateFont(element));
259         }
260         
261     }
262     
263     /* (non-Javadoc)
264      * @see org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object)
265      */

266     public Color getBackground(Object JavaDoc element) {
267         if(provider instanceof IColorProvider) {
268             return ((IColorProvider) provider).getBackground(element);
269         }
270         return null;
271     }
272     
273     /* (non-Javadoc)
274      * @see org.eclipse.jface.viewers.IFontProvider#getFont(java.lang.Object)
275      */

276     public Font getFont(Object JavaDoc element) {
277         if(provider instanceof IFontProvider) {
278             return ((IFontProvider) provider).getFont(element);
279         }
280         return null;
281     }
282     
283     /* (non-Javadoc)
284      * @see org.eclipse.jface.viewers.IColorProvider#getForeground(java.lang.Object)
285      */

286     public Color getForeground(Object JavaDoc element) {
287         if(provider instanceof IColorProvider) {
288             return ((IColorProvider) provider).getForeground(element);
289         }
290         return null;
291     }
292
293     /**
294      * Return the decoration context associated with this label provider.
295      * It will be passed to the decorator if the decorator is an
296      * instance of {@link LabelDecorator}.
297      * @return the decoration context associated with this label provider
298      *
299      * @since 3.2
300      */

301     public IDecorationContext getDecorationContext() {
302         return decorationContext;
303     }
304     
305     /**
306      * Set the decoration context that will be based to the decorator
307      * for this label provider if that decorator implements {@link LabelDecorator}.
308      * @param decorationContext the decoration context.
309      *
310      * @since 3.2
311      */

312     public void setDecorationContext(IDecorationContext decorationContext) {
313         Assert.isNotNull(decorationContext);
314         this.decorationContext = decorationContext;
315     }
316
317     /* (non-Javadoc)
318      * @see org.eclipse.jface.viewers.ITreePathLabelProvider#updateLabel(org.eclipse.jface.viewers.ViewerLabel, org.eclipse.jface.viewers.TreePath)
319      */

320     public void updateLabel(ViewerLabel settings, TreePath elementPath) {
321         ILabelDecorator currentDecorator = getLabelDecorator();
322         String JavaDoc oldText = settings.getText();
323         Object JavaDoc element = elementPath.getLastSegment();
324         boolean decorationReady = true;
325         if (currentDecorator instanceof LabelDecorator) {
326             LabelDecorator labelDecorator = (LabelDecorator) currentDecorator;
327            if (!labelDecorator.prepareDecoration(element, oldText, getDecorationContext())) {
328                 // The decoration is not ready but has been queued for processing
329
decorationReady = false;
330             }
331         } else if (currentDecorator instanceof IDelayedLabelDecorator) {
332             IDelayedLabelDecorator delayedDecorator = (IDelayedLabelDecorator) currentDecorator;
333             if (!delayedDecorator.prepareDecoration(element, oldText)) {
334                 // The decoration is not ready but has been queued for processing
335
decorationReady = false;
336             }
337         }
338         settings.setHasPendingDecorations(!decorationReady);
339         // update icon and label
340

341         if (provider instanceof ITreePathLabelProvider) {
342             ITreePathLabelProvider pprov = (ITreePathLabelProvider) provider;
343             if (decorationReady || oldText == null
344                     || settings.getText().length() == 0) {
345                 pprov.updateLabel(settings, elementPath);
346                 decorateSettings(settings, elementPath);
347             }
348         } else {
349             if (decorationReady || oldText == null
350                     || settings.getText().length() == 0) {
351                 settings.setText(getText(element));
352             }
353     
354             Image oldImage = settings.getImage();
355             if (decorationReady || oldImage == null) {
356                 settings.setImage(getImage(element));
357             }
358      
359             if(decorationReady) {
360                 updateForDecorationReady(settings,element);
361             }
362         }
363
364     }
365
366     /**
367      * Decorate the settings
368      * @param settings the settings obtained from the label provider
369      * @param elementPath the element path being decorated
370      */

371     private void decorateSettings(ViewerLabel settings, TreePath elementPath) {
372         Object JavaDoc element = elementPath.getLastSegment();
373         if (decorator != null) {
374             if (decorator instanceof LabelDecorator) {
375                 LabelDecorator labelDecorator = (LabelDecorator) decorator;
376                 String JavaDoc text = labelDecorator.decorateText(settings.getText(), element, getDecorationContext());
377                 if (text != null && text.length() > 0)
378                     settings.setText(text);
379                 Image image = labelDecorator.decorateImage(settings.getImage(), element, getDecorationContext());
380                 if (image != null)
381                     settings.setImage(image);
382                 
383             } else {
384                 String JavaDoc text = decorator.decorateText(settings.getText(), element);
385                 if (text != null && text.length() > 0)
386                     settings.setText(text);
387                 Image image = decorator.decorateImage(settings.getImage(), element);
388                 if (image != null)
389                     settings.setImage(image);
390             }
391             if(decorator instanceof IColorDecorator){
392                 IColorDecorator colorDecorator = (IColorDecorator) decorator;
393                 Color background = colorDecorator.decorateBackground(element);
394                 if (background != null)
395                     settings.setBackground(background);
396                 Color foreground = colorDecorator.decorateForeground(element);
397                 if (foreground != null)
398                     settings.setForeground(foreground);
399             }
400             
401             if(decorator instanceof IFontDecorator) {
402                 Font font = ((IFontDecorator) decorator).decorateFont(element);
403                 if (font != null)
404                     settings.setFont(font);
405             }
406         }
407     }
408 }
409
Popular Tags