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;
26 import org.eclipse.core.resources.IResource;
27 import org.eclipse.core.runtime.IPath;
28 import org.eclipse.core.runtime.IProgressMonitor;
32 * Abstract class for Java elements which implement ISourceReference.
34 /* package */ abstract class SourceRefElement extends JavaElement implements ISourceReference {
35 protected SourceRefElement(JavaElement parent, String name) {
39 * This element is being closed. Do any necessary cleanup.
41 protected void closing(Object info) throws JavaModelException {
42 // Do any necessary cleanup
45 * Returns a new element info for this element.
47 protected Object createElementInfo() {
48 return null; // not used for source ref elements
51 * @see ISourceManipulation
53 //public void copy(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException {
54 // if (container == null) {
55 // throw new IllegalArgumentException(ProjectPrefUtil.bind("operation.nullContainer")); //$NON-NLS-1$
57 // IJavaElement[] elements= new IJavaElement[] {this};
58 // IJavaElement[] containers= new IJavaElement[] {container};
59 // IJavaElement[] siblings= null;
60 // if (sibling != null) {
61 // siblings= new IJavaElement[] {sibling};
63 // String[] renamings= null;
64 // if (rename != null) {
65 // renamings= new String[] {rename};
67 // getJavaModel().copy(elements, containers, siblings, renamings, force, monitor);
70 * @see ISourceManipulation
72 //public void delete(boolean force, IProgressMonitor monitor) throws JavaModelException {
73 // IJavaElement[] elements = new IJavaElement[] {this};
74 // getJavaModel().delete(elements, force, monitor);
77 * @see JavaElement#generateInfos
79 protected void generateInfos(Object info, HashMap newElements, IProgressMonitor pm) throws JavaModelException {
80 Openable openableParent = (Openable)getOpenableParent();
81 if (openableParent == null) return;
83 JavaElementInfo openableParentInfo = (JavaElementInfo) JavaModelManager.getJavaModelManager().getInfo(openableParent);
84 if (openableParentInfo == null) {
85 openableParent.generateInfos(openableParent.createElementInfo(), newElements, pm);
91 public ICompilationUnit getCompilationUnit() {
92 return ((JavaElement)getParent()).getCompilationUnit();
95 * Elements within compilation units and class files have no
96 * corresponding resource.
100 public IResource getCorrespondingResource() throws JavaModelException {
101 if (!exists()) throw newNotPresentException();
105 * Return the first instance of IOpenable in the hierarchy of this
106 * type (going up the hierarchy from this type);
108 public IOpenable getOpenableParent() {
109 IJavaElement current = getParent();
110 while (current != null){
111 if (current instanceof IOpenable){
112 return (IOpenable) current;
114 current = current.getParent();
121 public IPath getPath() {
122 return this.getParent().getPath();
127 public IResource getResource() {
128 return this.getParent().getResource();
131 * @see ISourceReference
133 public String getSource() throws JavaModelException {
134 IOpenable openable = getOpenableParent();
135 IBuffer buffer = openable.getBuffer();
136 if (buffer == null) {
139 ISourceRange range = getSourceRange();
140 int offset = range.getOffset();
141 int length = range.getLength();
142 if (offset == -1 || length == 0 ) {
146 return buffer.getText(offset, length);
147 // jsurfer insert start
148 } catch (ArrayIndexOutOfBoundsException e) {
152 // jsurfer insert end
155 * @see ISourceReference
157 public ISourceRange getSourceRange() throws JavaModelException {
158 SourceRefElementInfo info = (SourceRefElementInfo) getElementInfo();
159 return info.getSourceRange();
164 public IResource getUnderlyingResource() throws JavaModelException {
165 if (!exists()) throw newNotPresentException();
166 return getParent().getUnderlyingResource();
169 * @see ISourceManipulation
171 //public void move(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException {
172 // if (container == null) {
173 // throw new IllegalArgumentException(ProjectPrefUtil.bind("operation.nullContainer")); //$NON-NLS-1$
175 // IJavaElement[] elements= new IJavaElement[] {this};
176 // IJavaElement[] containers= new IJavaElement[] {container};
177 // IJavaElement[] siblings= null;
178 // if (sibling != null) {
179 // siblings= new IJavaElement[] {sibling};
181 // String[] renamings= null;
182 // if (rename != null) {
183 // renamings= new String[] {rename};
185 // getJavaModel().move(elements, containers, siblings, renamings, force, monitor);
188 * @see ISourceManipulation
190 //public void rename(String name, boolean force, IProgressMonitor monitor) throws JavaModelException {
191 // if (name == null) {
192 // throw new IllegalArgumentException(ProjectPrefUtil.bind("element.nullName")); //$NON-NLS-1$
194 // IJavaElement[] elements= new IJavaElement[] {this};
195 // IJavaElement[] dests= new IJavaElement[] {this.getParent()};
196 // String[] renamings= new String[] {name};
197 // getJavaModel().rename(elements, dests, renamings, force, monitor);
200 * @see JavaElement#rootedAt(IJavaProject)
202 public IJavaElement rootedAt(IJavaProject project) {