KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > compare > JavaNode


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.compare;
12
13 import org.eclipse.swt.graphics.Image;
14
15 import org.eclipse.jface.resource.ImageDescriptor;
16
17 import org.eclipse.jface.text.IDocument;
18
19 import org.eclipse.compare.ITypedElement;
20 import org.eclipse.compare.structuremergeviewer.DocumentRangeNode;
21
22 import org.eclipse.jdt.core.IJavaElement;
23
24 import org.eclipse.jdt.internal.ui.JavaPlugin;
25
26 /**
27  * Comparable Java elements are represented as JavaNodes.
28  * Extends the DocumentRangeNode with method signature information.
29  */

30 class JavaNode extends DocumentRangeNode implements ITypedElement {
31     
32     public static final int CU= 0;
33     public static final int PACKAGE= 1;
34     public static final int IMPORT_CONTAINER= 2;
35     public static final int IMPORT= 3;
36     public static final int INTERFACE= 4;
37     public static final int CLASS= 5;
38     public static final int ENUM= 6;
39     public static final int ANNOTATION= 7;
40     public static final int FIELD= 8;
41     public static final int INIT= 9;
42     public static final int CONSTRUCTOR= 10;
43     public static final int METHOD= 11;
44
45     private int fInitializerCount= 1;
46
47     /**
48      * Creates a JavaNode under the given parent.
49      * @param parent the parent node
50      * @param type the Java elements type. Legal values are from the range CU to METHOD of this class.
51      * @param name the name of the Java element
52      * @param start the starting position of the java element in the underlying document
53      * @param length the number of characters of the java element in the underlying document
54      */

55     public JavaNode(JavaNode parent, int type, String JavaDoc name, int start, int length) {
56         super(parent, type, JavaCompareUtilities.buildID(type, name), parent.getDocument(), start, length);
57         parent.addChild(this);
58     }
59     
60     /**
61      * Creates a JavaNode for a CU. It represents the root of a
62      * JavaNode tree, so its parent is null.
63      * @param document the document which contains the Java element
64      */

65     public JavaNode(IDocument document) {
66         super(CU, JavaCompareUtilities.buildID(CU, "root"), document, 0, document.getLength()); //$NON-NLS-1$
67
}
68
69     public String JavaDoc getInitializerCount() {
70         return Integer.toString(fInitializerCount++);
71     }
72     
73     /**
74      * Extracts the method name from the signature.
75      * Used for smart matching.
76      */

77     public String JavaDoc extractMethodName() {
78         String JavaDoc id= getId();
79         int pos= id.indexOf('(');
80         if (pos > 0)
81             return id.substring(1, pos);
82         return id.substring(1);
83     }
84     
85     /**
86      * Extracts the method's arguments name the signature.
87      * Used for smart matching.
88      */

89     public String JavaDoc extractArgumentList() {
90         String JavaDoc id= getId();
91         int pos= id.indexOf('(');
92         if (pos >= 0)
93             return id.substring(pos+1);
94         return id.substring(1);
95     }
96     
97     /**
98      * Returns a name which is presented in the UI.
99      * @see ITypedElement#getName()
100      */

101     public String JavaDoc getName() {
102         
103         switch (getTypeCode()) {
104         case INIT:
105             return CompareMessages.JavaNode_initializer;
106         case IMPORT_CONTAINER:
107             return CompareMessages.JavaNode_importDeclarations;
108         case CU:
109             return CompareMessages.JavaNode_compilationUnit;
110         case PACKAGE:
111             return CompareMessages.JavaNode_packageDeclaration;
112         }
113         return getId().substring(1); // we strip away the type character
114
}
115     
116     /*
117      * @see ITypedElement#getType()
118      */

119     public String JavaDoc getType() {
120         return "java2"; //$NON-NLS-1$
121
}
122         
123     /**
124      * Returns a shared image for this Java element.
125      *
126      * see ITypedInput.getImage
127      */

128     public Image getImage() {
129                         
130         ImageDescriptor id= null;
131                     
132         switch (getTypeCode()) {
133         case CU:
134             id= JavaCompareUtilities.getImageDescriptor(IJavaElement.COMPILATION_UNIT);
135             break;
136         case PACKAGE:
137             id= JavaCompareUtilities.getImageDescriptor(IJavaElement.PACKAGE_DECLARATION);
138             break;
139         case IMPORT:
140             id= JavaCompareUtilities.getImageDescriptor(IJavaElement.IMPORT_DECLARATION);
141             break;
142         case IMPORT_CONTAINER:
143             id= JavaCompareUtilities.getImageDescriptor(IJavaElement.IMPORT_CONTAINER);
144             break;
145         case CLASS:
146             id= JavaCompareUtilities.getTypeImageDescriptor(true);
147             break;
148         case INTERFACE:
149             id= JavaCompareUtilities.getTypeImageDescriptor(false);
150             break;
151         case INIT:
152             id= JavaCompareUtilities.getImageDescriptor(IJavaElement.INITIALIZER);
153             break;
154         case CONSTRUCTOR:
155         case METHOD:
156             id= JavaCompareUtilities.getImageDescriptor(IJavaElement.METHOD);
157             break;
158         case FIELD:
159             id= JavaCompareUtilities.getImageDescriptor(IJavaElement.FIELD);
160             break;
161         case ENUM:
162             id= JavaCompareUtilities.getEnumImageDescriptor();
163             break;
164         case ANNOTATION:
165             id= JavaCompareUtilities.getAnnotationImageDescriptor();
166             break;
167         }
168         return JavaPlugin.getImageDescriptorRegistry().get(id);
169     }
170     
171     /*
172      * @see java.lang.Object#toString()
173      */

174     public String JavaDoc toString() {
175         return getType() + ": " + getName() //$NON-NLS-1$
176
+ "[" + getRange().offset + "+" + getRange().length + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
177
}
178 }
179
180
Popular Tags