801580eda655241755900855fc4950a86ec7473c
[phpeclipse.git] /
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.viewsupport;
12
13 import net.sourceforge.phpdt.core.ICompilationUnit;
14 import net.sourceforge.phpdt.core.IField;
15 import net.sourceforge.phpdt.core.IJavaElement;
16 import net.sourceforge.phpdt.core.IMethod;
17 import net.sourceforge.phpdt.core.IPackageFragment;
18 import net.sourceforge.phpdt.core.IPackageFragmentRoot;
19 import net.sourceforge.phpdt.core.IType;
20 import net.sourceforge.phpdt.core.JavaModelException;
21 import net.sourceforge.phpdt.core.Signature;
22 import net.sourceforge.phpdt.internal.corext.util.JavaModelUtil;
23 import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
24 import net.sourceforge.phpdt.ui.PreferenceConstants;
25 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
26
27 import org.eclipse.core.resources.IResource;
28 import org.eclipse.core.runtime.IAdaptable;
29 import org.eclipse.core.runtime.IPath;
30 import org.eclipse.jface.preference.IPreferenceStore;
31 import org.eclipse.ui.model.IWorkbenchAdapter;
32
33 public class JavaElementLabels {
34
35         /**
36          * Method names contain parameter types. e.g. <code>foo(int)</code>
37          */
38         public final static int M_PARAMETER_TYPES = 1 << 0;
39
40         /**
41          * Method names contain parameter names. e.g. <code>foo(index)</code>
42          */
43         public final static int M_PARAMETER_NAMES = 1 << 1;
44
45         /**
46          * Method names contain thrown exceptions. e.g.
47          * <code>foo throws IOException</code>
48          */
49         public final static int M_EXCEPTIONS = 1 << 2;
50
51         /**
52          * Method names contain return type (appended) e.g. <code>foo : int</code>
53          */
54         public final static int M_APP_RETURNTYPE = 1 << 3;
55
56         /**
57          * Method names contain return type (appended) e.g. <code>int foo</code>
58          */
59         public final static int M_PRE_RETURNTYPE = 1 << 4;
60
61         /**
62          * Method names are fully qualified. e.g. <code>java.util.Vector.size</code>
63          */
64         public final static int M_FULLY_QUALIFIED = 1 << 5;
65
66         /**
67          * Method names are post qualified. e.g.
68          * <code>size - java.util.Vector</code>
69          */
70         public final static int M_POST_QUALIFIED = 1 << 6;
71
72         /**
73          * Initializer names are fully qualified. e.g.
74          * <code>java.util.Vector.{ ... }</code>
75          */
76         public final static int I_FULLY_QUALIFIED = 1 << 7;
77
78         /**
79          * Type names are post qualified. e.g. <code>{ ... } - java.util.Map</code>
80          */
81         public final static int I_POST_QUALIFIED = 1 << 8;
82
83         /**
84          * Field names contain the declared type (appended) e.g.
85          * <code>int fHello</code>
86          */
87         public final static int F_APP_TYPE_SIGNATURE = 1 << 9;
88
89         /**
90          * Field names contain the declared type (prepended) e.g.
91          * <code>fHello : int</code>
92          */
93         public final static int F_PRE_TYPE_SIGNATURE = 1 << 10;
94
95         /**
96          * Fields names are fully qualified. e.g. <code>java.lang.System.out</code>
97          */
98         public final static int F_FULLY_QUALIFIED = 1 << 11;
99
100         /**
101          * Fields names are post qualified. e.g. <code>out - java.lang.System</code>
102          */
103         public final static int F_POST_QUALIFIED = 1 << 12;
104
105         /**
106          * Type names are fully qualified. e.g. <code>java.util.Map.MapEntry</code>
107          */
108         public final static int T_FULLY_QUALIFIED = 1 << 13;
109
110         /**
111          * Type names are type container qualified. e.g. <code>Map.MapEntry</code>
112          */
113         public final static int T_CONTAINER_QUALIFIED = 1 << 14;
114
115         /**
116          * Type names are post qualified. e.g. <code>MapEntry - java.util.Map</code>
117          */
118         public final static int T_POST_QUALIFIED = 1 << 15;
119
120         /**
121          * Declarations (import container / declarartion, package declarartion) are
122          * qualified. e.g. <code>java.util.Vector.class/import container</code>
123          */
124         public final static int D_QUALIFIED = 1 << 16;
125
126         /**
127          * Declarations (import container / declarartion, package declarartion) are
128          * post qualified. e.g.
129          * <code>import container - java.util.Vector.class</code>
130          */
131         public final static int D_POST_QUALIFIED = 1 << 17;
132
133         /**
134          * Class file names are fully qualified. e.g.
135          * <code>java.util.Vector.class</code>
136          */
137         public final static int CF_QUALIFIED = 1 << 18;
138
139         /**
140          * Class file names are post qualified. e.g.
141          * <code>Vector.class - java.util</code>
142          */
143         public final static int CF_POST_QUALIFIED = 1 << 19;
144
145         /**
146          * Compilation unit names are fully qualified. e.g.
147          * <code>java.util.Vector.java</code>
148          */
149         public final static int CU_QUALIFIED = 1 << 20;
150
151         /**
152          * Compilation unit names are post qualified. e.g.
153          * <code>Vector.java - java.util</code>
154          */
155         public final static int CU_POST_QUALIFIED = 1 << 21;
156
157         /**
158          * Package names are qualified. e.g. <code>MyProject/src/java.util</code>
159          */
160         public final static int P_QUALIFIED = 1 << 22;
161
162         /**
163          * Package names are post qualified. e.g.
164          * <code>java.util - MyProject/src</code>
165          */
166         public final static int P_POST_QUALIFIED = 1 << 23;
167
168         /**
169          * Package Fragment Roots contain variable name if from a variable. e.g.
170          * <code>JRE_LIB - c:\java\lib\rt.jar</code>
171          */
172         public final static int ROOT_VARIABLE = 1 << 24;
173
174         /**
175          * Package Fragment Roots contain the project name if not an archive
176          * (prepended). e.g. <code>MyProject/src</code>
177          */
178         public final static int ROOT_QUALIFIED = 1 << 25;
179
180         /**
181          * Package Fragment Roots contain the project name if not an archive
182          * (appended). e.g. <code>src - MyProject</code>
183          */
184         public final static int ROOT_POST_QUALIFIED = 1 << 26;
185
186         /**
187          * Add root path to all elements except Package Fragment Roots and Java
188          * projects. e.g. <code>java.lang.Vector - c:\java\lib\rt.jar</code>
189          * Option only applies to getElementLabel
190          */
191         public final static int APPEND_ROOT_PATH = 1 << 27;
192
193         /**
194          * Add root path to all elements except Package Fragment Roots and Java
195          * projects. e.g. <code>java.lang.Vector - c:\java\lib\rt.jar</code>
196          * Option only applies to getElementLabel
197          */
198         public final static int PREPEND_ROOT_PATH = 1 << 28;
199
200         /**
201          * Package names are compressed. e.g. <code>o*.e*.search</code>
202          */
203         public final static int P_COMPRESSED = 1 << 29;
204
205         /**
206          * Post qualify referenced package fragement roots. For example
207          * <code>jdt.jar - net.sourceforge.phpdt.ui</code> if the jar is
208          * referenced from another project.
209          */
210         public final static int REFERENCED_ROOT_POST_QUALIFIED = 1 << 30;
211
212         /**
213          * Qualify all elements
214          */
215         public final static int ALL_FULLY_QUALIFIED = F_FULLY_QUALIFIED
216                         | M_FULLY_QUALIFIED | I_FULLY_QUALIFIED | T_FULLY_QUALIFIED
217                         | D_QUALIFIED | CF_QUALIFIED | CU_QUALIFIED | P_QUALIFIED
218                         | ROOT_QUALIFIED;
219
220         /**
221          * Post qualify all elements
222          */
223         public final static int ALL_POST_QUALIFIED = F_POST_QUALIFIED
224                         | M_POST_QUALIFIED | I_POST_QUALIFIED | T_POST_QUALIFIED
225                         | D_POST_QUALIFIED | CF_POST_QUALIFIED | CU_POST_QUALIFIED
226                         | P_POST_QUALIFIED | ROOT_POST_QUALIFIED;
227
228         /**
229          * Default options (M_PARAMETER_TYPES enabled)
230          */
231         public final static int ALL_DEFAULT = M_PARAMETER_TYPES;
232
233         /**
234          * Default qualify options (All except Root and Package)
235          */
236         public final static int DEFAULT_QUALIFIED = F_FULLY_QUALIFIED
237                         | M_FULLY_QUALIFIED | I_FULLY_QUALIFIED | T_FULLY_QUALIFIED
238                         | D_QUALIFIED | CF_QUALIFIED | CU_QUALIFIED;
239
240         /**
241          * Default post qualify options (All except Root and Package)
242          */
243         public final static int DEFAULT_POST_QUALIFIED = F_POST_QUALIFIED
244                         | M_POST_QUALIFIED | I_POST_QUALIFIED | T_POST_QUALIFIED
245                         | D_POST_QUALIFIED | CF_POST_QUALIFIED | CU_POST_QUALIFIED;
246
247         public final static String CONCAT_STRING = PHPUIMessages
248                         .getString("JavaElementLabels.concat_string"); // " - ";
249                                                                                                                         // //$NON-NLS-1$
250
251         public final static String COMMA_STRING = PHPUIMessages
252                         .getString("JavaElementLabels.comma_string"); // ", ";
253                                                                                                                         // //$NON-NLS-1$
254
255         public final static String DECL_STRING = PHPUIMessages
256                         .getString("JavaElementLabels.declseparator_string"); // " "; //
257                                                                                                                                         // use for
258                                                                                                                                         // return
259                                                                                                                                         // type
260                                                                                                                                         // //$NON-NLS-1$
261
262         /*
263          * Package name compression
264          */
265         private static String fgPkgNamePattern = ""; //$NON-NLS-1$
266
267         private static String fgPkgNamePrefix;
268
269         private static String fgPkgNamePostfix;
270
271         private static int fgPkgNameChars;
272
273         private static int fgPkgNameLength = -1;
274
275         private JavaElementLabels() {
276         }
277
278         private static boolean getFlag(int flags, int flag) {
279                 return (flags & flag) != 0;
280         }
281
282         public static String getTextLabel(Object obj, int flags) {
283                 if (obj instanceof IJavaElement) {
284                         return getElementLabel((IJavaElement) obj, flags);
285                 } else if (obj instanceof IAdaptable) {
286                         IWorkbenchAdapter wbadapter = (IWorkbenchAdapter) ((IAdaptable) obj)
287                                         .getAdapter(IWorkbenchAdapter.class);
288                         if (wbadapter != null) {
289                                 return wbadapter.getLabel(obj);
290                         }
291                 }
292                 return ""; //$NON-NLS-1$
293         }
294
295         /**
296          * Returns the label for a Java element. Flags as defined above.
297          */
298         public static String getElementLabel(IJavaElement element, int flags) {
299                 StringBuffer buf = new StringBuffer(60);
300                 getElementLabel(element, flags, buf);
301                 return buf.toString();
302         }
303
304         /**
305          * Returns the label for a Java element. Flags as defined above.
306          */
307         public static void getElementLabel(IJavaElement element, int flags,
308                         StringBuffer buf) {
309                 int type = element.getElementType();
310                 IPackageFragmentRoot root = null;
311
312                 if (type != IJavaElement.JAVA_MODEL
313                                 && type != IJavaElement.JAVA_PROJECT
314                                 && type != IJavaElement.PACKAGE_FRAGMENT_ROOT)
315                         root = JavaModelUtil.getPackageFragmentRoot(element);
316                 if (root != null && getFlag(flags, PREPEND_ROOT_PATH)) {
317                         getPackageFragmentRootLabel(root, ROOT_QUALIFIED, buf);
318                         buf.append(CONCAT_STRING);
319                 }
320
321                 switch (type) {
322                 case IJavaElement.METHOD:
323                         getMethodLabel((IMethod) element, flags, buf);
324                         break;
325                 case IJavaElement.FIELD:
326                         getFieldLabel((IField) element, flags, buf);
327                         break;
328                 // case IJavaElement.INITIALIZER:
329                 // getInitializerLabel((IInitializer) element, flags, buf);
330                 // break;
331                 case IJavaElement.TYPE:
332                         getTypeLabel((IType) element, flags, buf);
333                         break;
334                 // case IJavaElement.CLASS_FILE:
335                 // getClassFileLabel((IClassFile) element, flags, buf);
336                 // break;
337                 case IJavaElement.COMPILATION_UNIT:
338                         getCompilationUnitLabel((ICompilationUnit) element, flags, buf);
339                         break;
340                 case IJavaElement.PACKAGE_FRAGMENT:
341                         getPackageFragmentLabel((IPackageFragment) element, flags, buf);
342                         break;
343                 case IJavaElement.PACKAGE_FRAGMENT_ROOT:
344                         getPackageFragmentRootLabel((IPackageFragmentRoot) element, flags,
345                                         buf);
346                         break;
347                 case IJavaElement.IMPORT_CONTAINER:
348                 case IJavaElement.IMPORT_DECLARATION:
349                 case IJavaElement.PACKAGE_DECLARATION:
350                         getDeclararionLabel(element, flags, buf);
351                         break;
352                 case IJavaElement.JAVA_PROJECT:
353                 case IJavaElement.JAVA_MODEL:
354                         buf.append(element.getElementName());
355                         break;
356                 default:
357                         buf.append(element.getElementName());
358                 }
359
360                 if (root != null && getFlag(flags, APPEND_ROOT_PATH)) {
361                         buf.append(CONCAT_STRING);
362                         getPackageFragmentRootLabel(root, ROOT_QUALIFIED, buf);
363                 }
364         }
365
366         /**
367          * Appends the label for a method to a StringBuffer. Considers the M_*
368          * flags.
369          */
370         public static void getMethodLabel(IMethod method, int flags,
371                         StringBuffer buf) {
372                 try {
373                         // return type
374                         if (getFlag(flags, M_PRE_RETURNTYPE) && method.exists()
375                                         && !method.isConstructor()) {
376                                 buf.append(Signature.getSimpleName(Signature.toString(method
377                                                 .getReturnType())));
378                                 buf.append(' ');
379                         }
380
381                         // qualification
382                         if (getFlag(flags, M_FULLY_QUALIFIED)) {
383                                 getTypeLabel(method.getDeclaringType(), T_FULLY_QUALIFIED
384                                                 | (flags & P_COMPRESSED), buf);
385                                 buf.append('.');
386                         }
387
388                         buf.append(method.getElementName());
389
390                         // parameters
391                         if (getFlag(flags, M_PARAMETER_TYPES | M_PARAMETER_NAMES)) {
392                                 buf.append('(');
393
394                                 String[] types = getFlag(flags, M_PARAMETER_TYPES) ? method
395                                                 .getParameterTypes() : null;
396                                 String[] names = (getFlag(flags, M_PARAMETER_NAMES) && method
397                                                 .exists()) ? method.getParameterNames() : null;
398                                 int nParams = types != null ? types.length : names.length;
399
400                                 for (int i = 0; i < nParams; i++) {
401                                         if (i > 0) {
402                                                 buf.append(COMMA_STRING); //$NON-NLS-1$
403                                         }
404                                         if (types != null) {
405                                                 buf.append(Signature.getSimpleName(Signature
406                                                                 .toString(types[i])));
407                                         }
408                                         if (names != null) {
409                                                 if (types != null) {
410                                                         buf.append(' ');
411                                                 }
412                                                 buf.append(names[i]);
413                                         }
414                                 }
415                                 buf.append(')');
416                         }
417
418                         if (getFlag(flags, M_EXCEPTIONS) && method.exists()) {
419                                 String[] types = method.getExceptionTypes();
420                                 if (types.length > 0) {
421                                         buf.append(" throws "); //$NON-NLS-1$
422                                         for (int i = 0; i < types.length; i++) {
423                                                 if (i > 0) {
424                                                         buf.append(COMMA_STRING);
425                                                 }
426                                                 buf.append(Signature.getSimpleName(Signature
427                                                                 .toString(types[i])));
428                                         }
429                                 }
430                         }
431
432                         if (getFlag(flags, M_APP_RETURNTYPE) && method.exists()
433                                         && !method.isConstructor()) {
434                                 buf.append(DECL_STRING);
435                                 buf.append(Signature.getSimpleName(Signature.toString(method
436                                                 .getReturnType())));
437                         }
438
439                         // post qualification
440                         if (getFlag(flags, M_POST_QUALIFIED)) {
441                                 buf.append(CONCAT_STRING);
442                                 getTypeLabel(method.getDeclaringType(), T_FULLY_QUALIFIED
443                                                 | (flags & P_COMPRESSED), buf);
444                         }
445
446                 } catch (JavaModelException e) {
447                         PHPeclipsePlugin.log(e); // NotExistsException will not reach
448                                                                                 // 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 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
465                                                 | (flags & P_COMPRESSED), buf);
466                                 buf.append('.');
467                         }
468                         buf.append(field.getElementName());
469
470                         if (getFlag(flags, F_APP_TYPE_SIGNATURE) && field.exists()) {
471                                 buf.append(DECL_STRING);
472                                 buf.append(Signature.toString(field.getTypeSignature()));
473                         }
474
475                         // post qualification
476                         if (getFlag(flags, F_POST_QUALIFIED)) {
477                                 buf.append(CONCAT_STRING);
478                                 getTypeLabel(field.getDeclaringType(), T_FULLY_QUALIFIED
479                                                 | (flags & P_COMPRESSED), buf);
480                         }
481
482                 } catch (JavaModelException e) {
483                         PHPeclipsePlugin.log(e); // NotExistsException will not reach
484                                                                                 // this point
485                 }
486         }
487
488         /**
489          * Appends the label for a initializer to a StringBuffer. Considers the I_*
490          * flags.
491          */
492         // public static void getInitializerLabel(IInitializer initializer, int
493         // flags, StringBuffer buf) {
494         // // qualification
495         // if (getFlag(flags, I_FULLY_QUALIFIED)) {
496         // getTypeLabel(initializer.getDeclaringType(), T_FULLY_QUALIFIED | (flags &
497         // P_COMPRESSED), buf);
498         // buf.append('.');
499         // }
500         // buf.append(JavaUIMessages.getString("JavaElementLabels.initializer"));
501         // //$NON-NLS-1$
502         //
503         // // post qualification
504         // if (getFlag(flags, I_POST_QUALIFIED)) {
505         // buf.append(CONCAT_STRING);
506         // getTypeLabel(initializer.getDeclaringType(), T_FULLY_QUALIFIED | (flags &
507         // P_COMPRESSED), buf);
508         // }
509         // }
510         /**
511          * Appends the label for a type to a StringBuffer. Considers the T_* flags.
512          */
513         public static void getTypeLabel(IType type, int flags, StringBuffer buf) {
514                 if (getFlag(flags, T_FULLY_QUALIFIED)) {
515                         if (type != null) { // jsurfer INSERT
516                                 IPackageFragment pack = type.getPackageFragment();
517                                 if (!pack.isDefaultPackage()) {
518                                         getPackageFragmentLabel(pack, (flags & P_COMPRESSED), buf);
519                                         buf.append('.');
520                                 }
521                                 buf.append(JavaModelUtil.getTypeQualifiedName(type));
522                         } // jsurfer INSERT
523                 } else if (getFlag(flags, T_CONTAINER_QUALIFIED)) {
524                         buf.append(JavaModelUtil.getTypeQualifiedName(type));
525                 } else {
526                         buf.append(type.getElementName());
527                 }
528                 // post qualification
529                 if (getFlag(flags, T_POST_QUALIFIED)) {
530                         buf.append(CONCAT_STRING);
531                         IType declaringType = type.getDeclaringType();
532                         if (declaringType != null) {
533                                 getTypeLabel(declaringType, T_FULLY_QUALIFIED
534                                                 | (flags & P_COMPRESSED), buf);
535                         } else {
536                                 getPackageFragmentLabel(type.getPackageFragment(),
537                                                 (flags & P_COMPRESSED), buf);
538                         }
539                 }
540         }
541
542         /**
543          * Appends the label for a declaration to a StringBuffer. Considers the D_*
544          * flags.
545          */
546         public static void getDeclararionLabel(IJavaElement declaration, int flags,
547                         StringBuffer buf) {
548                 if (getFlag(flags, D_QUALIFIED)) {
549                         IJavaElement openable = (IJavaElement) declaration.getOpenable();
550                         if (openable != null) {
551                                 buf.append(getElementLabel(openable, CF_QUALIFIED
552                                                 | CU_QUALIFIED));
553                                 buf.append('/');
554                         }
555                 }
556                 if (declaration.getElementType() == IJavaElement.IMPORT_CONTAINER) {
557                         buf.append(PHPUIMessages
558                                         .getString("JavaElementLabels.import_container")); //$NON-NLS-1$
559                 } else {
560                         buf.append(declaration.getElementName());
561                 }
562                 // post qualification
563                 if (getFlag(flags, D_POST_QUALIFIED)) {
564                         IJavaElement openable = (IJavaElement) declaration.getOpenable();
565                         if (openable != null) {
566                                 buf.append(CONCAT_STRING);
567                                 buf.append(getElementLabel(openable, CF_QUALIFIED
568                                                 | CU_QUALIFIED));
569                         }
570                 }
571         }
572
573         /**
574          * Appends the label for a class file to a StringBuffer. Considers the CF_*
575          * flags.
576          */
577         // public static void getClassFileLabel(IClassFile classFile, int flags,
578         // StringBuffer buf) {
579         // if (getFlag(flags, CF_QUALIFIED)) {
580         // IPackageFragment pack= (IPackageFragment) classFile.getParent();
581         // if (!pack.isDefaultPackage()) {
582         // buf.append(pack.getElementName());
583         // buf.append('.');
584         // }
585         // }
586         // buf.append(classFile.getElementName());
587         //              
588         // if (getFlag(flags, CF_POST_QUALIFIED)) {
589         // buf.append(CONCAT_STRING);
590         // getPackageFragmentLabel((IPackageFragment) classFile.getParent(), 0,
591         // buf);
592         // }
593         // }
594         /**
595          * Appends the label for a compilation unit to a StringBuffer. Considers the
596          * CU_* flags.
597          */
598         public static void getCompilationUnitLabel(ICompilationUnit cu, int flags,
599                         StringBuffer buf) {
600                 if (getFlag(flags, CU_QUALIFIED)) {
601                         IPackageFragment pack = (IPackageFragment) cu.getParent();
602                         if (!pack.isDefaultPackage()) {
603                                 buf.append(pack.getElementName());
604                                 buf.append('.');
605                         }
606                 }
607                 buf.append(cu.getElementName());
608
609                 if (getFlag(flags, CU_POST_QUALIFIED)) {
610                         buf.append(CONCAT_STRING);
611                         getPackageFragmentLabel((IPackageFragment) cu.getParent(), 0, buf);
612                 }
613         }
614
615         /**
616          * Appends the label for a package fragment to a StringBuffer. Considers the
617          * P_* flags.
618          */
619         public static void getPackageFragmentLabel(IPackageFragment pack,
620                         int flags, StringBuffer buf) {
621                 if (getFlag(flags, P_QUALIFIED)) {
622                         getPackageFragmentRootLabel(
623                                         (IPackageFragmentRoot) pack.getParent(), ROOT_QUALIFIED,
624                                         buf);
625                         buf.append('/');
626                 }
627                 refreshPackageNamePattern();
628                 if (pack.isDefaultPackage()) {
629                         buf.append(PHPUIMessages
630                                         .getString("JavaElementLabels.default_package")); //$NON-NLS-1$
631                 } else if (getFlag(flags, P_COMPRESSED) && fgPkgNameLength >= 0) {
632                         String name = pack.getElementName();
633                         int start = 0;
634                         int dot = name.indexOf('.', start);
635                         while (dot > 0) {
636                                 if (dot - start > fgPkgNameLength - 1) {
637                                         buf.append(fgPkgNamePrefix);
638                                         if (fgPkgNameChars > 0)
639                                                 buf.append(name.substring(start, Math.min(start
640                                                                 + fgPkgNameChars, dot)));
641                                         buf.append(fgPkgNamePostfix);
642                                 } else
643                                         buf.append(name.substring(start, dot + 1));
644                                 start = dot + 1;
645                                 dot = name.indexOf('.', start);
646                         }
647                         buf.append(name.substring(start));
648                 } else {
649                         buf.append(pack.getElementName());
650                 }
651                 if (getFlag(flags, P_POST_QUALIFIED)) {
652                         buf.append(CONCAT_STRING);
653                         getPackageFragmentRootLabel(
654                                         (IPackageFragmentRoot) pack.getParent(), ROOT_QUALIFIED,
655                                         buf);
656                 }
657         }
658
659         /**
660          * Appends the label for a package fragment root to a StringBuffer.
661          * Considers the ROOT_* flags.
662          */
663         public static void getPackageFragmentRootLabel(IPackageFragmentRoot root,
664                         int flags, StringBuffer buf) {
665                 if (root.isArchive())
666                         getArchiveLabel(root, flags, buf);
667                 else
668                         getFolderLabel(root, flags, buf);
669         }
670
671         private static void getArchiveLabel(IPackageFragmentRoot root, int flags,
672                         StringBuffer buf) {
673                 // Handle variables different
674                 if (getFlag(flags, ROOT_VARIABLE) && getVariableLabel(root, flags, buf))
675                         return;
676                 boolean external = root.isExternal();
677                 if (external)
678                         getExternalArchiveLabel(root, flags, buf);
679                 else
680                         getInternalArchiveLabel(root, flags, buf);
681         }
682
683         private static boolean getVariableLabel(IPackageFragmentRoot root,
684                         int flags, StringBuffer buf) {
685                 // try {
686                 // IClasspathEntry rawEntry= root.getRawClasspathEntry();
687                 // if (rawEntry != null) {
688                 // if (rawEntry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
689                 // buf.append(rawEntry.getPath().makeRelative());
690                 // buf.append(CONCAT_STRING);
691                 // if (root.isExternal())
692                 // buf.append(root.getPath().toOSString());
693                 // else
694                 // buf.append(root.getPath().makeRelative().toString());
695                 // return true;
696                 // }
697                 // }
698                 // } catch (JavaModelException e) {
699                 // PHPeclipsePlugin.log(e); // problems with class path
700                 // }
701                 return false;
702         }
703
704         private static void getExternalArchiveLabel(IPackageFragmentRoot root,
705                         int flags, StringBuffer buf) {
706                 IPath path = root.getPath();
707                 if (getFlag(flags, REFERENCED_ROOT_POST_QUALIFIED)) {
708                         int segements = path.segmentCount();
709                         if (segements > 0) {
710                                 buf.append(path.segment(segements - 1));
711                                 if (segements > 1 || path.getDevice() != null) {
712                                         buf.append(CONCAT_STRING);
713                                         buf.append(path.removeLastSegments(1).toOSString());
714                                 }
715                         } else {
716                                 buf.append(path.toOSString());
717                         }
718                 } else {
719                         buf.append(path.toOSString());
720                 }
721         }
722
723         private static void getInternalArchiveLabel(IPackageFragmentRoot root,
724                         int flags, StringBuffer buf) {
725                 IResource resource = root.getResource();
726                 boolean rootQualified = getFlag(flags, ROOT_QUALIFIED);
727                 boolean referencedQualified = getFlag(flags,
728                                 REFERENCED_ROOT_POST_QUALIFIED)
729                                 && JavaModelUtil.isReferenced(root) && resource != null;
730                 if (rootQualified) {
731                         buf.append(root.getPath().makeRelative().toString());
732                 } else {
733                         buf.append(root.getElementName());
734                         if (referencedQualified) {
735                                 buf.append(CONCAT_STRING);
736                                 buf.append(resource.getParent().getFullPath().makeRelative()
737                                                 .toString());
738                         } else if (getFlag(flags, ROOT_POST_QUALIFIED)) {
739                                 buf.append(CONCAT_STRING);
740                                 buf
741                                                 .append(root.getParent().getPath().makeRelative()
742                                                                 .toString());
743                         }
744                 }
745         }
746
747         private static void getFolderLabel(IPackageFragmentRoot root, int flags,
748                         StringBuffer buf) {
749                 IResource resource = root.getResource();
750                 boolean rootQualified = getFlag(flags, ROOT_QUALIFIED);
751                 boolean referencedQualified = getFlag(flags,
752                                 REFERENCED_ROOT_POST_QUALIFIED)
753                                 && JavaModelUtil.isReferenced(root) && resource != null;
754                 if (rootQualified) {
755                         buf.append(root.getPath().makeRelative().toString());
756                 } else {
757                         if (resource != null)
758                                 buf.append(resource.getProjectRelativePath().toString());
759                         else
760                                 buf.append(root.getElementName());
761                         if (referencedQualified) {
762                                 buf.append(CONCAT_STRING);
763                                 buf.append(resource.getProject().getName());
764                         } else if (getFlag(flags, ROOT_POST_QUALIFIED)) {
765                                 buf.append(CONCAT_STRING);
766                                 buf.append(root.getParent().getElementName());
767                         }
768                 }
769         }
770
771         private static void refreshPackageNamePattern() {
772                 String pattern = getPkgNamePatternForPackagesView();
773                 if (pattern.equals(fgPkgNamePattern))
774                         return;
775                 else if (pattern.equals("")) { //$NON-NLS-1$
776                         fgPkgNamePattern = ""; //$NON-NLS-1$
777                         fgPkgNameLength = -1;
778                         return;
779                 }
780                 fgPkgNamePattern = pattern;
781                 int i = 0;
782                 fgPkgNameChars = 0;
783                 fgPkgNamePrefix = ""; //$NON-NLS-1$
784                 fgPkgNamePostfix = ""; //$NON-NLS-1$
785                 while (i < pattern.length()) {
786                         char ch = pattern.charAt(i);
787                         if (Character.isDigit(ch)) {
788                                 fgPkgNameChars = ch - 48;
789                                 if (i > 0)
790                                         fgPkgNamePrefix = pattern.substring(0, i);
791                                 if (i >= 0)
792                                         fgPkgNamePostfix = pattern.substring(i + 1);
793                                 fgPkgNameLength = fgPkgNamePrefix.length() + fgPkgNameChars
794                                                 + fgPkgNamePostfix.length();
795                                 return;
796                         }
797                         i++;
798                 }
799                 fgPkgNamePrefix = pattern;
800                 fgPkgNameLength = pattern.length();
801         }
802
803         private static String getPkgNamePatternForPackagesView() {
804                 IPreferenceStore store = PreferenceConstants.getPreferenceStore();
805                 if (!store
806                                 .getBoolean(PreferenceConstants.APPEARANCE_COMPRESS_PACKAGE_NAMES))
807                         return ""; //$NON-NLS-1$
808                 return store
809                                 .getString(PreferenceConstants.APPEARANCE_PKG_NAME_PATTERN_FOR_PKG_VIEW);
810         }
811 }