KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > java > JavaCompletionProposalComparator


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

11 package org.eclipse.jdt.internal.ui.text.java;
12
13 import java.util.Comparator JavaDoc;
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 JavaDoc {
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     /**
31      * Constructor for CompletionProposalComparator.
32      */

33     public JavaCompletionProposalComparator() {
34         fOrderAlphabetically= false;
35     }
36     
37     public void setOrderAlphabetically(boolean orderAlphabetically) {
38         fOrderAlphabetically= orderAlphabetically;
39     }
40     
41     /* (non-Javadoc)
42      * @see Comparator#compare(Object, Object)
43      */

44     public int compare(Object JavaDoc o1, Object JavaDoc 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         // fix for bug 67468
57
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         // catch all
69
return 0;
70     }
71     
72 }
73
Popular Tags