1 11 package org.eclipse.jdt.ui.text.java; 12 13 import java.util.Comparator ; 14 15 import org.eclipse.jface.text.contentassist.ICompletionProposal; 16 import org.eclipse.jface.text.templates.TemplateProposal; 17 18 import org.eclipse.jdt.internal.ui.text.java.AbstractJavaCompletionProposal; 19 20 29 public final class CompletionProposalComparator implements Comparator { 30 31 private boolean fOrderAlphabetically; 32 33 36 public CompletionProposalComparator() { 37 fOrderAlphabetically= false; 38 } 39 40 47 public void setOrderAlphabetically(boolean orderAlphabetically) { 48 fOrderAlphabetically= orderAlphabetically; 49 } 50 51 54 public int compare(Object o1, Object o2) { 55 ICompletionProposal p1= (ICompletionProposal) o1; 56 ICompletionProposal p2= (ICompletionProposal) o2; 57 58 if (!fOrderAlphabetically) { 59 int r1= getRelevance(p1); 60 int r2= getRelevance(p2); 61 int relevanceDif= r2 - r1; 62 if (relevanceDif != 0) { 63 return relevanceDif; 64 } 65 } 66 70 return getSortKey(p1).compareToIgnoreCase(getSortKey(p2)); 72 } 73 74 private String getSortKey(ICompletionProposal p) { 75 if (p instanceof AbstractJavaCompletionProposal) 76 return ((AbstractJavaCompletionProposal) p).getSortString(); 77 return p.getDisplayString(); 78 } 79 80 private int getRelevance(ICompletionProposal obj) { 81 if (obj instanceof IJavaCompletionProposal) { 82 IJavaCompletionProposal jcp= (IJavaCompletionProposal) obj; 83 return jcp.getRelevance(); 84 } else if (obj instanceof TemplateProposal) { 85 TemplateProposal tp= (TemplateProposal) obj; 86 return tp.getRelevance(); 87 } 88 return 0; 90 } 91 92 } 93 | Popular Tags |