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;
 }
 /** Adds all of the openables defined within this package fragment root to the list. */
 private void injectAllOpenablesForPackageFragmentRoot(
     IProjectFragment root, ArrayList openables) {
   try {
     IModelElement[] packFrags = root.getChildren();
     for (int i = 0; i < packFrags.length; i++) {
       IScriptFolder packFrag = (IScriptFolder) packFrags[i];
       injectAllOpenablesForPackageFragment(packFrag, openables);
     }
   } catch (ModelException e) {
     return;
   }
 }
  /**
   * Returns the hierarchical packages inside a given fragment or root.
   *
   * @param parent The parent package fragment root
   * @param fragment The package to get the children for or 'null' to get the children of the root.
   * @param result Collection where the resulting elements are added
   * @throws JavaModelException
   */
  private void getHierarchicalPackageChildren(
      final IProjectFragment parent, final IScriptFolder fragment, final Collection<Object> result)
      throws ModelException {
    IModelElement[] children = parent.getChildren();

    if (fragment == null || fragment.isRootFolder()) {
      List<IModelElement> newElements = new ArrayList<IModelElement>();
      for (int i = 0; i < children.length; ++i) {
        if (children[i] instanceof IScriptFolder) {
          IScriptFolder scriptFolder = (IScriptFolder) children[i];
          if (scriptFolder.isRootFolder()) {
            IModelElement[] members = scriptFolder.getChildren();
            for (int j = 0; j < members.length; ++j) {
              newElements.add(members[j]);
            }
            continue;
          }
        }
        newElements.add(children[i]);
      }
      children = newElements.toArray(new IModelElement[newElements.size()]);
    }

    String prefix =
        fragment != null
            ? fragment.getElementName() + IScriptFolder.PACKAGE_DELIMETER_STR
            : ""; //$NON-NLS-1$
    int prefixLen = prefix.length();
    for (int i = 0; i < children.length; i++) {
      if (children[i] instanceof IScriptFolder) {
        IScriptFolder curr = (IScriptFolder) children[i];
        String name = curr.getElementName();
        if (name.startsWith(prefix)
            && name.length() > prefixLen
            && name.indexOf(IScriptFolder.PACKAGE_DELIMITER, prefixLen) == -1) {
          if (fFoldPackages) {
            curr = ScriptExplorerContentProvider.getFolded(children, curr);
          }
          result.add(curr);
        } /*
           * else if (fragment == null && curr.isRootFolder()) {
           * result.add(curr); }
           */
      } else {
        result.add(children[i]);
      }
    }
  }
  private static boolean hasReadOnlyResourcesAndSubResources(IModelElement modelElement)
      throws CoreException {
    switch (modelElement.getElementType()) {
      case IModelElement.SOURCE_MODULE:
        IResource resource = ReorgUtils.getResource(modelElement);
        return (resource != null && Resources.isReadOnly(resource));
      case IModelElement.SCRIPT_FOLDER:
        IResource packResource = ReorgUtils.getResource(modelElement);
        if (packResource == null) return false;
        IScriptFolder pack = (IScriptFolder) modelElement;
        if (Resources.isReadOnly(packResource)) return true;
        Object[] nonScript = pack.getForeignResources();
        for (int i = 0; i < nonScript.length; i++) {
          Object object = nonScript[i];
          if (object instanceof IResource
              && hasReadOnlyResourcesAndSubResources((IResource) object)) return true;
        }
        return hasReadOnlyResourcesAndSubResources(pack.getChildren());
      case IModelElement.PROJECT_FRAGMENT:
        IProjectFragment root = (IProjectFragment) modelElement;
        if (root.isArchive()) return false;
        IResource pfrResource = ReorgUtils.getResource(modelElement);
        if (pfrResource == null) return false;
        if (Resources.isReadOnly(pfrResource)) return true;
        Object[] nonScript1 = root.getForeignResources();
        for (int i = 0; i < nonScript1.length; i++) {
          Object object = nonScript1[i];
          if (object instanceof IResource
              && hasReadOnlyResourcesAndSubResources((IResource) object)) return true;
        }
        return hasReadOnlyResourcesAndSubResources(root.getChildren());

      case IModelElement.FIELD:
        //			case IModelElement.IMPORT_CONTAINER:
        //			case IModelElement.IMPORT_DECLARATION:
        //			case IModelElement.INITIALIZER:
      case IModelElement.METHOD:
        //			case IModelElement.PACKAGE_DECLARATION:
      case IModelElement.TYPE:
        return false;
      default:
        Assert.isTrue(false); // not handled here
        return false;
    }
  }
 /**
  * Returns the hierarchical packages inside a given folder.
  *
  * @param folder The parent folder
  * @param result Collection where the resulting elements are added
  * @throws CoreException thrown when elements could not be accessed
  */
 private void getHierarchicalPackagesInFolder(
     final IFolder folder, final Collection<Object> result) throws CoreException {
   IResource[] resources = folder.members();
   for (int i = 0; i < resources.length; i++) {
     IResource resource = resources[i];
     if (resource instanceof IFolder) {
       IFolder curr = (IFolder) resource;
       IModelElement element = DLTKCore.create(curr);
       if (element instanceof IScriptFolder) {
         if (fFoldPackages) {
           IScriptFolder fragment = (IScriptFolder) element;
           IProjectFragment root = (IProjectFragment) fragment.getParent();
           element = ScriptExplorerContentProvider.getFolded(root.getChildren(), fragment);
         }
         result.add(element);
       }
     }
   }
 }