1 11 package org.eclipse.jdt.internal.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.ui.text.java.IJavaCompletionProposal; 19 20 public class JavaCompletionProposalComparator implements Comparator { 21 22 private static JavaCompletionProposalComparator fgInstance= new JavaCompletionProposalComparator(); 23 24 public static JavaCompletionProposalComparator getInstance() { 25 return fgInstance; 26 } 27 28 private boolean fOrderAlphabetically; 29 30 33 public JavaCompletionProposalComparator() { 34 fOrderAlphabetically= false; 35 } 36 37 public void setOrderAlphabetically(boolean orderAlphabetically) { 38 fOrderAlphabetically= orderAlphabetically; 39 } 40 41 44 public int compare(Object o1, Object o2) { 45 ICompletionProposal p1= (ICompletionProposal) o1; 46 ICompletionProposal p2= (ICompletionProposal) o2; 47 48 if (!fOrderAlphabetically) { 49 int r1= getRelevance(p1); 50 int r2= getRelevance(p2); 51 int relevanceDif= r2 - r1; 52 if (relevanceDif != 0) { 53 return relevanceDif; 54 } 55 } 56 return p1.getDisplayString().compareToIgnoreCase(p2.getDisplayString()); 58 } 59 60 private int getRelevance(ICompletionProposal obj) { 61 if (obj instanceof IJavaCompletionProposal) { 62 IJavaCompletionProposal jcp= (IJavaCompletionProposal) obj; 63 return jcp.getRelevance(); 64 } else if (obj instanceof TemplateProposal) { 65 TemplateProposal tp= (TemplateProposal) obj; 66 return tp.getRelevance(); 67 } 68 return 0; 70 } 71 72 } 73 | Popular Tags |