KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > ui > internal > views > FederatedSearchSorter


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.help.ui.internal.views;
12
13 import org.eclipse.help.IHelpResource;
14 import org.eclipse.help.search.ISearchEngineResult;
15 import org.eclipse.jface.viewers.Viewer;
16 import org.eclipse.jface.viewers.ViewerComparator;
17
18 public class FederatedSearchSorter extends ViewerComparator {
19     public FederatedSearchSorter() {
20         super(ReusableHelpPart.SHARED_COLLATOR);
21     }
22     
23     public int category(Object JavaDoc element) {
24         if (element instanceof ISearchEngineResult) {
25             ISearchEngineResult r = (ISearchEngineResult)element;
26             IHelpResource c = r.getCategory();
27             if (c!=null) {
28                 String JavaDoc label = c.getLabel();
29                 if (label.length()==0)
30                     return 10;
31                 return 5;
32             }
33         }
34         return super.category(element);
35     }
36
37
38     /* (non-Javadoc)
39      * @see org.eclipse.jface.viewers.ViewerComparator#compare(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
40      */

41     public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
42         int cat1 = category(e1);
43         int cat2 = category(e2);
44
45         if (cat1 != cat2)
46             return cat1 - cat2;
47         try {
48             ISearchEngineResult r1 = (ISearchEngineResult) e1;
49             ISearchEngineResult r2 = (ISearchEngineResult) e2;
50             IHelpResource c1 = r1.getCategory();
51             IHelpResource c2 = r2.getCategory();
52             if (c1!=null && c2!=null) {
53                 int cat = super.compare(viewer, c1.getLabel(), c2.getLabel());
54                 if (cat!=0) return cat;
55             }
56             float rank1 = ((ISearchEngineResult) e1).getScore();
57             float rank2 = ((ISearchEngineResult) e2).getScore();
58             if (rank1 - rank2 > 0) {
59                 return -1;
60             } else if (rank1 == rank2) {
61                 return 0;
62             } else {
63                 return 1;
64             }
65         } catch (Exception JavaDoc e) {
66         }
67         return super.compare(viewer, e1, e2);
68     }
69 }
70
Popular Tags