KickJava   Java API By Example, From Geeks To Geeks.

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


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.ui;
12
13 import org.eclipse.core.runtime.IAdaptable;
14 import org.eclipse.core.runtime.IPath;
15
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.IStorage;
19
20 import org.eclipse.jface.preference.IPreferenceStore;
21
22 import org.eclipse.ui.model.IWorkbenchAdapter;
23
24 import org.eclipse.jdt.core.BindingKey;
25 import org.eclipse.jdt.core.ClasspathContainerInitializer;
26 import org.eclipse.jdt.core.Flags;
27 import org.eclipse.jdt.core.IClassFile;
28 import org.eclipse.jdt.core.IClasspathContainer;
29 import org.eclipse.jdt.core.IClasspathEntry;
30 import org.eclipse.jdt.core.ICompilationUnit;
31 import org.eclipse.jdt.core.IField;
32 import org.eclipse.jdt.core.IInitializer;
33 import org.eclipse.jdt.core.IJavaElement;
34 import org.eclipse.jdt.core.IJavaProject;
35 import org.eclipse.jdt.core.ILocalVariable;
36 import org.eclipse.jdt.core.IMember;
37 import org.eclipse.jdt.core.IMethod;
38 import org.eclipse.jdt.core.IPackageFragment;
39 import org.eclipse.jdt.core.IPackageFragmentRoot;
40 import org.eclipse.jdt.core.IType;
41 import org.eclipse.jdt.core.ITypeParameter;
42 import org.eclipse.jdt.core.JavaCore;
43 import org.eclipse.jdt.core.JavaModelException;
44 import org.eclipse.jdt.core.Signature;
45
46 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
47 import org.eclipse.jdt.internal.corext.util.Messages;
48
49 import org.eclipse.jdt.internal.ui.JavaPlugin;
50 import org.eclipse.jdt.internal.ui.JavaUIMessages;
51 import org.eclipse.jdt.internal.ui.viewsupport.StorageLabelProvider;
52
53 /**
54  * <code>JavaElementLabels</code> provides helper methods to render names of Java elements.
55  *
56  * @since 3.1
57  */

