9db0a5a843e08cbfbe6a51fbc607b919c762762d
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / SourceRefElement.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12
13 import java.util.HashMap;
14
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
26 import org.eclipse.core.resources.IResource;
27 import org.eclipse.core.runtime.IPath;
28 import org.eclipse.core.runtime.IProgressMonitor;
29
30
31 /**
32  * Abstract class for Java elements which implement ISourceReference.
33  */
34 /* package */ abstract class SourceRefElement extends JavaElement implements ISourceReference {
35         protected SourceRefElement(JavaElement parent, String name) {
36                 super(parent, name);
37         }
38 /**
39  * This element is being closed.  Do any necessary cleanup.
40  */
41 protected void closing(Object info) throws JavaModelException {
42         // Do any necessary cleanup
43 }
44 /**
45  * Returns a new element info for this element.
46  */
47 protected Object createElementInfo() {
48         return null; // not used for source ref elements
49 }
50 /**
51  * @see ISourceManipulation
52  */
53 //public void copy(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException {
54 //      if (container == null) {
55 //              throw new IllegalArgumentException(Util.bind("operation.nullContainer")); //$NON-NLS-1$
56 //      }
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};
62 //      }
63 //      String[] renamings= null;
64 //      if (rename != null) {
65 //              renamings= new String[] {rename};
66 //      }
67 //      getJavaModel().copy(elements, containers, siblings, renamings, force, monitor);
68 //}
69 /**
70  * @see ISourceManipulation
71  */
72 //public void delete(boolean force, IProgressMonitor monitor) throws JavaModelException {
73 //      IJavaElement[] elements = new IJavaElement[] {this};
74 //      getJavaModel().delete(elements, force, monitor);
75 //}
76 /*
77  * @see JavaElement#generateInfos
78  */
79 protected void generateInfos(Object info, HashMap newElements, IProgressMonitor pm) throws JavaModelException {
80         Openable openableParent = (Openable)getOpenableParent();
81         if (openableParent == null) return;
82
83         JavaElementInfo openableParentInfo = (JavaElementInfo) JavaModelManager.getJavaModelManager().getInfo(openableParent);
84         if (openableParentInfo == null) {
85                 openableParent.generateInfos(openableParent.createElementInfo(), newElements, pm);
86         }
87 }
88 /**
89  * @see IMember
90  */
91 public ICompilationUnit getCompilationUnit() {
92         return ((JavaElement)getParent()).getCompilationUnit();
93 }
94 /**
95  * Elements within compilation units and class files have no
96  * corresponding resource.
97  *
98  * @see IJavaElement
99  */
100 public IResource getCorrespondingResource() throws JavaModelException {
101         if (!exists()) throw newNotPresentException();
102         return null;
103 }
104 /**
105  * Return the first instance of IOpenable in the hierarchy of this
106  * type (going up the hierarchy from this type);
107  */
108 public IOpenable getOpenableParent() {
109         IJavaElement current = getParent();
110         while (current != null){
111                 if (current instanceof IOpenable){
112                         return (IOpenable) current;
113                 }
114                 current = current.getParent();
115         }
116         return null;
117 }
118 /*
119  * @see IJavaElement
120  */
121 public IPath getPath() {
122         return this.getParent().getPath();
123 }
124 /*
125  * @see IJavaElement
126  */
127 public IResource getResource() {
128         return this.getParent().getResource();
129 }
130 /**
131  * @see ISourceReference
132  */
133 public String getSource() throws JavaModelException {
134         IOpenable openable = getOpenableParent();
135         IBuffer buffer = openable.getBuffer();
136         if (buffer == null) {
137                 return null;
138         }
139         ISourceRange range = getSourceRange();
140         int offset = range.getOffset();
141         int length = range.getLength();
142         if (offset == -1 || length == 0 ) {
143                 return null;
144         }
145         try {
146           return buffer.getText(offset, length);
147           // jsurfer insert start
148         } catch (ArrayIndexOutOfBoundsException e) {
149                 
150         }
151         return null;
152 //       jsurfer insert end
153 }
154 /**
155  * @see ISourceReference
156  */
157 public ISourceRange getSourceRange() throws JavaModelException {
158         SourceRefElementInfo info = (SourceRefElementInfo) getElementInfo();
159         return info.getSourceRange();
160 }
161 /**
162  * @see IJavaElement
163  */
164 public IResource getUnderlyingResource() throws JavaModelException {
165         if (!exists()) throw newNotPresentException();
166         return getParent().getUnderlyingResource();
167 }
168 /**
169  * @see ISourceManipulation
170  */
171 //public void move(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException {
172 //      if (container == null) {
173 //              throw new IllegalArgumentException(Util.bind("operation.nullContainer")); //$NON-NLS-1$
174 //      }
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};
180 //      }
181 //      String[] renamings= null;
182 //      if (rename != null) {
183 //              renamings= new String[] {rename};
184 //      }
185 //      getJavaModel().move(elements, containers, siblings, renamings, force, monitor);
186 //}
187 /**
188  * @see ISourceManipulation
189  */
190 //public void rename(String name, boolean force, IProgressMonitor monitor) throws JavaModelException {
191 //      if (name == null) {
192 //              throw new IllegalArgumentException(Util.bind("element.nullName")); //$NON-NLS-1$
193 //      }
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);
198 //}
199 /*
200  * @see JavaElement#rootedAt(IJavaProject)
201  */
202 public IJavaElement rootedAt(IJavaProject project) {
203         // not needed
204         return null;
205 }
206 }