06f1c5f0c7f47fd33ba0d51af22213f393473c38
[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 && ProjectPrefUtil.isValidCompilationUnitName(child.getName())) {
81 //                                                              childElement = new CompilationUnit(this, child.getName(), DefaultWorkingCopyOwner.PRIMARY);
82 //                                                              vChildren.add(childElement);
83 //                                                      } else if (ProjectPrefUtil.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 //                                      && !ProjectPrefUtil.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 && ProjectPrefUtil.isValidCompilationUnitName(child.getName())) {
138 ////                                                    childElement = getCompilationUnit(child.getName());
139 ////                                                    vChildren.add(childElement);
140 ////                                            } else if (ProjectPrefUtil.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(ProjectPrefUtil.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 a php file extension
237  */
238 public ICompilationUnit getCompilationUnit(String cuName) {
239         if (!  PHPFileUtil.isValidPHPUnitName(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                 return root.getPath().append(this.getElementName());
296         }
297 }
298 /**
299  * @see IJavaElement#getResource()
300  */
301 public IResource getResource() {
302         PackageFragmentRoot root = this.getPackageFragmentRoot();
303         if (root.isArchive()) {
304                 return root.getResource();
305         } else {
306                 String elementName = this.getElementName();
307                 if (elementName.length() == 0) {
308                         return root.getResource();
309                 } else {
310 //                      return ((IContainer)root.getResource()).getFolder(new Path(this.getElementName().replace('.', '/')));
311                         return ((IContainer)root.getResource()).getFolder(new Path(this.getElementName()));
312                 }
313         }
314 }
315 /**
316  * @see IJavaElement#getUnderlyingResource()
317  */
318 public IResource getUnderlyingResource() throws JavaModelException {
319         IResource rootResource = parent.getUnderlyingResource();
320         if (rootResource == null) {
321                 //jar package fragment root that has no associated resource
322                 return null;
323         }
324         // the underlying resource may be a folder or a project (in the case that the project folder
325         // is atually the package fragment root)
326 //      if (rootResource.getType() == IResource.FOLDER || rootResource.getType() == IResource.PROJECT) {
327 //              IContainer folder = (IContainer) rootResource;
328 //              String[] segs = Signature.getSimpleNames(fName);
329 //              for (int i = 0; i < segs.length; ++i) {
330 //                      IResource child = folder.findMember(segs[i]);
331 //                      if (child == null || child.getType() != IResource.FOLDER) {
332 //                              throw newNotPresentException();
333 //                      }
334 //                      folder = (IFolder) child;
335 //              }
336 //              return folder;
337 //      } else {
338                 return rootResource;
339 //      }
340 }
341 /**
342  * @see IPackageFragment#hasSubpackages()
343  */
344 //public boolean hasSubpackages() throws JavaModelException {
345 //      IJavaElement[] packages= ((IPackageFragmentRoot)getParent()).getChildren();
346 //      String name = getElementName();
347 //      int nameLength = name.length();
348 //      String packageName = isDefaultPackage() ? name : name+"."; //$NON-NLS-1$
349 //      for (int i= 0; i < packages.length; i++) {
350 //              String otherName = packages[i].getElementName();
351 //              if (otherName.length() > nameLength && otherName.startsWith(packageName)) {
352 //                      return true;
353 //              }
354 //      }
355 //      return false;
356 //}
357 /**
358  * @see IPackageFragment#isDefaultPackage()
359  */
360 public boolean isDefaultPackage() {
361         return this.getElementName().length() == 0;
362 }
363 /**
364  * @see ISourceManipulation#move(IJavaElement, IJavaElement, String, boolean, IProgressMonitor)
365  */
366 //public void move(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException {
367 //      if (container == null) {
368 //              throw new IllegalArgumentException(ProjectPrefUtil.bind("operation.nullContainer")); //$NON-NLS-1$
369 //      }
370 //      IJavaElement[] elements= new IJavaElement[] {this};
371 //      IJavaElement[] containers= new IJavaElement[] {container};
372 //      IJavaElement[] siblings= null;
373 //      if (sibling != null) {
374 //              siblings= new IJavaElement[] {sibling};
375 //      }
376 //      String[] renamings= null;
377 //      if (rename != null) {
378 //              renamings= new String[] {rename};
379 //      }
380 //      getJavaModel().move(elements, containers, siblings, renamings, force, monitor);
381 //}
382 //protected void openWhenClosed(IProgressMonitor pm) throws JavaModelException {
383 //      if (!this.resourceExists()) throw newNotPresentException();
384 //      super.openWhenClosed(pm);
385 //}
386 /**
387  * Recomputes the children of this element, based on the current state
388  * of the workbench.
389  */
390 //public void refreshChildren() {
391 //      try {
392 //              OpenableElementInfo info= (OpenableElementInfo)getElementInfo();
393 //              computeChildren(info, getResource());
394 //      } catch (JavaModelException e) {
395 //              // do nothing.
396 //      }
397 //}
398 /**
399  * @see ISourceManipulation#rename(String, boolean, IProgressMonitor)
400  */
401 //public void rename(String name, boolean force, IProgressMonitor monitor) throws JavaModelException {
402 //      if (name == null) {
403 //              throw new IllegalArgumentException(ProjectPrefUtil.bind("element.nullName")); //$NON-NLS-1$
404 //      }
405 //      IJavaElement[] elements= new IJavaElement[] {this};
406 //      IJavaElement[] dests= new IJavaElement[] {this.getParent()};
407 //      String[] renamings= new String[] {name};
408 //      getJavaModel().rename(elements, dests, renamings, force, monitor);
409 //}
410 ///*
411 // * @see JavaElement#rootedAt(IJavaProject)
412 // */
413 //public IJavaElement rootedAt(IJavaProject project) {
414 //      return
415 //              new PackageFragment(
416 //                      (IPackageFragmentRoot)((JavaElement)parent).rootedAt(project), 
417 //                      name);
418 //}
419 /**
420  * Debugging purposes
421  */
422 protected void toStringChildren(int tab, StringBuffer buffer, Object info) {
423         if (tab == 0) {
424                 super.toStringChildren(tab, buffer, info);
425         }
426 }
427 /**
428  * Debugging purposes
429  */
430 protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
431         buffer.append(this.tabString(tab));
432         if (getElementName().length() == 0) {
433                 buffer.append("[default]"); //$NON-NLS-1$
434         } else {
435                 buffer.append(getElementName());
436         }
437         if (info == null) {
438                 buffer.append(" (not open)"); //$NON-NLS-1$
439         } else {
440                 if (tab > 0) {
441                         buffer.append(" (...)"); //$NON-NLS-1$
442                 }
443         }
444 }
445 }