58 public class JavaElementLabels {
59     
60     
61     /**
62      * Method names contain parameter types.
63      * e.g. <code>foo(int)</code>
64      */

65     public final static long M_PARAMETER_TYPES= 1L << 0;
66     
67     /**
68      * Method names contain parameter names.
69      * e.g. <code>foo(index)</code>
70      */

71     public final static long M_PARAMETER_NAMES= 1L << 1;
72     
73     /**
74      * Method names contain type parameters prepended.
75      * e.g. <code>&lt;A&gt; foo(A index)</code>
76      */

77     public final static long M_PRE_TYPE_PARAMETERS= 1L << 2;
78     
79     /**
80      * Method names contain type parameters appended.
81      * e.g. <code>foo(A index) &lt;A&gt;</code>
82      */

83     public final static long M_APP_TYPE_PARAMETERS= 1L << 3;
84     
85     /**
86      * Method names contain thrown exceptions.
87      * e.g. <code>foo throws IOException</code>
88      */

89     public final static long M_EXCEPTIONS= 1L << 4;
90     
91     /**
92      * Method names contain return type (appended)
93      * e.g. <code>foo : int</code>
94      */

95     public final static long M_APP_RETURNTYPE= 1L << 5;
96     
97     /**
98      * Method names contain return type (appended)
99      * e.g. <code>int foo</code>
100      */

101     public final static long M_PRE_RETURNTYPE= 1L << 6;
102
103     /**
104      * Method names are fully qualified.
105      * e.g. <code>java.util.Vector.size</code>
106      */

107     public final static long M_FULLY_QUALIFIED= 1L << 7;
108     
109     /**
110      * Method names are post qualified.
111      * e.g. <code>size - java.util.Vector</code>
112      */

113     public final static long M_POST_QUALIFIED= 1L << 8;
114     
115     /**
116      * Initializer names are fully qualified.
117      * e.g. <code>java.util.Vector.{ ... }</code>
118      */

119     public final static long I_FULLY_QUALIFIED= 1L << 10;
120     
121     /**
122      * Type names are post qualified.
123      * e.g. <code>{ ... } - java.util.Map</code>
124      */

125     public final static long I_POST_QUALIFIED= 1L << 11;
126     
127     /**
128      * Field names contain the declared type (appended)
129      * e.g. <code>fHello : int</code>
130      */

131     public final static long F_APP_TYPE_SIGNATURE= 1L << 14;
132     
133     /**
134      * Field names contain the declared type (prepended)
135      * e.g. <code>int fHello</code>
136      */

137     public final static long F_PRE_TYPE_SIGNATURE= 1L << 15;
138
139     /**
140      * Fields names are fully qualified.
141      * e.g. <code>java.lang.System.out</code>
142      */

143     public final static long F_FULLY_QUALIFIED= 1L << 16;
144     
145     /**
146      * Fields names are post qualified.
147      * e.g. <code>out - java.lang.System</code>
148      */

149     public final static long F_POST_QUALIFIED= 1L << 17;
150     
151     /**
152      * Type names are fully qualified.
153      * e.g. <code>java.util.Map.MapEntry</code>
154      */

155     public final static long T_FULLY_QUALIFIED= 1L << 18;
156     
157     /**
158      * Type names are type container qualified.
159      * e.g. <code>Map.MapEntry</code>
160      */

161     public final static long T_CONTAINER_QUALIFIED= 1L << 19;
162     
163     /**
164      * Type names are post qualified.
165      * e.g. <code>MapEntry - java.util.Map</code>
166      */

167     public final static long T_POST_QUALIFIED= 1L << 20;
168     
169     /**
170      * Type names contain type parameters.
171      * e.g. <code>Map&lt;S, T&gt;</code>
172      */

173     public final static long T_TYPE_PARAMETERS= 1L << 21;
174     
175     /**
176      * Declarations (import container / declaration, package declaration) are qualified.
177      * e.g. <code>java.util.Vector.class/import container</code>
178      */

179     public final static long D_QUALIFIED= 1L << 24;
180     
181     /**
182      * Declarations (import container / declaration, package declaration) are post qualified.
183      * e.g. <code>import container - java.util.Vector.class</code>
184      */

185     public final static long D_POST_QUALIFIED= 1L << 25;
186
187     /**
188      * Class file names are fully qualified.
189      * e.g. <code>java.util.Vector.class</code>
190      */

191     public final static long CF_QUALIFIED= 1L << 27;
192     
193     /**
194      * Class file names are post qualified.
195      * e.g. <code>Vector.class - java.util</code>
196      */

197     public final static long CF_POST_QUALIFIED= 1L << 28;
198     
199     /**
200      * Compilation unit names are fully qualified.
201      * e.g. <code>java.util.Vector.java</code>
202      */

203     public final static long CU_QUALIFIED= 1L << 31;
204     
205     /**
206      * Compilation unit names are post qualified.
207      * e.g. <code>Vector.java - java.util</code>
208      */

209     public final static long CU_POST_QUALIFIED= 1L << 32;
210
211     /**
212      * Package names are qualified.
213      * e.g. <code>MyProject/src/java.util</code>
214      */

215     public final static long P_QUALIFIED= 1L << 35;
216     
217     /**
218      * Package names are post qualified.
219      * e.g. <code>java.util - MyProject/src</code>
220      */

221     public final static long P_POST_QUALIFIED= 1L << 36;
222     
223     /**
224      * Package names are compressed.
225      * e.g. <code>o*.e*.search</code>
226      */

227     public final static long P_COMPRESSED= 1L << 37;
228
229     /**
230      * Package Fragment Roots contain variable name if from a variable.
231      * e.g. <code>JRE_LIB - c:\java\lib\rt.jar</code>
232      */

233     public final static long ROOT_VARIABLE= 1L << 40;
234     
235     /**
236      * Package Fragment Roots contain the project name if not an archive (prepended).
237      * e.g. <code>MyProject/src</code>
238      */

239     public final static long ROOT_QUALIFIED= 1L << 41;
240     
241     /**
242      * Package Fragment Roots contain the project name if not an archive (appended).
243      * e.g. <code>src - MyProject</code>
244      */

245     public final static long ROOT_POST_QUALIFIED= 1L << 42;
246     
247     /**
248      * Add root path to all elements except Package Fragment Roots and Java projects.
249      * e.g. <code>java.lang.Vector - c:\java\lib\rt.jar</code>
250      * Option only applies to getElementLabel
251      */

252     public final static long APPEND_ROOT_PATH= 1L << 43;
253
254     /**
255      * Add root path to all elements except Package Fragment Roots and Java projects.
256      * e.g. <code>java.lang.Vector - c:\java\lib\rt.jar</code>
257      * Option only applies to getElementLabel
258      */

259     public final static long PREPEND_ROOT_PATH= 1L << 44;
260
261     /**
262      * Post qualify referenced package fragment roots. For example
263      * <code>jdt.jar - org.eclipse.jdt.ui</code> if the jar is referenced
264      * from another project.
265      */

266     public final static long REFERENCED_ROOT_POST_QUALIFIED= 1L << 45;
267     
268     /**
269      * Specified to use the resolved information of a IType, IMethod or IField. See {@link IType#isResolved()}.
270      * If resolved information is available, types will be rendered with type parameters of the instantiated type.
271      * Resolved method render with the parameter types of the method instance.
272      * <code>Vector&lt;String&gt;.get(String)</code>
273      */

274     public final static long USE_RESOLVED= 1L << 48;
275     
276     /**
277      * Prepend first category (if any) to field.
278      * @since 3.2
279      */

280     public final static long F_CATEGORY= 1L << 49;
281     /**
282      * Prepend first category (if any) to method.
283      * @since 3.2
284      */

285     public final static long M_CATEGORY= 1L << 50;
286     /**
287      * Prepend first category (if any) to type.
288      * @since 3.2
289      */

290     public final static long T_CATEGORY= 1L << 51;
291     
292     /**
293      * Show category for all elements.
294      * @since 3.2
295      */

296     public final static long ALL_CATEGORY= new Long JavaDoc(JavaElementLabels.F_CATEGORY | JavaElementLabels.M_CATEGORY | JavaElementLabels.T_CATEGORY).longValue();
297     
298     /**
299      * Qualify all elements
300      */

301     public final static long ALL_FULLY_QUALIFIED= new Long JavaDoc(F_FULLY_QUALIFIED | M_FULLY_QUALIFIED | I_FULLY_QUALIFIED | T_FULLY_QUALIFIED | D_QUALIFIED | CF_QUALIFIED | CU_QUALIFIED | P_QUALIFIED | ROOT_QUALIFIED).longValue();
302
303     
304     /**
305      * Post qualify all elements
306      */

307     public final static long ALL_POST_QUALIFIED= new Long JavaDoc(F_POST_QUALIFIED | M_POST_QUALIFIED | I_POST_QUALIFIED | T_POST_QUALIFIED | D_POST_QUALIFIED | CF_POST_QUALIFIED | CU_POST_QUALIFIED | P_POST_QUALIFIED | ROOT_POST_QUALIFIED).longValue();
308
309     /**
310      * Default options (M_PARAMETER_TYPES, M_APP_TYPE_PARAMETERS & T_TYPE_PARAMETERS enabled)
311      */

312     public final static long ALL_DEFAULT= new Long JavaDoc(M_PARAMETER_TYPES | M_APP_TYPE_PARAMETERS | T_TYPE_PARAMETERS).longValue();
313
314     /**
315      * Default qualify options (All except Root and Package)
316      */

317     public final static long DEFAULT_QUALIFIED= new Long JavaDoc(F_FULLY_QUALIFIED | M_FULLY_QUALIFIED | I_FULLY_QUALIFIED | T_FULLY_QUALIFIED | D_QUALIFIED | CF_QUALIFIED | CU_QUALIFIED).longValue();
318
319     /**
320      * Default post qualify options (All except Root and Package)
321      */

322     public final static long DEFAULT_POST_QUALIFIED= new Long JavaDoc(F_POST_QUALIFIED | M_POST_QUALIFIED | I_POST_QUALIFIED | T_POST_QUALIFIED | D_POST_QUALIFIED | CF_POST_QUALIFIED | CU_POST_QUALIFIED).longValue();
323
324     /**
325      * User-readable string for separating post qualified names (e.g. " - ").
326      */

327     public final static String JavaDoc CONCAT_STRING= JavaUIMessages.JavaElementLabels_concat_string;
328     /**
329      * User-readable string for separating list items (e.g. ", ").
330      */

331     public final static String JavaDoc COMMA_STRING= JavaUIMessages.JavaElementLabels_comma_string;
332     /**
333      * User-readable string for separating the return type (e.g. " : ").
334      */

335     public final static String JavaDoc DECL_STRING= JavaUIMessages.JavaElementLabels_declseparator_string;
336     /**
337      * User-readable string for concatenating categories (e.g. " ").
338      * XXX: to be made API post 3.2
339      * @since 3.2
340      */

341     private final static String JavaDoc CATEGORY_SEPARATOR_STRING= JavaUIMessages.JavaElementLabels_category_separator_string;
342     /**
343      * User-readable string for ellipsis ("...").
344      */

345     public final static String JavaDoc ELLIPSIS_STRING= "..."; //$NON-NLS-1$
346
/**
347      * User-readable string for the default package name (e.g. "(default package)").
348      */

349     public final static String JavaDoc DEFAULT_PACKAGE= JavaUIMessages.JavaElementLabels_default_package;
350     
351     
352     private final static long QUALIFIER_FLAGS= P_COMPRESSED | USE_RESOLVED;
353     
354     /*
355      * Package name compression
356      */

357     private static String JavaDoc fgPkgNamePattern= ""; //$NON-NLS-1$
358
private static String JavaDoc fgPkgNamePrefix;
359     private static String JavaDoc fgPkgNamePostfix;
360     private static int fgPkgNameChars;
361     private static int fgPkgNameLength= -1;
362
363     private JavaElementLabels() {
364     }
365
366     private static final boolean getFlag(long flags, long flag) {
367         return (flags & flag) != 0;
368     }
369     
370     /**
371      * Returns the label of the given object. The object must be of type {@link IJavaElement} or adapt to {@link IWorkbenchAdapter}. The empty string is returned
372      * if the element type is not known.
373      * @param obj Object to get the label from.
374      * @param flags The rendering flags
375      * @return Returns the label or the empty string if the object type is not supported.
376      */

377     public static String JavaDoc getTextLabel(Object JavaDoc obj, long flags) {
378         if (obj instanceof IJavaElement) {
379             return getElementLabel((IJavaElement) obj, flags);
380         } else if (obj instanceof IResource) {
381             return ((IResource) obj).getName();
382         } else if (obj instanceof IStorage) {
383             StorageLabelProvider storageLabelProvider= new StorageLabelProvider();
384             String JavaDoc label= storageLabelProvider.getText(obj);
385             storageLabelProvider.dispose();
386             return label;
387         } else if (obj instanceof IAdaptable) {
388             IWorkbenchAdapter wbadapter= (IWorkbenchAdapter) ((IAdaptable)obj).getAdapter(IWorkbenchAdapter.class);
389             if (wbadapter != null) {
390                 return wbadapter.getLabel(obj);
391             }
392         }
393         return ""; //$NON-NLS-1$
394
}
395                 
396     /**
397      * Returns the label for a Java element with the flags as defined by this class.
398      * @param element The element to render.
399      * @param flags The rendering flags.
400      * @return the label of the Java element
401      */

402     public static String JavaDoc getElementLabel(IJavaElement element, long flags) {
403         StringBuffer JavaDoc buf= new StringBuffer JavaDoc(60);
404         getElementLabel(element, flags, buf);
405         return buf.toString();
406     }
407     
408     /**
409      * Returns the label for a Java element with the flags as defined by this class.
410      * @param element The element to render.
411      * @param flags The rendering flags.
412      * @param buf The buffer to append the resulting label to.
413      */

414     public static void getElementLabel(IJavaElement element, long flags, StringBuffer JavaDoc buf) {
415         int type= element.getElementType();
416         IPackageFragmentRoot root= null;
417         
418         if (type != IJavaElement.JAVA_MODEL && type != IJavaElement.JAVA_PROJECT && type != IJavaElement.PACKAGE_FRAGMENT_ROOT)
419             root= JavaModelUtil.getPackageFragmentRoot(element);
420         if (root != null && getFlag(flags, PREPEND_ROOT_PATH)) {
421             getPackageFragmentRootLabel(root, ROOT_QUALIFIED, buf);
422             buf.append(CONCAT_STRING);
423         }
424         
425         switch (type) {
426             case IJavaElement.METHOD:
427                 getMethodLabel((IMethod) element, flags, buf);
428                 break;
429             case IJavaElement.FIELD:
430                 getFieldLabel((IField) element, flags, buf);
431                 break;
432             case IJavaElement.LOCAL_VARIABLE:
433                 getLocalVariableLabel((ILocalVariable) element, flags, buf);
434                 break;
435             case IJavaElement.INITIALIZER:
436                 getInitializerLabel((IInitializer) element, flags, buf);
437                 break;
438             case IJavaElement.TYPE:
439                 getTypeLabel((IType) element, flags, buf);
440                 break;
441             case IJavaElement.CLASS_FILE:
442                 getClassFileLabel((IClassFile) element, flags, buf);
443                 break;
444             case IJavaElement.COMPILATION_UNIT:
445                 getCompilationUnitLabel((ICompilationUnit) element, flags, buf);
446                 break;
447             case IJavaElement.PACKAGE_FRAGMENT:
448                 getPackageFragmentLabel((IPackageFragment) element, flags, buf);
449                 break;
450             case IJavaElement.PACKAGE_FRAGMENT_ROOT:
451                 getPackageFragmentRootLabel((IPackageFragmentRoot) element, flags, buf);
452                 break;
453             case IJavaElement.IMPORT_CONTAINER:
454             case IJavaElement.IMPORT_DECLARATION:
455             case IJavaElement.PACKAGE_DECLARATION:
456                 getDeclarationLabel(element, flags, buf);
457                 break;
458             case IJavaElement.JAVA_PROJECT:
459             case IJavaElement.JAVA_MODEL:
460                 buf.append(element.getElementName());
461                 break;
462             default:
463                 buf.append(element.getElementName());
464         }
465         
466         if (root != null && getFlag(flags, APPEND_ROOT_PATH)) {
467             buf.append(CONCAT_STRING);
468             getPackageFragmentRootLabel(root, ROOT_QUALIFIED, buf);
469         }
470     }
471
472     /**
473      * Appends the label for a method to a {@link StringBuffer}. Considers the M_* flags.
474      * @param method The element to render.
475      * @param flags The rendering flags. Flags with names starting with 'M_' are considered.
476      * @param buf The buffer to append the resulting label to.
477      */

478     public static void getMethodLabel(IMethod method, long flags, StringBuffer JavaDoc buf) {
479         try {
480             BindingKey resolvedKey= getFlag(flags, USE_RESOLVED) && method.isResolved() ? new BindingKey(method.getKey()) : null;
481             String JavaDoc resolvedSig= (resolvedKey != null) ? resolvedKey.toSignature() : null;
482             
483             // type parameters
484
if (getFlag(flags, M_PRE_TYPE_PARAMETERS)) {
485                 if (resolvedKey != null) {
486                     if (resolvedKey.isParameterizedMethod()) {
487                         String JavaDoc[] typeArgRefs= resolvedKey.getTypeArguments();
488                         if (typeArgRefs.length > 0) {
489                             getTypeArgumentSignaturesLabel(typeArgRefs, flags, buf);
490                             buf.append(' ');
491                         }
492                     } else {
493                         String JavaDoc[] typeParameterSigs= Signature.getTypeParameters(resolvedSig);
494                         if (typeParameterSigs.length > 0) {
495                             getTypeParameterSignaturesLabel(typeParameterSigs, flags, buf);
496                             buf.append(' ');
497                         }
498                     }
499                 } else if (method.exists()) {
500                     ITypeParameter[] typeParameters= method.getTypeParameters();
501                     if (typeParameters.length > 0) {
502                         getTypeParametersLabel(typeParameters, flags, buf);
503                         buf.append(' ');
504                     }
505                 }
506             }
507             
508             // return type
509
if (getFlag(flags, M_PRE_RETURNTYPE) && method.exists() && !method.isConstructor()) {
510                 String JavaDoc returnTypeSig= resolvedSig != null ? Signature.getReturnType(resolvedSig) : method.getReturnType();
511                 getTypeSignatureLabel(returnTypeSig, flags, buf);
512                 buf.append(' ');
513             }
514             
515             // qualification
516
if (getFlag(flags, M_FULLY_QUALIFIED)) {
517                 getTypeLabel(method.getDeclaringType(), T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf);
518                 buf.append('.');
519             }
520                 
521             buf.append(method.getElementName());
522             
523             // parameters
524
buf.append('(');
525             if (getFlag(flags, M_PARAMETER_TYPES | M_PARAMETER_NAMES)) {
526                 String JavaDoc[] types= null;
527                 int nParams= 0;
528                 boolean renderVarargs= false;
529                 if (getFlag(flags, M_PARAMETER_TYPES)) {
530                     if (resolvedSig != null) {
531                         types= Signature.getParameterTypes(resolvedSig);
532                     } else {
533                         types= method.getParameterTypes();
534                     }
535                     nParams= types.length;
536                     renderVarargs= method.exists() && Flags.isVarargs(method.getFlags());
537                 }
538                 String JavaDoc[] names= null;
539                 if (getFlag(flags, M_PARAMETER_NAMES) && method.exists()) {
540                     names= method.getParameterNames();
541                     if (types == null) {
542                         nParams= names.length;
543                     } else { // types != null
544
if (nParams != names.length) {
545                             if (resolvedSig != null && types.length > names.length) {
546                                 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=99137
547
nParams= names.length;
548                                 String JavaDoc[] typesWithoutSyntheticParams= new String JavaDoc[nParams];
549                                 System.arraycopy(types, types.length - nParams, typesWithoutSyntheticParams, 0, nParams);
550                                 types= typesWithoutSyntheticParams;
551                             } else {
552                                 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=101029
553
// JavaPlugin.logErrorMessage("JavaElementLabels: Number of param types(" + nParams + ") != number of names(" + names.length + "): " + method.getElementName()); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
554
names= null; // no names rendered
555
}
556                         }
557                     }
558                 }
559                 
560                 for (int i= 0; i < nParams; i++) {
561                     if (i > 0) {
562                         buf.append(COMMA_STRING);
563                     }
564                     if (types != null) {
565                         String JavaDoc paramSig= types[i];
566                         if (renderVarargs && (i == nParams - 1)) {
567                             int newDim= Signature.getArrayCount(paramSig) - 1;
568                             getTypeSignatureLabel(Signature.getElementType(paramSig), flags, buf);
569                             for (int k= 0; k < newDim; k++) {
570                                 buf.append('[').append(']');
571                             }
572                             buf.append(ELLIPSIS_STRING);
573                         } else {
574                             getTypeSignatureLabel(paramSig, flags, buf);
575                         }
576                     }
577                     if (names != null) {
578                         if (types != null) {
579                             buf.append(' ');
580                         }
581                         buf.append(names[i]);
582                     }
583                 }
584             } else {
585                 if (method.getParameterTypes().length > 0) {
586                     buf.append(ELLIPSIS_STRING);
587                 }
588             }
589             buf.append(')');
590                     
591             if (getFlag(flags, M_EXCEPTIONS)) {
592                 String JavaDoc[] types;
593                 if (resolvedKey != null) {
594                     types= resolvedKey.getThrownExceptions();
595                 } else {
596                     types= method.exists() ? method.getExceptionTypes() : new String JavaDoc[0];
597                 }
598                 if (types.length > 0) {
599                     buf.append(" throws "); //$NON-NLS-1$
600
for (int i= 0; i < types.length; i++) {
601                         if (i > 0) {
602                             buf.append(COMMA_STRING);
603                         }
604                         getTypeSignatureLabel(types[i], flags, buf);
605                     }
606                 }
607             }
608             
609             if (getFlag(flags, M_APP_TYPE_PARAMETERS)) {
610                 if (resolvedKey != null) {
611                     if (resolvedKey.isParameterizedMethod()) {
612                         String JavaDoc[] typeArgRefs= resolvedKey.getTypeArguments();
613                         if (typeArgRefs.length > 0) {
614                             buf.append(' ');
615                             getTypeArgumentSignaturesLabel(typeArgRefs, flags, buf);
616                         }
617                     } else {
618                         String JavaDoc[] typeParameterSigs= Signature.getTypeParameters(resolvedSig);
619                         if (typeParameterSigs.length > 0) {
620                             buf.append(' ');
621                             getTypeParameterSignaturesLabel(typeParameterSigs, flags, buf);
622                         }
623                     }
624                 } else if (method.exists()) {
625                     ITypeParameter[] typeParameters= method.getTypeParameters();
626                     if (typeParameters.length > 0) {
627                         buf.append(' ');
628                         getTypeParametersLabel(typeParameters, flags, buf);
629                     }
630                 }
631             }
632             
633             if (getFlag(flags, M_APP_RETURNTYPE) && method.exists() && !method.isConstructor()) {
634                 buf.append(DECL_STRING);
635                 String JavaDoc returnTypeSig= resolvedSig != null ? Signature.getReturnType(resolvedSig) : method.getReturnType();
636                 getTypeSignatureLabel(returnTypeSig, flags, buf);
637             }
638
639             // category
640
if (getFlag(flags, M_CATEGORY) && method.exists())
641                 getCategoryLabel(method, buf);
642             
643             // post qualification
644
if (getFlag(flags, M_POST_QUALIFIED)) {
645                 buf.append(CONCAT_STRING);
646                 getTypeLabel(method.getDeclaringType(), T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf);
647             }
648             
649         } catch (JavaModelException e) {
650             JavaPlugin.log(e); // NotExistsException will not reach this point
651
}
652     }
653
654     private static void getCategoryLabel(IMember member, StringBuffer JavaDoc buf) throws JavaModelException {
655         String JavaDoc[] categories= member.getCategories();
656         if (categories.length > 0) {
657             StringBuffer JavaDoc categoriesBuf= new StringBuffer JavaDoc(30);
658             for (int i= 0; i < categories.length; i++) {
659                 if (i > 0)
660                     categoriesBuf.append(CATEGORY_SEPARATOR_STRING);
661                 categoriesBuf.append(categories[i]);
662             }
663             buf.append(CONCAT_STRING);
664             buf.append(Messages.format(JavaUIMessages.JavaElementLabels_category , categoriesBuf.toString()));
665         }
666     }
667     
668     private static void getTypeParametersLabel(ITypeParameter[] typeParameters, long flags, StringBuffer JavaDoc buf) {
669         if (typeParameters.length > 0) {
670             buf.append('<');
671             for (int i = 0; i < typeParameters.length; i++) {
672                 if (i > 0) {
673                     buf.append(COMMA_STRING);
674                 }
675                 buf.append(typeParameters[i].getElementName());
676             }
677             buf.append('>');
678         }
679     }
680     
681     /**
682      * Appends the label for a field to a {@link StringBuffer}. Considers the F_* flags.
683      * @param field The element to render.
684      * @param flags The rendering flags. Flags with names starting with 'F_' are considered.
685      * @param buf The buffer to append the resulting label to.
686      */

687     public static void getFieldLabel(IField field, long flags, StringBuffer JavaDoc buf) {
688         try {
689             
690             if (getFlag(flags, F_PRE_TYPE_SIGNATURE) && field.exists() && !Flags.isEnum(field.getFlags())) {
691                 if (getFlag(flags, USE_RESOLVED) && field.isResolved()) {
692                     getTypeSignatureLabel(new BindingKey(field.getKey()).toSignature(), flags, buf);
693                 } else {
694                     getTypeSignatureLabel(field.getTypeSignature(), flags, buf);
695                 }
696                 buf.append(' ');
697             }
698             
699             // qualification
700
if (getFlag(flags, F_FULLY_QUALIFIED)) {
701                 getTypeLabel(field.getDeclaringType(), T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf);
702                 buf.append('.');
703             }
704             buf.append(field.getElementName());
705             
706             if (getFlag(flags, F_APP_TYPE_SIGNATURE) && field.exists() && !Flags.isEnum(field.getFlags())) {
707                 buf.append(DECL_STRING);
708                 if (getFlag(flags, USE_RESOLVED) && field.isResolved()) {
709                     getTypeSignatureLabel(new BindingKey(field.getKey()).toSignature(), flags, buf);
710                 } else {
711                     getTypeSignatureLabel(field.getTypeSignature(), flags, buf);
712                 }
713             }
714
715             // category
716
if (getFlag(flags, F_CATEGORY) && field.exists())
717                 getCategoryLabel(field, buf);
718
719             // post qualification
720
if (getFlag(flags, F_POST_QUALIFIED)) {
721                 buf.append(CONCAT_STRING);
722                 getTypeLabel(field.getDeclaringType(), T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf);
723             }
724
725         } catch (JavaModelException e) {
726             JavaPlugin.log(e); // NotExistsException will not reach this point
727
}
728     }
729     
730     /**
731      * Appends the label for a local variable to a {@link StringBuffer}.
732      * @param localVariable The element to render.
733      * @param flags The rendering flags. Flags with names starting with 'F_' are considered.
734      * @param buf The buffer to append the resulting label to.
735      */

736     public static void getLocalVariableLabel(ILocalVariable localVariable, long flags, StringBuffer JavaDoc buf) {
737         if (getFlag(flags, F_PRE_TYPE_SIGNATURE)) {
738             getTypeSignatureLabel(localVariable.getTypeSignature(), flags, buf);
739             buf.append(' ');
740         }
741         
742         if (getFlag(flags, F_FULLY_QUALIFIED)) {
743             getElementLabel(localVariable.getParent(), M_PARAMETER_TYPES | M_FULLY_QUALIFIED | T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf);
744             buf.append('.');
745         }
746         
747         buf.append(localVariable.getElementName());
748         
749         if (getFlag(flags, F_APP_TYPE_SIGNATURE)) {
750             buf.append(DECL_STRING);
751             getTypeSignatureLabel(localVariable.getTypeSignature(), flags, buf);
752         }
753         
754         // post qualification
755
if (getFlag(flags, F_POST_QUALIFIED)) {
756             buf.append(CONCAT_STRING);
757             getElementLabel(localVariable.getParent(), M_PARAMETER_TYPES | M_FULLY_QUALIFIED | T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf);
758         }
759     }
760     
761     /**
762      * Appends the label for a initializer to a {@link StringBuffer}. Considers the I_* flags.
763      * @param initializer The element to render.
764      * @param flags The rendering flags. Flags with names starting with 'I_' are considered.
765      * @param buf The buffer to append the resulting label to.
766      */

767     public static void getInitializerLabel(IInitializer initializer, long flags, StringBuffer JavaDoc buf) {
768         // qualification
769
if (getFlag(flags, I_FULLY_QUALIFIED)) {
770             getTypeLabel(initializer.getDeclaringType(), T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf);
771             buf.append('.');
772         }
773         buf.append(JavaUIMessages.JavaElementLabels_initializer);
774
775         // post qualification
776
if (getFlag(flags, I_POST_QUALIFIED)) {
777             buf.append(CONCAT_STRING);
778             getTypeLabel(initializer.getDeclaringType(), T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf);
779         }
780     }
781     
782     private static void getTypeSignatureLabel(String JavaDoc typeSig, long flags, StringBuffer JavaDoc buf) {
783         int sigKind= Signature.getTypeSignatureKind(typeSig);
784         switch (sigKind) {
785             case Signature.BASE_TYPE_SIGNATURE:
786                 buf.append(Signature.toString(typeSig));
787                 break;
788             case Signature.ARRAY_TYPE_SIGNATURE:
789                 getTypeSignatureLabel(Signature.getElementType(typeSig), flags, buf);
790                 for (int dim= Signature.getArrayCount(typeSig); dim > 0; dim--) {
791                     buf.append('[').append(']');
792                 }
793                 break;
794             case Signature.CLASS_TYPE_SIGNATURE:
795                 String JavaDoc baseType= Signature.toString(Signature.getTypeErasure(typeSig));
796                 buf.append(Signature.getSimpleName(baseType));
797                 
798                 String JavaDoc[] typeArguments= Signature.getTypeArguments(typeSig);
799                 getTypeArgumentSignaturesLabel(typeArguments, flags, buf);
800                 break;
801             case Signature.TYPE_VARIABLE_SIGNATURE:
802                 buf.append(Signature.toString(typeSig));
803                 break;
804             case Signature.WILDCARD_TYPE_SIGNATURE:
805                 char ch= typeSig.charAt(0);
806                 if (ch == Signature.C_STAR) { //workaround for bug 85713
807
buf.append('?');
808                 } else {
809                     if (ch == Signature.C_EXTENDS) {
810                         buf.append("? extends "); //$NON-NLS-1$
811
getTypeSignatureLabel(typeSig.substring(1), flags, buf);
812                     } else if (ch == Signature.C_SUPER) {
813                         buf.append("? super "); //$NON-NLS-1$
814
getTypeSignatureLabel(typeSig.substring(1), flags, buf);
815                     }
816                 }
817                 break;
818             case Signature.CAPTURE_TYPE_SIGNATURE:
819                 getTypeSignatureLabel(typeSig.substring(1), flags, buf);
820                 break;
821             default:
822                 // unknown
823
}
824     }
825     
826     private static void getTypeArgumentSignaturesLabel(String JavaDoc[] typeArgsSig, long flags, StringBuffer JavaDoc buf) {
827         if (typeArgsSig.length > 0) {
828             buf.append('<');
829             for (int i = 0; i < typeArgsSig.length; i++) {
830                 if (i > 0) {
831                     buf.append(COMMA_STRING);
832                 }
833                 getTypeSignatureLabel(typeArgsSig[i], flags, buf);
834             }
835             buf.append('>');
836         }
837     }
838     
839     private static void getTypeParameterSignaturesLabel(String JavaDoc[] typeParamSigs, long flags, StringBuffer JavaDoc buf) {
840         if (typeParamSigs.length > 0) {
841             buf.append('<');
842             for (int i = 0; i < typeParamSigs.length; i++) {
843                 if (i > 0) {
844                     buf.append(COMMA_STRING);
845                 }
846                 buf.append(Signature.getTypeVariable(typeParamSigs[i]));
847             }
848             buf.append('>');
849         }
850     }
851     
852
853     /**
854      * Appends the label for a type to a {@link StringBuffer}. Considers the T_* flags.
855      * @param type The element to render.
856      * @param flags The rendering flags. Flags with names starting with 'T_' are considered.
857      * @param buf The buffer to append the resulting label to.
858      */

859     public static void getTypeLabel(IType type, long flags, StringBuffer JavaDoc buf) {
860         
861         if (getFlag(flags, T_FULLY_QUALIFIED)) {
862             IPackageFragment pack= type.getPackageFragment();
863             if (!pack.isDefaultPackage()) {
864                 getPackageFragmentLabel(pack, (flags & QUALIFIER_FLAGS), buf);
865                 buf.append('.');
866             }
867         }
868         if (getFlag(flags, T_FULLY_QUALIFIED | T_CONTAINER_QUALIFIED)) {
869             IType declaringType= type.getDeclaringType();
870             if (declaringType != null) {
871                 getTypeLabel(declaringType, T_CONTAINER_QUALIFIED | (flags & QUALIFIER_FLAGS), buf);
872                 buf.append('.');
873             }
874             int parentType= type.getParent().getElementType();
875             if (parentType == IJavaElement.METHOD || parentType == IJavaElement.FIELD || parentType == IJavaElement.INITIALIZER) { // anonymous or local
876
getElementLabel(type.getParent(), 0, buf);
877                 buf.append('.');
878             }
879         }
880         
881         String JavaDoc typeName= type.getElementName();
882         if (typeName.length() == 0) { // anonymous
883
try {
884                 if (type.getParent() instanceof IField && type.isEnum()) {
885                     typeName= '{' + ELLIPSIS_STRING + '}';
886                 } else {
887                     String JavaDoc supertypeName;
888                     String JavaDoc[] superInterfaceNames= type.getSuperInterfaceNames();
889                     if (superInterfaceNames.length > 0) {
890                         supertypeName= Signature.getSimpleName(superInterfaceNames[0]);
891                     } else {
892                         supertypeName= Signature.getSimpleName(type.getSuperclassName());
893                     }
894                     typeName= Messages.format(JavaUIMessages.JavaElementLabels_anonym_type , supertypeName);
895                 }
896             } catch (JavaModelException e) {
897                 //ignore
898
typeName= JavaUIMessages.JavaElementLabels_anonym;
899             }
900         }
901         buf.append(typeName);
902         if (getFlag(flags, T_TYPE_PARAMETERS)) {
903             if (getFlag(flags, USE_RESOLVED) && type.isResolved()) {
904                 BindingKey key= new BindingKey(type.getKey());
905                 if (key.isParameterizedType()) {
906                     String JavaDoc[] typeArguments= key.getTypeArguments();
907                     getTypeArgumentSignaturesLabel(typeArguments, flags, buf);
908                 } else {
909                     String JavaDoc[] typeParameters= Signature.getTypeParameters(key.toSignature());
910                     getTypeParameterSignaturesLabel(typeParameters, flags, buf);
911                 }
912             } else if (type.exists()) {
913                 try {
914                     getTypeParametersLabel(type.getTypeParameters(), flags, buf);
915                 } catch (JavaModelException e) {
916                     // ignore
917
}
918             }
919         }
920         
921         // category
922
if (getFlag(flags, T_CATEGORY) && type.exists()) {
923             try {
924                 getCategoryLabel(type, buf);
925             } catch (JavaModelException e) {
926                 // ignore
927
}
928         }
929
930         // post qualification
931
if (getFlag(flags, T_POST_QUALIFIED)) {
932             buf.append(CONCAT_STRING);
933             IType declaringType= type.getDeclaringType();
934             if (declaringType != null) {
935                 getTypeLabel(declaringType, T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf);
936                 int parentType= type.getParent().getElementType();
937                 if (parentType == IJavaElement.METHOD || parentType == IJavaElement.FIELD || parentType == IJavaElement.INITIALIZER) { // anonymous or local
938
buf.append('.');
939                     getElementLabel(type.getParent(), 0, buf);
940                 }
941             } else {
942                 getPackageFragmentLabel(type.getPackageFragment(), flags & QUALIFIER_FLAGS, buf);
943             }
944         }
945     }
946
947     /**
948      * Appends the label for a import container, import or package declaration to a {@link StringBuffer}. Considers the D_* flags.
949      * @param declaration The element to render.
950      * @param flags The rendering flags. Flags with names starting with 'D_' are considered.
951      * @param buf The buffer to append the resulting label to.
952      */

953     public static void getDeclarationLabel(IJavaElement declaration, long flags, StringBuffer JavaDoc buf) {
954         if (getFlag(flags, D_QUALIFIED)) {
955             IJavaElement openable= (IJavaElement) declaration.getOpenable();
956             if (openable != null) {
957                 buf.append(getElementLabel(openable, CF_QUALIFIED | CU_QUALIFIED | (flags & QUALIFIER_FLAGS)));
958                 buf.append('/');
959             }
960         }
961         if (declaration.getElementType() == IJavaElement.IMPORT_CONTAINER) {
962             buf.append(JavaUIMessages.JavaElementLabels_import_container);
963         } else {
964             buf.append(declaration.getElementName());
965         }
966         // post qualification
967
if (getFlag(flags, D_POST_QUALIFIED)) {
968             IJavaElement openable= (IJavaElement) declaration.getOpenable();
969             if (openable != null) {
970                 buf.append(CONCAT_STRING);
971                 buf.append(getElementLabel(openable, CF_QUALIFIED | CU_QUALIFIED | (flags & QUALIFIER_FLAGS)));
972             }
973         }
974     }
975     
976     /**
977      * Appends the label for a class file to a {@link StringBuffer}. Considers the CF_* flags.
978      * @param classFile The element to render.
979      * @param flags The rendering flags. Flags with names starting with 'CF_' are considered.
980      * @param buf The buffer to append the resulting label to.
981      */

982     public static void getClassFileLabel(IClassFile classFile, long flags, StringBuffer JavaDoc buf) {
983         if (getFlag(flags, CF_QUALIFIED)) {
984             IPackageFragment pack= (IPackageFragment) classFile.getParent();
985             if (!pack.isDefaultPackage()) {
986                 getPackageFragmentLabel(pack, (flags & QUALIFIER_FLAGS), buf);
987                 buf.append('.');
988             }
989         }
990         buf.append(classFile.getElementName());
991         
992         if (getFlag(flags, CF_POST_QUALIFIED)) {
993             buf.append(CONCAT_STRING);
994             getPackageFragmentLabel((IPackageFragment) classFile.getParent(), flags & QUALIFIER_FLAGS, buf);
995         }
996     }
997
998     /**
999      * Appends the label for a compilation unit to a {@link StringBuffer}. Considers the CU_* flags.
1000     * @param cu The element to render.
1001     * @param flags The rendering flags. Flags with names starting with 'CU_' are considered.
1002     * @param buf The buffer to append the resulting label to.
1003     */

1004    public static void getCompilationUnitLabel(ICompilationUnit cu, long flags, StringBuffer JavaDoc buf) {
1005        if (getFlag(flags, CU_QUALIFIED)) {
1006            IPackageFragment pack= (IPackageFragment) cu.getParent();
1007            if (!pack.isDefaultPackage()) {
1008                getPackageFragmentLabel(pack, (flags & QUALIFIER_FLAGS), buf);
1009                buf.append('.');
1010            }
1011        }
1012        buf.append(cu.getElementName());
1013        
1014        if (getFlag(flags, CU_POST_QUALIFIED)) {
1015            buf.append(CONCAT_STRING);
1016            getPackageFragmentLabel((IPackageFragment) cu.getParent(), flags & QUALIFIER_FLAGS, buf);
1017        }
1018    }
1019
1020    /**
1021     * Appends the label for a package fragment to a {@link StringBuffer}. Considers the P_* flags.
1022     * @param pack The element to render.
1023     * @param flags The rendering flags. Flags with names starting with P_' are considered.
1024     * @param buf The buffer to append the resulting label to.
1025     */

1026    public static void getPackageFragmentLabel(IPackageFragment pack, long flags, StringBuffer JavaDoc buf) {
1027        if (getFlag(flags, P_QUALIFIED)) {
1028            getPackageFragmentRootLabel((IPackageFragmentRoot) pack.getParent(), ROOT_QUALIFIED, buf);
1029            buf.append('/');
1030        }
1031        refreshPackageNamePattern();
1032        if (pack.isDefaultPackage()) {
1033            buf.append(DEFAULT_PACKAGE);
1034        } else if (getFlag(flags, P_COMPRESSED) && fgPkgNameLength >= 0) {
1035                String JavaDoc name= pack.getElementName();
1036                int start= 0;
1037                int dot= name.indexOf('.', start);
1038                while (dot > 0) {
1039                    if (dot - start > fgPkgNameLength-1) {
1040                        buf.append(fgPkgNamePrefix);
1041                        if (fgPkgNameChars > 0)
1042                            buf.append(name.substring(start, Math.min(start+ fgPkgNameChars, dot)));
1043                        buf.append(fgPkgNamePostfix);
1044                    } else
1045                        buf.append(name.substring(start, dot + 1));
1046                    start= dot + 1;
1047                    dot= name.indexOf('.', start);
1048                }
1049                buf.append(name.substring(start));
1050        } else {
1051            buf.append(pack.getElementName());
1052        }
1053        if (getFlag(flags, P_POST_QUALIFIED)) {
1054            buf.append(CONCAT_STRING);
1055            getPackageFragmentRootLabel((IPackageFragmentRoot) pack.getParent(), ROOT_QUALIFIED, buf);
1056        }
1057    }
1058
1059    /**
1060     * Appends the label for a package fragment root to a {@link StringBuffer}. Considers the ROOT_* flags.
1061     * @param root The element to render.
1062     * @param flags The rendering flags. Flags with names starting with ROOT_' are considered.
1063     * @param buf The buffer to append the resulting label to.
1064     */

1065    public static void getPackageFragmentRootLabel(IPackageFragmentRoot root, long flags, StringBuffer JavaDoc buf) {
1066        if (root.isArchive())
1067            getArchiveLabel(root, flags, buf);
1068        else
1069            getFolderLabel(root, flags, buf);
1070    }
1071    
1072    private static void getArchiveLabel(IPackageFragmentRoot root, long flags, StringBuffer JavaDoc buf) {
1073        // Handle variables different
1074
if (getFlag(flags, ROOT_VARIABLE) && getVariableLabel(root, flags, buf))
1075            return;
1076        boolean external= root.isExternal();
1077        if (external)
1078            getExternalArchiveLabel(root, flags, buf);
1079        else
1080            getInternalArchiveLabel(root, flags, buf);
1081    }
1082    
1083    private static boolean getVariableLabel(IPackageFragmentRoot root, long flags, StringBuffer JavaDoc buf) {
1084        try {
1085            IClasspathEntry rawEntry= root.getRawClasspathEntry();
1086            if (rawEntry != null && rawEntry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
1087                IPath path= rawEntry.getPath().makeRelative();
1088                if (getFlag(flags, REFERENCED_ROOT_POST_QUALIFIED)) {
1089                    int segements= path.segmentCount();
1090                    if (segements > 0) {
1091                        buf.append(path.segment(segements - 1));
1092                        if (segements > 1) {
1093                            buf.append(CONCAT_STRING);
1094                            buf.append(path.removeLastSegments(1).toOSString());
1095                        }
1096                    } else {
1097                        buf.append(path.toString());
1098                    }
1099                } else {
1100                    buf.append(path.toString());
1101                }
1102                buf.append(CONCAT_STRING);
1103                if (root.isExternal())
1104                    buf.append(root.getPath().toOSString());
1105                else
1106                    buf.append(root.getPath().makeRelative().toString());
1107                return true;
1108            }
1109        } catch (JavaModelException e) {
1110            JavaPlugin.log(e); // problems with class path
1111
}
1112        return false;
1113    }
1114
1115    private static void getExternalArchiveLabel(IPackageFragmentRoot root, long flags, StringBuffer JavaDoc buf) {
1116        IPath path= root.getPath();
1117        if (getFlag(flags, REFERENCED_ROOT_POST_QUALIFIED)) {
1118            int segements= path.segmentCount();
1119            if (segements > 0) {
1120                buf.append(path.segment(segements - 1));
1121                if (segements > 1 || path.getDevice() != null) {
1122                    buf.append(CONCAT_STRING);
1123                    buf.append(path.removeLastSegments(1).toOSString());
1124                }
1125            } else {
1126                buf.append(path.toOSString());
1127            }
1128        } else {
1129            buf.append(path.toOSString());
1130        }
1131    }
1132
1133    private static void getInternalArchiveLabel(IPackageFragmentRoot root, long flags, StringBuffer JavaDoc buf) {
1134        IResource resource= root.getResource();
1135        boolean rootQualified= getFlag(flags, ROOT_QUALIFIED);
1136        boolean referencedQualified= getFlag(flags, REFERENCED_ROOT_POST_QUALIFIED) && isReferenced(root);
1137        if (rootQualified) {
1138            buf.append(root.getPath().makeRelative().toString());
1139        } else {
1140            buf.append(root.getElementName());
1141            if (referencedQualified) {
1142                buf.append(CONCAT_STRING);
1143                buf.append(resource.getParent().getFullPath().makeRelative().toString());
1144            } else if (getFlag(flags, ROOT_POST_QUALIFIED)) {
1145                buf.append(CONCAT_STRING);
1146                buf.append(root.getParent().getPath().makeRelative().toString());
1147            }
1148        }
1149    }
1150
1151    private static void getFolderLabel(IPackageFragmentRoot root, long flags, StringBuffer JavaDoc buf) {
1152        IResource resource= root.getResource();
1153        boolean rootQualified= getFlag(flags, ROOT_QUALIFIED);
1154        boolean referencedQualified= getFlag(flags, REFERENCED_ROOT_POST_QUALIFIED) && isReferenced(root);
1155        if (rootQualified) {
1156            buf.append(root.getPath().makeRelative().toString());
1157        } else {
1158            if (resource != null) {
1159                IPath projectRelativePath= resource.getProjectRelativePath();
1160                if (projectRelativePath.segmentCount() == 0) {
1161                    buf.append(resource.getName());
1162                    referencedQualified= false;
1163                } else {
1164                    buf.append(projectRelativePath.toString());
1165                }
1166            } else
1167                buf.append(root.getElementName());
1168            if (referencedQualified) {
1169                buf.append(CONCAT_STRING);
1170                buf.append(resource.getProject().getName());
1171            } else if (getFlag(flags, ROOT_POST_QUALIFIED)) {
1172                buf.append(CONCAT_STRING);
1173                buf.append(root.getParent().getElementName());
1174            }
1175        }
1176    }
1177    
1178    /**
1179     * Returns <code>true</code> if the given package fragment root is
1180     * referenced. This means it is own by a different project but is referenced
1181     * by the root's parent. Returns <code>false</code> if the given root
1182     * doesn't have an underlying resource.
1183     *
1184     * @since 3.2
1185     */

1186    private static boolean isReferenced(IPackageFragmentRoot root) {
1187        IResource resource= root.getResource();
1188        if (resource != null) {
1189            IProject jarProject= resource.getProject();
1190            IProject container= root.getJavaProject().getProject();
1191            return !container.equals(jarProject);
1192        }
1193        return false;
1194    }
1195
1196    private static void refreshPackageNamePattern() {
1197        String JavaDoc pattern= getPkgNamePatternForPackagesView();
1198        final String JavaDoc EMPTY_STRING= ""; //$NON-NLS-1$
1199
if (pattern.equals(fgPkgNamePattern))
1200            return;
1201        else if (pattern.length() == 0) {
1202            fgPkgNamePattern= EMPTY_STRING;
1203            fgPkgNameLength= -1;
1204            return;
1205        }
1206        fgPkgNamePattern= pattern;
1207        int i= 0;
1208        fgPkgNameChars= 0;
1209        fgPkgNamePrefix= EMPTY_STRING;
1210        fgPkgNamePostfix= EMPTY_STRING;
1211        while (i < pattern.length()) {
1212            char ch= pattern.charAt(i);
1213            if (Character.isDigit(ch)) {
1214                fgPkgNameChars= ch-48;
1215                if (i > 0)
1216                    fgPkgNamePrefix= pattern.substring(0, i);
1217                if (i >= 0)
1218                    fgPkgNamePostfix= pattern.substring(i+1);
1219                fgPkgNameLength= fgPkgNamePrefix.length() + fgPkgNameChars + fgPkgNamePostfix.length();
1220                return;
1221            }
1222            i++;
1223        }
1224        fgPkgNamePrefix= pattern;
1225        fgPkgNameLength= pattern.length();
1226    }
1227    
1228    private static String JavaDoc getPkgNamePatternForPackagesView() {
1229        IPreferenceStore store= PreferenceConstants.getPreferenceStore();
1230        if (!store.getBoolean(PreferenceConstants.APPEARANCE_COMPRESS_PACKAGE_NAMES))
1231            return ""; //$NON-NLS-1$
1232
return store.getString(PreferenceConstants.APPEARANCE_PKG_NAME_PATTERN_FOR_PKG_VIEW);
1233    }
1234    
1235    /**
1236     * Returns the label of a classpath container
1237     * @param containerPath The path of the container.
1238     * @param project The project the container is resolved in.
1239     * @return Returns the label of the classpath container
1240     * @throws JavaModelException Thrown when the resolving of the container failed.
1241     */

1242    public static String JavaDoc getContainerEntryLabel(IPath containerPath, IJavaProject project) throws JavaModelException {
1243        IClasspathContainer container= JavaCore.getClasspathContainer(containerPath, project);
1244        if (container != null) {
1245            return container.getDescription();
1246        }
1247        ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerPath.segment(0));
1248        if (initializer != null) {
1249            return initializer.getDescription(containerPath, project);
1250        }
1251        return containerPath.toString();
1252    }
1253    
1254}
1255
Popular Tags