KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > extensions > ExtensionPriorityComparator


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.ui.internal.navigator.extensions;
13
14 import java.util.Comparator JavaDoc;
15
16 import org.eclipse.ui.navigator.INavigatorContentDescriptor;
17 import org.eclipse.ui.navigator.INavigatorContentExtension;
18
19 /**
20  * @since 3.2
21  *
22  */

23 public class ExtensionPriorityComparator implements Comparator JavaDoc {
24
25     /**
26      * The initialized singleton instance.
27      */

28     public static final ExtensionPriorityComparator INSTANCE = new ExtensionPriorityComparator(true);
29
30     /**
31      * The initialized singleton instance.
32      */

33     public static final ExtensionPriorityComparator DESCENDING = new ExtensionPriorityComparator(false);
34     
35     private final int sortAscending;
36     
37     public ExtensionPriorityComparator(boolean toSortAscending) {
38         sortAscending = toSortAscending ? 1 : -1;
39     }
40
41     /*
42      * (non-Javadoc)
43      *
44      * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
45      */

46     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
47
48         INavigatorContentDescriptor lvalue = null;
49         INavigatorContentDescriptor rvalue = null;
50
51         if (o1 instanceof INavigatorContentDescriptor) {
52             lvalue = (INavigatorContentDescriptor) o1;
53         } else if (o1 instanceof INavigatorContentExtension) {
54             lvalue = ((INavigatorContentExtension) o1).getDescriptor();
55         }
56
57         if (o2 instanceof INavigatorContentDescriptor) {
58             rvalue = (INavigatorContentDescriptor) o2;
59         } else if (o2 instanceof INavigatorContentExtension) {
60             rvalue = ((INavigatorContentExtension) o2).getDescriptor();
61         }
62
63         if (lvalue == null || rvalue == null) {
64             return -1 * sortAscending;
65         }
66
67         int c = lvalue.getPriority() - rvalue.getPriority();
68         if (c != 0) {
69             return c * sortAscending;
70         }
71         return lvalue.getId().compareTo(rvalue.getId()) * sortAscending;
72
73     }
74
75 }
76
Popular Tags