Пример #1
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);
  }