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 net.sourceforge.phpdt.core.IBuffer;
14 import net.sourceforge.phpdt.core.ICompilationUnit;
15 import net.sourceforge.phpdt.core.IJavaElement;
16 import net.sourceforge.phpdt.core.IJavaProject;
17 import net.sourceforge.phpdt.core.IOpenable;
18 import net.sourceforge.phpdt.core.ISourceRange;
19 import net.sourceforge.phpdt.core.ISourceReference;
20 import net.sourceforge.phpdt.core.JavaModelException;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.core.runtime.IProgressMonitor;
28 * Abstract class for Java elements which implement ISourceReference.
30 /* package */ abstract class SourceRefElement extends JavaElement implements ISourceReference {
31 protected SourceRefElement(int type, IJavaElement parent, String name) {
32 super(type, parent, name);
35 * @see ISourceManipulation
37 //public void copy(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException {
38 // if (container == null) {
39 // throw new IllegalArgumentException(Util.bind("operation.nullContainer")); //$NON-NLS-1$
41 // IJavaElement[] elements= new IJavaElement[] {this};
42 // IJavaElement[] containers= new IJavaElement[] {container};
43 // IJavaElement[] siblings= null;
44 // if (sibling != null) {
45 // siblings= new IJavaElement[] {sibling};
47 // String[] renamings= null;
48 // if (rename != null) {
49 // renamings= new String[] {rename};
51 // getJavaModel().copy(elements, containers, siblings, renamings, force, monitor);
54 * @see ISourceManipulation
56 //public void delete(boolean force, IProgressMonitor monitor) throws JavaModelException {
57 // IJavaElement[] elements = new IJavaElement[] {this};
58 // getJavaModel().delete(elements, force, monitor);
63 public ICompilationUnit getCompilationUnit() {
64 return ((JavaElement)getParent()).getCompilationUnit();
67 * Elements within compilation units and class files have no
68 * corresponding resource.
72 public IResource getCorrespondingResource() throws JavaModelException {
73 if (!exists()) throw newNotPresentException();
77 * Return the first instance of IOpenable in the hierarchy of this
78 * type (going up the hierarchy from this type);
80 public IOpenable getOpenableParent() {
81 IJavaElement current = getParent();
82 while (current != null){
83 if (current instanceof IOpenable){
84 return (IOpenable) current;
86 current = current.getParent();
93 public IPath getPath() {
94 return this.getParent().getPath();
99 public IResource getResource() {
100 return this.getParent().getResource();
103 * @see ISourceReference
105 public String getSource() throws JavaModelException {
106 IOpenable openable = getOpenableParent();
107 IBuffer buffer = openable.getBuffer();
108 if (buffer == null) {
111 ISourceRange range = getSourceRange();
112 int offset = range.getOffset();
113 int length = range.getLength();
114 if (offset == -1 || length == 0 ) {
117 return buffer.getText(offset, length);
120 * @see ISourceReference
122 public ISourceRange getSourceRange() throws JavaModelException {
123 SourceRefElementInfo info = (SourceRefElementInfo) getElementInfo();
124 return info.getSourceRange();
129 public IResource getUnderlyingResource() throws JavaModelException {
130 if (!exists()) throw newNotPresentException();
131 return getParent().getUnderlyingResource();
134 * @see ISourceManipulation
136 //public void move(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException {
137 // if (container == null) {
138 // throw new IllegalArgumentException(Util.bind("operation.nullContainer")); //$NON-NLS-1$
140 // IJavaElement[] elements= new IJavaElement[] {this};
141 // IJavaElement[] containers= new IJavaElement[] {container};
142 // IJavaElement[] siblings= null;
143 // if (sibling != null) {
144 // siblings= new IJavaElement[] {sibling};
146 // String[] renamings= null;
147 // if (rename != null) {
148 // renamings= new String[] {rename};
150 // getJavaModel().move(elements, containers, siblings, renamings, force, monitor);
153 * @see ISourceManipulation
155 //public void rename(String name, boolean force, IProgressMonitor monitor) throws JavaModelException {
156 // if (name == null) {
157 // throw new IllegalArgumentException(Util.bind("element.nullName")); //$NON-NLS-1$
159 // IJavaElement[] elements= new IJavaElement[] {this};
160 // IJavaElement[] dests= new IJavaElement[] {this.getParent()};
161 // String[] renamings= new String[] {name};
162 // getJavaModel().rename(elements, dests, renamings, force, monitor);
165 * @see JavaElement#rootedAt(IJavaProject)
167 public IJavaElement rootedAt(IJavaProject project) {