Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / PackageFragment.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 net.sourceforge.phpdt.core.ICompilationUnit;
14 import net.sourceforge.phpdt.core.IJavaElement;
15 import net.sourceforge.phpdt.core.IJavaProject;
16 import net.sourceforge.phpdt.core.IPackageFragment;
17 import net.sourceforge.phpdt.core.IPackageFragmentRoot;
18 import net.sourceforge.phpdt.core.JavaModelException;
19
20 import org.eclipse.core.resources.IContainer;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.Path;
24
25
26 /**
27  * @see IPackageFragment
28  */
29 public class PackageFragment extends Openable implements IPackageFragment {
30                 /**
31          * Constant empty list of compilation units
32          */
33         protected static ICompilationUnit[] fgEmptyCompilationUnitList= new ICompilationUnit[] {};
34 /**
35  * Constructs a handle for a package fragment
36  *
37  * @see IPackageFragment
38  */
39 protected PackageFragment(IPackageFragmentRoot root, String name) {
40         super(PACKAGE_FRAGMENT, null, name);
41 }
42 /**
43  * Compute the children of this package fragment.
44  *
45  * <p>Package fragments which are folders recognize files based on the
46  * type of the fragment
47  * <p>Package fragments which are in a jar only recognize .class files (
48  * @see JarPackageFragment).
49  */
50 //protected boolean computeChildren(OpenableElementInfo info, IResource resource) throws JavaModelException {
51 //      ArrayList vChildren = new ArrayList();
52 //      int kind = getKind();
53 //      String extType;
54 //      if (kind == IPackageFragmentRoot.K_SOURCE) {
55 //              extType = "java"; //$NON-NLS-1$
56 //      } else {
57 //              extType = "class"; //$NON-NLS-1$
58 //      }
59 //      try {
60 //              char[][] exclusionPatterns = ((PackageFragmentRoot)getPackageFragmentRoot()).fullExclusionPatternChars();
61 //              IResource[] members = ((IContainer) resource).members();
62 //              for (int i = 0, max = members.length; i < max; i++) {
63 //                      IResource child = members[i];
64 //                      if (child.getType() != IResource.FOLDER
65 //                                      && !Util.isExcluded(child, exclusionPatterns)) {
66 //                              String extension = child.getProjectRelativePath().getFileExtension();
67 //                              if (extension != null) {
68 //                                      if (extension.equalsIgnoreCase(extType)) {
69 //                                              IJavaElement childElement;
70 //                                              if (kind == IPackageFragmentRoot.K_SOURCE && Util.isValidCompilationUnitName(child.getName())) {
71 //                                                      childElement = getCompilationUnit(child.getName());
72 //                                                      vChildren.add(childElement);
73 //                                              } else if (Util.isValidClassFileName(child.getName())) {
74 //                                                      childElement = getClassFile(child.getName());
75 //                                                      vChildren.add(childElement);
76 //                                              }
77 //                                      }
78 //                              }
79 //                      }
80 //              }
81 //      } catch (CoreException e) {
82 //              throw new JavaModelException(e);
83 //      }
84 //      IJavaElement[] children = new IJavaElement[vChildren.size()];
85 //      vChildren.toArray(children);
86 //      info.setChildren(children);
87 //      return true;
88 //}
89 /**
90  * Returns true if this fragment contains at least one java resource.
91  * Returns false otherwise.
92  */
93 //public boolean containsJavaResources() throws JavaModelException {
94 //      return ((PackageFragmentInfo) getElementInfo()).containsJavaResources();
95 //}
96 /**
97  * @see ISourceManipulation
98  */
99 //public void copy(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException {
100 //      if (container == null) {
101 //              throw new IllegalArgumentException(Util.bind("operation.nullContainer")); //$NON-NLS-1$
102 //      }
103 //      IJavaElement[] elements= new IJavaElement[] {this};
104 //      IJavaElement[] containers= new IJavaElement[] {container};
105 //      IJavaElement[] siblings= null;
106 //      if (sibling != null) {
107 //              siblings= new IJavaElement[] {sibling};
108 //      }
109 //      String[] renamings= null;
110 //      if (rename != null) {
111 //              renamings= new String[] {rename};
112 //      }
113 //      getJavaModel().copy(elements, containers, siblings, renamings, force, monitor);
114 //}
115 /**
116  * @see IPackageFragment
117  */
118 public ICompilationUnit createCompilationUnit(String name, String contents, boolean force, IProgressMonitor monitor) throws JavaModelException {
119 //      CreateCompilationUnitOperation op= new CreateCompilationUnitOperation(this, name, contents, force);
120 //      runOperation(op, monitor);
121 //      return getCompilationUnit(name);
122     return null;
123 }
124 /**
125  * @see JavaElement
126  */
127 //protected OpenableElementInfo createElementInfo() {
128 //      return new PackageFragmentInfo();
129 //}
130 /**
131  * @see ISourceManipulation
132  */
133 //public void delete(boolean force, IProgressMonitor monitor) throws JavaModelException {
134 //      IJavaElement[] elements = new IJavaElement[] {this};
135 //      getJavaModel().delete(elements, force, monitor);
136 //}
137 /**
138  * @see Openable
139  */
140 //protected boolean generateInfos(OpenableElementInfo info, IProgressMonitor pm, Map newElements, IResource underlyingResource) throws JavaModelException {
141 //      
142 //      return computeChildren(info, underlyingResource);
143 //}
144 ///**
145 // * @see IPackageFragment#getClassFile(String)
146 // */
147 //public IClassFile getClassFile(String name) {
148 //      return new ClassFile(this, name);
149 //}
150 /**
151  * Returns a the collection of class files in this - a folder package fragment which has a root
152  * that has its kind set to <code>IPackageFragmentRoot.K_Source</code> does not
153  * recognize class files.
154  *
155  * @see IPackageFragment#getClassFiles()
156  */
157 //public IClassFile[] getClassFiles() throws JavaModelException {
158 //      if (getKind() == IPackageFragmentRoot.K_SOURCE) {
159 //              return fgEmptyClassFileList;
160 //      }
161 //      
162 //      ArrayList list = getChildrenOfType(CLASS_FILE);
163 //      IClassFile[] array= new IClassFile[list.size()];
164 //      list.toArray(array);
165 //      return array;
166 //}
167 /**
168  * @see IPackageFragment#getCompilationUnit(String)
169  */
170 public ICompilationUnit getCompilationUnit(String name) {
171         return new CompilationUnit(this, name);
172 }
173 /**
174  * @see IPackageFragment#getCompilationUnits()
175  */
176 //public ICompilationUnit[] getCompilationUnits() throws JavaModelException {
177 //      if (getKind() == IPackageFragmentRoot.K_BINARY) {
178 //              return fgEmptyCompilationUnitList;
179 //      }
180 //      
181 //      ArrayList list = getChildrenOfType(COMPILATION_UNIT);
182 //      ICompilationUnit[] array= new ICompilationUnit[list.size()];
183 //      list.toArray(array);
184 //      return array;
185 //}
186 /**
187  * @see JavaElement#getHandleMementoDelimiter()
188  */
189 protected char getHandleMementoDelimiter() {
190         return JavaElement.JEM_PACKAGEFRAGMENT;
191 }
192 /**
193  * @see IPackageFragment#getKind()
194  */
195 //public int getKind() throws JavaModelException {
196 //      return ((IPackageFragmentRoot)getParent()).getKind();
197 //}
198 /**
199  * Returns an array of non-java resources contained in the receiver.
200  */
201 //public Object[] getNonJavaResources() throws JavaModelException {
202 //      if (this.isDefaultPackage()) {
203 //              // We don't want to show non java resources of the default package (see PR #1G58NB8)
204 //              return JavaElementInfo.NO_NON_JAVA_RESOURCES;
205 //      } else {
206 //              return ((PackageFragmentInfo) getElementInfo()).getNonJavaResources(getResource(), (PackageFragmentRoot)getPackageFragmentRoot());
207 //      }
208 //}
209 /**
210  * @see IJavaElement#getPath()
211  */
212 //public IPath getPath() {
213 //      PackageFragmentRoot root = this.getPackageFragmentRoot();
214 //      if (root.isArchive()) {
215 //              return root.getPath();
216 //      } else {
217 //              return root.getPath().append(this.getElementName().replace('.', '/'));
218 //      }
219 //}
220 /**
221  * @see IJavaElement#getResource()
222  */
223 public IResource getResource() {
224         PackageFragmentRoot root = this.getPackageFragmentRoot();
225         if (root.isArchive()) {
226                 return root.getResource();
227         } else {
228                 String elementName = this.getElementName();
229                 if (elementName.length() == 0) {
230                         return root.getResource();
231                 } else {
232                         return ((IContainer)root.getResource()).getFolder(new Path(this.getElementName().replace('.', '/')));
233                 }
234         }
235 }
236 /**
237  * @see IJavaElement#getUnderlyingResource()
238  */
239 public IResource getUnderlyingResource() throws JavaModelException {
240         IResource rootResource = fParent.getUnderlyingResource();
241         if (rootResource == null) {
242                 //jar package fragment root that has no associated resource
243                 return null;
244         }
245         // the underlying resource may be a folder or a project (in the case that the project folder
246         // is atually the package fragment root)
247 //      if (rootResource.getType() == IResource.FOLDER || rootResource.getType() == IResource.PROJECT) {
248 //              IContainer folder = (IContainer) rootResource;
249 //              String[] segs = Signature.getSimpleNames(fName);
250 //              for (int i = 0; i < segs.length; ++i) {
251 //                      IResource child = folder.findMember(segs[i]);
252 //                      if (child == null || child.getType() != IResource.FOLDER) {
253 //                              throw newNotPresentException();
254 //                      }
255 //                      folder = (IFolder) child;
256 //              }
257 //              return folder;
258 //      } else {
259                 return rootResource;
260 //      }
261 }
262 /**
263  * @see IPackageFragment#hasSubpackages()
264  */
265 //public boolean hasSubpackages() throws JavaModelException {
266 //      IJavaElement[] packages= ((IPackageFragmentRoot)getParent()).getChildren();
267 //      String name = getElementName();
268 //      int nameLength = name.length();
269 //      String packageName = isDefaultPackage() ? name : name+"."; //$NON-NLS-1$
270 //      for (int i= 0; i < packages.length; i++) {
271 //              String otherName = packages[i].getElementName();
272 //              if (otherName.length() > nameLength && otherName.startsWith(packageName)) {
273 //                      return true;
274 //              }
275 //      }
276 //      return false;
277 //}
278 /**
279  * @see IPackageFragment#isDefaultPackage()
280  */
281 public boolean isDefaultPackage() {
282         return this.getElementName().length() == 0;
283 }
284 /**
285  * @see ISourceManipulation#move(IJavaElement, IJavaElement, String, boolean, IProgressMonitor)
286  */
287 //public void move(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException {
288 //      if (container == null) {
289 //              throw new IllegalArgumentException(Util.bind("operation.nullContainer")); //$NON-NLS-1$
290 //      }
291 //      IJavaElement[] elements= new IJavaElement[] {this};
292 //      IJavaElement[] containers= new IJavaElement[] {container};
293 //      IJavaElement[] siblings= null;
294 //      if (sibling != null) {
295 //              siblings= new IJavaElement[] {sibling};
296 //      }
297 //      String[] renamings= null;
298 //      if (rename != null) {
299 //              renamings= new String[] {rename};
300 //      }
301 //      getJavaModel().move(elements, containers, siblings, renamings, force, monitor);
302 //}
303 //protected void openWhenClosed(IProgressMonitor pm) throws JavaModelException {
304 //      if (!this.resourceExists()) throw newNotPresentException();
305 //      super.openWhenClosed(pm);
306 //}
307 /**
308  * Recomputes the children of this element, based on the current state
309  * of the workbench.
310  */
311 //public void refreshChildren() {
312 //      try {
313 //              OpenableElementInfo info= (OpenableElementInfo)getElementInfo();
314 //              computeChildren(info, getResource());
315 //      } catch (JavaModelException e) {
316 //              // do nothing.
317 //      }
318 //}
319 /**
320  * @see ISourceManipulation#rename(String, boolean, IProgressMonitor)
321  */
322 //public void rename(String name, boolean force, IProgressMonitor monitor) throws JavaModelException {
323 //      if (name == null) {
324 //              throw new IllegalArgumentException(Util.bind("element.nullName")); //$NON-NLS-1$
325 //      }
326 //      IJavaElement[] elements= new IJavaElement[] {this};
327 //      IJavaElement[] dests= new IJavaElement[] {this.getParent()};
328 //      String[] renamings= new String[] {name};
329 //      getJavaModel().rename(elements, dests, renamings, force, monitor);
330 //}
331 /*
332  * @see JavaElement#rootedAt(IJavaProject)
333  */
334 public IJavaElement rootedAt(IJavaProject project) {
335         return
336                 new PackageFragment(
337                         (IPackageFragmentRoot)((JavaElement)fParent).rootedAt(project), 
338                         fName);
339 }
340 /**
341  * Debugging purposes
342  */
343 protected void toStringChildren(int tab, StringBuffer buffer, Object info) {
344         if (tab == 0) {
345                 super.toStringChildren(tab, buffer, info);
346         }
347 }
348 /**
349  * Debugging purposes
350  */
351 protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
352         buffer.append(this.tabString(tab));
353         if (getElementName().length() == 0) {
354                 buffer.append("[default]"); //$NON-NLS-1$
355         } else {
356                 buffer.append(getElementName());
357         }
358         if (info == null) {
359                 buffer.append(" (not open)"); //$NON-NLS-1$
360         } else {
361                 if (tab > 0) {
362                         buffer.append(" (...)"); //$NON-NLS-1$
363                 }
364         }
365 }
366 }