KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > SourceRefElement


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.core;
12
13 import java.util.HashMap JavaDoc;
14
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.jdt.core.*;
19 import org.eclipse.jdt.core.dom.ASTNode;
20 import org.eclipse.jdt.core.dom.CompilationUnit;
21 import org.eclipse.jdt.internal.core.util.DOMFinder;
22 import org.eclipse.jdt.internal.core.util.MementoTokenizer;
23 import org.eclipse.jdt.internal.core.util.Messages;
24
25 /**
26  * Abstract class for Java elements which implement ISourceReference.
27  */

28 public abstract class SourceRefElement extends JavaElement implements ISourceReference {
29     /*
30      * A count to uniquely identify this element in the case
31      * that a duplicate named element exists. For example, if
32      * there are two fields in a compilation unit with the
33      * same name, the occurrence count is used to distinguish
34      * them. The occurrence count starts at 1 (thus the first
35      * occurrence is occurrence 1, not occurrence 0).
36      */

37     public int occurrenceCount = 1;
38
39 protected SourceRefElement(JavaElement parent) {
40     super(parent);
41 }
42 /**
43  * This element is being closed. Do any necessary cleanup.
44  */

45 protected void closing(Object JavaDoc info) throws JavaModelException {
46     // Do any necessary cleanup
47
}
48 /**
49  * Returns a new element info for this element.
50  */

51 protected Object JavaDoc createElementInfo() {
52     return null; // not used for source ref elements
53
}
54 /**
55  * @see ISourceManipulation
56  */

57 public void copy(IJavaElement container, IJavaElement sibling, String JavaDoc rename, boolean force, IProgressMonitor monitor) throws JavaModelException {
58     if (container == null) {
59         throw new IllegalArgumentException JavaDoc(Messages.operation_nullContainer);
60     }
61     IJavaElement[] elements= new IJavaElement[] {this};
62     IJavaElement[] containers= new IJavaElement[] {container};
63     IJavaElement[] siblings= null;
64     if (sibling != null) {
65         siblings= new IJavaElement[] {sibling};
66     }
67     String JavaDoc[] renamings= null;
68     if (rename != null) {
69         renamings= new String JavaDoc[] {rename};
70     }
71     getJavaModel().copy(elements, containers, siblings, renamings, force, monitor);
72 }
73 /**
74  * @see ISourceManipulation
75  */

76 public void delete(boolean force, IProgressMonitor monitor) throws JavaModelException {
77     IJavaElement[] elements = new IJavaElement[] {this};
78     getJavaModel().delete(elements, force, monitor);
79 }
80 public boolean equals(Object JavaDoc o) {
81     if (!(o instanceof SourceRefElement)) return false;
82     return this.occurrenceCount == ((SourceRefElement)o).occurrenceCount &&
83             super.equals(o);
84 }
85 /**
86  * Returns the <code>ASTNode</code> that corresponds to this <code>JavaElement</code>
87  * or <code>null</code> if there is no corresponding node.
88  */

89 public ASTNode findNode(CompilationUnit ast) {
90     DOMFinder finder = new DOMFinder(ast, this, false);
91     try {
92         return finder.search();
93     } catch (JavaModelException e) {
94         // receiver doesn't exist
95
return null;
96     }
97 }
98 /*
99  * @see JavaElement#generateInfos
100  */

101 protected void generateInfos(Object JavaDoc info, HashMap JavaDoc newElements, IProgressMonitor pm) throws JavaModelException {
102     Openable openableParent = (Openable)getOpenableParent();
103     if (openableParent == null) return;
104
105     JavaElementInfo openableParentInfo = (JavaElementInfo) JavaModelManager.getJavaModelManager().getInfo(openableParent);
106     if (openableParentInfo == null) {
107         openableParent.generateInfos(openableParent.createElementInfo(), newElements, pm);
108     }
109 }
110 /**
111  * @see IMember
112  */

113 public ICompilationUnit getCompilationUnit() {
114     return (ICompilationUnit) getAncestor(COMPILATION_UNIT);
115 }
116 /**
117  * Elements within compilation units and class files have no
118  * corresponding resource.
119  *
120  * @see IJavaElement
121  */

122 public IResource getCorrespondingResource() throws JavaModelException {
123     if (!exists()) throw newNotPresentException();
124     return null;
125 }
126 /*
127  * @see JavaElement
128  */

129 public IJavaElement getHandleFromMemento(String JavaDoc token, MementoTokenizer memento, WorkingCopyOwner workingCopyOwner) {
130     switch (token.charAt(0)) {
131         case JEM_COUNT:
132             return getHandleUpdatingCountFromMemento(memento, workingCopyOwner);
133     }
134     return this;
135 }
136 protected void getHandleMemento(StringBuffer JavaDoc buff) {
137     super.getHandleMemento(buff);
138     if (this.occurrenceCount > 1) {
139         buff.append(JEM_COUNT);
140         buff.append(this.occurrenceCount);
141     }
142 }
143 /*
144  * Update the occurence count of the receiver and creates a Java element handle from the given memento.
145  * The given working copy owner is used only for compilation unit handles.
146  */

147 public IJavaElement getHandleUpdatingCountFromMemento(MementoTokenizer memento, WorkingCopyOwner owner) {
148     if (!memento.hasMoreTokens()) return this;
149     this.occurrenceCount = Integer.parseInt(memento.nextToken());
150     if (!memento.hasMoreTokens()) return this;
151     String JavaDoc token = memento.nextToken();
152     return getHandleFromMemento(token, memento, owner);
153 }
154 /*
155  * @see IMember#getOccurrenceCount()
156  */

157 public int getOccurrenceCount() {
158     return this.occurrenceCount;
159 }
160 /**
161  * Return the first instance of IOpenable in the hierarchy of this
162  * type (going up the hierarchy from this type);
163  */

164 public IOpenable getOpenableParent() {
165     IJavaElement current = getParent();
166     while (current != null){
167         if (current instanceof IOpenable){
168             return (IOpenable) current;
169         }
170         current = current.getParent();
171     }
172     return null;
173 }
174 /*
175  * @see IJavaElement
176  */

177 public IPath getPath() {
178     return this.getParent().getPath();
179 }
180 /*
181  * @see IJavaElement
182  */

183 public IResource getResource() {
184     return this.getParent().getResource();
185 }
186 /**
187  * @see ISourceReference
188  */

189 public String JavaDoc getSource() throws JavaModelException {
190     IOpenable openable = getOpenableParent();
191     IBuffer buffer = openable.getBuffer();
192     if (buffer == null) {
193         return null;
194     }
195     ISourceRange range = getSourceRange();
196     int offset = range.getOffset();
197     int length = range.getLength();
198     if (offset == -1 || length == 0 ) {
199         return null;
200     }
201     try {
202         return buffer.getText(offset, length);
203     } catch(RuntimeException JavaDoc e) {
204         return null;
205     }
206 }
207 /**
208  * @see ISourceReference
209  */

210 public ISourceRange getSourceRange() throws JavaModelException {
211     SourceRefElementInfo info = (SourceRefElementInfo) getElementInfo();
212     return info.getSourceRange();
213 }
214 /**
215  * @see IJavaElement
216  */

217 public IResource getUnderlyingResource() throws JavaModelException {
218     if (!exists()) throw newNotPresentException();
219     return getParent().getUnderlyingResource();
220 }
221 /**
222  * @see IParent
223  */

224 public boolean hasChildren() throws JavaModelException {
225     return getChildren().length > 0;
226 }
227 /**
228  * @see IJavaElement
229  */

230 public boolean isStructureKnown() throws JavaModelException {
231     // structure is always known inside an openable
232
return true;
233 }
234 /**
235  * @see ISourceManipulation
236  */

237 public void move(IJavaElement container, IJavaElement sibling, String JavaDoc rename, boolean force, IProgressMonitor monitor) throws JavaModelException {
238     if (container == null) {
239         throw new IllegalArgumentException JavaDoc(Messages.operation_nullContainer);
240     }
241     IJavaElement[] elements= new IJavaElement[] {this};
242     IJavaElement[] containers= new IJavaElement[] {container};
243     IJavaElement[] siblings= null;
244     if (sibling != null) {
245         siblings= new IJavaElement[] {sibling};
246     }
247     String JavaDoc[] renamings= null;
248     if (rename != null) {
249         renamings= new String JavaDoc[] {rename};
250     }
251     getJavaModel().move(elements, containers, siblings, renamings, force, monitor);
252 }
253 /**
254  * @see ISourceManipulation
255  */

256 public void rename(String JavaDoc newName, boolean force, IProgressMonitor monitor) throws JavaModelException {
257     if (newName == null) {
258         throw new IllegalArgumentException JavaDoc(Messages.element_nullName);
259     }
260     IJavaElement[] elements= new IJavaElement[] {this};
261     IJavaElement[] dests= new IJavaElement[] {this.getParent()};
262     String JavaDoc[] renamings= new String JavaDoc[] {newName};
263     getJavaModel().rename(elements, dests, renamings, force, monitor);
264 }
265 protected void toStringName(StringBuffer JavaDoc buffer) {
266     super.toStringName(buffer);
267     if (this.occurrenceCount > 1) {
268         buffer.append("#"); //$NON-NLS-1$
269
buffer.append(this.occurrenceCount);
270     }
271 }
272 }
273
Popular Tags