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;
34 * Abstract class for Java elements which implement ISourceReference.
36 /* package */ abstract class SourceRefElement extends JavaElement implements ISourceReference {
37 protected SourceRefElement(JavaElement parent, String name) {
41 * This element is being closed. Do any necessary cleanup.
43 protected void closing(Object info) throws JavaModelException {
44 // Do any necessary cleanup
47 * Returns a new element info for this element.
49 protected Object createElementInfo() {
50 return null; // not used for source ref elements
53 * @see ISourceManipulation
55 //public void copy(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException {
56 // if (container == null) {
57 // throw new IllegalArgumentException(ProjectPrefUtil.bind("operation.nullContainer")); //$NON-NLS-1$
59 // IJavaElement[] elements= new IJavaElement[] {this};
60 // IJavaElement[] containers= new IJavaElement[] {container};
61 // IJavaElement[] siblings= null;
62 // if (sibling != null) {
63 // siblings= new IJavaElement[] {sibling};
65 // String[] renamings= null;
66 // if (rename != null) {
67 // renamings= new String[] {rename};
69 // getJavaModel().copy(elements, containers, siblings, renamings, force, monitor);
72 * @see ISourceManipulation
74 //public void delete(boolean force, IProgressMonitor monitor) throws JavaModelException {
75 // IJavaElement[] elements = new IJavaElement[] {this};
76 // getJavaModel().delete(elements, force, monitor);
79 * @see JavaElement#generateInfos
81 protected void generateInfos(Object info, HashMap newElements, IProgressMonitor pm) throws JavaModelException {
82 Openable openableParent = (Openable)getOpenableParent();
83 if (openableParent == null) return;
85 JavaElementInfo openableParentInfo = (JavaElementInfo) JavaModelManager.getJavaModelManager().getInfo(openableParent);
86 if (openableParentInfo == null) {
87 openableParent.generateInfos(openableParent.createElementInfo(), newElements, pm);
93 public ICompilationUnit getCompilationUnit() {
94 return ((JavaElement)getParent()).getCompilationUnit();
97 * Elements within compilation units and class files have no
98 * corresponding resource.
102 public IResource getCorrespondingResource() throws JavaModelException {
103 if (!exists()) throw newNotPresentException();
109 public IJavaElement getHandleFromMemento(String token, MementoTokenizer memento, WorkingCopyOwner workingCopyOwner) {
110 switch (token.charAt(0)) {
112 return getHandleUpdatingCountFromMemento(memento, workingCopyOwner);
117 * Return the first instance of IOpenable in the hierarchy of this
118 * type (going up the hierarchy from this type);
120 public IOpenable getOpenableParent() {
121 IJavaElement current = getParent();
122 while (current != null){
123 if (current instanceof IOpenable){
124 return (IOpenable) current;
126 current = current.getParent();
134 public IPath getPath() {
135 return this.getParent().getPath();
140 public IResource getResource() {
141 return this.getParent().getResource();
144 * @see ISourceReference
146 public String getSource() throws JavaModelException {
147 IOpenable openable = getOpenableParent();
148 IBuffer buffer = openable.getBuffer();
149 if (buffer == null) {
152 ISourceRange range = getSourceRange();
153 int offset = range.getOffset();
154 int length = range.getLength();
155 if (offset == -1 || length == 0 ) {
159 return buffer.getText(offset, length);
160 // jsurfer insert start
161 } catch (ArrayIndexOutOfBoundsException e) {
165 // jsurfer insert end
168 * @see ISourceReference
170 public ISourceRange getSourceRange() throws JavaModelException {
171 SourceRefElementInfo info = (SourceRefElementInfo) getElementInfo();
172 return info.getSourceRange();
177 public IResource getUnderlyingResource() throws JavaModelException {
178 if (!exists()) throw newNotPresentException();
179 return getParent().getUnderlyingResource();
182 * @see ISourceManipulation
184 //public void move(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException {
185 // if (container == null) {
186 // throw new IllegalArgumentException(ProjectPrefUtil.bind("operation.nullContainer")); //$NON-NLS-1$
188 // IJavaElement[] elements= new IJavaElement[] {this};
189 // IJavaElement[] containers= new IJavaElement[] {container};
190 // IJavaElement[] siblings= null;
191 // if (sibling != null) {
192 // siblings= new IJavaElement[] {sibling};
194 // String[] renamings= null;
195 // if (rename != null) {
196 // renamings= new String[] {rename};
198 // getJavaModel().move(elements, containers, siblings, renamings, force, monitor);
201 * @see ISourceManipulation
203 //public void rename(String name, boolean force, IProgressMonitor monitor) throws JavaModelException {
204 // if (name == null) {
205 // throw new IllegalArgumentException(ProjectPrefUtil.bind("element.nullName")); //$NON-NLS-1$
207 // IJavaElement[] elements= new IJavaElement[] {this};
208 // IJavaElement[] dests= new IJavaElement[] {this.getParent()};
209 // String[] renamings= new String[] {name};
210 // getJavaModel().rename(elements, dests, renamings, force, monitor);
213 * @see JavaElement#rootedAt(IJavaProject)
215 public IJavaElement rootedAt(IJavaProject project) {