1 11 package org.eclipse.jdt.internal.ui.viewsupport; 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.IClassFile; 18 import org.eclipse.jdt.core.ICompilationUnit; 19 import org.eclipse.jdt.core.IJavaElement; 20 import org.eclipse.jdt.core.IMember; 21 import org.eclipse.jdt.core.ISourceRange; 22 import org.eclipse.jdt.core.ISourceReference; 23 import org.eclipse.jdt.core.IType; 24 import org.eclipse.jdt.core.JavaModelException; 25 26 27 33 public class SourcePositionComparator extends ViewerComparator { 34 35 38 public int compare(Viewer viewer, Object e1, Object e2) { 39 if (!(e1 instanceof ISourceReference)) 40 return 0; 41 if (!(e2 instanceof ISourceReference)) 42 return 0; 43 44 IJavaElement parent1= ((IJavaElement)e1).getParent(); 45 if (parent1 == null || !parent1.equals(((IJavaElement)e2).getParent())) { 46 IType t1= getOutermostDeclaringType(e1); 47 if (t1 == null) 48 return 0; 49 50 IType t2= getOutermostDeclaringType(e2); 51 try { 52 if (!t1.equals(t2)) { 53 if (t2 == null) 54 return 0; 55 56 if (Flags.isPublic(t1.getFlags()) && Flags.isPublic(t2.getFlags())) 57 return 0; 58 59 if (!t1.getPackageFragment().equals(t2.getPackageFragment())) 60 return 0; 61 62 ICompilationUnit cu1= (ICompilationUnit)((IJavaElement)e1).getAncestor(IJavaElement.COMPILATION_UNIT); 63 if (cu1 != null) { 64 if (!cu1.equals(((IJavaElement)e2).getAncestor(IJavaElement.COMPILATION_UNIT))) 65 return 0; 66 } else { 67 IClassFile cf1= (IClassFile)((IJavaElement)e1).getAncestor(IJavaElement.CLASS_FILE); 68 if (cf1 == null) 69 return 0; 70 IClassFile cf2= (IClassFile)((IJavaElement)e2).getAncestor(IJavaElement.CLASS_FILE); 71 String source1= cf1.getSource(); 72 if (source1 != null && !source1.equals(cf2.getSource())) 73 return 0; 74 } 75 } 76 } catch (JavaModelException e3) { 77 return 0; 78 } 79 } 80 81 try { 82 ISourceRange sr1= ((ISourceReference)e1).getSourceRange(); 83 ISourceRange sr2= ((ISourceReference)e2).getSourceRange(); 84 if (sr1 == null || sr2 == null) 85 return 0; 86 87 return sr1.getOffset() - sr2.getOffset(); 88 89 } catch (JavaModelException e) { 90 return 0; 91 } 92 } 93 94 private IType getOutermostDeclaringType(Object element) { 95 if (!(element instanceof IMember)) 96 return null; 97 98 IType declaringType; 99 if (element instanceof IType) 100 declaringType= (IType)element; 101 else { 102 declaringType= ((IMember)element).getDeclaringType(); 103 if (declaringType == null) 104 return null; 105 } 106 107 IType declaringTypeDeclaringType= declaringType.getDeclaringType(); 108 while (declaringTypeDeclaringType != null) { 109 declaringType= declaringTypeDeclaringType; 110 declaringTypeDeclaringType= declaringType.getDeclaringType(); 111 } 112 return declaringType; 113 } 114 } 115 | Popular Tags |