KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > browsing > JavaElementTypeComparator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.jdt.internal.ui.browsing;
12
13 import java.util.Comparator JavaDoc;
14
15 import org.eclipse.jdt.core.IJavaElement;
16
17 public class JavaElementTypeComparator implements Comparator JavaDoc {
18
19
20     /**
21      * Compares two Java element types. A type is considered to be
22      * greater if it may contain the other.
23      *
24      * @return an int less than 0 if object1 is less than object2,
25      * 0 if they are equal, and > 0 if object1 is greater
26      *
27      * @see Comparator#compare(Object, Object)
28      */

29     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
30         if (!(o1 instanceof IJavaElement) || !(o2 instanceof IJavaElement))
31             throw new ClassCastException JavaDoc();
32         return getIdForJavaElement((IJavaElement)o1) - 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 JavaDoc o1, int elementType) {
45         if (!(o1 instanceof IJavaElement))
46             throw new ClassCastException JavaDoc();
47         return getIdForJavaElement((IJavaElement)o1) - getIdForJavaElementType(elementType);
48     }
49
50     int getIdForJavaElement(IJavaElement element) {
51         return getIdForJavaElementType(element.getElementType());
52     }
53
54     int getIdForJavaElementType(int elementType) {
55         switch (elementType) {
56             case IJavaElement.JAVA_MODEL:
57                 return 130;
58             case IJavaElement.JAVA_PROJECT:
59                 return 120;
60             case IJavaElement.PACKAGE_FRAGMENT_ROOT:
61                 return 110;
62             case IJavaElement.PACKAGE_FRAGMENT:
63                 return 100;
64             case IJavaElement.COMPILATION_UNIT:
65                 return 90;
66             case IJavaElement.CLASS_FILE:
67                 return 80;
68             case IJavaElement.TYPE:
69                 return 70;
70             case IJavaElement.FIELD:
71                 return 60;
72             case IJavaElement.METHOD:
73                 return 50;
74             case IJavaElement.INITIALIZER:
75                 return 40;
76             case IJavaElement.PACKAGE_DECLARATION:
77                 return 30;
78             case IJavaElement.IMPORT_CONTAINER:
79                 return 20;
80             case IJavaElement.IMPORT_DECLARATION:
81                 return 10;
82             default :
83                 return 1;
84         }
85     }
86 }
87
Popular Tags