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.IOpenable;
20 import net.sourceforge.phpdt.core.ISourceRange;
21 import net.sourceforge.phpdt.core.ISourceReference;
22 import net.sourceforge.phpdt.core.JavaModelException;
24 import org.eclipse.core.resources.IResource;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import net.sourceforge.phpdt.internal.core.JavaElementInfo;
28 import net.sourceforge.phpdt.internal.core.JavaModelManager;
29 import net.sourceforge.phpdt.internal.core.Openable;
31 import net.sourceforge.phpdt.internal.core.JavaElement;
35 * Abstract class for Java elements which implement ISourceReference.
37 /* package */ abstract class SourceRefElement extends JavaElement implements ISourceReference {
38 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
48 * Returns a new element info for this element.
50 protected Object createElementInfo() {
51 return null; // not used for source ref elements
54 * @see ISourceManipulation
56 //public void copy(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException {
57 // if (container == null) {
58 // throw new IllegalArgumentException(Util.bind("operation.nullContainer")); //$NON-NLS-1$
60 // IJavaElement[] elements= new IJavaElement[] {this};
61 // IJavaElement[] containers= new IJavaElement[] {container};
62 // IJavaElement[] siblings= null;
63 // if (sibling != null) {
64 // siblings= new IJavaElement[] {sibling};
66 // String[] renamings= null;
67 // if (rename != null) {
68 // renamings= new String[] {rename};
70 // getJavaModel().copy(elements, containers, siblings, renamings, force, monitor);
73 * @see ISourceManipulation
75 //public void delete(boolean force, IProgressMonitor monitor) throws JavaModelException {
76 // IJavaElement[] elements = new IJavaElement[] {this};
77 // getJavaModel().delete(elements, force, monitor);
80 * @see JavaElement#generateInfos
82 protected void generateInfos(Object info, HashMap newElements, IProgressMonitor pm) throws JavaModelException {
83 Openable openableParent = (Openable)getOpenableParent();
84 if (openableParent == null) return;
86 JavaElementInfo openableParentInfo = (JavaElementInfo) JavaModelManager.getJavaModelManager().getInfo(openableParent);
87 if (openableParentInfo == null) {
88 openableParent.generateInfos(openableParent.createElementInfo(), newElements, pm);
94 public ICompilationUnit getCompilationUnit() {
95 return ((JavaElement)getParent()).getCompilationUnit();
98 * Elements within compilation units and class files have no
99 * corresponding resource.
103 public IResource getCorrespondingResource() throws JavaModelException {
104 if (!exists()) throw newNotPresentException();
108 * Return the first instance of IOpenable in the hierarchy of this
109 * type (going up the hierarchy from this type);
111 public IOpenable getOpenableParent() {
112 IJavaElement current = getParent();
113 while (current != null){
114 if (current instanceof IOpenable){
115 return (IOpenable) current;
117 current = current.getParent();
124 public IPath getPath() {
125 return this.getParent().getPath();
130 public IResource getResource() {
131 return this.getParent().getResource();
134 * @see ISourceReference
136 public String getSource() throws JavaModelException {
137 IOpenable openable = getOpenableParent();
138 IBuffer buffer = openable.getBuffer();
139 if (buffer == null) {
142 ISourceRange range = getSourceRange();
143 int offset = range.getOffset();
144 int length = range.getLength();
145 if (offset == -1 || length == 0 ) {
149 return buffer.getText(offset, length);
150 // jsurfer insert start
151 } catch (ArrayIndexOutOfBoundsException e) {
155 // jsurfer insert end
158 * @see ISourceReference
160 public ISourceRange getSourceRange() throws JavaModelException {
161 SourceRefElementInfo info = (SourceRefElementInfo) getElementInfo();
162 return info.getSourceRange();
167 public IResource getUnderlyingResource() throws JavaModelException {
168 if (!exists()) throw newNotPresentException();
169 return getParent().getUnderlyingResource();
172 * @see ISourceManipulation
174 //public void move(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException {
175 // if (container == null) {
176 // throw new IllegalArgumentException(Util.bind("operation.nullContainer")); //$NON-NLS-1$
178 // IJavaElement[] elements= new IJavaElement[] {this};
179 // IJavaElement[] containers= new IJavaElement[] {container};
180 // IJavaElement[] siblings= null;
181 // if (sibling != null) {
182 // siblings= new IJavaElement[] {sibling};
184 // String[] renamings= null;
185 // if (rename != null) {
186 // renamings= new String[] {rename};
188 // getJavaModel().move(elements, containers, siblings, renamings, force, monitor);
191 * @see ISourceManipulation
193 //public void rename(String name, boolean force, IProgressMonitor monitor) throws JavaModelException {
194 // if (name == null) {
195 // throw new IllegalArgumentException(Util.bind("element.nullName")); //$NON-NLS-1$
197 // IJavaElement[] elements= new IJavaElement[] {this};
198 // IJavaElement[] dests= new IJavaElement[] {this.getParent()};
199 // String[] renamings= new String[] {name};
200 // getJavaModel().rename(elements, dests, renamings, force, monitor);
203 * @see JavaElement#rootedAt(IJavaProject)
205 public IJavaElement rootedAt(IJavaProject project) {