protected Object internalGetParent(final Object element) {
    if (!fIsFlatLayout && element instanceof IScriptFolder) {
      return getHierarchicalPackageParent((IScriptFolder) element);
    } else if (element instanceof IProjectFragment) {
      // since we insert logical package containers we have to fix
      // up the parent for package fragment roots so that they refer
      // to the container and containers refer to the project
      IProjectFragment root = (IProjectFragment) element;

      try {
        IBuildpathEntry entry = root.getRawBuildpathEntry();
        if (entry != null) {
          int entryKind = entry.getEntryKind();
          if (entryKind == IBuildpathEntry.BPE_CONTAINER) {
            return new BuildPathContainer(root.getScriptProject(), entry);
          } else if (fShowLibrariesNode && (entryKind == IBuildpathEntry.BPE_LIBRARY /*
																		 * ||
																		 * entryKind
																		 * ==
																		 * IBuildpathEntry
																		 * .
																		 * BPE_VARIABLE
																		 */)) {
            return new LibraryContainer(root.getScriptProject());
          }
        }
      } catch (ModelException e) {
        // fall through
      }
    } else if (element instanceof ProjectFragmentContainer) {
      return ((ProjectFragmentContainer) element).getScriptProject();
    }
    return super.internalGetParent(element);
  }
 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;
 }
  /**
   * 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);
  }