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
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;
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;
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;
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;
36 * This class is basically a clone of org.eclipse.cdt.core.model.util.CElementLabels
37 * (refer to bug 325799 for the reasoning)
41 * Computes labels for objects of type {@link CElement}.
43 public class CElementLabels {
45 * Method names contain parameter types.
46 * e.g. <code>foo(int)</code>
48 public final static long M_PARAMETER_TYPES= 1L << 0;
51 * Method definition names without qualifier.
52 * e.g. <code>foo(int)</code>
54 public final static long M_SIMPLE_NAME= 1L << 1;
57 * Method names contain thrown exceptions.
58 * e.g. <code>foo throw( IOException )</code>
60 public final static long M_EXCEPTIONS= 1L << 2;
63 * Method names contain return type (appended)
64 * e.g. <code>foo : int</code>
66 public final static long M_APP_RETURNTYPE= 1L << 3;
69 * Method names contain return type (appended)
70 * e.g. <code>int foo</code>
72 public final static long M_PRE_RETURNTYPE= 1L << 4;
75 * Method names are fully qualified.
76 * e.g. <code>ClassName::size</code>
78 public final static long M_FULLY_QUALIFIED= 1L << 5;
81 * Method names are post qualified.
82 * e.g. <code>size - ClassName</code>
84 public final static long M_POST_QUALIFIED= 1L << 6;
87 * Templates are qualified with template parameters.
88 * e.g. <code>ClassName<T></code>
90 public final static long TEMPLATE_PARAMETERS= 1L << 7;
93 * Static field names without qualifier.
94 * e.g. <code>fHello</code>
96 public final static long F_SIMPLE_NAME= 1L << 8;
99 * Field names contain the declared type (appended)
100 * e.g. <code>fHello: int</code>
102 public final static long F_APP_TYPE_SIGNATURE= 1L << 9;
105 * Field names contain the declared type (prepended)
106 * e.g. <code>int fHello</code>
108 public final static long F_PRE_TYPE_SIGNATURE= 1L << 10;
111 * Fields names are fully qualified.
112 * e.g. <code>ClassName::fField</code>
114 public final static long F_FULLY_QUALIFIED= 1L << 11;
117 * Fields names are post qualified.
118 * e.g. <code>fField - ClassName</code>
120 public final static long F_POST_QUALIFIED= 1L << 12;
123 * Type names are fully qualified.
124 * e.g. <code>namespace::ClassName</code>
126 public final static long T_FULLY_QUALIFIED= 1L << 13;
129 * Instances and specializations are qualified with arguments, templates with template parameter names.
130 * The flag overrides {@link #TEMPLATE_PARAMETERS}.
132 public final static long TEMPLATE_ARGUMENTS= 1L << 14;
135 * Append base class specifications to type names.
136 * e.g. <code>MyClass : public BaseClass</code>
138 public final static long T_INHERITANCE= 1L << 16;
141 * Translation unit names contain the full path.
142 * e.g. <code>/MyProject/src/ClassName.cpp</code>
144 public final static long TU_QUALIFIED= 1L << 20;
147 * Translation unit names are post qualified with their path.
148 * e.g. <code>ClassName.cpp - /MyProject/src</code>
150 public final static long TU_POST_QUALIFIED= 1L << 21;
153 * Source roots contain the project name (prepended).
154 * e.g. <code>MyProject/src</code>
156 public final static long ROOT_QUALIFIED= 1L << 25;
159 * Source roots contain the project name (appended).
160 * e.g. <code>src - MyProject</code>
162 public final static long ROOT_POST_QUALIFIED= 1L << 26;
165 * Add source root path.
166 * e.g. <code>func() - MyProject/src</code>
167 * Option only applies to getElementLabel
169 public final static long APPEND_ROOT_PATH= 1L << 27;
172 * Prepend source root path.
173 * e.g. <code>MyProject/src - func()</code>
174 * Option only applies to getElementLabel
176 public final static long PREPEND_ROOT_PATH= 1L << 28;
179 * Post qualify container project. For example
180 * <code>folder - MyProject</code> if the folder is in project MyProject.
182 public final static long PROJECT_POST_QUALIFIED= 1L << 30;
185 * Post qualify symbols with file.
186 * e.g. <code>func() - /proj/folder/file.cpp</code>
188 public final static long MF_POST_FILE_QUALIFIED= 1L << 31;
191 * Specifies to apply color styles to labels. This flag only applies to methods taking or returning a {@link StyledString}.
193 public final static long COLORIZE= 1L << 32;
196 * Qualify all elements
198 public final static long ALL_FULLY_QUALIFIED= F_FULLY_QUALIFIED | M_FULLY_QUALIFIED | T_FULLY_QUALIFIED | TU_QUALIFIED | ROOT_QUALIFIED;
201 * Post qualify all elements
203 public final static long ALL_POST_QUALIFIED= F_POST_QUALIFIED | M_POST_QUALIFIED | TU_POST_QUALIFIED | ROOT_POST_QUALIFIED;
206 * Default options (M_PARAMETER_TYPES enabled)
208 public final static long ALL_DEFAULT= M_PARAMETER_TYPES;
211 * Default qualify options (All except Root)
213 public final static long DEFAULT_QUALIFIED= F_FULLY_QUALIFIED | M_FULLY_QUALIFIED | T_FULLY_QUALIFIED | TU_QUALIFIED;
216 * Default post qualify options (All except Root)
218 public final static long DEFAULT_POST_QUALIFIED= F_POST_QUALIFIED | M_POST_QUALIFIED | TU_POST_QUALIFIED;
221 * Separator for appending qualifiers
223 public final static String CONCAT_STRING= CoreModelMessages.getString("CElementLabels.concat_string"); // " - "; //$NON-NLS-1$
226 * Separator for parameters, base classes, exceptions, etc.
228 public final static String COMMA_STRING = CoreModelMessages.getString("CElementLabels.comma_string"); // ", "; //$NON-NLS-1$
231 * Separator for appending (return) type
233 public final static String DECL_STRING = CoreModelMessages.getString("CElementLabels.declseparator_string"); // " "; // use for return type //$NON-NLS-1$
237 //====================
240 private CElementLabels() {
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)}.
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
252 public static String getTextLabel(Object obj, long flags) {
253 if (obj instanceof ICElement) {
254 return getElementLabel((ICElement) obj, flags);
256 } else if (obj instanceof IResource) {
257 return BasicElementLabels.getResourceName((IResource) obj);
259 } else if (obj instanceof IStorage) {
260 return BasicElementLabels.getResourceName(((IStorage) obj).getName());
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));
268 return ""; //$NON-NLS-1$
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)}.
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
280 public static StyledString getStyledTextLabel(Object obj, long flags) {
281 if (obj instanceof ICElement) {
282 return getStyledElementLabel((ICElement) obj, flags);
284 } else if (obj instanceof IResource) {
285 return getStyledResourceLabel((IResource) obj);
287 } else if (obj instanceof IStorage) {
288 return getStyledStorageLabel((IStorage) obj);
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)));
296 return new StyledString();
300 * Returns the styled string for the given resource.
301 * The returned label is BiDi-processed with {@link TextProcessor#process(String, String)}.
303 * @param resource the resource
304 * @return the styled string
306 private static StyledString getStyledResourceLabel(IResource resource) {
307 StyledString result= new StyledString(resource.getName());
308 return Strings.markLTR(result);
312 * Returns the styled string for the given storage.
313 * The returned label is BiDi-processed with {@link TextProcessor#process(String, String)}.
315 * @param storage the storage
316 * @return the styled string
318 private static StyledString getStyledStorageLabel(IStorage storage) {
319 StyledString result= new StyledString(storage.getName());
320 return Strings.markLTR(result);
325 * Returns the label for a Java element with the flags as defined by this class.
327 * @param element the element to render
328 * @param flags the rendering flags
329 * @return the label of the Java element
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());
338 * Returns the styled label for a Java element with the flags as defined by this class.
340 * @param element the element to render
341 * @param flags the rendering flags
342 * @return the label of the Java element
345 public static StyledString getStyledElementLabel(ICElement element, long flags) {
346 StyledString result= new StyledString();
347 getElementLabel(element, flags, result);
348 return Strings.markCElementLabelLTR(result);
352 * Returns the label for a Java element with the flags as defined by this class.
354 * @param element the element to render
355 * @param flags the rendering flags
356 * @param buf the buffer to append the resulting label to
358 public static void getElementLabel(ICElement element, long flags, StringBuffer buf) {
359 new CElementLabelComposer(buf).appendElementLabel(element, flags);
363 * Returns the styled label for a Java element with the flags as defined by this class.
365 * @param element the element to render
366 * @param flags the rendering flags
367 * @param result the buffer to append the resulting label to
369 public static void getElementLabel(ICElement element, long flags, StyledString result) {
370 new CElementLabelComposer(result).appendElementLabel(element, flags);
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.
379 public static void getMacroLabel(IMacro macro, int flags, StringBuffer buf) {
380 new CElementLabelComposer(buf).appendMacroLabel(macro, flags);
384 * Appends the label for a macro to a {@link StyledString}. Considers the M_* flags.
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
390 public static void getMethodLabel(IMacro macro, long flags, StyledString result) {
391 new CElementLabelComposer(result).appendMacroLabel(macro, flags);
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
400 public static void getMethodLabel( IMethodDeclaration method, int flags, StringBuffer buf ) {
401 new CElementLabelComposer(buf).appendMethodLabel(method, flags);
405 * Appends the label for a macro to a {@link StyledString}. Considers the M_* flags.
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
411 public static void getMethodLabel(IMethodDeclaration method, long flags, StyledString result) {
412 new CElementLabelComposer(result).appendMethodLabel(method, flags);
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
421 public static void getFieldLabel(IField field, int flags, StringBuffer buf ) {
422 new CElementLabelComposer(buf).appendFieldLabel(field, flags);
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
431 public static void getFieldLabel(IField field, int flags, StyledString result ) {
432 new CElementLabelComposer(result).appendFieldLabel(field, flags);
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
441 public static void getVariableLabel(IVariableDeclaration var, int flags, StringBuffer buf ) {
442 new CElementLabelComposer(buf).appendVariableLabel(var, flags);
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
451 public static void getVariableLabel(IVariableDeclaration var, int flags, StyledString result ) {
452 new CElementLabelComposer(result).appendVariableLabel(var, flags);
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
461 public static void getEnumeratorLabel(IEnumerator var, int flags, StringBuffer buf ) {
462 new CElementLabelComposer(buf).appendEnumeratorLabel(var, flags);
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
471 public static void getEnumeratorLabel(IEnumerator var, int flags, StyledString result ) {
472 new CElementLabelComposer(result).appendEnumeratorLabel(var, flags);
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
481 public static void getFunctionLabel(IFunctionDeclaration func, int flags, StringBuffer buf) {
482 new CElementLabelComposer(buf).appendFunctionLabel(func, flags);
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
491 public static void getFunctionLabel(IFunctionDeclaration func, int flags, StyledString result) {
492 new CElementLabelComposer(result).appendFunctionLabel(func, flags);
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
501 public static void getTypeDefLabel(ITypeDef typedef, int flags, StringBuffer buf ) {
502 new CElementLabelComposer(buf).appendTypeDefLabel(typedef, flags);
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
511 public static void getTypeDefLabel(ITypeDef typedef, int flags, StyledString result ) {
512 new CElementLabelComposer(result).appendTypeDefLabel(typedef, flags);