KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.viewsupport;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.core.runtime.IAdaptable;
15 import org.eclipse.core.runtime.IPath;
16
17 import org.eclipse.jface.preference.IPreferenceStore;
18
19 import org.eclipse.ui.model.IWorkbenchAdapter;
20
21 import org.eclipse.jdt.core.*;
22 import org.eclipse.jdt.core.IClassFile;
23 import org.eclipse.jdt.core.IClasspathEntry;
24 import org.eclipse.jdt.core.ICompilationUnit;
25 import org.eclipse.jdt.core.IField;
26 import org.eclipse.jdt.core.IInitializer;
27 import org.eclipse.jdt.core.IJavaElement;
28 import org.eclipse.jdt.core.ILocalVariable;
29 import org.eclipse.jdt.core.IMethod;
30 import org.eclipse.jdt.core.IPackageFragment;
31 import org.eclipse.jdt.core.IPackageFragmentRoot;
32 import org.eclipse.jdt.core.IType;
33 import org.eclipse.jdt.core.JavaModelException;
34 import org.eclipse.jdt.core.Signature;
35
36 import org.eclipse.jdt.ui.PreferenceConstants;
37
38 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
39
40 import org.eclipse.jdt.internal.ui.JavaPlugin;
41 import org.eclipse.jdt.internal.ui.JavaUIMessages;
42
43 public class JavaElementLabels {
44     
45     /**
46      * Method names contain parameter types.
47      * e.g. <code>foo(int)</code>
48      */

49     public final static int M_PARAMETER_TYPES= 1 << 0;
50     
51     /**
52      * Method names contain parameter names.
53      * e.g. <code>foo(index)</code>
54      */

55     public final static int M_PARAMETER_NAMES= 1 << 1;
56     
57     /**
58      * Method names contain thrown exceptions.
59      * e.g. <code>foo throws IOException</code>
60      */

61     public final static int M_EXCEPTIONS= 1 << 2;
62     
63     /**
64      * Method names contain return type (appended)
65      * e.g. <code>foo : int</code>
66      */

67     public final static int M_APP_RETURNTYPE= 1 << 3;
68     
69     /**
70      * Method names contain return type (appended)
71      * e.g. <code>int foo</code>
72      */

73     public final static int M_PRE_RETURNTYPE= 1 << 4;
74
75     /**
76      * Method names are fully qualified.
77      * e.g. <code>java.util.Vector.size</code>
78      */

79     public final static int M_FULLY_QUALIFIED= 1 << 5;
80     
81     /**
82      * Method names are post qualified.
83      * e.g. <code>size - java.util.Vector</code>
84      */

85     public final static int M_POST_QUALIFIED= 1 << 6;
86     
87     /**
88      * Initializer names are fully qualified.
89      * e.g. <code>java.util.Vector.{ ... }</code>
90      */

91     public final static int I_FULLY_QUALIFIED= 1 << 7;
92     
93     /**
94      * Type names are post qualified.
95      * e.g. <code>{ ... } - java.util.Map</code>
96      */

97     public final static int I_POST_QUALIFIED= 1 << 8;
98     
99     /**
100      * Field names contain the declared type (appended)
101      * e.g. <code>fHello : int</code>
102      */

103     public final static int F_APP_TYPE_SIGNATURE= 1 << 9;
104     
105     /**
106      * Field names contain the declared type (prepended)
107      * e.g. <code>int fHello</code>
108      */

109     public final static int F_PRE_TYPE_SIGNATURE= 1 << 10;
110
111     /**
112      * Fields names are fully qualified.
113      * e.g. <code>java.lang.System.out</code>
114      */

115     public final static int F_FULLY_QUALIFIED= 1 << 11;
116     
117     /**
118      * Fields names are post qualified.
119      * e.g. <code>out - java.lang.System</code>
120      */

121     public final static int F_POST_QUALIFIED= 1 << 12;
122     
123     /**
124      * Type names are fully qualified.
125      * e.g. <code>java.util.Map.MapEntry</code>
126      */

127     public final static int T_FULLY_QUALIFIED= 1 << 13;
128     
129     /**
130      * Type names are type container qualified.
131      * e.g. <code>Map.MapEntry</code>
132      */

133     public final static int T_CONTAINER_QUALIFIED= 1 << 14;
134     
135     /**
136      * Type names are post qualified.
137      * e.g. <code>MapEntry - java.util.Map</code>
138      */

139     public final static int T_POST_QUALIFIED= 1 << 15;
140     
141     /**
142      * Declarations (import container / declarartion, package declarartion) are qualified.
143      * e.g. <code>java.util.Vector.class/import container</code>
144      */

145     public final static int D_QUALIFIED= 1 << 16;
146     
147     /**
148      * Declarations (import container / declarartion, package declarartion) are post qualified.
149      * e.g. <code>import container - java.util.Vector.class</code>
150      */

151     public final static int D_POST_QUALIFIED= 1 << 17;
152
153     /**
154      * Class file names are fully qualified.
155      * e.g. <code>java.util.Vector.class</code>
156      */

157     public final static int CF_QUALIFIED= 1 << 18;
158     
159     /**
160      * Class file names are post qualified.
161      * e.g. <code>Vector.class - java.util</code>
162      */

163     public final static int CF_POST_QUALIFIED= 1 << 19;
164     
165     /**
166      * Compilation unit names are fully qualified.
167      * e.g. <code>java.util.Vector.java</code>
168      */

169     public final static int CU_QUALIFIED= 1 << 20;
170     
171     /**
172      * Compilation unit names are post qualified.
173      * e.g. <code>Vector.java - java.util</code>
174      */

175     public final static int CU_POST_QUALIFIED= 1 << 21;
176
177     /**
178      * Package names are qualified.
179      * e.g. <code>MyProject/src/java.util</code>
180      */

181     public final static int P_QUALIFIED= 1 << 22;
182     
183     /**
184      * Package names are post qualified.
185      * e.g. <code>java.util - MyProject/src</code>
186      */

187     public final static int P_POST_QUALIFIED= 1 << 23;
188
189     /**
190      * Package Fragment Roots contain variable name if from a variable.
191      * e.g. <code>JRE_LIB - c:\java\lib\rt.jar</code>
192      */

193     public final static int ROOT_VARIABLE= 1 << 24;
194     
195     /**
196      * Package Fragment Roots contain the project name if not an archive (prepended).
197      * e.g. <code>MyProject/src</code>
198      */

199     public final static int ROOT_QUALIFIED= 1 << 25;
200     
201     /**
202      * Package Fragment Roots contain the project name if not an archive (appended).
203      * e.g. <code>src - MyProject</code>
204      */

205     public final static int ROOT_POST_QUALIFIED= 1 << 26;
206     
207     /**
208      * Add root path to all elements except Package Fragment Roots and Java projects.
209      * e.g. <code>java.lang.Vector - c:\java\lib\rt.jar</code>
210      * Option only applies to getElementLabel
211      */

212     public final static int APPEND_ROOT_PATH= 1 << 27;
213
214     /**
215      * Add root path to all elements except Package Fragment Roots and Java projects.
216      * e.g. <code>java.lang.Vector - c:\java\lib\rt.jar</code>
217      * Option only applies to getElementLabel
218      */

219     public final static int PREPEND_ROOT_PATH= 1 << 28;
220
221     /**
222      * Package names are compressed.
223      * e.g. <code>o*.e*.search</code>
224      */

225     public final static int P_COMPRESSED= 1 << 29;
226     
227     /**
228      * Post qualify referenced package fragement roots. For example
229      * <code>jdt.jar - org.eclipse.jdt.ui</code> if the jar is referenced
230      * from another project.
231      */

232     public final static int REFERENCED_ROOT_POST_QUALIFIED= 1 << 30;
233     
234     /**
235      * Qualify all elements
236      */

237     public final static int ALL_FULLY_QUALIFIED= F_FULLY_QUALIFIED | M_FULLY_QUALIFIED | I_FULLY_QUALIFIED | T_FULLY_QUALIFIED | D_QUALIFIED | CF_QUALIFIED | CU_QUALIFIED | P_QUALIFIED | ROOT_QUALIFIED;
238
239     /**
240      * Post qualify all elements
241      */

242     public final static int ALL_POST_QUALIFIED= 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;
243
244     /**
245      * Default options (M_PARAMETER_TYPES enabled)
246      */

247     public final static int ALL_DEFAULT= M_PARAMETER_TYPES;
248
249     /**
250      * Default qualify options (All except Root and Package)
251      */

252     public final static int DEFAULT_QUALIFIED= F_FULLY_QUALIFIED | M_FULLY_QUALIFIED | I_FULLY_QUALIFIED | T_FULLY_QUALIFIED | D_QUALIFIED | CF_QUALIFIED | CU_QUALIFIED;
253
254     /**
255      * Default post qualify options (All except Root and Package)
256      */

257     public final static int DEFAULT_POST_QUALIFIED= F_POST_QUALIFIED | M_POST_QUALIFIED | I_POST_QUALIFIED | T_POST_QUALIFIED | D_POST_QUALIFIED | CF_POST_QUALIFIED | CU_POST_QUALIFIED;
258
259     /**
260      * User-readable string for separating post qualified names (e.g. " - ").
261      */

262     public final static String JavaDoc CONCAT_STRING= JavaUIMessages.getString("JavaElementLabels.concat_string"); //$NON-NLS-1$
263
/**
264      * User-readable string for separating list items (e.g. ", ").
265      */

266     public final static String JavaDoc COMMA_STRING= JavaUIMessages.getString("JavaElementLabels.comma_string"); //$NON-NLS-1$
267
/**
268      * User-readable string for separating the return type (e.g. " : ").
269      */

270     public final static String JavaDoc DECL_STRING= JavaUIMessages.getString("JavaElementLabels.declseparator_string"); //$NON-NLS-1$
271
/**
272      * User-readable string for the default package name (e.g. "(default package)").
273      */

274     public final static String JavaDoc DEFAULT_PACKAGE= JavaUIMessages.getString("JavaElementLabels.default_package"); //$NON-NLS-1$
275

276     /*
277      * Package name compression
278      */

279     private static String JavaDoc fgPkgNamePattern= ""; //$NON-NLS-1$
280
private static String JavaDoc fgPkgNamePrefix;
281     private static String JavaDoc fgPkgNamePostfix;
282     private static int fgPkgNameChars;
283     private static int fgPkgNameLength= -1;
284
285     private JavaElementLabels() {
286     }
287
288     private static boolean getFlag(int flags, int flag) {
289         return (flags & flag) != 0;
290     }
291     
292     public static String JavaDoc getTextLabel(Object JavaDoc obj, int flags) {
293         if (obj instanceof IJavaElement) {
294             return getElementLabel((IJavaElement) obj, flags);
295         } else if (obj instanceof IAdaptable) {
296             IWorkbenchAdapter wbadapter= (IWorkbenchAdapter) ((IAdaptable)obj).getAdapter(IWorkbenchAdapter.class);
297             if (wbadapter != null) {
298                 return wbadapter.getLabel(obj);
299             }
300         }
301         return ""; //$NON-NLS-1$
302
}
303                 
304     /**
305      * Returns the label for a Java element. Flags as defined above.
306      */

307     public static String JavaDoc getElementLabel(IJavaElement element, int flags) {
308         StringBuffer JavaDoc buf= new StringBuffer JavaDoc(60);
309         getElementLabel(element, flags, buf);
310         return buf.toString();
311     }
312     
313     /**
314      * Returns the label for a Java element. Flags as defined above.
315      */

316     public static void getElementLabel(IJavaElement element, int flags, StringBuffer JavaDoc buf) {
317         int type= element.getElementType();
318         IPackageFragmentRoot root= null;
319         
320         if (type != IJavaElement.JAVA_MODEL && type != IJavaElement.JAVA_PROJECT && type != IJavaElement.PACKAGE_FRAGMENT_ROOT)
321             root= JavaModelUtil.getPackageFragmentRoot(element);
322         if (root != null && getFlag(flags, PREPEND_ROOT_PATH)) {
323             getPackageFragmentRootLabel(root, ROOT_QUALIFIED, buf);
324             buf.append(CONCAT_STRING);
325         }
326         
327         switch (type) {
328             case IJavaElement.METHOD:
329                 getMethodLabel((IMethod) element, flags, buf);
330                 break;
331             case IJavaElement.FIELD:
332                 getFieldLabel((IField) element, flags, buf);
333                 break;
334             case IJavaElement.LOCAL_VARIABLE:
335                 getLocalVariableLabel((ILocalVariable) element, flags, buf);
336                 break;
337             case IJavaElement.INITIALIZER:
338                 getInitializerLabel((IInitializer) element, flags, buf);
339                 break;
340             case IJavaElement.TYPE:
341                 getTypeLabel((IType) element, flags, buf);
342                 break;
343             case IJavaElement.CLASS_FILE:
344                 getClassFileLabel((IClassFile) element, flags, buf);
345                 break;
346             case IJavaElement.COMPILATION_UNIT:
347                 getCompilationUnitLabel((ICompilationUnit) element, flags, buf);
348                 break;
349             case IJavaElement.PACKAGE_FRAGMENT:
350                 getPackageFragmentLabel((IPackageFragment) element, flags, buf);
351                 break;
352             case IJavaElement.PACKAGE_FRAGMENT_ROOT:
353                 getPackageFragmentRootLabel((IPackageFragmentRoot) element, flags, buf);
354                 break;
355             case IJavaElement.IMPORT_CONTAINER:
356             case IJavaElement.IMPORT_DECLARATION:
357             case IJavaElement.PACKAGE_DECLARATION:
358                 getDeclararionLabel(element, flags, buf);
359                 break;
360             case IJavaElement.JAVA_PROJECT:
361             case IJavaElement.JAVA_MODEL:
362                 buf.append(element.getElementName());
363                 break;
364             default:
365                 buf.append(element.getElementName());
366         }
367         
368         if (root != null && getFlag(flags, APPEND_ROOT_PATH)) {
369             buf.append(CONCAT_STRING);
370             getPackageFragmentRootLabel(root, ROOT_QUALIFIED, buf);
371         }
372     }
373
374     /**
375      * Appends the label for a method to a StringBuffer. Considers the M_* flags.
376      */

377     public static void getMethodLabel(IMethod method, int flags, StringBuffer JavaDoc buf) {
378         try {
379             // return type
380
if (getFlag(flags, M_PRE_RETURNTYPE) && method.exists() && !method.isConstructor()) {
381                 buf.append(Signature.getSimpleName(Signature.toString(method.getReturnType())));
382                 buf.append(' ');
383             }
384             
385             // qualification
386
if (getFlag(flags, M_FULLY_QUALIFIED)) {
387                 getTypeLabel(method.getDeclaringType(), T_FULLY_QUALIFIED | (flags & P_COMPRESSED), buf);
388                 buf.append('.');
389             }
390                 
391             buf.append(method.getElementName());
392             
393             // parameters
394
buf.append('(');
395             if (getFlag(flags, M_PARAMETER_TYPES | M_PARAMETER_NAMES)) {
396             
397             
398                 String JavaDoc[] types= getFlag(flags, M_PARAMETER_TYPES) ? method.getParameterTypes() : null;
399                 String JavaDoc[] names= (getFlag(flags, M_PARAMETER_NAMES) && method.exists()) ? method.getParameterNames() : null;
400                 int nParams= types != null ? types.length : names.length;
401                 
402                 for (int i= 0; i < nParams; i++) {
403                     if (i > 0) {
404                         buf.append(COMMA_STRING); //$NON-NLS-1$
405
}
406                     if (types != null) {
407                         buf.append(Signature.getSimpleName(Signature.toString(types[i])));
408                     }
409                     if (names != null) {
410                         if (types != null) {
411                             buf.append(' ');
412                         }
413                         buf.append(names[i]);
414                     }
415                 }
416             } else {
417                 if (method.getParameterTypes().length > 0) {
418                     buf.append(".."); //$NON-NLS-1$
419
}
420             }
421             buf.append(')');
422                     
423             if (getFlag(flags, M_EXCEPTIONS) && method.exists()) {
424                 String JavaDoc[] types= method.getExceptionTypes();
425                 if (types.length > 0) {
426                     buf.append(" throws "); //$NON-NLS-1$
427
for (int i= 0; i < types.length; i++) {
428                         if (i > 0) {
429                             buf.append(COMMA_STRING);
430                         }
431                         buf.append(Signature.getSimpleName(Signature.toString(types[i])));
432                     }
433                 }
434             }
435             
436             if (getFlag(flags, M_APP_RETURNTYPE) && method.exists() && !method.isConstructor()) {
437                 buf.append(DECL_STRING);
438                 buf.append(Signature.getSimpleName(Signature.toString(method.getReturnType())));
439             }
440             
441             // post qualification
442
if (getFlag(flags, M_POST_QUALIFIED)) {
443                 buf.append(CONCAT_STRING);
444                 getTypeLabel(method.getDeclaringType(), T_FULLY_QUALIFIED | (flags & P_COMPRESSED), buf);
445             }
446             
447         } catch (JavaModelException e) {
448             JavaPlugin.log(e); // NotExistsException will not reach this point
449
}
450     }
451     
452     /**
453      * Appends the label for a field to a StringBuffer. Considers the F_* flags.
454      */

455     public static void getFieldLabel(IField field, int flags, StringBuffer JavaDoc buf) {
456         try {
457             if (getFlag(flags, F_PRE_TYPE_SIGNATURE) && field.exists()) {
458                 buf.append(Signature.toString(field.getTypeSignature()));
459                 buf.append(' ');
460             }
461             
462             // qualification
463
if (getFlag(flags, F_FULLY_QUALIFIED)) {
464                 getTypeLabel(field.getDeclaringType(), T_FULLY_QUALIFIED | (flags & P_COMPRESSED), buf);
465                 buf.append('.');
466             }
467             buf.append(field.getElementName());
468             
469             if (getFlag(flags, F_APP_TYPE_SIGNATURE) && field.exists()) {
470                 buf.append(DECL_STRING);
471                 buf.append(Signature.toString(field.getTypeSignature()));
472             }
473             
474             // post qualification
475
if (getFlag(flags, F_POST_QUALIFIED)) {
476                 buf.append(CONCAT_STRING);
477                 getTypeLabel(field.getDeclaringType(), T_FULLY_QUALIFIED | (flags & P_COMPRESSED), buf);
478             }
479             
480         } catch (JavaModelException e) {
481             JavaPlugin.log(e); // NotExistsException will not reach this point
482
}
483     }
484     
485     /**
486      * Appends the label for a local variable to a StringBuffer.
487      *
488      * @since 3.0
489      */

490     public static void getLocalVariableLabel(ILocalVariable localVariable, int flags, StringBuffer JavaDoc buf) {
491         buf.append(Signature.toString(localVariable.getTypeSignature()));
492         buf.append(' ');
493         buf.append(localVariable.getElementName());
494         
495         buf.append(CONCAT_STRING);
496         getElementLabel(localVariable.getParent(), M_PARAMETER_TYPES | M_FULLY_QUALIFIED | T_FULLY_QUALIFIED | (flags & P_COMPRESSED), buf);
497
498     }
499     
500     /**
501      * Appends the label for a initializer to a StringBuffer. Considers the I_* flags.
502      */

503     public static void getInitializerLabel(IInitializer initializer, int flags, StringBuffer JavaDoc buf) {
504         // qualification
505
if (getFlag(flags, I_FULLY_QUALIFIED)) {
506             getTypeLabel(initializer.getDeclaringType(), T_FULLY_QUALIFIED | (flags & P_COMPRESSED), buf);
507             buf.append('.');
508         }
509         buf.append(JavaUIMessages.getString("JavaElementLabels.initializer")); //$NON-NLS-1$
510

511         // post qualification
512
if (getFlag(flags, I_POST_QUALIFIED)) {
513             buf.append(CONCAT_STRING);
514             getTypeLabel(initializer.getDeclaringType(), T_FULLY_QUALIFIED | (flags & P_COMPRESSED), buf);
515         }
516     }
517
518     /**
519      * Appends the label for a type to a StringBuffer. Considers the T_* flags.
520      */

521     public static void getTypeLabel(IType type, int flags, StringBuffer JavaDoc buf) {
522         if (getFlag(flags, T_FULLY_QUALIFIED)) {
523             IPackageFragment pack= type.getPackageFragment();
524             if (!pack.isDefaultPackage()) {
525                 getPackageFragmentLabel(pack, (flags & P_COMPRESSED), buf);
526                 buf.append('.');
527             }
528         }
529         if (getFlag(flags, T_FULLY_QUALIFIED | T_CONTAINER_QUALIFIED)) {
530             IType declaringType= type.getDeclaringType();
531             if (declaringType != null) {
532                 getTypeLabel(declaringType, T_CONTAINER_QUALIFIED, buf);
533                 buf.append('.');
534             }
535             int parentType= type.getParent().getElementType();
536             if (parentType == IJavaElement.METHOD || parentType == IJavaElement.FIELD || parentType == IJavaElement.INITIALIZER) { // anonymous or local
537
getElementLabel(type.getParent(), 0, buf);
538                 buf.append('.');
539             }
540         }
541         
542         String JavaDoc typeName= type.getElementName();
543         if (typeName.length() == 0) { // anonymous
544
try {
545                 String JavaDoc superclassName= Signature.getSimpleName(type.getSuperclassName());
546                 typeName= JavaUIMessages.getFormattedString("JavaElementLabels.anonym_type" , superclassName); //$NON-NLS-1$
547
} catch (JavaModelException e) {
548                 //ignore
549
typeName= JavaUIMessages.getString("JavaElementLabels.anonym"); //$NON-NLS-1$
550
}
551         }
552         buf.append(typeName);
553         
554         // post qualification
555
if (getFlag(flags, T_POST_QUALIFIED)) {
556             buf.append(CONCAT_STRING);
557             IType declaringType= type.getDeclaringType();
558             if (declaringType != null) {
559                 getTypeLabel(declaringType, T_FULLY_QUALIFIED | (flags & P_COMPRESSED), buf);
560                 int parentType= type.getParent().getElementType();
561                 if (parentType == IJavaElement.METHOD || parentType == IJavaElement.FIELD || parentType == IJavaElement.INITIALIZER) { // anonymous or local
562
buf.append('.');
563                     getElementLabel(type.getParent(), 0, buf);
564                 }
565             } else {
566                 getPackageFragmentLabel(type.getPackageFragment(), (flags & P_COMPRESSED), buf);
567             }
568         }
569     }
570
571     /**
572      * Appends the label for a declaration to a StringBuffer. Considers the D_* flags.
573      */

574     public static void getDeclararionLabel(IJavaElement declaration, int flags, StringBuffer JavaDoc buf) {
575         if (getFlag(flags, D_QUALIFIED)) {
576             IJavaElement openable= (IJavaElement) declaration.getOpenable();
577             if (openable != null) {
578                 buf.append(getElementLabel(openable, CF_QUALIFIED | CU_QUALIFIED));
579                 buf.append('/');
580             }
581         }
582         if (declaration.getElementType() == IJavaElement.IMPORT_CONTAINER) {
583             buf.append(JavaUIMessages.getString("JavaElementLabels.import_container")); //$NON-NLS-1$
584
} else {
585             buf.append(declaration.getElementName());
586         }
587         // post qualification
588
if (getFlag(flags, D_POST_QUALIFIED)) {
589             IJavaElement openable= (IJavaElement) declaration.getOpenable();
590             if (openable != null) {
591                 buf.append(CONCAT_STRING);
592                 buf.append(getElementLabel(openable, CF_QUALIFIED | CU_QUALIFIED));
593             }
594         }
595     }
596     
597     /**
598      * Appends the label for a class file to a StringBuffer. Considers the CF_* flags.
599      */

600     public static void getClassFileLabel(IClassFile classFile, int flags, StringBuffer JavaDoc buf) {
601         if (getFlag(flags, CF_QUALIFIED)) {
602             IPackageFragment pack= (IPackageFragment) classFile.getParent();
603             if (!pack.isDefaultPackage()) {
604                 buf.append(pack.getElementName());
605                 buf.append('.');
606             }
607         }
608         buf.append(classFile.getElementName());
609         
610         if (getFlag(flags, CF_POST_QUALIFIED)) {
611             buf.append(CONCAT_STRING);
612             getPackageFragmentLabel((IPackageFragment) classFile.getParent(), 0, buf);
613         }
614     }
615
616     /**
617      * Appends the label for a compilation unit to a StringBuffer. Considers the CU_* flags.
618      */

619     public static void getCompilationUnitLabel(ICompilationUnit cu, int flags, StringBuffer JavaDoc buf) {
620         if (getFlag(flags, CU_QUALIFIED)) {
621             IPackageFragment pack= (IPackageFragment) cu.getParent();
622             if (!pack.isDefaultPackage()) {
623                 buf.append(pack.getElementName());
624                 buf.append('.');
625             }
626         }
627         buf.append(cu.getElementName());
628         
629         if (getFlag(flags, CU_POST_QUALIFIED)) {
630             buf.append(CONCAT_STRING);
631             getPackageFragmentLabel((IPackageFragment) cu.getParent(), 0, buf);
632         }
633     }
634
635     /**
636      * Appends the label for a package fragment to a StringBuffer. Considers the P_* flags.
637      */

638     public static void getPackageFragmentLabel(IPackageFragment pack, int flags, StringBuffer JavaDoc buf) {
639         if (getFlag(flags, P_QUALIFIED)) {
640             getPackageFragmentRootLabel((IPackageFragmentRoot) pack.getParent(), ROOT_QUALIFIED, buf);
641             buf.append('/');
642         }
643         refreshPackageNamePattern();
644         if (pack.isDefaultPackage()) {
645             buf.append(DEFAULT_PACKAGE);
646         } else if (getFlag(flags, P_COMPRESSED) && fgPkgNameLength >= 0) {
647                 String JavaDoc name= pack.getElementName();
648                 int start= 0;
649                 int dot= name.indexOf('.', start);
650                 while (dot > 0) {
651                     if (dot - start > fgPkgNameLength-1) {
652                         buf.append(fgPkgNamePrefix);
653                         if (fgPkgNameChars > 0)
654                             buf.append(name.substring(start, Math.min(start+ fgPkgNameChars, dot)));
655                         buf.append(fgPkgNamePostfix);
656                     } else
657                         buf.append(name.substring(start, dot + 1));
658                     start= dot + 1;
659                     dot= name.indexOf('.', start);
660                 }
661                 buf.append(name.substring(start));
662         } else {
663             buf.append(pack.getElementName());
664         }
665         if (getFlag(flags, P_POST_QUALIFIED)) {
666             buf.append(CONCAT_STRING);
667             getPackageFragmentRootLabel((IPackageFragmentRoot) pack.getParent(), ROOT_QUALIFIED, buf);
668         }
669     }
670
671     /**
672      * Appends the label for a package fragment root to a StringBuffer. Considers the ROOT_* flags.
673      */

674     public static void getPackageFragmentRootLabel(IPackageFragmentRoot root, int flags, StringBuffer JavaDoc buf) {
675         if (root.isArchive())
676             getArchiveLabel(root, flags, buf);
677         else
678             getFolderLabel(root, flags, buf);
679     }
680     
681     private static void getArchiveLabel(IPackageFragmentRoot root, int flags, StringBuffer JavaDoc buf) {
682         // Handle variables different
683
if (getFlag(flags, ROOT_VARIABLE) && getVariableLabel(root, flags, buf))
684             return;
685         boolean external= root.isExternal();
686         if (external)
687             getExternalArchiveLabel(root, flags, buf);
688         else
689             getInternalArchiveLabel(root, flags, buf);
690     }
691     
692     private static boolean getVariableLabel(IPackageFragmentRoot root, int flags, StringBuffer JavaDoc buf) {
693         try {
694             IClasspathEntry rawEntry= root.getRawClasspathEntry();
695             if (rawEntry != null) {
696                 if (rawEntry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
697                     buf.append(rawEntry.getPath().makeRelative());
698                     buf.append(CONCAT_STRING);
699                     if (root.isExternal())
700                         buf.append(root.getPath().toOSString());
701                     else
702                         buf.append(root.getPath().makeRelative().toString());
703                     return true;
704                 }
705             }
706         } catch (JavaModelException e) {
707             JavaPlugin.log(e); // problems with class path
708
}
709         return false;
710     }
711
712     private static void getExternalArchiveLabel(IPackageFragmentRoot root, int flags, StringBuffer JavaDoc buf) {
713         IPath path= root.getPath();
714         if (getFlag(flags, REFERENCED_ROOT_POST_QUALIFIED)) {
715             int segements= path.segmentCount();
716             if (segements > 0) {
717                 buf.append(path.segment(segements - 1));
718                 if (segements > 1 || path.getDevice() != null) {
719                     buf.append(CONCAT_STRING);
720                     buf.append(path.removeLastSegments(1).toOSString());
721                 }
722             } else {
723                 buf.append(path.toOSString());
724             }
725         } else {
726             buf.append(path.toOSString());
727         }
728     }
729
730     private static void getInternalArchiveLabel(IPackageFragmentRoot root, int flags, StringBuffer JavaDoc buf) {
731         IResource resource= root.getResource();
732         boolean rootQualified= getFlag(flags, ROOT_QUALIFIED);
733         boolean referencedQualified= getFlag(flags, REFERENCED_ROOT_POST_QUALIFIED) && JavaModelUtil.isReferenced(root) && resource != null;
734         if (rootQualified) {
735             buf.append(root.getPath().makeRelative().toString());
736         } else {
737             buf.append(root.getElementName());
738             if (referencedQualified) {
739                 buf.append(CONCAT_STRING);
740                 buf.append(resource.getParent().getFullPath().makeRelative().toString());
741             } else if (getFlag(flags, ROOT_POST_QUALIFIED)) {
742                 buf.append(CONCAT_STRING);
743                 buf.append(root.getParent().getPath().makeRelative().toString());
744             }
745         }
746     }
747
748     private static void getFolderLabel(IPackageFragmentRoot root, int flags, StringBuffer JavaDoc buf) {
749         IResource resource= root.getResource();
750         boolean rootQualified= getFlag(flags, ROOT_QUALIFIED);
751         boolean referencedQualified= getFlag(flags, REFERENCED_ROOT_POST_QUALIFIED) && JavaModelUtil.isReferenced(root) && resource != null;
752         if (rootQualified) {
753             buf.append(root.getPath().makeRelative().toString());
754         } else {
755             if (resource != null)
756                 buf.append(resource.getProjectRelativePath().toString());
757             else
758                 buf.append(root.getElementName());
759             if (referencedQualified) {
760                 buf.append(CONCAT_STRING);
761                 buf.append(resource.getProject().getName());
762             } else if (getFlag(flags, ROOT_POST_QUALIFIED)) {
763                 buf.append(CONCAT_STRING);
764                 buf.append(root.getParent().getElementName());
765             }
766         }
767     }
768
769     private static void refreshPackageNamePattern() {
770         String JavaDoc pattern= getPkgNamePatternForPackagesView();
771         if (pattern.equals(fgPkgNamePattern))
772             return;
773         else if (pattern.equals("")) { //$NON-NLS-1$
774
fgPkgNamePattern= ""; //$NON-NLS-1$
775
fgPkgNameLength= -1;
776             return;
777         }
778         fgPkgNamePattern= pattern;
779         int i= 0;
780         fgPkgNameChars= 0;
781         fgPkgNamePrefix= ""; //$NON-NLS-1$
782
fgPkgNamePostfix= ""; //$NON-NLS-1$
783
while (i < pattern.length()) {
784             char ch= pattern.charAt(i);
785             if (Character.isDigit(ch)) {
786                 fgPkgNameChars= ch-48;
787                 if (i > 0)
788                     fgPkgNamePrefix= pattern.substring(0, i);
789                 if (i >= 0)
790                     fgPkgNamePostfix= pattern.substring(i+1);
791                 fgPkgNameLength= fgPkgNamePrefix.length() + fgPkgNameChars + fgPkgNamePostfix.length();
792                 return;
793             }
794             i++;
795         }
796         fgPkgNamePrefix= pattern;
797         fgPkgNameLength= pattern.length();
798     }
799     
800     private static String JavaDoc getPkgNamePatternForPackagesView() {
801         IPreferenceStore store= PreferenceConstants.getPreferenceStore();
802         if (!store.getBoolean(PreferenceConstants.APPEARANCE_COMPRESS_PACKAGE_NAMES))
803             return ""; //$NON-NLS-1$
804
return store.getString(PreferenceConstants.APPEARANCE_PKG_NAME_PATTERN_FOR_PKG_VIEW);
805     }
806     
807     public static String JavaDoc getContainerEntryLabel(IPath containerPath, IJavaProject project) throws JavaModelException {
808         IClasspathContainer container= JavaCore.getClasspathContainer(containerPath, project);
809         if (container != null) {
810             return container.getDescription();
811         }
812         ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerPath.segment(0));
813         if (initializer != null) {
814             return initializer.getDescription(containerPath, project);
815         }
816         return containerPath.toString();
817     }
818     
819 }
820
Popular Tags