3m9 compatible;
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / PackageFragment.java
index 6b75157..ea8f6b4 100644 (file)
 package net.sourceforge.phpdt.internal.core;
 
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.Map;
 
 import net.sourceforge.phpdt.core.ICompilationUnit;
 import net.sourceforge.phpdt.core.IJavaElement;
-import net.sourceforge.phpdt.core.IJavaProject;
 import net.sourceforge.phpdt.core.IPackageFragment;
 import net.sourceforge.phpdt.core.IPackageFragmentRoot;
 import net.sourceforge.phpdt.core.JavaModelException;
+import net.sourceforge.phpdt.internal.core.util.Util;
+import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil;
 
 import org.eclipse.core.resources.IContainer;
 import org.eclipse.core.resources.IResource;
@@ -36,61 +38,121 @@ public class PackageFragment extends Openable implements IPackageFragment {
         * Constant empty list of compilation units
         */
        protected static ICompilationUnit[] fgEmptyCompilationUnitList= new ICompilationUnit[] {};
-/**
- * Constructs a handle for a package fragment
- *
- * @see IPackageFragment
- */
-protected PackageFragment(IPackageFragmentRoot root, String name) {
-       super(PACKAGE_FRAGMENT, root, name);
-}
-/**
- * Compute the children of this package fragment.
- *
- * <p>Package fragments which are folders recognize files based on the
- * type of the fragment
- * <p>Package fragments which are in a jar only recognize .class files (
- * @see JarPackageFragment).
- */
-protected boolean computeChildren(OpenableElementInfo info, IResource resource) throws JavaModelException {
-       ArrayList vChildren = new ArrayList();
-//     int kind = getKind();
-       String extType;
-//     if (kind == IPackageFragmentRoot.K_SOURCE) {
-               extType = "php"; //$NON-NLS-1$
-//     } else {
-//             extType = "class"; //$NON-NLS-1$
-//     }
-       try {
-               char[][] exclusionPatterns = ((PackageFragmentRoot)getPackageFragmentRoot()).fullExclusionPatternChars();
-               IResource[] members = ((IContainer) resource).members();
-               for (int i = 0, max = members.length; i < max; i++) {
-                       IResource child = members[i];
-                       if (child.getType() != IResource.FOLDER
-                                       && !Util.isExcluded(child, exclusionPatterns)) {
-                               String extension = child.getProjectRelativePath().getFileExtension();
-                               if (extension != null) {
-                                       if (extension.equalsIgnoreCase(extType)) {
-                                               IJavaElement childElement;
-//                                             if (kind == IPackageFragmentRoot.K_SOURCE && Util.isValidCompilationUnitName(child.getName())) {
-//                                                     childElement = getCompilationUnit(child.getName());
-//                                                     vChildren.add(childElement);
-//                                             } else if (Util.isValidClassFileName(child.getName())) {
-//                                                     childElement = getClassFile(child.getName());
-//                                                     vChildren.add(childElement);
-//                                             }
+       /**
+        * Constructs a handle for a package fragment
+        *
+        * @see IPackageFragment
+        */
+       protected PackageFragment(PackageFragmentRoot root, String name) {
+               super(root, name);
+       }
+       /**
+        * @see Openable
+        */
+       protected boolean buildStructure(OpenableElementInfo info, IProgressMonitor pm, Map newElements, IResource underlyingResource) throws JavaModelException {
+
+               // check whether this pkg can be opened
+               if (!underlyingResource.isAccessible()) throw newNotPresentException();
+
+               int kind = getKind();
+               String extType;
+//             if (kind == IPackageFragmentRoot.K_SOURCE) {
+                       extType = "php"; //EXTENSION_java;
+//             } else {
+//                     extType = EXTENSION_class;
+//             }
+
+               // add compilation units/class files from resources
+               HashSet vChildren = new HashSet();
+               try {
+                   PackageFragmentRoot root = getPackageFragmentRoot();
+//                     char[][] inclusionPatterns = root.fullInclusionPatternChars();
+                       char[][] exclusionPatterns = root.fullExclusionPatternChars();
+                       IResource[] members = ((IContainer) underlyingResource).members();
+                       for (int i = 0, max = members.length; i < max; i++) {
+                               IResource child = members[i];
+                               if (child.getType() != IResource.FOLDER
+                                               && !Util.isExcluded(child, exclusionPatterns)) {
+                                       String extension = child.getProjectRelativePath().getFileExtension();
+                                       if (extension != null) {
+                                               if (extension.equalsIgnoreCase(extType)) {
+                                                       IJavaElement childElement;
+//                                                     if (kind == IPackageFragmentRoot.K_SOURCE && Util.isValidCompilationUnitName(child.getName())) {
+//                                                             childElement = new CompilationUnit(this, child.getName(), DefaultWorkingCopyOwner.PRIMARY);
+//                                                             vChildren.add(childElement);
+//                                                     } else if (Util.isValidClassFileName(child.getName())) {
+//                                                             childElement = getClassFile(child.getName());
+//                                                             vChildren.add(childElement);
+//                                                     }
+                                               }
                                        }
                                }
                        }
+               } catch (CoreException e) {
+                       throw new JavaModelException(e);
                }
-       } catch (CoreException e) {
-               throw new JavaModelException(e);
+               
+//             if (kind == IPackageFragmentRoot.K_SOURCE) {
+//                     // add primary compilation units
+//                     ICompilationUnit[] primaryCompilationUnits = getCompilationUnits(DefaultWorkingCopyOwner.PRIMARY);
+//                     for (int i = 0, length = primaryCompilationUnits.length; i < length; i++) {
+//                             ICompilationUnit primary = primaryCompilationUnits[i];
+//                             vChildren.add(primary);
+//                     }
+//             }
+               
+               IJavaElement[] children = new IJavaElement[vChildren.size()];
+               vChildren.toArray(children);
+               info.setChildren(children);
+               return true;
        }
-       IJavaElement[] children = new IJavaElement[vChildren.size()];
-       vChildren.toArray(children);
-       info.setChildren(children);
-       return true;
-}
+///**
+// * Compute the children of this package fragment.
+// *
+// * <p>Package fragments which are folders recognize files based on the
+// * type of the fragment
+// * <p>Package fragments which are in a jar only recognize .class files (
+// * @see JarPackageFragment).
+// */
+//protected boolean computeChildren(OpenableElementInfo info, IResource resource) throws JavaModelException {
+//     ArrayList vChildren = new ArrayList();
+////   int kind = getKind();
+//     String extType;
+////   if (kind == IPackageFragmentRoot.K_SOURCE) {
+//             extType = "php"; //$NON-NLS-1$
+////   } else {
+////           extType = "class"; //$NON-NLS-1$
+////   }
+//     try {
+//             char[][] exclusionPatterns = ((PackageFragmentRoot)getPackageFragmentRoot()).fullExclusionPatternChars();
+//             IResource[] members = ((IContainer) resource).members();
+//             for (int i = 0, max = members.length; i < max; i++) {
+//                     IResource child = members[i];
+//                     if (child.getType() != IResource.FOLDER
+//                                     && !Util.isExcluded(child, exclusionPatterns)) {
+//                             String extension = child.getProjectRelativePath().getFileExtension();
+//                             if (extension != null) {
+//                                     if (extension.equalsIgnoreCase(extType)) {
+//                                             IJavaElement childElement;
+////                                           if (kind == IPackageFragmentRoot.K_SOURCE && Util.isValidCompilationUnitName(child.getName())) {
+////                                                   childElement = getCompilationUnit(child.getName());
+////                                                   vChildren.add(childElement);
+////                                           } else if (Util.isValidClassFileName(child.getName())) {
+////                                                   childElement = getClassFile(child.getName());
+////                                                   vChildren.add(childElement);
+////                                           }
+//                                     }
+//                             }
+//                     }
+//             }
+//     } catch (CoreException e) {
+//             throw new JavaModelException(e);
+//     }
+//     IJavaElement[] children = new IJavaElement[vChildren.size()];
+//     vChildren.toArray(children);
+//     info.setChildren(children);
+//     return true;
+//}
 /**
  * Returns true if this fragment contains at least one java resource.
  * Returns false otherwise.
@@ -129,7 +191,7 @@ public ICompilationUnit createCompilationUnit(String name, String contents, bool
 /**
  * @see JavaElement
  */
-protected OpenableElementInfo createElementInfo() {
+protected Object createElementInfo() {
        return new PackageFragmentInfo();
 }
 /**
@@ -139,13 +201,13 @@ protected OpenableElementInfo createElementInfo() {
 //     IJavaElement[] elements = new IJavaElement[] {this};
 //     getJavaModel().delete(elements, force, monitor);
 //}
-/**
- * @see Openable
- */
-protected boolean generateInfos(OpenableElementInfo info, IProgressMonitor pm, Map newElements, IResource underlyingResource) throws JavaModelException {
-       
-       return computeChildren(info, underlyingResource);
-}
+///**
+// * @see Openable
+// */
+//protected boolean generateInfos(OpenableElementInfo info, IProgressMonitor pm, Map newElements, IResource underlyingResource) throws JavaModelException {
+//     
+//     return computeChildren(info, underlyingResource);
+//}
 ///**
 // * @see IPackageFragment#getClassFile(String)
 // */
@@ -171,9 +233,13 @@ protected boolean generateInfos(OpenableElementInfo info, IProgressMonitor pm, M
 //}
 /**
  * @see IPackageFragment#getCompilationUnit(String)
+ * @exception IllegalArgumentExcpetion if the name does not end with ".java"
  */
-public ICompilationUnit getCompilationUnit(String name) {
-       return new CompilationUnit(this, name);
+public ICompilationUnit getCompilationUnit(String cuName) {
+       if (!  PHPFileUtil.isPHPFileName(cuName)) {
+               throw new IllegalArgumentException(Util.bind("convention.unit.notJavaName")); //$NON-NLS-1$
+       }
+       return new CompilationUnit(this, cuName, DefaultWorkingCopyOwner.PRIMARY);
 }
 /**
  * @see IPackageFragment#getCompilationUnits()
@@ -189,6 +255,12 @@ public ICompilationUnit[] getCompilationUnits() throws JavaModelException {
        return array;
 }
 /**
+ * @see IJavaElement
+ */
+public int getElementType() {
+       return PACKAGE_FRAGMENT;
+}
+/**
  * @see JavaElement#getHandleMementoDelimiter()
  */
 protected char getHandleMementoDelimiter() {
@@ -242,7 +314,7 @@ public IResource getResource() {
  * @see IJavaElement#getUnderlyingResource()
  */
 public IResource getUnderlyingResource() throws JavaModelException {
-       IResource rootResource = fParent.getUnderlyingResource();
+       IResource rootResource = parent.getUnderlyingResource();
        if (rootResource == null) {
                //jar package fragment root that has no associated resource
                return null;
@@ -333,15 +405,15 @@ public boolean isDefaultPackage() {
 //     String[] renamings= new String[] {name};
 //     getJavaModel().rename(elements, dests, renamings, force, monitor);
 //}
-/*
- * @see JavaElement#rootedAt(IJavaProject)
- */
-public IJavaElement rootedAt(IJavaProject project) {
-       return
-               new PackageFragment(
-                       (IPackageFragmentRoot)((JavaElement)fParent).rootedAt(project), 
-                       fName);
-}
+///*
+// * @see JavaElement#rootedAt(IJavaProject)
+// */
+//public IJavaElement rootedAt(IJavaProject project) {
+//     return
+//             new PackageFragment(
+//                     (IPackageFragmentRoot)((JavaElement)parent).rootedAt(project), 
+//                     name);
+//}
 /**
  * Debugging purposes
  */