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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.ui;
13 import java.text.Collator;
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;
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;
40 * Sorter for Java elements. Ordered by element category, then by element name.
41 * Package fragment roots are sorted as ordered on the classpath.
44 * This class may be instantiated; it is not intended to be subclassed.
49 public class JavaElementSorter extends ViewerSorter {
51 private static final int PROJECTS= 1;
52 private static final int PACKAGEFRAGMENTROOTS= 2;
53 private static final int PACKAGEFRAGMENT= 3;
55 private static final int COMPILATIONUNITS= 4;
56 private static final int CLASSFILES= 5;
58 private static final int RESOURCEFOLDERS= 7;
59 private static final int RESOURCES= 8;
60 private static final int STORAGE= 9;
62 private static final int PACKAGE_DECL= 10;
63 private static final int IMPORT_CONTAINER= 11;
64 private static final int IMPORT_DECLARATION= 12;
66 // Includes all categories ordered using the OutlineSortOrderPage:
67 // types, initializers, methods & fields
68 private static final int MEMBERSOFFSET= 15;
70 private static final int JAVAELEMENTS= 50;
71 private static final int OTHERS= 51;
73 public JavaElementSorter() {
74 super(null); // delay initialization of collator
78 * @deprecated Bug 22518. Method never used: does not override ViewerSorter#isSorterProperty(Object, String).
79 * Method could be removed, but kept for API compatibility.
81 public boolean isSorterProperty(Object element, Object property) {
86 * @see ViewerSorter#category
88 public int category(Object element) {
89 if (element instanceof IJavaElement) {
91 IJavaElement je= (IJavaElement) element;
93 switch (je.getElementType()) {
94 case IJavaElement.METHOD:
96 IMethod method= (IMethod) je;
97 if (method.isConstructor()) {
98 return getMemberCategory(MembersOrderPreferenceCache.CONSTRUCTORS_INDEX);
100 int flags= method.getFlags();
101 if (Flags.isStatic(flags))
102 return getMemberCategory(MembersOrderPreferenceCache.STATIC_METHODS_INDEX);
104 return getMemberCategory(MembersOrderPreferenceCache.METHOD_INDEX);
106 case IJavaElement.FIELD :
108 int flags= ((IField) je).getFlags();
109 if (Flags.isStatic(flags))
110 return getMemberCategory(MembersOrderPreferenceCache.STATIC_FIELDS_INDEX);
112 return getMemberCategory(MembersOrderPreferenceCache.FIELDS_INDEX);
114 // case IJavaElement.INITIALIZER :
116 // int flags= ((IInitializer) je).getFlags();
117 // if (Flags.isStatic(flags))
118 // return getMemberCategory(MembersOrderPreferenceCache.STATIC_INIT_INDEX);
120 // return getMemberCategory(MembersOrderPreferenceCache.INIT_INDEX);
122 case IJavaElement.TYPE :
123 return getMemberCategory(MembersOrderPreferenceCache.TYPE_INDEX);
124 case IJavaElement.PACKAGE_DECLARATION :
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;
135 return PACKAGEFRAGMENT;
136 case IJavaElement.PACKAGE_FRAGMENT_ROOT :
137 return PACKAGEFRAGMENTROOTS;
138 case IJavaElement.JAVA_PROJECT :
140 case IJavaElement.CLASS_FILE :
142 case IJavaElement.COMPILATION_UNIT :
143 return COMPILATIONUNITS;
146 } catch (JavaModelException e) {
147 PHPeclipsePlugin.log(e);
150 } else if (element instanceof IFile) {
152 } else if (element instanceof IProject) {
154 } else if (element instanceof IContainer) {
155 return RESOURCEFOLDERS;
156 } else if (element instanceof IStorage) {
158 // } else if (element instanceof ClassPathContainer) {
159 // return PACKAGEFRAGMENTROOTS;
164 private int getMemberCategory(int kind) {
165 int offset= PHPeclipsePlugin.getDefault().getMemberOrderPreferenceCache().getIndex(kind);
166 return offset + MEMBERSOFFSET;
170 * @see ViewerSorter#compare
172 public int compare(Viewer viewer, Object e1, Object e2) {
173 int cat1= category(e1);
174 int cat2= category(e2);
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));
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);
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);
201 String name1= ((IJavaElement) e1).getElementName();
202 String name2= ((IJavaElement) e2).getElementName();
204 // java element are sorted by name
205 int cmp= getCollator().compare(name1, name2);
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]));
220 return params1.length - params2.length;
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$
235 // return JavaModelUtil.getPackageFragmentRoot((IJavaElement)element);
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);
250 return 0; // can't compare
253 // private int getClassPathIndex(IPackageFragmentRoot root) {
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)) {
262 // } catch (JavaModelException e) {
265 // return Integer.MAX_VALUE;
269 * @see org.eclipse.jface.viewers.ViewerSorter#getCollator()
271 public final Collator getCollator() {
272 if (collator == null) {
273 collator= Collator.getInstance();