KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > text > java > CompletionProposalComparator


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

11 package org.eclipse.jdt.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.internal.ui.text.java.AbstractJavaCompletionProposal;
19
20 /**
21  * Comparator for java completion proposals. Completion proposals can be sorted by relevance or
22  * alphabetically.
23  * <p>
24  * Note: this comparator imposes orderings that are inconsistent with equals.
25  * </p>
26  *
27  * @since 3.1
28  */

29 public final class CompletionProposalComparator implements Comparator JavaDoc {
30
31     private boolean fOrderAlphabetically;
32
33     /**
34      * Creates a comparator that sorts by relevance.
35      */

36     public CompletionProposalComparator() {
37         fOrderAlphabetically= false;
38     }
39
40     /**
41      * Sets the sort order. Default is <code>false</code>, i.e. order by
42      * relevance.
43      *
44      * @param orderAlphabetically <code>true</code> to order alphabetically,
45      * <code>false</code> to order by relevance
46      */

47     public void setOrderAlphabetically(boolean orderAlphabetically) {
48         fOrderAlphabetically= orderAlphabetically;
49     }
50
51     /*
52      * @see Comparator#compare(Object, Object)
53      */

54     public int compare(Object JavaDoc o1, Object JavaDoc 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         /*
67          * TODO the correct (but possibly much slower) sorting would use a
68          * collator.
69          */

70         // fix for bug 67468
71
return getSortKey(p1).compareToIgnoreCase(getSortKey(p2));
72     }
73
74     private String JavaDoc 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         // catch all
89
return 0;
90     }
91
92 }
93
Popular Tags