1) Added missing strings for italic, underline and strike through.
[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 import net.sourceforge.phpdt.core.WorkingCopyOwner;
26 import net.sourceforge.phpdt.internal.core.util.MementoTokenizer;
27
28 import org.eclipse.core.resources.IResource;
29 import org.eclipse.core.runtime.IPath;
30 import org.eclipse.core.runtime.IProgressMonitor;
31
32 /**
33  * Abstract class for Java elements which implement ISourceReference.
34  */
35 /* package */abstract class SourceRefElement extends JavaElement implements
36                 ISourceReference {
37         protected SourceRefElement(JavaElement parent, String name) {
38                 super(parent, name);
39         }
40
41         /**
42          * This element is being closed. Do any necessary cleanup.
43          */
44         protected void closing(Object info) throws JavaModelException {
45                 // Do any necessary cleanup
46         }
47
48         /**
49          * Returns a new element info for this element.
50          */
51         protected Object createElementInfo() {
52                 return null; // not used for source ref elements
53         }
54
55         /**
56          * @see ISourceManipulation
57          */
58         // public void copy(IJavaElement container, IJavaElement sibling, String
59         // rename, boolean force, IProgressMonitor monitor) throws
60         // JavaModelException {
61         // if (container == null) {
62         // throw new
63         // IllegalArgumentException(ProjectPrefUtil.bind("operation.nullContainer"));
64         // //$NON-NLS-1$
65         // }
66         // IJavaElement[] elements= new IJavaElement[] {this};
67         // IJavaElement[] containers= new IJavaElement[] {container};
68         // IJavaElement[] siblings= null;
69         // if (sibling != null) {
70         // siblings= new IJavaElement[] {sibling};
71         // }
72         // String[] renamings= null;
73         // if (rename != null) {
74         // renamings= new String[] {rename};
75         // }
76         // getJavaModel().copy(elements, containers, siblings, renamings, force,
77         // monitor);
78         // }
79         /**
80          * @see ISourceManipulation
81          */
82         // public void delete(boolean force, IProgressMonitor monitor) throws
83         // JavaModelException {
84         // IJavaElement[] elements = new IJavaElement[] {this};
85         // getJavaModel().delete(elements, force, monitor);
86         // }
87         /*
88          * @see JavaElement#generateInfos
89          */
90         protected void generateInfos(Object info, HashMap newElements,
91                         IProgressMonitor pm) throws JavaModelException {
92                 Openable openableParent = (Openable) getOpenableParent();
93                 if (openableParent == null)
94                         return;
95
96                 JavaElementInfo openableParentInfo = (JavaElementInfo) JavaModelManager
97                                 .getJavaModelManager().getInfo(openableParent);
98                 if (openableParentInfo == null) {
99                         openableParent.generateInfos(openableParent.createElementInfo(),
100                                         newElements, pm);
101                 }
102         }
103
104         /**
105          * @see IMember
106          */
107         public ICompilationUnit getCompilationUnit() {
108                 return ((JavaElement) getParent()).getCompilationUnit();
109         }
110
111         /**
112          * Elements within compilation units and class files have no corresponding
113          * resource.
114          * 
115          * @see IJavaElement
116          */
117         public IResource getCorrespondingResource() throws JavaModelException {
118                 if (!exists())
119                         throw newNotPresentException();
120                 return null;
121         }
122
123         /*
124          * @see JavaElement
125          */
126         public IJavaElement getHandleFromMemento(String token,
127                         MementoTokenizer memento, WorkingCopyOwner workingCopyOwner) {
128                 switch (token.charAt(0)) {
129                 case JEM_COUNT:
130                         return getHandleUpdatingCountFromMemento(memento, workingCopyOwner);
131                 }
132                 return this;
133         }
134
135         /**
136          * Return the first instance of IOpenable in the hierarchy of this type
137          * (going up the hierarchy from this type);
138          */
139         public IOpenable getOpenableParent() {
140                 IJavaElement current = getParent();
141                 while (current != null) {
142                         if (current instanceof IOpenable) {
143                                 return (IOpenable) current;
144                         }
145                         current = current.getParent();
146                 }
147                 return null;
148         }
149
150         /*
151          * @see IJavaElement
152          */
153         public IPath getPath() {
154                 return this.getParent().getPath();
155         }
156
157         /*
158          * @see IJavaElement
159          */
160         public IResource getResource() {
161                 return this.getParent().getResource();
162         }
163
164         /**
165          * @see ISourceReference
166          */
167         public String getSource() throws JavaModelException {
168                 IOpenable openable = getOpenableParent();
169                 IBuffer buffer = openable.getBuffer();
170                 if (buffer == null) {
171                         return null;
172                 }
173                 ISourceRange range = getSourceRange();
174                 int offset = range.getOffset();
175                 int length = range.getLength();
176                 if (offset == -1 || length == 0) {
177                         return null;
178                 }
179                 try {
180                         return buffer.getText(offset, length);
181                         // jsurfer insert start
182                 } catch (ArrayIndexOutOfBoundsException e) {
183
184                 }
185                 return null;
186                 // jsurfer insert end
187         }
188
189         /**
190          * @see ISourceReference
191          */
192         public ISourceRange getSourceRange() throws JavaModelException {
193                 SourceRefElementInfo info = (SourceRefElementInfo) getElementInfo();
194                 return info.getSourceRange();
195         }
196
197         /**
198          * @see IJavaElement
199          */
200         public IResource getUnderlyingResource() throws JavaModelException {
201                 if (!exists())
202                         throw newNotPresentException();
203                 return getParent().getUnderlyingResource();
204         }
205
206         /**
207          * @see ISourceManipulation
208          */
209         // public void move(IJavaElement container, IJavaElement sibling, String
210         // rename, boolean force, IProgressMonitor monitor) throws
211         // JavaModelException {
212         // if (container == null) {
213         // throw new
214         // IllegalArgumentException(ProjectPrefUtil.bind("operation.nullContainer"));
215         // //$NON-NLS-1$
216         // }
217         // IJavaElement[] elements= new IJavaElement[] {this};
218         // IJavaElement[] containers= new IJavaElement[] {container};
219         // IJavaElement[] siblings= null;
220         // if (sibling != null) {
221         // siblings= new IJavaElement[] {sibling};
222         // }
223         // String[] renamings= null;
224         // if (rename != null) {
225         // renamings= new String[] {rename};
226         // }
227         // getJavaModel().move(elements, containers, siblings, renamings, force,
228         // monitor);
229         // }
230         /**
231          * @see ISourceManipulation
232          */
233         // public void rename(String name, boolean force, IProgressMonitor monitor)
234         // throws JavaModelException {
235         // if (name == null) {
236         // throw new
237         // IllegalArgumentException(ProjectPrefUtil.bind("element.nullName"));
238         // //$NON-NLS-1$
239         // }
240         // IJavaElement[] elements= new IJavaElement[] {this};
241         // IJavaElement[] dests= new IJavaElement[] {this.getParent()};
242         // String[] renamings= new String[] {name};
243         // getJavaModel().rename(elements, dests, renamings, force, monitor);
244         // }
245         /*
246          * @see JavaElement#rootedAt(IJavaProject)
247          */
248         public IJavaElement rootedAt(IJavaProject project) {
249                 // not needed
250                 return null;
251         }
252 }