/**
  * Execute the operation - creates the new script folder and any side effect folders.
  *
  * @exception ModelException if the operation is unable to complete
  */
 @Override
 protected void executeOperation() throws ModelException {
   ModelElementDelta delta = null;
   IProjectFragment root = (IProjectFragment) getParentElement();
   beginTask(Messages.operation_createScriptFolderProgress, this.pkgName.segmentCount());
   IContainer parentFolder = (IContainer) root.getResource();
   IPath sideEffectPackageName = Path.EMPTY;
   ArrayList<IScriptFolder> results = new ArrayList<IScriptFolder>(this.pkgName.segmentCount());
   int i;
   for (i = 0; i < this.pkgName.segmentCount(); i++) {
     String subFolderName = this.pkgName.segment(i);
     sideEffectPackageName = sideEffectPackageName.append(subFolderName);
     IResource subFolder = parentFolder.findMember(subFolderName);
     if (subFolder == null) {
       createFolder(parentFolder, subFolderName, force);
       parentFolder = parentFolder.getFolder(new Path(subFolderName));
       IScriptFolder addedFrag = root.getScriptFolder(sideEffectPackageName);
       if (!Util.isExcluded(parentFolder, root)) {
         if (delta == null) {
           delta = newModelElementDelta();
         }
         delta.added(addedFrag);
       }
       results.add(addedFrag);
     } else {
       parentFolder = (IContainer) subFolder;
     }
     worked(1);
   }
   if (results.size() > 0) {
     this.resultElements = new IModelElement[results.size()];
     results.toArray(this.resultElements);
     if (delta != null) {
       addDelta(delta);
     }
   }
   done();
 }
  public static Object[] computeFolderForeignResources(
      ScriptProject project,
      IContainer folder,
      char[][] inclusionPatterns,
      char[][] exclusionPatterns)
      throws ModelException {
    Object[] nonScriptResources = new IResource[5];
    int nonScriptResourcesCounter = 0;
    try {
      IBuildpathEntry[] classpath = project.getResolvedBuildpath();
      IResource[] members = folder.members();
      nextResource:
      for (int i = 0, max = members.length; i < max; i++) {
        IResource member = members[i];
        switch (member.getType()) {
          case IResource.FILE:
            String fileName = member.getName();

            // ignore .java files that are not excluded
            if (Util.isValidSourceModule(project, member)
                && !Util.isExcluded(member, inclusionPatterns, exclusionPatterns))
              continue nextResource;
            // ignore .zip file on buildpath
            if (org.eclipse.dltk.compiler.util.Util.isArchiveFileName(
                    DLTKLanguageManager.getLanguageToolkit(project), fileName)
                && isBuildpathEntry(member.getFullPath(), classpath)) continue nextResource;
            // All other resources should be in folders.
            // continue nextResource;
            break;

          case IResource.FOLDER:
            // ignore valid packages or excluded folders that correspond
            // to a nested pkg fragment root
            if (Util.isValidFolderNameForPackage(folder, member.getName())
                && (!Util.isExcluded(member, inclusionPatterns, exclusionPatterns)
                    || isBuildpathEntry(member.getFullPath(), classpath))) continue nextResource;
            break;
        }
        if (nonScriptResources.length == nonScriptResourcesCounter) {
          // resize
          System.arraycopy(
              nonScriptResources,
              0,
              (nonScriptResources = new IResource[nonScriptResourcesCounter * 2]),
              0,
              nonScriptResourcesCounter);
        }
        nonScriptResources[nonScriptResourcesCounter++] = member;
      }
      if (nonScriptResources.length != nonScriptResourcesCounter) {
        System.arraycopy(
            nonScriptResources,
            0,
            (nonScriptResources = new IResource[nonScriptResourcesCounter]),
            0,
            nonScriptResourcesCounter);
      }
      return nonScriptResources;
    } catch (CoreException e) {
      throw new ModelException(e);
    }
  }
 /**
  * Returns the package fragment root corresponding to a given resource path.
  *
  * @param resourcePathString path of expected package fragment root.
  * @return the {@link IProjectFragment package fragment root} which path match the given one or
  *     <code>null</code> if none was found.
  */
 public IProjectFragment projectFragment(String resourcePathString) {
   int index = -1;
   int separatorIndex = resourcePathString.indexOf(FILE_ENTRY_SEPARATOR);
   boolean isZIPFile = separatorIndex != -1;
   boolean isSpecial = resourcePathString.startsWith(IBuildpathEntry.BUILDPATH_SPECIAL);
   if (isZIPFile) {
     // internal or external jar (case 3, 4, or 5)
     String zipPath = resourcePathString.substring(0, separatorIndex);
     String relativePath = resourcePathString.substring(separatorIndex + 1);
     index = indexOf(zipPath, relativePath);
   } else {
     // resource in workspace (case 1 or 2)
     index = indexOf(resourcePathString);
   }
   if (index >= 0) {
     int idx = projectIndexes[index];
     String projectPath = idx == -1 ? null : (String) this.projectPaths.get(idx);
     if (projectPath != null) {
       IScriptProject project =
           DLTKCore.create(ResourcesPlugin.getWorkspace().getRoot().getProject(projectPath));
       if (isZIPFile) {
         return project.getProjectFragment(this.containerPaths[index]);
       }
       if (isSpecial) {
         return project.getProjectFragment(this.containerPaths[index]);
       }
       Object target =
           Model.getTarget(
               ResourcesPlugin.getWorkspace().getRoot(),
               Path.fromPortableString(
                   this.containerPaths[index] + '/' + this.relativePaths[index]),
               false);
       if (target instanceof IProject) {
         return project.getProjectFragment((IProject) target);
       }
       if (target instanceof IResource) {
         IModelElement element = DLTKCore.create((IResource) target);
         return (IProjectFragment) element.getAncestor(IModelElement.PROJECT_FRAGMENT);
       }
       if (target instanceof IFileHandle) {
         try {
           IProjectFragment[] fragments = project.getProjectFragments();
           IFileHandle t = (IFileHandle) target;
           IPath absPath = t.getFullPath();
           for (int i = 0; i < fragments.length; ++i) {
             IProjectFragment f = fragments[i];
             if (f.isExternal()) {
               IPath pPath = f.getPath();
               if (pPath.isPrefixOf(absPath) && !Util.isExcluded(absPath, f, t.isDirectory())) {
                 return f;
               }
             }
           }
         } catch (ModelException e) {
           e.printStackTrace();
           return null;
         }
       }
     }
   }
   return null;
 }