/**
  * Possible failures:
  *
  * <ul>
  *   <li>NO_ELEMENTS_TO_PROCESS - the root supplied to the operation is <code>null</code>.
  *   <li>INVALID_NAME - the name provided to the operation is <code>null</code> or is not a valid
  *       script folder name.
  *   <li>READ_ONLY - the root provided to this operation is read only.
  *   <li>NAME_COLLISION - there is a pre-existing resource (file) with the same name as a folder
  *       in the script folder's hierarchy.
  *   <li>ELEMENT_NOT_PRESENT - the underlying resource for the root is missing
  * </ul>
  *
  * @see IScriptModelStatus
  * @see ScriptConventions
  */
 @Override
 public IModelStatus verify() {
   if (getParentElement() == null) {
     return new ModelStatus(IModelStatusConstants.NO_ELEMENTS_TO_PROCESS);
   }
   IPath packageName = this.pkgName == null ? null : this.pkgName.append("."); // $NON-NLS-1$
   String packageNameValue = null;
   if (packageName != null) {
     packageNameValue = packageName.toOSString();
   }
   if (this.pkgName == null
       || (this.pkgName.segmentCount() > 0
           && !Util.isValidFolderNameForPackage(
               (IContainer) getParentElement().getResource(), packageName.toString()))) {
     return new ModelStatus(IModelStatusConstants.INVALID_NAME, packageNameValue);
   }
   IProjectFragment root = (IProjectFragment) getParentElement();
   if (root.isReadOnly()) {
     return new ModelStatus(IModelStatusConstants.READ_ONLY, root);
   }
   IContainer parentFolder = (IContainer) root.getResource();
   int i;
   for (i = 0; i < this.pkgName.segmentCount(); i++) {
     IResource subFolder = parentFolder.findMember(this.pkgName.segment(i));
     if (subFolder != null) {
       if (subFolder.getType() != IResource.FOLDER) {
         return new ModelStatus(
             IModelStatusConstants.NAME_COLLISION,
             Messages.bind(Messages.status_nameCollision, subFolder.getFullPath().toString()));
       }
       parentFolder = (IContainer) subFolder;
     }
   }
   return ModelStatus.VERIFIED_OK;
 }
 public Object getHierarchicalPackageParent(final IScriptFolder child) {
   String name = child.getElementName();
   IProjectFragment parent = (IProjectFragment) child.getParent();
   int index = name.lastIndexOf(IScriptFolder.PACKAGE_DELIMITER);
   if (index != -1) {
     String realParentName = name.substring(0, index);
     IScriptFolder element = parent.getScriptFolder(realParentName);
     if (element != null && element.exists()) {
       try {
         if (fFoldPackages
             && ScriptExplorerContentProvider.isEmpty(element)
             && ScriptExplorerContentProvider.findSinglePackageChild(element, parent.getChildren())
                 != null) {
           return getHierarchicalPackageParent(element);
         }
       } catch (ModelException e) {
         // ignore
       }
       return element;
     } else if (element != null) { // bug 65240
       IResource resource = element.getResource();
       if (resource != null) {
         return resource;
       }
     }
   }
   if (parent.getResource() instanceof IProject) {
     return parent.getScriptProject();
   }
   return parent;
 }
 /**
  * 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();
 }
Пример #4
0
  /**
   * Add an element to the script search scope.
   *
   * @param element The element we want to add to current script search scope
   * @throws ModelException May happen if some Script Model info are not available
   */
  public void add(IModelElement element) throws ModelException {
    if (!natureFilter(element)) {
      return;
    }
    IPath containerPath = null;
    String containerPathToString = null;
    int includeMask = SOURCES | APPLICATION_LIBRARIES | SYSTEM_LIBRARIES;
    switch (element.getElementType()) {
      case IModelElement.SCRIPT_MODEL:
        // a workspace scope should be used
        break;
      case IModelElement.SCRIPT_PROJECT:
        add((ScriptProject) element, null, includeMask, new HashSet<IProject>(2), null);
        break;
      case IModelElement.PROJECT_FRAGMENT:
        IProjectFragment root = (IProjectFragment) element;
        String projectPath = null;
        if (!root.isExternal()) {
          IPath rootPath = root.getPath();
          containerPath =
              root.getKind() == IProjectFragment.K_SOURCE ? root.getParent().getPath() : rootPath;
          containerPathToString = containerPath.toString();
          IResource rootResource = root.getResource();
          projectPath = root.getScriptProject().getPath().toString();
          if (rootResource != null && rootResource.isAccessible()) {
            String relativePath =
                Util.relativePath(rootResource.getFullPath(), containerPath.segmentCount());
            add(projectPath, relativePath, containerPathToString, false /* not a package */, null);
          } else {
            add(
                projectPath,
                org.eclipse.dltk.compiler.util.Util.EMPTY_STRING,
                containerPathToString,
                false /* not a package */,
                null);
          }
        } else {
          projectPath = root.getScriptProject().getPath().toString();
          containerPath = root.getPath();
          containerPathToString = containerPath.toString();
          add(
              projectPath,
              org.eclipse.dltk.compiler.util.Util.EMPTY_STRING,
              containerPathToString,
              false /* not a package */,
              null);
        }
        break;
      case IModelElement.SCRIPT_FOLDER:
        root = (IProjectFragment) element.getParent();
        projectPath = root.getScriptProject().getPath().toString();
        if (root.isArchive()) {
          if (DLTKCore.DEBUG) {
            System.err.println("TODO: Check. Bug possible..."); // $NON-NLS-1$
          }
          String relativePath = ((ModelElement) element).getPath().toString() + '/';
          containerPath = root.getPath();
          containerPathToString = containerPath.toString();
          add(projectPath, relativePath, containerPathToString, true /* package */, null);
        } else {
          IResource resource = element.getResource();
          if (resource != null) {
            if (resource.isAccessible()) {
              containerPath =
                  root.getKind() == IProjectFragment.K_SOURCE
                      ? root.getParent().getPath()
                      : root.getPath();
            } else {
              // for working copies, get resource container full path
              containerPath = resource.getParent().getFullPath();
            }
            containerPathToString = containerPath.toString();
            String relativePath =
                Util.relativePath(resource.getFullPath(), containerPath.segmentCount());
            add(projectPath, relativePath, containerPathToString, true /* package */, null);
          }
        }
        break;
      default:
        // remember sub-cu (or sub-class file) script elements
        if (element instanceof IMember) {
          if (this.elements == null) {
            this.elements = new ArrayList<IModelElement>();
          }
          this.elements.add(element);
        }
        root = (IProjectFragment) element.getAncestor(IModelElement.PROJECT_FRAGMENT);
        projectPath = root.getScriptProject().getPath().toString();
        String relativePath;
        if (root.getKind() == IProjectFragment.K_SOURCE && !root.isExternal()) {
          containerPath = root.getParent().getPath();
          relativePath = Util.relativePath(getPath(element, false /* full path */), 1 /*
																				 * remove
																				 * project
																				 * segmet
																				 */);
        } else {
          containerPath = root.getPath();
          relativePath = getPath(element, true /* relative path */).toString();
        }
        containerPathToString = containerPath.toString();
        add(projectPath, relativePath, containerPathToString, false /*
																		 * not a
																		 * package
																		 */, null);
    }

    if (containerPath != null) addEnclosingProjectOrArchive(containerPath);
  }