KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > viewsupport > JavaUILabelProvider


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.jdt.internal.ui.viewsupport;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.ListenerList;
16 import org.eclipse.core.runtime.SafeRunner;
17
18 import org.eclipse.core.resources.IStorage;
19
20 import org.eclipse.swt.graphics.Color;
21 import org.eclipse.swt.graphics.Image;
22
23 import org.eclipse.jface.util.SafeRunnable;
24 import org.eclipse.jface.viewers.IColorProvider;
25 import org.eclipse.jface.viewers.ILabelDecorator;
26 import org.eclipse.jface.viewers.ILabelProvider;
27 import org.eclipse.jface.viewers.ILabelProviderListener;
28 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
29
30 import org.eclipse.jdt.ui.JavaElementLabels;
31
32 public class JavaUILabelProvider implements ILabelProvider, IColorProvider, IRichLabelProvider {
33     
34     protected ListenerList fListeners = new ListenerList();
35     
36     protected JavaElementImageProvider fImageLabelProvider;
37     protected StorageLabelProvider fStorageLabelProvider;
38     
39     private ArrayList JavaDoc fLabelDecorators;
40
41     private int fImageFlags;
42     private long fTextFlags;
43
44     /**
45      * Creates a new label provider with default flags.
46      */

47     public JavaUILabelProvider() {
48         this(JavaElementLabels.ALL_DEFAULT, JavaElementImageProvider.OVERLAY_ICONS);
49     }
50
51     /**
52      * @param textFlags Flags defined in <code>JavaElementLabels</code>.
53      * @param imageFlags Flags defined in <code>JavaElementImageProvider</code>.
54      */

55     public JavaUILabelProvider(long textFlags, int imageFlags) {
56         fImageLabelProvider= new JavaElementImageProvider();
57         fLabelDecorators= null;
58         
59         fStorageLabelProvider= new StorageLabelProvider();
60         fImageFlags= imageFlags;
61         fTextFlags= textFlags;
62     }
63     
64     /**
65      * Adds a decorator to the label provider
66      * @param decorator the decorator to add
67      */

68     public void addLabelDecorator(ILabelDecorator decorator) {
69         if (fLabelDecorators == null) {
70             fLabelDecorators= new ArrayList JavaDoc(2);
71         }
72         fLabelDecorators.add(decorator);
73     }
74     
75     /**
76      * Sets the textFlags.
77      * @param textFlags The textFlags to set
78      */

79     public final void setTextFlags(long textFlags) {
80         fTextFlags= textFlags;
81     }
82
83     /**
84      * Sets the imageFlags
85      * @param imageFlags The imageFlags to set
86      */

87     public final void setImageFlags(int imageFlags) {
88         fImageFlags= imageFlags;
89     }
90     
91     /**
92      * Gets the image flags.
93      * Can be overwritten by super classes.
94      * @return Returns a int
95      */

96     public final int getImageFlags() {
97         return fImageFlags;
98     }
99
100     /**
101      * Gets the text flags.
102      * @return Returns a int
103      */

104     public final long getTextFlags() {
105         return fTextFlags;
106     }
107     
108     /**
109      * Evaluates the image flags for a element.
110      * Can be overwritten by super classes.
111      * @param element the element to compute the image flags for
112      * @return Returns a int
113      */

114     protected int evaluateImageFlags(Object JavaDoc element) {
115         return getImageFlags();
116     }
117
118     /**
119      * Evaluates the text flags for a element. Can be overwritten by super classes.
120      * @param element the element to compute the text flags for
121      * @return Returns a int
122      */

123     protected long evaluateTextFlags(Object JavaDoc element) {
124         return getTextFlags();
125     }
126     
127     protected Image decorateImage(Image image, Object JavaDoc element) {
128         if (fLabelDecorators != null && image != null) {
129             for (int i= 0; i < fLabelDecorators.size(); i++) {
130                 ILabelDecorator decorator= (ILabelDecorator) fLabelDecorators.get(i);
131                 image= decorator.decorateImage(image, element);
132             }
133         }
134         return image;
135     }
136
137     /* (non-Javadoc)
138      * @see ILabelProvider#getImage
139      */

140     public Image getImage(Object JavaDoc element) {
141         Image result= fImageLabelProvider.getImageLabel(element, evaluateImageFlags(element));
142         if (result == null && (element instanceof IStorage)) {
143             result= fStorageLabelProvider.getImage(element);
144         }
145         
146         return decorateImage(result, element);
147     }
148
149     protected String JavaDoc decorateText(String JavaDoc text, Object JavaDoc element) {
150         if (fLabelDecorators != null && text.length() > 0) {
151             for (int i= 0; i < fLabelDecorators.size(); i++) {
152                 ILabelDecorator decorator= (ILabelDecorator) fLabelDecorators.get(i);
153                 String JavaDoc decorated= decorator.decorateText(text, element);
154                 if (decorated != null) {
155                     text= decorated;
156                 }
157             }
158         }
159         return text;
160     }
161
162
163     /* (non-Javadoc)
164      * @see ILabelProvider#getText
165      */

166     public String JavaDoc getText(Object JavaDoc element) {
167         String JavaDoc result= JavaElementLabels.getTextLabel(element, evaluateTextFlags(element));
168         if (result.length() == 0 && (element instanceof IStorage)) {
169             result= fStorageLabelProvider.getText(element);
170         }
171         return decorateText(result, element);
172     }
173     
174     public ColoredString getRichTextLabel(Object JavaDoc element) {
175         ColoredString string= ColoredJavaElementLabels.getTextLabel(element, evaluateTextFlags(element) | ColoredJavaElementLabels.COLORIZE);
176         if (string.length() == 0 && (element instanceof IStorage)) {
177             string= new ColoredString(fStorageLabelProvider.getText(element));
178         }
179         String JavaDoc decorated= decorateText(string.getString(), element);
180         if (decorated != null) {
181             return ColoredJavaElementLabels.decorateColoredString(string, decorated, ColoredJavaElementLabels.DECORATIONS_STYLE);
182         }
183         return string;
184     }
185     
186     /* (non-Javadoc)
187      * @see IBaseLabelProvider#dispose
188      */

189     public void dispose() {
190         if (fLabelDecorators != null) {
191             for (int i= 0; i < fLabelDecorators.size(); i++) {
192                 ILabelDecorator decorator= (ILabelDecorator) fLabelDecorators.get(i);
193                 decorator.dispose();
194             }
195             fLabelDecorators= null;
196         }
197         fStorageLabelProvider.dispose();
198         fImageLabelProvider.dispose();
199     }
200     
201     /* (non-Javadoc)
202      * @see IBaseLabelProvider#addListener(ILabelProviderListener)
203      */

204     public void addListener(ILabelProviderListener listener) {
205         if (fLabelDecorators != null) {
206             for (int i= 0; i < fLabelDecorators.size(); i++) {
207                 ILabelDecorator decorator= (ILabelDecorator) fLabelDecorators.get(i);
208                 decorator.addListener(listener);
209             }
210         }
211         fListeners.add(listener);
212     }
213
214     /* (non-Javadoc)
215      * @see IBaseLabelProvider#isLabelProperty(Object, String)
216      */

217     public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {
218         return true;
219     }
220
221     /* (non-Javadoc)
222      * @see IBaseLabelProvider#removeListener(ILabelProviderListener)
223      */

224     public void removeListener(ILabelProviderListener listener) {
225         if (fLabelDecorators != null) {
226             for (int i= 0; i < fLabelDecorators.size(); i++) {
227                 ILabelDecorator decorator= (ILabelDecorator) fLabelDecorators.get(i);
228                 decorator.removeListener(listener);
229             }
230         }
231         fListeners.remove(listener);
232     }
233     
234     public static ILabelDecorator[] getDecorators(boolean errortick, ILabelDecorator extra) {
235         if (errortick) {
236             if (extra == null) {
237                 return new ILabelDecorator[] {};
238             } else {
239                 return new ILabelDecorator[] { extra };
240             }
241         }
242         if (extra != null) {
243             return new ILabelDecorator[] { extra };
244         }
245         return null;
246     }
247
248     /* (non-Javadoc)
249      * @see org.eclipse.jface.viewers.IColorProvider#getForeground(java.lang.Object)
250      */

251     public Color getForeground(Object JavaDoc element) {
252         return null;
253     }
254
255     /* (non-Javadoc)
256      * @see org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object)
257      */

258     public Color getBackground(Object JavaDoc element) {
259         return null;
260     }
261     
262     /**
263      * Fires a label provider changed event to all registered listeners
264      * Only listeners registered at the time this method is called are notified.
265      *
266      * @param event a label provider changed event
267      *
268      * @see ILabelProviderListener#labelProviderChanged
269      */

270     protected void fireLabelProviderChanged(final LabelProviderChangedEvent event) {
271         Object JavaDoc[] listeners = fListeners.getListeners();
272         for (int i = 0; i < listeners.length; ++i) {
273             final ILabelProviderListener l = (ILabelProviderListener) listeners[i];
274             SafeRunner.run(new SafeRunnable() {
275                 public void run() {
276                     l.labelProviderChanged(event);
277                 }
278             });
279         }
280     }
281
282 }
283
Popular Tags