KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > typehierarchy > AbstractHierarchyViewerSorter


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

11 package org.eclipse.jdt.internal.ui.typehierarchy;
12
13 import org.eclipse.jface.viewers.Viewer;
14 import org.eclipse.jface.viewers.ViewerComparator;
15
16 import org.eclipse.jdt.core.Flags;
17 import org.eclipse.jdt.core.IMethod;
18 import org.eclipse.jdt.core.IType;
19 import org.eclipse.jdt.core.ITypeHierarchy;
20 import org.eclipse.jdt.core.JavaModelException;
21
22 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
23 import org.eclipse.jdt.internal.corext.util.MethodOverrideTester;
24
25 import org.eclipse.jdt.ui.JavaElementComparator;
26
27 import org.eclipse.jdt.internal.ui.viewsupport.SourcePositionComparator;
28
29 /**
30   */

31 public abstract class AbstractHierarchyViewerSorter extends ViewerComparator {
32     
33     private static final int OTHER= 1;
34     private static final int CLASS= 2;
35     private static final int INTERFACE= 3;
36     private static final int ANONYM= 4;
37     
38     private JavaElementComparator fNormalSorter;
39     private SourcePositionComparator fSourcePositonSorter;
40     
41     public AbstractHierarchyViewerSorter() {
42         fNormalSorter= new JavaElementComparator();
43         fSourcePositonSorter= new SourcePositionComparator();
44     }
45     
46     protected abstract ITypeHierarchy getHierarchy(IType type);
47     public abstract boolean isSortByDefiningType();
48     public abstract boolean isSortAlphabetically();
49     
50     
51     protected int getTypeFlags(IType type) throws JavaModelException {
52         return type.getFlags();
53     }
54     
55     /* (non-Javadoc)
56      * @see org.eclipse.jface.viewers.ViewerSorter#category(java.lang.Object)
57      */

58     public int category(Object JavaDoc element) {
59         if (element instanceof IType) {
60             IType type= (IType) element;
61             if (type.getElementName().length() == 0) {
62                 return ANONYM;
63             }
64             try {
65                 int flags= getTypeFlags(type);
66                 if (Flags.isInterface(flags)) {
67                     return INTERFACE;
68                 } else {
69                     return CLASS;
70                 }
71             } catch (JavaModelException e) {
72                 // ignore
73
}
74         }
75         return OTHER;
76     }
77
78     /* (non-Javadoc)
79      * @see org.eclipse.jface.viewers.ViewerSorter#compare(null, null, null)
80      */

81     public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
82         if (!isSortAlphabetically() && !isSortByDefiningType()) {
83             return fSourcePositonSorter.compare(viewer, e1, e2);
84         }
85         
86         int cat1= category(e1);
87         int cat2= category(e2);
88
89         if (cat1 != cat2)
90             return cat1 - cat2;
91         
92         if (cat1 == OTHER) { // method or field
93
if (isSortByDefiningType()) {
94                 try {
95                     IType def1= (e1 instanceof IMethod) ? getDefiningType((IMethod) e1) : null;
96                     IType def2= (e2 instanceof IMethod) ? getDefiningType((IMethod) e2) : null;
97                     if (def1 != null) {
98                         if (def2 != null) {
99                             if (!def2.equals(def1)) {
100                                 return compareInHierarchy(def1, def2);
101                             }
102                         } else {
103                             return -1;
104                         }
105                     } else {
106                         if (def2 != null) {
107                             return 1;
108                         }
109                     }
110                 } catch (JavaModelException e) {
111                     // ignore, default to normal comparison
112
}
113             }
114             if (isSortAlphabetically()) {
115                 return fNormalSorter.compare(viewer, e1, e2); // use appearance pref page settings
116
}
117             return 0;
118         } else if (cat1 == ANONYM) {
119             return 0;
120         } else if (isSortAlphabetically()) {
121             String JavaDoc name1= ((IType) e1).getElementName();
122             String JavaDoc name2= ((IType) e2).getElementName();
123             return getComparator().compare(name1, name2);
124         }
125         return 0;
126     }
127     
128     private IType getDefiningType(IMethod method) throws JavaModelException {
129         int flags= method.getFlags();
130         if (Flags.isPrivate(flags) || Flags.isStatic(flags) || method.isConstructor()) {
131             return null;
132         }
133     
134         IType declaringType= method.getDeclaringType();
135         ITypeHierarchy hierarchy= getHierarchy(declaringType);
136         if (hierarchy != null) {
137             MethodOverrideTester tester= new MethodOverrideTester(declaringType, hierarchy);
138             IMethod res= tester.findDeclaringMethod(method, true);
139             if (res != null) {
140                 return res.getDeclaringType();
141             }
142         }
143         return null;
144     }
145     
146
147     private int compareInHierarchy(IType def1, IType def2) {
148         if (JavaModelUtil.isSuperType(getHierarchy(def1), def2, def1)) {
149             return 1;
150         } else if (JavaModelUtil.isSuperType(getHierarchy(def2), def1, def2)) {
151             return -1;
152         }
153         // interfaces after classes
154
try {
155             int flags1= getTypeFlags(def1);
156             int flags2= getTypeFlags(def2);
157             if (Flags.isInterface(flags1)) {
158                 if (!Flags.isInterface(flags2)) {
159                     return 1;
160                 }
161             } else if (Flags.isInterface(flags2)) {
162                 return -1;
163             }
164         } catch (JavaModelException e) {
165             // ignore
166
}
167         String JavaDoc name1= def1.getElementName();
168         String JavaDoc name2= def2.getElementName();
169         
170         return getComparator().compare(name1, name2);
171     }
172
173 }
174
Popular Tags