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 ) {
148 return buffer.getText(offset, length);
151 * @see ISourceReference
153 public ISourceRange getSourceRange() throws JavaModelException {
154 SourceRefElementInfo info = (SourceRefElementInfo) getElementInfo();
155 return info.getSourceRange();
160 public IResource getUnderlyingResource() throws JavaModelException {
161 if (!exists()) throw newNotPresentException();
162 return getParent().getUnderlyingResource();
165 * @see ISourceManipulation
167 //public void move(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException {
168 // if (container == null) {
169 // throw new IllegalArgumentException(Util.bind("operation.nullContainer")); //$NON-NLS-1$
171 // IJavaElement[] elements= new IJavaElement[] {this};
172 // IJavaElement[] containers= new IJavaElement[] {container};
173 // IJavaElement[] siblings= null;
174 // if (sibling != null) {
175 // siblings= new IJavaElement[] {sibling};
177 // String[] renamings= null;
178 // if (rename != null) {
179 // renamings= new String[] {rename};
181 // getJavaModel().move(elements, containers, siblings, renamings, force, monitor);
184 * @see ISourceManipulation
186 //public void rename(String name, boolean force, IProgressMonitor monitor) throws JavaModelException {
187 // if (name == null) {
188 // throw new IllegalArgumentException(Util.bind("element.nullName")); //$NON-NLS-1$
190 // IJavaElement[] elements= new IJavaElement[] {this};
191 // IJavaElement[] dests= new IJavaElement[] {this.getParent()};
192 // String[] renamings= new String[] {name};
193 // getJavaModel().rename(elements, dests, renamings, force, monitor);
196 * @see JavaElement#rootedAt(IJavaProject)
198 public IJavaElement rootedAt(IJavaProject project) {