001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.compare;
011:
012: import org.eclipse.swt.graphics.Image;
013:
014: import org.eclipse.jface.resource.ImageDescriptor;
015:
016: import org.eclipse.jface.text.IDocument;
017:
018: import org.eclipse.compare.ITypedElement;
019: import org.eclipse.compare.structuremergeviewer.DocumentRangeNode;
020:
021: import org.eclipse.jdt.core.IJavaElement;
022:
023: import org.eclipse.jdt.internal.ui.JavaPlugin;
024:
025: /**
026: * Comparable Java elements are represented as JavaNodes.
027: * Extends the DocumentRangeNode with method signature information.
028: */
029: class JavaNode extends DocumentRangeNode implements ITypedElement {
030:
031: public static final int CU = 0;
032: public static final int PACKAGE = 1;
033: public static final int IMPORT_CONTAINER = 2;
034: public static final int IMPORT = 3;
035: public static final int INTERFACE = 4;
036: public static final int CLASS = 5;
037: public static final int ENUM = 6;
038: public static final int ANNOTATION = 7;
039: public static final int FIELD = 8;
040: public static final int INIT = 9;
041: public static final int CONSTRUCTOR = 10;
042: public static final int METHOD = 11;
043:
044: private int fInitializerCount = 1;
045:
046: /**
047: * Creates a JavaNode under the given parent.
048: * @param parent the parent node
049: * @param type the Java elements type. Legal values are from the range CU to METHOD of this class.
050: * @param name the name of the Java element
051: * @param start the starting position of the java element in the underlying document
052: * @param length the number of characters of the java element in the underlying document
053: */
054: public JavaNode(JavaNode parent, int type, String name, int start,
055: int length) {
056: super (parent, type, JavaCompareUtilities.buildID(type, name),
057: parent.getDocument(), start, length);
058: parent.addChild(this );
059: }
060:
061: /**
062: * Creates a JavaNode for a CU. It represents the root of a
063: * JavaNode tree, so its parent is null.
064: * @param document the document which contains the Java element
065: */
066: public JavaNode(IDocument document) {
067: super (
068: CU,
069: JavaCompareUtilities.buildID(CU, "root"), document, 0, document.getLength()); //$NON-NLS-1$
070: }
071:
072: public String getInitializerCount() {
073: return Integer.toString(fInitializerCount++);
074: }
075:
076: /**
077: * Extracts the method name from the signature.
078: * Used for smart matching.
079: */
080: public String extractMethodName() {
081: String id = getId();
082: int pos = id.indexOf('(');
083: if (pos > 0)
084: return id.substring(1, pos);
085: return id.substring(1);
086: }
087:
088: /**
089: * Extracts the method's arguments name the signature.
090: * Used for smart matching.
091: */
092: public String extractArgumentList() {
093: String id = getId();
094: int pos = id.indexOf('(');
095: if (pos >= 0)
096: return id.substring(pos + 1);
097: return id.substring(1);
098: }
099:
100: /**
101: * Returns a name which is presented in the UI.
102: * @see ITypedElement#getName()
103: */
104: public String getName() {
105:
106: switch (getTypeCode()) {
107: case INIT:
108: return CompareMessages.JavaNode_initializer;
109: case IMPORT_CONTAINER:
110: return CompareMessages.JavaNode_importDeclarations;
111: case CU:
112: return CompareMessages.JavaNode_compilationUnit;
113: case PACKAGE:
114: return CompareMessages.JavaNode_packageDeclaration;
115: }
116: return getId().substring(1); // we strip away the type character
117: }
118:
119: /*
120: * @see ITypedElement#getType()
121: */
122: public String getType() {
123: return "java2"; //$NON-NLS-1$
124: }
125:
126: /**
127: * Returns a shared image for this Java element.
128: *
129: * see ITypedInput.getImage
130: */
131: public Image getImage() {
132:
133: ImageDescriptor id = null;
134:
135: switch (getTypeCode()) {
136: case CU:
137: id = JavaCompareUtilities
138: .getImageDescriptor(IJavaElement.COMPILATION_UNIT);
139: break;
140: case PACKAGE:
141: id = JavaCompareUtilities
142: .getImageDescriptor(IJavaElement.PACKAGE_DECLARATION);
143: break;
144: case IMPORT:
145: id = JavaCompareUtilities
146: .getImageDescriptor(IJavaElement.IMPORT_DECLARATION);
147: break;
148: case IMPORT_CONTAINER:
149: id = JavaCompareUtilities
150: .getImageDescriptor(IJavaElement.IMPORT_CONTAINER);
151: break;
152: case CLASS:
153: id = JavaCompareUtilities.getTypeImageDescriptor(true);
154: break;
155: case INTERFACE:
156: id = JavaCompareUtilities.getTypeImageDescriptor(false);
157: break;
158: case INIT:
159: id = JavaCompareUtilities
160: .getImageDescriptor(IJavaElement.INITIALIZER);
161: break;
162: case CONSTRUCTOR:
163: case METHOD:
164: id = JavaCompareUtilities
165: .getImageDescriptor(IJavaElement.METHOD);
166: break;
167: case FIELD:
168: id = JavaCompareUtilities
169: .getImageDescriptor(IJavaElement.FIELD);
170: break;
171: case ENUM:
172: id = JavaCompareUtilities.getEnumImageDescriptor();
173: break;
174: case ANNOTATION:
175: id = JavaCompareUtilities.getAnnotationImageDescriptor();
176: break;
177: }
178: return JavaPlugin.getImageDescriptorRegistry().get(id);
179: }
180:
181: /*
182: * @see java.lang.Object#toString()
183: */
184: public String toString() {
185: return getType()
186: + ": " + getName() //$NON-NLS-1$
187: + "[" + getRange().offset + "+" + getRange().length + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
188: }
189: }
|