KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > JavaElementLabelProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ui;
12
13 import org.eclipse.core.resources.IStorage;
14
15 import org.eclipse.swt.graphics.Image;
16
17 import org.eclipse.jface.viewers.LabelProvider;
18
19 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
20 import org.eclipse.jdt.internal.ui.viewsupport.StorageLabelProvider;
21
22 /**
23  * Standard label provider for Java elements.
24  * Use this class when you want to present the Java elements in a viewer.
25  * <p>
26  * The implementation also handles non-Java elements by forwarding the requests to the
27  * <code>IWorkbenchAdapter</code> of the element.
28  * </p>
29  * <p>
30  * This class may be instantiated; it is not intended to be subclassed.
31  * </p>
32  */

33 public class JavaElementLabelProvider extends LabelProvider {
34     
35     /**
36      * Flag (bit mask) indicating that methods labels include the method return type (appended).
37      */

38     public final static int SHOW_RETURN_TYPE= 0x001;
39     
40     /**
41      * Flag (bit mask) indicating that method label include parameter types.
42      */

43     public final static int SHOW_PARAMETERS= 0x002;
44     
45     /**
46      * Flag (bit mask) indicating that the label of a member should include the container.
47      * For example, include the name of the type enclosing a field.
48      * @deprecated Use SHOW_QUALIFIED or SHOW_ROOT instead
49      */

50     public final static int SHOW_CONTAINER= 0x004;
51
52     /**
53      * Flag (bit mask) indicating that the label of a type should be fully qualified.
54      * For example, include the fully qualified name of the type enclosing a type.
55      * @deprecated Use SHOW_QUALIFIED instead
56      */

57     public final static int SHOW_CONTAINER_QUALIFICATION= 0x008;
58
59     /**
60      * Flag (bit mask) indicating that the label should include overlay icons
61      * for element type and modifiers.
62      */

63     public final static int SHOW_OVERLAY_ICONS= 0x010;
64
65     /**
66      * Flag (bit mask) indicating that a field label should include the declared type.
67      */

68     public final static int SHOW_TYPE= 0x020;
69
70     /**
71      * Flag (bit mask) indicating that the label should include the name of the
72      * package fragment root (appended).
73      */

74     public final static int SHOW_ROOT= 0x040;
75     
76     /**
77      * Flag (bit mask) indicating that the label qualification of a type should
78      * be shown after the name.
79      * @deprecated SHOW_POST_QUALIFIED instead
80      */

81     public final static int SHOW_POSTIFIX_QUALIFICATION= 0x080;
82
83     /**
84      * Flag (bit mask) indicating that the label should show the icons with no space
85      * reserved for overlays.
86      */

87     public final static int SHOW_SMALL_ICONS= 0x100;
88     
89     /**
90      * Flag (bit mask) indicating that the package fragment roots from class path variables should
91      * be rendered with the variable in the name
92      */

93     public final static int SHOW_VARIABLE= 0x200;
94     
95     /**
96      * Flag (bit mask) indicating that compilation units, class files, types, declarations and members
97      * should be rendered qualified.
98      * Examples: <code>java.lang.String</code>, <code>java.util.Vector.size()</code>
99      *
100      * @since 2.0
101      */

102     public final static int SHOW_QUALIFIED= 0x400;
103
104     /**
105      * Flag (bit mask) indicating that compilation units, class files, types, declarations and members
106      * should be rendered qualified.The qualification is appended.
107      * Examples: <code>String - java.lang</code>, <code>size() - java.util.Vector</code>
108      *
109      * @since 2.0
110      */

111     public final static int SHOW_POST_QUALIFIED= 0x800;
112     
113     
114     /**
115      * Constant (value <code>0</code>) indicating that the label should show
116      * the basic images only.
117      */

118     public final static int SHOW_BASICS= 0x000;
119     
120     
121     /**
122      * Constant indicating the default label rendering.
123      * Currently the default is equivalent to
124      * <code>SHOW_PARAMETERS | SHOW_OVERLAY_ICONS</code>.
125      */

126     public final static int SHOW_DEFAULT= new Integer JavaDoc(SHOW_PARAMETERS | SHOW_OVERLAY_ICONS).intValue();
127
128     private JavaElementImageProvider fImageLabelProvider;
129     
130     private StorageLabelProvider fStorageLabelProvider;
131     private int fFlags;
132     private int fImageFlags;
133     private long fTextFlags;
134     
135     /**
136      * Creates a new label provider with <code>SHOW_DEFAULT</code> flag.
137      *
138      * @see #SHOW_DEFAULT
139      * @since 2.0
140      */

141     public JavaElementLabelProvider() {
142         this(SHOW_DEFAULT);
143     }
144
145     /**
146      * Creates a new label provider.
147      *
148      * @param flags the initial options; a bitwise OR of <code>SHOW_* </code> constants
149      */

150     public JavaElementLabelProvider(int flags) {
151         fImageLabelProvider= new JavaElementImageProvider();
152         fStorageLabelProvider= new StorageLabelProvider();
153         fFlags= flags;
154         updateImageProviderFlags();
155         updateTextProviderFlags();
156     }
157     
158     private boolean getFlag( int flag) {
159         return (fFlags & flag) != 0;
160     }
161     
162     /**
163      * Turns on the rendering options specified in the given flags.
164      *
165      * @param flags the options; a bitwise OR of <code>SHOW_* </code> constants
166      */

167     public void turnOn(int flags) {
168         fFlags |= flags;
169         updateImageProviderFlags();
170         updateTextProviderFlags();
171     }
172     
173     /**
174      * Turns off the rendering options specified in the given flags.
175      *
176      * @param flags the initial options; a bitwise OR of <code>SHOW_* </code> constants
177      */

178     public void turnOff(int flags) {
179         fFlags &= (~flags);
180         updateImageProviderFlags();
181         updateTextProviderFlags();
182     }
183     
184     private void updateImageProviderFlags() {
185         fImageFlags= 0;
186         if (getFlag(SHOW_OVERLAY_ICONS)) {
187             fImageFlags |= JavaElementImageProvider.OVERLAY_ICONS;
188         }
189         if (getFlag(SHOW_SMALL_ICONS)) {
190             fImageFlags |= JavaElementImageProvider.SMALL_ICONS;
191         }
192     }
193     
194     private void updateTextProviderFlags() {
195         fTextFlags= JavaElementLabels.T_TYPE_PARAMETERS;
196         if (getFlag(SHOW_RETURN_TYPE)) {
197             fTextFlags |= JavaElementLabels.M_APP_RETURNTYPE;
198         }
199         if (getFlag(SHOW_PARAMETERS)) {
200             fTextFlags |= JavaElementLabels.M_PARAMETER_TYPES;
201         }
202         if (getFlag(SHOW_CONTAINER)) {
203             fTextFlags |= JavaElementLabels.P_POST_QUALIFIED | JavaElementLabels.T_POST_QUALIFIED | JavaElementLabels.CF_POST_QUALIFIED | JavaElementLabels.CU_POST_QUALIFIED | JavaElementLabels.M_POST_QUALIFIED | JavaElementLabels.F_POST_QUALIFIED;
204         }
205         if (getFlag(SHOW_POSTIFIX_QUALIFICATION)) {
206             fTextFlags |= (JavaElementLabels.T_POST_QUALIFIED | JavaElementLabels.CF_POST_QUALIFIED | JavaElementLabels.CU_POST_QUALIFIED);
207         } else if (getFlag(SHOW_CONTAINER_QUALIFICATION)) {
208             fTextFlags |=(JavaElementLabels.T_FULLY_QUALIFIED | JavaElementLabels.CF_QUALIFIED | JavaElementLabels.CU_QUALIFIED);
209         }
210         if (getFlag(SHOW_TYPE)) {
211             fTextFlags |= JavaElementLabels.F_APP_TYPE_SIGNATURE;
212         }
213         if (getFlag(SHOW_ROOT)) {
214             fTextFlags |= JavaElementLabels.APPEND_ROOT_PATH;
215         }
216         if (getFlag(SHOW_VARIABLE)) {
217             fTextFlags |= JavaElementLabels.ROOT_VARIABLE;
218         }
219         if (getFlag(SHOW_QUALIFIED)) {
220             fTextFlags |= (JavaElementLabels.F_FULLY_QUALIFIED | JavaElementLabels.M_FULLY_QUALIFIED | JavaElementLabels.I_FULLY_QUALIFIED
221                 | JavaElementLabels.T_FULLY_QUALIFIED | JavaElementLabels.D_QUALIFIED | JavaElementLabels.CF_QUALIFIED | JavaElementLabels.CU_QUALIFIED);
222         }
223         if (getFlag(SHOW_POST_QUALIFIED)) {
224             fTextFlags |= (JavaElementLabels.F_POST_QUALIFIED | JavaElementLabels.M_POST_QUALIFIED | JavaElementLabels.I_POST_QUALIFIED
225             | JavaElementLabels.T_POST_QUALIFIED | JavaElementLabels.D_POST_QUALIFIED | JavaElementLabels.CF_POST_QUALIFIED | JavaElementLabels.CU_POST_QUALIFIED);
226         }
227     }
228
229     /* (non-Javadoc)
230      * @see ILabelProvider#getImage
231      */

232     public Image getImage(Object JavaDoc element) {
233         Image result= fImageLabelProvider.getImageLabel(element, fImageFlags);
234         if (result != null) {
235             return result;
236         }
237
238         if (element instanceof IStorage)
239             return fStorageLabelProvider.getImage(element);
240
241         return result;
242     }
243
244     /* (non-Javadoc)
245      * @see ILabelProvider#getText
246      */

247     public String JavaDoc getText(Object JavaDoc element) {
248         String JavaDoc text= JavaElementLabels.getTextLabel(element, fTextFlags);
249         if (text.length() > 0) {
250             return text;
251         }
252
253         if (element instanceof IStorage)
254             return fStorageLabelProvider.getText(element);
255
256         return text;
257     }
258
259     /* (non-Javadoc)
260      *
261      * @see IBaseLabelProvider#dispose
262      */

263     public void dispose() {
264         fStorageLabelProvider.dispose();
265         fImageLabelProvider.dispose();
266     }
267 }
268
Popular Tags