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