upload tizen1.0 source
[sdk/ide/product.git] / org.eclipse.cdt.ui / src / org / eclipse / cdt / internal / ui / viewsupport / CElementLabels.java
1 /*******************************************************************************
2  * Copyright (c) 2003, 2010 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 Corp. - Rational Software - initial implementation
10  *     Markus Schorn (Wind River Systems)
11  *     Patrick Hofer [bug 325799]
12  *******************************************************************************/
13 package org.eclipse.cdt.internal.ui.viewsupport;
14
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.resources.IStorage;
17 import org.eclipse.core.runtime.IAdaptable;
18 import org.eclipse.jface.viewers.StyledString;
19 import org.eclipse.osgi.util.TextProcessor;
20 import org.eclipse.ui.model.IWorkbenchAdapter;
21
22 import org.eclipse.cdt.core.model.ICElement;
23 import org.eclipse.cdt.core.model.IEnumerator;
24 import org.eclipse.cdt.core.model.IField;
25 import org.eclipse.cdt.core.model.IFunctionDeclaration;
26 import org.eclipse.cdt.core.model.IMacro;
27 import org.eclipse.cdt.core.model.IMethodDeclaration;
28 import org.eclipse.cdt.core.model.ITypeDef;
29 import org.eclipse.cdt.core.model.IVariableDeclaration;
30
31 import org.eclipse.cdt.internal.core.model.CElement;
32 import org.eclipse.cdt.internal.core.model.CoreModelMessages;
33 import org.eclipse.cdt.internal.corext.util.Strings;
34
35 /*
36  * This class is basically a clone of org.eclipse.cdt.core.model.util.CElementLabels 
37  * (refer to bug 325799 for the reasoning)
38  */
39
40 /**
41  * Computes labels for objects of type {@link CElement}.
42  */
43 public class CElementLabels {
44         /**
45          * Method names contain parameter types.
46          * e.g. <code>foo(int)</code>
47          */
48         public final static long M_PARAMETER_TYPES= 1L << 0;
49
50         /**
51          * Method definition names without qualifier.
52          * e.g. <code>foo(int)</code>
53          */
54         public final static long M_SIMPLE_NAME= 1L << 1;
55
56         /**
57          * Method names contain thrown exceptions.
58          * e.g. <code>foo throw( IOException )</code>
59          */
60         public final static long M_EXCEPTIONS= 1L << 2;
61         
62         /**
63          * Method names contain return type (appended)
64          * e.g. <code>foo : int</code>
65          */
66         public final static long M_APP_RETURNTYPE= 1L << 3;
67
68         /**
69          * Method names contain return type (appended)
70          * e.g. <code>int foo</code>
71          */
72         public final static long M_PRE_RETURNTYPE= 1L << 4;     
73
74         /**
75          * Method names are fully qualified.
76          * e.g. <code>ClassName::size</code>
77          */
78         public final static long M_FULLY_QUALIFIED= 1L << 5;
79
80         /**
81          * Method names are post qualified.
82          * e.g. <code>size - ClassName</code>
83          */
84         public final static long M_POST_QUALIFIED= 1L << 6;
85
86         /**
87          * Templates are qualified with template parameters.
88          * e.g. <code>ClassName<T></code>
89          */
90         public final static long TEMPLATE_PARAMETERS= 1L << 7;
91
92         /**
93          * Static field names without qualifier.
94          * e.g. <code>fHello</code>
95          */
96         public final static long F_SIMPLE_NAME= 1L << 8;
97
98         /**
99          * Field names contain the declared type (appended)
100          * e.g. <code>fHello: int</code>
101          */
102         public final static long F_APP_TYPE_SIGNATURE= 1L << 9;
103
104         /**
105          * Field names contain the declared type (prepended)
106          * e.g. <code>int fHello</code>
107          */
108         public final static long F_PRE_TYPE_SIGNATURE= 1L << 10;        
109
110         /**
111          * Fields names are fully qualified.
112          * e.g. <code>ClassName::fField</code>
113          */
114         public final static long F_FULLY_QUALIFIED= 1L << 11;
115
116         /**
117          * Fields names are post qualified.
118          * e.g. <code>fField - ClassName</code>
119          */
120         public final static long F_POST_QUALIFIED= 1L << 12;    
121
122         /**
123          * Type names are fully qualified.
124          * e.g. <code>namespace::ClassName</code>
125          */
126         public final static long T_FULLY_QUALIFIED= 1L << 13;
127
128         /**
129          * Instances and specializations are qualified with arguments, templates with template parameter names.
130          * The flag overrides {@link #TEMPLATE_PARAMETERS}.
131          */
132         public final static long TEMPLATE_ARGUMENTS= 1L << 14;
133
134         /**
135          * Append base class specifications to type names.
136          * e.g. <code>MyClass : public BaseClass</code>
137          */
138         public final static long T_INHERITANCE= 1L << 16;
139
140         /**
141          * Translation unit names contain the full path.
142          * e.g. <code>/MyProject/src/ClassName.cpp</code>
143          */     
144         public final static long TU_QUALIFIED= 1L << 20;
145
146         /**
147          * Translation unit names are post qualified with their path.
148          * e.g. <code>ClassName.cpp - /MyProject/src</code>
149          */     
150         public final static long TU_POST_QUALIFIED= 1L << 21;
151
152         /**
153          * Source roots contain the project name (prepended).
154          * e.g. <code>MyProject/src</code>
155          */
156         public final static long ROOT_QUALIFIED= 1L << 25;
157
158         /**
159          * Source roots contain the project name (appended).
160          * e.g. <code>src - MyProject</code>
161          */
162         public final static long ROOT_POST_QUALIFIED= 1L << 26; 
163
164         /**
165          * Add source root path.
166          * e.g. <code>func() - MyProject/src</code>
167          * Option only applies to getElementLabel
168          */
169         public final static long APPEND_ROOT_PATH= 1L << 27;
170
171         /**
172          * Prepend source root path.
173          * e.g. <code>MyProject/src - func()</code>
174          * Option only applies to getElementLabel
175          */
176         public final static long PREPEND_ROOT_PATH= 1L << 28;
177
178         /**
179          * Post qualify container project. For example
180          * <code>folder - MyProject</code> if the folder is in project MyProject.
181          */
182         public final static long PROJECT_POST_QUALIFIED= 1L << 30; 
183
184         /**
185          * Post qualify symbols with file. 
186          * e.g. <code>func() - /proj/folder/file.cpp</code> 
187          */
188         public final static long MF_POST_FILE_QUALIFIED= 1L << 31;
189
190         /**
191          * Specifies to apply color styles to labels. This flag only applies to methods taking or returning a {@link StyledString}.
192          */
193         public final static long COLORIZE= 1L << 32;
194         
195         /**
196          * Qualify all elements
197          */
198         public final static long ALL_FULLY_QUALIFIED= F_FULLY_QUALIFIED | M_FULLY_QUALIFIED | T_FULLY_QUALIFIED | TU_QUALIFIED | ROOT_QUALIFIED;
199
200         /**
201          * Post qualify all elements
202          */
203         public final static long ALL_POST_QUALIFIED= F_POST_QUALIFIED | M_POST_QUALIFIED  | TU_POST_QUALIFIED | ROOT_POST_QUALIFIED;
204
205         /**
206          *  Default options (M_PARAMETER_TYPES enabled)
207          */
208         public final static long ALL_DEFAULT= M_PARAMETER_TYPES;
209
210         /**
211          *  Default qualify options (All except Root)
212          */
213         public final static long DEFAULT_QUALIFIED= F_FULLY_QUALIFIED | M_FULLY_QUALIFIED | T_FULLY_QUALIFIED | TU_QUALIFIED;
214
215         /**
216          *  Default post qualify options (All except Root)
217          */
218         public final static long DEFAULT_POST_QUALIFIED= F_POST_QUALIFIED | M_POST_QUALIFIED | TU_POST_QUALIFIED;
219
220         /**
221          * Separator for appending qualifiers
222          */
223         public final static String CONCAT_STRING= CoreModelMessages.getString("CElementLabels.concat_string"); // " - "; //$NON-NLS-1$
224         
225         /**
226          * Separator for parameters, base classes, exceptions, etc.
227          */
228         public final static String COMMA_STRING = CoreModelMessages.getString("CElementLabels.comma_string"); // ", "; //$NON-NLS-1$
229         
230         /**
231          * Separator for appending (return) type
232          */
233         public final static String DECL_STRING  = CoreModelMessages.getString("CElementLabels.declseparator_string"); // "  "; // use for return type //$NON-NLS-1$
234
235         
236         
237         //====================
238         
239         
240         private CElementLabels() {
241         }
242
243         /**
244          * Returns the label of the given object. The object must be of type {@link ICElement} or adapt to {@link IWorkbenchAdapter}.
245          * If the element type is not known, the empty string is returned.
246          * The returned label is BiDi-processed with {@link TextProcessor#process(String, String)}.
247          *
248          * @param obj object to get the label for
249          * @param flags the rendering flags
250          * @return the label or the empty string if the object type is not supported
251          */
252         public static String getTextLabel(Object obj, long flags) {
253                 if (obj instanceof ICElement) {
254                         return getElementLabel((ICElement) obj, flags);
255
256                 } else if (obj instanceof IResource) {
257                         return BasicElementLabels.getResourceName((IResource) obj);
258
259                 } else if (obj instanceof IStorage) {
260                         return BasicElementLabels.getResourceName(((IStorage) obj).getName());
261
262                 } else if (obj instanceof IAdaptable) {
263                         IWorkbenchAdapter wbadapter= (IWorkbenchAdapter) ((IAdaptable)obj).getAdapter(IWorkbenchAdapter.class);
264                         if (wbadapter != null) {
265                                 return Strings.markLTR(wbadapter.getLabel(obj));
266                         }
267                 }
268                 return ""; //$NON-NLS-1$
269         }
270
271         /**
272          * Returns the styled label of the given object. The object must be of type {@link ICElement} or adapt to {@link IWorkbenchAdapter}.
273          * If the element type is not known, the empty string is returned.
274          * The returned label is BiDi-processed with {@link TextProcessor#process(String, String)}.
275          *
276          * @param obj object to get the label for
277          * @param flags the rendering flags
278          * @return the label or the empty string if the object type is not supported
279          */
280         public static StyledString getStyledTextLabel(Object obj, long flags) {
281                 if (obj instanceof ICElement) {
282                         return getStyledElementLabel((ICElement) obj, flags);
283
284                 } else if (obj instanceof IResource) {
285                         return getStyledResourceLabel((IResource) obj);
286
287                 } else if (obj instanceof IStorage) {
288                         return getStyledStorageLabel((IStorage) obj);
289
290                 } else if (obj instanceof IAdaptable) {
291                         IWorkbenchAdapter wbadapter= (IWorkbenchAdapter) ((IAdaptable)obj).getAdapter(IWorkbenchAdapter.class);
292                         if (wbadapter != null) {
293                                 return Strings.markLTR(new StyledString(wbadapter.getLabel(obj)));
294                         }
295                 }
296                 return new StyledString();
297         }
298
299         /**
300          * Returns the styled string for the given resource.
301          * The returned label is BiDi-processed with {@link TextProcessor#process(String, String)}.
302          *
303          * @param resource the resource
304          * @return the styled string
305          */
306         private static StyledString getStyledResourceLabel(IResource resource) {
307                 StyledString result= new StyledString(resource.getName());
308                 return Strings.markLTR(result);
309         }
310
311         /**
312          * Returns the styled string for the given storage.
313          * The returned label is BiDi-processed with {@link TextProcessor#process(String, String)}.
314          *
315          * @param storage the storage
316          * @return the styled string
317          */
318         private static StyledString getStyledStorageLabel(IStorage storage) {
319                 StyledString result= new StyledString(storage.getName());
320                 return Strings.markLTR(result);
321         }
322
323
324         /**
325          * Returns the label for a Java element with the flags as defined by this class.
326          *
327          * @param element the element to render
328          * @param flags the rendering flags
329          * @return the label of the Java element
330          */
331         public static String getElementLabel(ICElement element, long flags) {
332                 StringBuffer result= new StringBuffer();
333                 getElementLabel(element, flags, result);
334                 return Strings.markCElementLabelLTR(result.toString());
335         }
336
337         /**
338          * Returns the styled label for a Java element with the flags as defined by this class.
339          *
340          * @param element the element to render
341          * @param flags the rendering flags
342          * @return the label of the Java element
343          *
344          */
345         public static StyledString getStyledElementLabel(ICElement element, long flags) {
346                 StyledString result= new StyledString();
347                 getElementLabel(element, flags, result);
348                 return Strings.markCElementLabelLTR(result);
349         }
350
351         /**
352          * Returns the label for a Java element with the flags as defined by this class.
353          *
354          * @param element the element to render
355          * @param flags the rendering flags
356          * @param buf the buffer to append the resulting label to
357          */
358         public static void getElementLabel(ICElement element, long flags, StringBuffer buf) {
359                 new CElementLabelComposer(buf).appendElementLabel(element, flags);
360         }
361
362         /**
363          * Returns the styled label for a Java element with the flags as defined by this class.
364          *
365          * @param element the element to render
366          * @param flags the rendering flags
367          * @param result the buffer to append the resulting label to
368          */
369         public static void getElementLabel(ICElement element, long flags, StyledString result) {
370                 new CElementLabelComposer(result).appendElementLabel(element, flags);
371         }
372         
373         /**
374          * Appends the label for a macro definition to a StringBuffer.
375          * @param macro a macro definition
376          * @param flags {@link #MF_POST_FILE_QUALIFIED}, or 0.
377          * @param buf the buffer to append the label to.
378          */
379         public static void getMacroLabel(IMacro macro, int flags, StringBuffer buf) {
380                 new CElementLabelComposer(buf).appendMacroLabel(macro, flags);
381         }
382
383         /**
384          * Appends the label for a macro to a {@link StyledString}. Considers the M_* flags.
385          *
386          * @param macro the element to render
387          * @param flags the rendering flags. Flags with names starting with 'M_' are considered.
388          * @param result the buffer to append the resulting label to
389          */
390         public static void getMethodLabel(IMacro macro, long flags, StyledString result) {
391                 new CElementLabelComposer(result).appendMacroLabel(macro, flags);
392         }
393         
394         /**
395          * Appends the label for a method declaration to a StringBuffer.
396          * @param method a method declaration
397          * @param flags any of the M_* flags, and MF_POST_FILE_QUALIFIED
398          * @param buf the buffer to append the label
399          */
400         public static void getMethodLabel( IMethodDeclaration method, int flags, StringBuffer buf ) {
401                 new CElementLabelComposer(buf).appendMethodLabel(method, flags);
402         }
403
404         /**
405          * Appends the label for a macro to a {@link StyledString}. Considers the M_* flags.
406          *
407          * @param method the element to render
408          * @param flags the rendering flags. Flags with names starting with 'M_' are considered.
409          * @param result the buffer to append the resulting label to
410          */
411         public static void getMethodLabel(IMethodDeclaration method, long flags, StyledString result) {
412                 new CElementLabelComposer(result).appendMethodLabel(method, flags);
413         }
414         
415         /**
416          * Appends the label for a field to a StringBuffer.
417          * @param field a field
418          * @param flags any of the F_* flags, and MF_POST_FILE_QUALIFIED
419          * @param buf the buffer to append the label
420          */
421         public static void getFieldLabel(IField field, int flags, StringBuffer buf ) {
422                 new CElementLabelComposer(buf).appendFieldLabel(field, flags);  
423         }
424
425         /**
426          * Appends the label for a field to a {@link StyledString}.
427          * @param field a field
428          * @param flags any of the F_* flags, and MF_POST_FILE_QUALIFIED
429          * @param result the buffer to append the label
430          */
431         public static void getFieldLabel(IField field, int flags, StyledString result ) {
432                 new CElementLabelComposer(result).appendFieldLabel(field, flags);       
433         }
434
435         /**
436          * Appends the label for a variable declaration to a StringBuffer.
437          * @param var a variable declaration
438          * @param flags any of the F_* flags, and MF_POST_FILE_QUALIFIED
439          * @param buf the buffer to append the label
440          */
441         public static void getVariableLabel(IVariableDeclaration var, int flags, StringBuffer buf ) {
442                 new CElementLabelComposer(buf).appendVariableLabel(var, flags); 
443         }
444
445         /**
446          * Appends the label for a variable declaration to a {@link StyledString}.
447          * @param var a variable declaration
448          * @param flags any of the F_* flags, and MF_POST_FILE_QUALIFIED
449          * @param result the buffer to append the label
450          */
451         public static void getVariableLabel(IVariableDeclaration var, int flags, StyledString result ) {
452                 new CElementLabelComposer(result).appendVariableLabel(var, flags);      
453         }
454
455         /**
456          * Appends the label for an enumerator to a StringBuffer.
457          * @param var an enumerator
458          * @param flags any of the F_* flags, and MF_POST_FILE_QUALIFIED
459          * @param buf the buffer to append the label
460          */
461         public static void getEnumeratorLabel(IEnumerator var, int flags, StringBuffer buf ) {
462                 new CElementLabelComposer(buf).appendEnumeratorLabel(var, flags);       
463         }
464
465         /**
466          * Appends the label for an enumerator to a {@link StyledString}.
467          * @param var an enumerator
468          * @param flags any of the F_* flags, and MF_POST_FILE_QUALIFIED
469          * @param result the buffer to append the label
470          */
471         public static void getEnumeratorLabel(IEnumerator var, int flags, StyledString result ) {
472                 new CElementLabelComposer(result).appendEnumeratorLabel(var, flags);    
473         }
474
475         /**
476          * Appends the label for a function declaration to a StringBuffer.
477          * @param func a function declaration
478          * @param flags any of the M_* flags, and MF_POST_FILE_QUALIFIED
479          * @param buf the buffer to append the label
480          */
481         public static void getFunctionLabel(IFunctionDeclaration func, int flags, StringBuffer buf) {
482                 new CElementLabelComposer(buf).appendFunctionLabel(func, flags);        
483         }
484
485         /**
486          * Appends the label for a function declaration to a {@link StyledString}.
487          * @param func a function declaration
488          * @param flags any of the M_* flags, and MF_POST_FILE_QUALIFIED
489          * @param result the buffer to append the label
490          */
491         public static void getFunctionLabel(IFunctionDeclaration func, int flags, StyledString result) {
492                 new CElementLabelComposer(result).appendFunctionLabel(func, flags);     
493         }
494
495         /**
496          * Appends the label for a type definition to a StringBuffer.
497          * @param typedef a type definition
498          * @param flags any of the F_* flags, and MF_POST_FILE_QUALIFIED
499          * @param buf the buffer to append the label
500          */
501         public static void getTypeDefLabel(ITypeDef typedef, int flags, StringBuffer buf ) {
502                 new CElementLabelComposer(buf).appendTypeDefLabel(typedef, flags);              
503         }
504
505         /**
506          * Appends the label for a type definition to a {@link StyledString}.
507          * @param typedef a type definition
508          * @param flags any of the F_* flags, and MF_POST_FILE_QUALIFIED
509          * @param result the buffer to append the label
510          */
511         public static void getTypeDefLabel(ITypeDef typedef, int flags, StyledString result ) {
512                 new CElementLabelComposer(result).appendTypeDefLabel(typedef, flags);           
513         }
514         
515 }