01: /*******************************************************************************
02: * Copyright (c) 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: ******************************************************************************/package org.eclipse.ui.internal.navigator.extensions;
11:
12: import java.util.Comparator;
13:
14: import org.eclipse.ui.navigator.INavigatorContentDescriptor;
15: import org.eclipse.ui.navigator.INavigatorContentExtension;
16:
17: /**
18: * @since 3.2
19: *
20: */
21: public class ExtensionPriorityComparator implements Comparator {
22:
23: /**
24: * The initialized singleton instance.
25: */
26: public static final ExtensionPriorityComparator INSTANCE = new ExtensionPriorityComparator(
27: true);
28:
29: /**
30: * The initialized singleton instance.
31: */
32: public static final ExtensionPriorityComparator DESCENDING = new ExtensionPriorityComparator(
33: 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 o1, Object 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: }
|