95bcc5eb597c6af70552a65a5f07d0c057bf480e
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / ui / JavaElementSorter.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.ui;
12
13 import java.text.Collator;
14
15 import net.sourceforge.phpdt.core.Flags;
16 import net.sourceforge.phpdt.core.IField;
17 import net.sourceforge.phpdt.core.IJavaElement;
18 import net.sourceforge.phpdt.core.IMethod;
19 import net.sourceforge.phpdt.core.IPackageFragment;
20 import net.sourceforge.phpdt.core.IPackageFragmentRoot;
21 import net.sourceforge.phpdt.core.JavaModelException;
22 import net.sourceforge.phpdt.core.Signature;
23 import net.sourceforge.phpdt.internal.ui.preferences.MembersOrderPreferenceCache;
24 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
25
26 import org.eclipse.core.resources.IContainer;
27 import org.eclipse.core.resources.IFile;
28 import org.eclipse.core.resources.IProject;
29 import org.eclipse.core.resources.IStorage;
30 import org.eclipse.core.runtime.IAdaptable;
31 import org.eclipse.jface.viewers.ContentViewer;
32 import org.eclipse.jface.viewers.IBaseLabelProvider;
33 import org.eclipse.jface.viewers.ILabelProvider;
34 import org.eclipse.jface.viewers.Viewer;
35 import org.eclipse.jface.viewers.ViewerSorter;
36 import org.eclipse.ui.model.IWorkbenchAdapter;
37
38
39 /**
40  * Sorter for Java elements. Ordered by element category, then by element name. 
41  * Package fragment roots are sorted as ordered on the classpath.
42  * 
43  * <p>
44  * This class may be instantiated; it is not intended to be subclassed.
45  * </p>
46  * 
47  * @since 2.0
48  */
49 public class JavaElementSorter extends ViewerSorter {
50         
51         private static final int PROJECTS= 1;
52         private static final int PACKAGEFRAGMENTROOTS= 2;
53         private static final int PACKAGEFRAGMENT= 3;
54
55         private static final int COMPILATIONUNITS= 4;
56         private static final int CLASSFILES= 5;
57         
58         private static final int RESOURCEFOLDERS= 7;
59         private static final int RESOURCES= 8;
60         private static final int STORAGE= 9;    
61         
62         private static final int PACKAGE_DECL=  10;
63         private static final int IMPORT_CONTAINER= 11;
64         private static final int IMPORT_DECLARATION= 12;
65         
66         // Includes all categories ordered using the OutlineSortOrderPage:
67         // types, initializers, methods & fields
68         private static final int MEMBERSOFFSET= 15;
69         
70         private static final int JAVAELEMENTS= 50;
71         private static final int OTHERS= 51;
72         
73         public JavaElementSorter() {
74                 super(null); // delay initialization of collator
75         }
76                 
77         /**
78          * @deprecated Bug 22518. Method never used: does not override ViewerSorter#isSorterProperty(Object, String).
79          * Method could be removed, but kept for API compatibility.
80          */     
81         public boolean isSorterProperty(Object element, Object property) {
82                 return true;
83         }
84
85         /*
86          * @see ViewerSorter#category
87          */
88         public int category(Object element) {
89                 if (element instanceof IJavaElement) {
90                         try {
91                                 IJavaElement je= (IJavaElement) element;
92
93                                 switch (je.getElementType()) {
94                                         case IJavaElement.METHOD:
95                                                 {
96                                                         IMethod method= (IMethod) je;
97                                                         if (method.isConstructor()) {
98                                                                 return getMemberCategory(MembersOrderPreferenceCache.CONSTRUCTORS_INDEX);
99                                                         }
100                                                         int flags= method.getFlags();
101                                                         if (Flags.isStatic(flags))
102                                                                 return getMemberCategory(MembersOrderPreferenceCache.STATIC_METHODS_INDEX);
103                                                         else
104                                                                 return getMemberCategory(MembersOrderPreferenceCache.METHOD_INDEX);
105                                                 }
106                                         case IJavaElement.FIELD :
107                                                 {
108                                                         int flags= ((IField) je).getFlags();
109                                                         if (Flags.isStatic(flags))
110                                                                 return getMemberCategory(MembersOrderPreferenceCache.STATIC_FIELDS_INDEX);
111                                                         else
112                                                                 return getMemberCategory(MembersOrderPreferenceCache.FIELDS_INDEX);
113                                                 }
114 //                                      case IJavaElement.INITIALIZER :
115 //                                              {
116 //                                                      int flags= ((IInitializer) je).getFlags();
117 //                                                      if (Flags.isStatic(flags))
118 //                                                              return getMemberCategory(MembersOrderPreferenceCache.STATIC_INIT_INDEX);
119 //                                                      else
120 //                                                              return getMemberCategory(MembersOrderPreferenceCache.INIT_INDEX);
121 //                                              }
122                                         case IJavaElement.TYPE :
123                                                 return getMemberCategory(MembersOrderPreferenceCache.TYPE_INDEX);
124                                         case IJavaElement.PACKAGE_DECLARATION :
125                                                 return PACKAGE_DECL;
126                                         case IJavaElement.IMPORT_CONTAINER :
127                                                 return IMPORT_CONTAINER;
128                                         case IJavaElement.IMPORT_DECLARATION :
129                                                 return IMPORT_DECLARATION;
130                                         case IJavaElement.PACKAGE_FRAGMENT :
131                                                 IPackageFragment pack= (IPackageFragment) je;
132                                                 if (pack.getParent().getResource() instanceof IProject) {
133                                                         return PACKAGEFRAGMENTROOTS;
134                                                 }
135                                                 return PACKAGEFRAGMENT;
136                                         case IJavaElement.PACKAGE_FRAGMENT_ROOT :
137                                                 return PACKAGEFRAGMENTROOTS;
138                                         case IJavaElement.JAVA_PROJECT :
139                                                 return PROJECTS;
140                                         case IJavaElement.CLASS_FILE :
141                                                 return CLASSFILES;
142                                         case IJavaElement.COMPILATION_UNIT :
143                                                 return COMPILATIONUNITS;
144                                 }
145
146                         } catch (JavaModelException e) {
147                                 PHPeclipsePlugin.log(e);
148                         }
149                         return JAVAELEMENTS;
150                 } else if (element instanceof IFile) {
151                         return RESOURCES;
152                 } else if (element instanceof IProject) {
153                         return PROJECTS;
154                 } else if (element instanceof IContainer) {
155                         return RESOURCEFOLDERS;
156                 } else if (element instanceof IStorage) {
157                         return STORAGE;
158 //              } else if (element instanceof ClassPathContainer) {
159 //                      return PACKAGEFRAGMENTROOTS;
160                 }
161                 return OTHERS;
162         }
163         
164         private int getMemberCategory(int kind) {
165                 int offset= PHPeclipsePlugin.getDefault().getMemberOrderPreferenceCache().getIndex(kind);
166                 return offset + MEMBERSOFFSET;
167         }               
168
169         /*
170          * @see ViewerSorter#compare
171          */
172         public int compare(Viewer viewer, Object e1, Object e2) {
173                 int cat1= category(e1);
174                 int cat2= category(e2);
175
176                 if (cat1 != cat2)
177                         return cat1 - cat2;
178                 
179                 if (cat1 == PROJECTS) {
180                         IWorkbenchAdapter a1= (IWorkbenchAdapter)((IAdaptable)e1).getAdapter(IWorkbenchAdapter.class);
181                         IWorkbenchAdapter a2= (IWorkbenchAdapter)((IAdaptable)e2).getAdapter(IWorkbenchAdapter.class);
182                                 return getCollator().compare(a1.getLabel(e1), a2.getLabel(e2));
183                 }
184                         
185 //              if (cat1 == PACKAGEFRAGMENTROOTS) {
186 //                      IPackageFragmentRoot root1= getPackageFragmentRoot(e1);
187 //                      IPackageFragmentRoot root2= getPackageFragmentRoot(e2);
188 //                      if (!root1.getPath().equals(root2.getPath())) {
189 //                              int p1= getClassPathIndex(root1);
190 //                              int p2= getClassPathIndex(root2);
191 //                              if (p1 != p2) {
192 //                                      return p1 - p2;
193 //                              }
194 //                      }
195 //              }
196                 // non - java resources are sorted using the label from the viewers label provider
197                 if (cat1 == PROJECTS || cat1 == RESOURCES || cat1 == RESOURCEFOLDERS || cat1 == STORAGE || cat1 == OTHERS) {
198                         return compareWithLabelProvider(viewer, e1, e2);
199                 }
200                 
201                 String name1= ((IJavaElement) e1).getElementName();
202                 String name2= ((IJavaElement) e2).getElementName();
203                 
204                 // java element are sorted by name
205                 int cmp= getCollator().compare(name1, name2);
206                 if (cmp != 0) {
207                         return cmp;
208                 }
209                 
210                 if (e1 instanceof IMethod) {
211                         String[] params1= ((IMethod) e1).getParameterTypes();
212                         String[] params2= ((IMethod) e2).getParameterTypes();
213                         int len= Math.min(params1.length, params2.length);
214                         for (int i = 0; i < len; i++) {
215                                 cmp= getCollator().compare(Signature.toString(params1[i]), Signature.toString(params2[i]));
216                                 if (cmp != 0) {
217                                         return cmp;
218                                 }
219                         }
220                         return params1.length - params2.length;
221                 }
222                 return 0;
223         }
224
225 //      private IPackageFragmentRoot getPackageFragmentRoot(Object element) {
226 //              if (element instanceof ClassPathContainer) {
227 //                      // return first package fragment root from the container
228 //                      ClassPathContainer cp= (ClassPathContainer)element;
229 //                      Object[] roots= cp.getPackageFragmentRoots();
230 //                      if (roots.length > 0)
231 //                              return (IPackageFragmentRoot)roots[0];
232 //                      // non resolvable - return a dummy package fragment root
233 //                      return cp.getJavaProject().getPackageFragmentRoot("Non-Resolvable");  //$NON-NLS-1$
234 //              }
235 //              return JavaModelUtil.getPackageFragmentRoot((IJavaElement)element);
236 //      }
237         
238         private int compareWithLabelProvider(Viewer viewer, Object e1, Object e2) {
239                 if (viewer == null || !(viewer instanceof ContentViewer)) {
240                         IBaseLabelProvider prov = ((ContentViewer) viewer).getLabelProvider();
241                         if (prov instanceof ILabelProvider) {
242                                 ILabelProvider lprov= (ILabelProvider) prov;
243                                 String name1 = lprov.getText(e1);
244                                 String name2 = lprov.getText(e2);
245                                 if (name1 != null && name2 != null) {
246                                         return getCollator().compare(name1, name2);
247                                 }
248                         }
249                 }
250                 return 0; // can't compare
251         }
252                         
253 //      private int getClassPathIndex(IPackageFragmentRoot root) {
254 //              try {
255 //                      IPath rootPath= root.getPath();
256 //                      IPackageFragmentRoot[] roots= root.getJavaProject().getPackageFragmentRoots();
257 //                      for (int i= 0; i < roots.length; i++) {
258 //                              if (roots[i].getPath().equals(rootPath)) {
259 //                                      return i;
260 //                              }
261 //                      }
262 //              } catch (JavaModelException e) {
263 //              }
264 //
265 //              return Integer.MAX_VALUE;
266 //      }
267         
268         /* (non-Javadoc)
269          * @see org.eclipse.jface.viewers.ViewerSorter#getCollator()
270          */
271         public final Collator getCollator() {
272                 if (collator == null) {
273                         collator= Collator.getInstance();
274                 }
275                 return collator;
276         }
277
278 }