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