1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
13 import java.util.HashMap;
15 import net.sourceforge.phpdt.core.IBuffer;
16 import net.sourceforge.phpdt.core.ICompilationUnit;
17 import net.sourceforge.phpdt.core.IJavaElement;
18 import net.sourceforge.phpdt.core.IJavaProject;
19 import net.sourceforge.phpdt.core.IMember;
20 import net.sourceforge.phpdt.core.IOpenable;
21 import net.sourceforge.phpdt.core.ISourceManipulation;
22 import net.sourceforge.phpdt.core.ISourceRange;
23 import net.sourceforge.phpdt.core.ISourceReference;
24 import net.sourceforge.phpdt.core.JavaModelException;
25 import net.sourceforge.phpdt.core.WorkingCopyOwner;
26 import net.sourceforge.phpdt.internal.core.util.MementoTokenizer;
28 import org.eclipse.core.resources.IResource;
29 import org.eclipse.core.runtime.IPath;
30 import org.eclipse.core.runtime.IProgressMonitor;
33 * Abstract class for Java elements which implement ISourceReference.
35 /* package */abstract class SourceRefElement extends JavaElement implements
37 protected SourceRefElement(JavaElement parent, String name) {
42 * This element is being closed. Do any necessary cleanup.
44 protected void closing(Object info) throws JavaModelException {
45 // Do any necessary cleanup
49 * Returns a new element info for this element.
51 protected Object createElementInfo() {
52 return null; // not used for source ref elements
56 * @see ISourceManipulation
58 // public void copy(IJavaElement container, IJavaElement sibling, String
59 // rename, boolean force, IProgressMonitor monitor) throws
60 // JavaModelException {
61 // if (container == null) {
63 // IllegalArgumentException(ProjectPrefUtil.bind("operation.nullContainer"));
66 // IJavaElement[] elements= new IJavaElement[] {this};
67 // IJavaElement[] containers= new IJavaElement[] {container};
68 // IJavaElement[] siblings= null;
69 // if (sibling != null) {
70 // siblings= new IJavaElement[] {sibling};
72 // String[] renamings= null;
73 // if (rename != null) {
74 // renamings= new String[] {rename};
76 // getJavaModel().copy(elements, containers, siblings, renamings, force,
80 * @see ISourceManipulation
82 // public void delete(boolean force, IProgressMonitor monitor) throws
83 // JavaModelException {
84 // IJavaElement[] elements = new IJavaElement[] {this};
85 // getJavaModel().delete(elements, force, monitor);
88 * @see JavaElement#generateInfos
90 protected void generateInfos(Object info, HashMap newElements,
91 IProgressMonitor pm) throws JavaModelException {
92 Openable openableParent = (Openable) getOpenableParent();
93 if (openableParent == null)
96 JavaElementInfo openableParentInfo = (JavaElementInfo) JavaModelManager
97 .getJavaModelManager().getInfo(openableParent);
98 if (openableParentInfo == null) {
99 openableParent.generateInfos(openableParent.createElementInfo(),
107 public ICompilationUnit getCompilationUnit() {
108 return ((JavaElement) getParent()).getCompilationUnit();
112 * Elements within compilation units and class files have no corresponding
117 public IResource getCorrespondingResource() throws JavaModelException {
119 throw newNotPresentException();
126 public IJavaElement getHandleFromMemento(String token,
127 MementoTokenizer memento, WorkingCopyOwner workingCopyOwner) {
128 switch (token.charAt(0)) {
130 return getHandleUpdatingCountFromMemento(memento, workingCopyOwner);
136 * Return the first instance of IOpenable in the hierarchy of this type
137 * (going up the hierarchy from this type);
139 public IOpenable getOpenableParent() {
140 IJavaElement current = getParent();
141 while (current != null) {
142 if (current instanceof IOpenable) {
143 return (IOpenable) current;
145 current = current.getParent();
153 public IPath getPath() {
154 return this.getParent().getPath();
160 public IResource getResource() {
161 return this.getParent().getResource();
165 * @see ISourceReference
167 public String getSource() throws JavaModelException {
168 IOpenable openable = getOpenableParent();
169 IBuffer buffer = openable.getBuffer();
170 if (buffer == null) {
173 ISourceRange range = getSourceRange();
174 int offset = range.getOffset();
175 int length = range.getLength();
176 if (offset == -1 || length == 0) {
180 return buffer.getText(offset, length);
181 // jsurfer insert start
182 } catch (ArrayIndexOutOfBoundsException e) {
186 // jsurfer insert end
190 * @see ISourceReference
192 public ISourceRange getSourceRange() throws JavaModelException {
193 SourceRefElementInfo info = (SourceRefElementInfo) getElementInfo();
194 return info.getSourceRange();
200 public IResource getUnderlyingResource() throws JavaModelException {
202 throw newNotPresentException();
203 return getParent().getUnderlyingResource();
207 * @see ISourceManipulation
209 // public void move(IJavaElement container, IJavaElement sibling, String
210 // rename, boolean force, IProgressMonitor monitor) throws
211 // JavaModelException {
212 // if (container == null) {
214 // IllegalArgumentException(ProjectPrefUtil.bind("operation.nullContainer"));
217 // IJavaElement[] elements= new IJavaElement[] {this};
218 // IJavaElement[] containers= new IJavaElement[] {container};
219 // IJavaElement[] siblings= null;
220 // if (sibling != null) {
221 // siblings= new IJavaElement[] {sibling};
223 // String[] renamings= null;
224 // if (rename != null) {
225 // renamings= new String[] {rename};
227 // getJavaModel().move(elements, containers, siblings, renamings, force,
231 * @see ISourceManipulation
233 // public void rename(String name, boolean force, IProgressMonitor monitor)
234 // throws JavaModelException {
235 // if (name == null) {
237 // IllegalArgumentException(ProjectPrefUtil.bind("element.nullName"));
240 // IJavaElement[] elements= new IJavaElement[] {this};
241 // IJavaElement[] dests= new IJavaElement[] {this.getParent()};
242 // String[] renamings= new String[] {name};
243 // getJavaModel().rename(elements, dests, renamings, force, monitor);
246 * @see JavaElement#rootedAt(IJavaProject)
248 public IJavaElement rootedAt(IJavaProject project) {