01: /*******************************************************************************
02: * Copyright (c) 2000, 2005 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.jdt.internal.ui.browsing;
11:
12: import java.util.Comparator;
13:
14: import org.eclipse.jdt.core.IJavaElement;
15:
16: public class JavaElementTypeComparator implements Comparator {
17:
18: /**
19: * Compares two Java element types. A type is considered to be
20: * greater if it may contain the other.
21: *
22: * @return an int less than 0 if object1 is less than object2,
23: * 0 if they are equal, and > 0 if object1 is greater
24: *
25: * @see Comparator#compare(Object, Object)
26: */
27: public int compare(Object o1, Object o2) {
28: if (!(o1 instanceof IJavaElement)
29: || !(o2 instanceof IJavaElement))
30: throw new ClassCastException();
31: return getIdForJavaElement((IJavaElement) o1)
32: - getIdForJavaElement((IJavaElement) o2);
33: }
34:
35: /**
36: * Compares two Java element types. A type is considered to be
37: * greater if it may contain the other.
38: *
39: * @return an int < 0 if object1 is less than object2,
40: * 0 if they are equal, and > 0 if object1 is greater
41: *
42: * @see Comparator#compare(Object, Object)
43: */
44: public int compare(Object o1, int elementType) {
45: if (!(o1 instanceof IJavaElement))
46: throw new ClassCastException();
47: return getIdForJavaElement((IJavaElement) o1)
48: - getIdForJavaElementType(elementType);
49: }
50:
51: int getIdForJavaElement(IJavaElement element) {
52: return getIdForJavaElementType(element.getElementType());
53: }
54:
55: int getIdForJavaElementType(int elementType) {
56: switch (elementType) {
57: case IJavaElement.JAVA_MODEL:
58: return 130;
59: case IJavaElement.JAVA_PROJECT:
60: return 120;
61: case IJavaElement.PACKAGE_FRAGMENT_ROOT:
62: return 110;
63: case IJavaElement.PACKAGE_FRAGMENT:
64: return 100;
65: case IJavaElement.COMPILATION_UNIT:
66: return 90;
67: case IJavaElement.CLASS_FILE:
68: return 80;
69: case IJavaElement.TYPE:
70: return 70;
71: case IJavaElement.FIELD:
72: return 60;
73: case IJavaElement.METHOD:
74: return 50;
75: case IJavaElement.INITIALIZER:
76: return 40;
77: case IJavaElement.PACKAGE_DECLARATION:
78: return 30;
79: case IJavaElement.IMPORT_CONTAINER:
80: return 20;
81: case IJavaElement.IMPORT_DECLARATION:
82: return 10;
83: default:
84: return 1;
85: }
86: }
87: }
|