1 /*******************************************************************************
2  * Copyright (c) 2000, 2016 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.help.ui.internal.views;
15 
16 import org.eclipse.help.IHelpResource;
17 import org.eclipse.help.search.ISearchEngineResult;
18 import org.eclipse.jface.viewers.Viewer;
19 import org.eclipse.jface.viewers.ViewerComparator;
20 
21 public class FederatedSearchSorter extends ViewerComparator {
FederatedSearchSorter()22 	public FederatedSearchSorter() {
23 		super(ReusableHelpPart.SHARED_COLLATOR);
24 	}
25 
26 	@Override
category(Object element)27 	public int category(Object element) {
28 		if (element instanceof ISearchEngineResult) {
29 			ISearchEngineResult r = (ISearchEngineResult)element;
30 			IHelpResource c = r.getCategory();
31 			if (c!=null) {
32 				String label = c.getLabel();
33 				if (label.length()==0)
34 					return 10;
35 				return 5;
36 			}
37 		}
38 		return super.category(element);
39 	}
40 
41 
42 	@Override
compare(Viewer viewer, Object e1, Object e2)43 	public int compare(Viewer viewer, Object e1, Object e2) {
44 		int cat1 = category(e1);
45 		int cat2 = category(e2);
46 
47 		if (cat1 != cat2)
48 			return cat1 - cat2;
49 		try {
50 			ISearchEngineResult r1 = (ISearchEngineResult) e1;
51 			ISearchEngineResult r2 = (ISearchEngineResult) e2;
52 			IHelpResource c1 = r1.getCategory();
53 			IHelpResource c2 = r2.getCategory();
54 			if (c1!=null && c2!=null) {
55 				int cat = super.compare(viewer, c1.getLabel(), c2.getLabel());
56 				if (cat!=0) return cat;
57 			}
58 			float rank1 = ((ISearchEngineResult) e1).getScore();
59 			float rank2 = ((ISearchEngineResult) e2).getScore();
60 			if (rank1 - rank2 > 0) {
61 				return -1;
62 			} else if (rank1 == rank2) {
63 				return 0;
64 			} else {
65 				return 1;
66 			}
67 		} catch (Exception e) {
68 		}
69 		return super.compare(viewer, e1, e2);
70 	}
71 }
72