1 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 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 58 public int category(Object 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 } 74 } 75 return OTHER; 76 } 77 78 81 public int compare(Viewer viewer, Object e1, Object 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) { 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 } 113 } 114 if (isSortAlphabetically()) { 115 return fNormalSorter.compare(viewer, e1, e2); } 117 return 0; 118 } else if (cat1 == ANONYM) { 119 return 0; 120 } else if (isSortAlphabetically()) { 121 String name1= ((IType) e1).getElementName(); 122 String 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 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 } 167 String name1= def1.getElementName(); 168 String name2= def2.getElementName(); 169 170 return getComparator().compare(name1, name2); 171 } 172 173 } 174 | Popular Tags |