new version with WorkingCopy Management
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / ui / JavaElementLabelProvider.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.ui;
12
13 import net.sourceforge.phpdt.internal.ui.viewsupport.JavaElementImageProvider;
14 import net.sourceforge.phpdt.internal.ui.viewsupport.JavaElementLabels;
15 import net.sourceforge.phpdt.internal.ui.viewsupport.StorageLabelProvider;
16
17 import org.eclipse.core.resources.IStorage;
18 import org.eclipse.jface.viewers.LabelProvider;
19 import org.eclipse.swt.graphics.Image;
20
21
22
23 /**
24  * Standard label provider for Java elements.
25  * Use this class when you want to present the Java elements in a viewer.
26  * <p>
27  * The implementation also handles non-Java elements by forwarding the requests to the
28  * <code>IWorkbenchAdapter</code> of the element.
29  * </p>
30  * <p>
31  * This class may be instantiated; it is not intended to be subclassed.
32  * </p>
33  */
34 public class JavaElementLabelProvider extends LabelProvider {
35         
36         /**
37          * Flag (bit mask) indicating that methods labels include the method return type. (appended)
38          */
39         public final static int SHOW_RETURN_TYPE=                               0x001;
40         
41         /**
42          * Flag (bit mask) indicating that method label include method parameter types.
43          */
44         public final static int SHOW_PARAMETERS=                                0x002;
45         
46         /**
47          * Flag (bit mask) indicating that the label of a member should include the container.
48          * For example, include the name of the type enclosing a field.
49          * @deprecated Use SHOW_QUALIFIED or SHOW_ROOT instead
50          */
51         public final static int SHOW_CONTAINER=                         0x004;
52
53         /**
54          * Flag (bit mask) indicating that the label of a type should be fully qualified.
55          * For example, include the fully qualified name of the type enclosing a type.
56          * @deprecated Use SHOW_QUALIFIED instead
57          */
58         public final static int SHOW_CONTAINER_QUALIFICATION=   0x008;
59
60         /**
61          * Flag (bit mask) indicating that the label should include overlay icons
62          * for element type and modifiers.
63          */
64         public final static int SHOW_OVERLAY_ICONS=                     0x010;
65
66         /**
67          * Flag (bit mask) indicating thata field label should include the declared type.
68          */
69         public final static int SHOW_TYPE=                                      0x020;
70
71         /**
72          * Flag (bit mask) indicating that the label should include the name of the
73          * package fragment root (appended).
74          */
75         public final static int SHOW_ROOT=                                      0x040;
76         
77         /**
78          * Flag (bit mask) indicating that the label qualification of a type should
79          * be shown after the name.
80          * @deprecated SHOW_POST_QUALIFIED instead
81          */
82         public final static int SHOW_POSTIFIX_QUALIFICATION=            0x080;
83
84         /**
85          * Flag (bit mask) indicating that the label should show the icons with no space
86          * reserved for overlays.
87          */
88         public final static int SHOW_SMALL_ICONS=                       0x100;
89         
90         /**
91          * Flag (bit mask) indicating that the packagefragment roots from variables should
92          * be rendered with the variable in the name
93          */
94         public final static int SHOW_VARIABLE=                  0x200;
95         
96         /**
97          * Flag (bit mask) indicating that Complation Units, Class Files, Types, Declarations and Members
98          * should be rendered qualified.
99          * Examples: java.lang.String, java.util.Vector.size()
100          * 
101          * @since 2.0
102          */
103         public final static int SHOW_QUALIFIED=                         0x400;
104
105         /**
106          * Flag (bit mask) indicating that Complation Units, Class Files, Types, Declarations and Members
107          * should be rendered qualified. The qualifcation is appended
108          * Examples: String - java.lang, size() - java.util.Vector
109          * 
110          * @since 2.0
111          */
112         public final static int SHOW_POST_QUALIFIED=    0x800;  
113         
114         
115         /**
116          * Constant (value <code>0</code>) indicating that the label should show 
117          * the basic images only.
118          */
119         public final static int SHOW_BASICS= 0x000;
120         
121         
122         /**
123          * Constant indicating the default label rendering.
124          * Currently the default is equivalent to
125          * <code>SHOW_PARAMETERS | SHOW_OVERLAY_ICONS</code>.
126          */
127         public final static int SHOW_DEFAULT= new Integer(SHOW_PARAMETERS | SHOW_OVERLAY_ICONS).intValue();
128
129         private JavaElementImageProvider fImageLabelProvider;
130         
131         private StorageLabelProvider fStorageLabelProvider;
132         private int fFlags;
133         private int fImageFlags;
134         private int fTextFlags;
135         
136         /**
137          * Creates a new label provider with <code>SHOW_DEFAULT</code> flag.
138          *
139          * @see #SHOW_DEFAULT
140          * @since 2.0
141          */
142         public JavaElementLabelProvider() {
143                 this(SHOW_DEFAULT);
144         }
145
146         /**
147          * Creates a new label provider.
148          *
149          * @param flags the initial options; a bitwise OR of <code>SHOW_* </code> constants
150          */
151         public JavaElementLabelProvider(int flags) {
152                 fImageLabelProvider= new JavaElementImageProvider();
153                 fStorageLabelProvider= new StorageLabelProvider();
154                 fFlags= flags;
155                 updateImageProviderFlags();
156                 updateTextProviderFlags();              
157         }
158         
159         private boolean getFlag( int flag) {
160                 return (fFlags & flag) != 0;
161         }
162         
163         /**
164          * Turns on the rendering options specified in the given flags.
165          *
166          * @param flags the options; a bitwise OR of <code>SHOW_* </code> constants
167          */
168         public void turnOn(int flags) {
169                 fFlags |= flags;
170                 updateImageProviderFlags();
171                 updateTextProviderFlags();
172         }
173         
174         /**
175          * Turns off the rendering options specified in the given flags.
176          *
177          * @param flags the initial options; a bitwise OR of <code>SHOW_* </code> constants
178          */
179         public void turnOff(int flags) {
180                 fFlags &= (~flags);
181                 updateImageProviderFlags();
182                 updateTextProviderFlags();
183         }
184         
185         private void updateImageProviderFlags() {
186                 fImageFlags= 0;
187                 if (getFlag(SHOW_OVERLAY_ICONS)) {
188                         fImageFlags |= JavaElementImageProvider.OVERLAY_ICONS;
189                 }
190                 if (getFlag(SHOW_SMALL_ICONS)) {
191                         fImageFlags |= JavaElementImageProvider.SMALL_ICONS;
192                 }
193         }       
194         
195         private void updateTextProviderFlags() {
196                 fTextFlags= 0;
197                 if (getFlag(SHOW_RETURN_TYPE)) {
198                         fTextFlags |= JavaElementLabels.M_APP_RETURNTYPE;
199                 }
200                 if (getFlag(SHOW_PARAMETERS)) {
201                         fTextFlags |= JavaElementLabels.M_PARAMETER_TYPES;
202                 }               
203                 if (getFlag(SHOW_CONTAINER)) {
204                         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;
205                 }
206                 if (getFlag(SHOW_POSTIFIX_QUALIFICATION)) {
207                         fTextFlags |= (JavaElementLabels.T_POST_QUALIFIED | JavaElementLabels.CF_POST_QUALIFIED  | JavaElementLabels.CU_POST_QUALIFIED);
208                 } else if (getFlag(SHOW_CONTAINER_QUALIFICATION)) {
209                         fTextFlags |=(JavaElementLabels.T_FULLY_QUALIFIED | JavaElementLabels.CF_QUALIFIED  | JavaElementLabels.CU_QUALIFIED);
210                 }
211                 if (getFlag(SHOW_TYPE)) {
212                         fTextFlags |= JavaElementLabels.F_APP_TYPE_SIGNATURE;
213                 }
214                 if (getFlag(SHOW_ROOT)) {
215                         fTextFlags |= JavaElementLabels.APPEND_ROOT_PATH;
216                 }                       
217                 if (getFlag(SHOW_VARIABLE)) {
218                         fTextFlags |= JavaElementLabels.ROOT_VARIABLE;
219                 }
220                 if (getFlag(SHOW_QUALIFIED)) {
221                         fTextFlags |= (JavaElementLabels.F_FULLY_QUALIFIED | JavaElementLabels.M_FULLY_QUALIFIED | JavaElementLabels.I_FULLY_QUALIFIED 
222                                 | JavaElementLabels.T_FULLY_QUALIFIED | JavaElementLabels.D_QUALIFIED | JavaElementLabels.CF_QUALIFIED  | JavaElementLabels.CU_QUALIFIED);
223                 }
224                 if (getFlag(SHOW_POST_QUALIFIED)) {
225                         fTextFlags |= (JavaElementLabels.F_POST_QUALIFIED | JavaElementLabels.M_POST_QUALIFIED | JavaElementLabels.I_POST_QUALIFIED 
226                         | JavaElementLabels.T_POST_QUALIFIED | JavaElementLabels.D_POST_QUALIFIED | JavaElementLabels.CF_POST_QUALIFIED  | JavaElementLabels.CU_POST_QUALIFIED);
227                 }               
228         }
229
230         /* (non-Javadoc)
231          * @see ILabelProvider#getImage
232          */
233         public Image getImage(Object element) {
234                 Image result= fImageLabelProvider.getImageLabel(element, fImageFlags);
235                 if (result != null) {
236                         return result;
237                 }
238
239                 if (element instanceof IStorage) 
240                         return fStorageLabelProvider.getImage(element);
241
242                 return result;
243         }
244
245         /* (non-Javadoc)
246          * @see ILabelProvider#getText
247          */
248         public String getText(Object element) {
249                 String text= JavaElementLabels.getTextLabel(element, fTextFlags);
250                 if (text.length() > 0) {
251                         return text;
252                 }
253
254                 if (element instanceof IStorage)
255                         return fStorageLabelProvider.getText(element);
256
257                 return text;
258         }
259
260         /* (non-Javadoc)
261          * 
262          * @see IBaseLabelProvider#dispose
263          */
264         public void dispose() {
265                 fStorageLabelProvider.dispose();
266                 fImageLabelProvider.dispose();
267         }
268 }