protected IFile getManifest(IPackageFragmentRoot[] roots, IJavaProject javaProject)
      throws CoreException {

    IFolder metaFolder = null;
    for (IPackageFragmentRoot root : roots) {
      if (!root.isArchive() && !root.isExternal()) {
        IResource resource = root.getResource();
        metaFolder = getMetaFolder(resource);
        if (metaFolder != null) {
          break;
        }
      }
    }

    // Otherwise look for manifest file in the java project:
    if (metaFolder == null) {
      metaFolder = getMetaFolder(javaProject.getProject());
    }

    if (metaFolder != null) {
      IResource[] members = metaFolder.members();
      if (members != null) {
        for (IResource mem : members) {
          if (MANIFEST_FILE.equals(mem.getName().toUpperCase()) && mem instanceof IFile) {
            return (IFile) mem;
          }
        }
      }
    }

    return null;
  }
  private static Change createPackageFragmentRootDeleteChange(IPackageFragmentRoot root)
      throws JavaModelException {
    IResource resource = root.getResource();
    if (resource != null && resource.isLinked()) {
      // XXX using this code is a workaround for jcore bug 31998
      // jcore cannot handle linked stuff
      // normally, we should always create DeletePackageFragmentRootChange
      CompositeChange composite =
          new DynamicValidationStateChange(
              RefactoringCoreMessages.DeleteRefactoring_delete_package_fragment_root);

      ClasspathChange change =
          ClasspathChange.removeEntryChange(root.getJavaProject(), root.getRawClasspathEntry());
      if (change != null) {
        composite.add(change);
      }
      Assert.isTrue(!Checks.isClasspathDelete(root)); // checked in preconditions
      composite.add(createDeleteChange(resource));

      return composite;
    } else {
      Assert.isTrue(!root.isExternal());
      // TODO remove the query argument
      return new DeletePackageFragmentRootChange(root, true, null);
    }
  }
 public void initFromSelection() {
   IJavaElement je = getSelectedJavaElement(selection);
   if (je instanceof IJavaProject) {
     IJavaProject jp = (IJavaProject) je;
     if (jp.isOpen()) {
       // default to the first source dir
       // we find in the selected project
       try {
         for (IPackageFragmentRoot pfr : jp.getAllPackageFragmentRoots()) {
           if (!pfr.isExternal() && !pfr.isArchive()) {
             je = pfr;
             break;
           }
         }
       } catch (JavaModelException e) {
       }
     }
   }
   if (je instanceof IPackageFragmentRoot) {
     sourceDir = (IPackageFragmentRoot) je;
     packageFragment = sourceDir.getPackageFragment("");
     packageName = packageFragment.getElementName();
   } else if (je instanceof IPackageFragment) {
     packageFragment = (IPackageFragment) je;
     packageName = packageFragment.getElementName();
     sourceDir = (IPackageFragmentRoot) packageFragment.getAncestor(PACKAGE_FRAGMENT_ROOT);
   }
 }
 private PackageFragmentRootData getData(IPackageFragmentRoot root) {
   final boolean isCachable = root.isArchive() || root.isExternal();
   if (isCachable) {
     return getCachedData(root);
   }
   PackageFragmentRootData data = initializeData(root);
   return data;
 }
 /** @since 2.4 */
 @Override
 public Pair<URI, URI> getURIMapping(IPackageFragmentRoot root) throws JavaModelException {
   PackageFragmentRootData data = getData(root);
   if (data.uriPrefix == null) return null;
   IPath path = root.isExternal() ? root.getPath() : root.getUnderlyingResource().getLocation();
   URI physical = null;
   if (root.isArchive()) {
     String archiveScheme =
         "zip".equalsIgnoreCase(root.getPath().getFileExtension()) ? "zip" : "jar";
     physical = URI.createURI(archiveScheme + ":file:" + path.toFile().getPath() + "!/");
   } else {
     physical = URI.createFileURI(path.toFile().getPath() + "/");
   }
   return Tuples.create(data.uriPrefix, physical);
 }
 /** @since 2.4 */
 private void updateCache(IJavaProject project) {
   Set<PackageFragmentRootData> datas = newHashSet();
   try {
     if (project.exists() && project.getProject().isAccessible()) {
       for (IPackageFragmentRoot root : project.getPackageFragmentRoots()) {
         boolean isCachable = root.isArchive() || root.isExternal();
         if (isCachable) datas.add(getCachedData(root));
       }
     }
   } catch (JavaModelException e) {
     if (!e.isDoesNotExist())
       log.error("Error getting package fragments roots of " + project.getElementName(), e);
   } finally {
     clearCache(project, datas);
   }
 }
 /** @since 2.5 */
 @Override
 public URI getUri(/* @NonNull */ IStorage storage) {
   if (storage instanceof IJarEntryResource) {
     final IJarEntryResource casted = (IJarEntryResource) storage;
     IPackageFragmentRoot packageFragmentRoot = casted.getPackageFragmentRoot();
     Map<URI, IStorage> data = getAllEntries(packageFragmentRoot);
     for (Map.Entry<URI, IStorage> entry : data.entrySet()) {
       if (entry.getValue().equals(casted)) return entry.getKey();
     }
     if (packageFragmentRoot.exists() && packageFragmentRoot.isArchive()) {
       IPath jarPath = packageFragmentRoot.getPath();
       URI jarURI;
       if (packageFragmentRoot.isExternal()) {
         jarURI = URI.createFileURI(jarPath.toOSString());
       } else {
         jarURI = URI.createPlatformResourceURI(jarPath.toString(), true);
       }
       URI result = URI.createURI("archive:" + jarURI + "!" + storage.getFullPath());
       return result;
     }
   }
   return null;
 }