Example usage for org.eclipse.jdt.core IPackageFragmentRoot getResource

List of usage examples for org.eclipse.jdt.core IPackageFragmentRoot getResource

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IPackageFragmentRoot getResource.

Prototype

IResource getResource();

Source Link

Document

Returns the innermost resource enclosing this element.

Usage

From source file:org.eclipse.jpt.jpa.ui.internal.wizards.entity.EntityClassWizardPage.java

License:Open Source License

@Override
protected ISelectionStatusValidator getContainerDialogSelectionValidator() {
    return new ISelectionStatusValidator() {
        public IStatus validate(Object[] selection) {
            if (selection != null && selection[0] != null) {
                if (selection[0] instanceof IProject) {
                    IProject project = (IProject) selection[0];
                    IJavaProject javaProject = JavaCore.create(project);
                    for (IPackageFragmentRoot root : JavaProjectTools.getSourceFolders(javaProject)) {
                        if (project.equals(root.getResource())) {
                            return WTPCommonPlugin.OK_STATUS;
                        }// ww w  .  ja  v  a2 s.co m
                    }
                } else {
                    return WTPCommonPlugin.OK_STATUS;
                }
            }
            return WTPCommonPlugin.createErrorStatus(J2EEUIMessages.CONTAINER_SELECTION_DIALOG_VALIDATOR_MESG);
        }
    };
}

From source file:org.eclipse.jpt.jpa.ui.internal.wizards.entity.EntityClassWizardPage.java

License:Open Source License

@Override
protected ViewerFilter getContainerDialogViewerFilter() {
    return new ViewerFilter() {
        @Override//from w ww.  j a va  2s .  c om
        public boolean select(Viewer viewer, Object parent, Object element) {
            String projectName = (String) model.getProperty(IEntityDataModelProperties.PROJECT_NAME);
            if (element instanceof IProject) {
                IProject project = (IProject) element;
                return project.getName().equals(projectName);
            } else if (element instanceof IFolder) {
                IFolder folder = (IFolder) element;
                // only show source folders
                IProject project = ProjectUtilities.getProject(projectName);
                IJavaProject javaProject = JavaCore.create(project);
                for (IPackageFragmentRoot root : JavaProjectTools.getSourceFolders(javaProject)) {
                    if (folder.equals(root.getResource()))
                        return true;
                }
            }
            return false;
        }
    };
}

From source file:org.eclipse.jst.j2ee.internal.archive.ConnectorComponentArchiveLoadAdapter.java

License:Open Source License

private IArchiveResource getNestedJar(IPackageFragmentRoot sourceRoot)
        throws JavaModelException, ArchiveOpenFailureException {
    IPath outputPath = sourceRoot.getRawClasspathEntry().getOutputLocation();
    if (outputPath == null) {
        IProject project = vComponent.getProject();
        try {//from w  ww  .j  a  va 2  s. c  o  m
            if (project.hasNature(JavaCore.NATURE_ID)) {
                IJavaProject javaProject = JavaCore.create(project);
                outputPath = javaProject.getOutputLocation();
            }
        } catch (CoreException e) {
            J2EEPlugin.logError(e);
        }
        if (outputPath == null) {
            return null;
        }
    }

    IFolder javaOutputFolder = ResourcesPlugin.getWorkspace().getRoot().getFolder(outputPath);
    indexClassesForOutputFolder(javaOutputFolder);
    IContainer sourceContainer = (IContainer) sourceRoot.getResource();

    int sourceContainerSegmentCount = sourceContainer.getProjectRelativePath().segmentCount();
    boolean isModuleRoot = knownDD.getProjectRelativePath().toString()
            .startsWith(sourceContainer.getProjectRelativePath().toString());
    Set iFilesSet = new HashSet();
    boolean foundJava = gatherFilesForJAR(iFilesSet, sourceContainer, isModuleRoot, false,
            sourceContainerSegmentCount);
    if (!isModuleRoot || foundJava) {
        List<IFile> iFilesList = Collections.list(Collections.enumeration(iFilesSet));
        for (int i = 0; i < iFilesList.size(); i++) {
            filesHolder.removeIFile(iFilesList.get(i));
        }
        IArchiveResource nestedArchive = createNestedArchive(iFilesList, sourceContainer, javaOutputFolder);
        return nestedArchive;
    }
    return null;
}

From source file:org.eclipse.jst.j2ee.internal.jca.archive.operations.ConnectorComponentLoadStrategyImpl.java

License:Open Source License

private File getNestedJar(IPackageFragmentRoot sourceRoot) throws JavaModelException {
    IPath outputPath = sourceRoot.getRawClasspathEntry().getOutputLocation();
    if (outputPath == null) {
        IProject project = vComponent.getProject();
        try {//from w w  w .ja v  a2  s  .c o m
            if (project.hasNature(JavaCore.NATURE_ID)) {
                IJavaProject javaProject = JavaCore.create(project);
                outputPath = javaProject.getOutputLocation();
            }
        } catch (CoreException e) {
            Logger.getLogger().logError(e);
        }
        if (outputPath == null) {
            return null;
        }
    }

    IFolder javaOutputFolder = ResourcesPlugin.getWorkspace().getRoot().getFolder(outputPath);
    indexClassesForOutputFolder(javaOutputFolder);
    IContainer sourceContainer = (IContainer) sourceRoot.getResource();

    int sourceContainerSegmentCount = sourceContainer.getProjectRelativePath().segmentCount();
    boolean isModuleRoot = knownDD.getProjectRelativePath().toString()
            .startsWith(sourceContainer.getProjectRelativePath().toString());
    Set iFilesSet = new HashSet();
    boolean foundJava = gatherFilesForJAR(iFilesSet, sourceContainer, isModuleRoot, false,
            sourceContainerSegmentCount);
    if (!isModuleRoot || foundJava) {
        List iFilesList = Collections.list(Collections.enumeration(iFilesSet));
        for (int i = 0; i < iFilesList.size(); i++) {
            filesHolder.removeIFile((IFile) iFilesList.get(i));
        }
        File nestedArchive = createNestedArchive(iFilesList, sourceContainer, javaOutputFolder);
        return nestedArchive;
    }
    return null;
}

From source file:org.eclipse.jst.jsf.common.ui.internal.utils.JavaModelUtil.java

License:Open Source License

/**
 * Returns <code>true</code> if the given package fragment root is
 * referenced. This means it is own by a different project but is referenced
 * by the root's parent. Returns <code>false</code> if the given root
 * doesn't have an underlying resource.//from   ww w  .  j  av  a2  s .c o  m
 * @param root 
 * @return true if root is referenced
 */
public static boolean isReferenced(IPackageFragmentRoot root) {
    IResource resource = root.getResource();
    if (resource != null) {
        IProject jarProject = resource.getProject();
        IProject container = root.getJavaProject().getProject();
        return !container.equals(jarProject);
    }
    return false;
}

From source file:org.eclipse.modisco.java.discoverer.internal.io.library.ManifestReader.java

License:Open Source License

/**
 * Extract Manifest information./*from ww w.j  a  v a 2s. c  o m*/
 */
public static void completeArchiveWithManifest(final IPackageFragmentRoot physicalArchive,
        final Archive modelArchive, final JavaFactory factory) {
    try {
        File jarFile = null;
        if (physicalArchive.isExternal()) {
            jarFile = new File(physicalArchive.getPath().toOSString());
        } else {
            jarFile = new File(physicalArchive.getResource().getRawLocation().toOSString());
        }
        java.util.jar.JarFile jar = new java.util.jar.JarFile(jarFile);

        java.util.jar.Manifest manifest = jar.getManifest();
        if (manifest != null) {
            Manifest modelManifest = factory.createManifest();
            modelArchive.setManifest(modelManifest);

            java.util.jar.Attributes mainAttrs = manifest.getMainAttributes();
            modelManifest.getMainAttributes().addAll(readAttributes(mainAttrs, factory));

            for (Map.Entry<String, java.util.jar.Attributes> entry : manifest.getEntries().entrySet()) {
                ManifestEntry modelEntry = factory.createManifestEntry();
                modelEntry.setName(entry.getKey());
                modelEntry.getAttributes().addAll(readAttributes(entry.getValue(), factory));
                modelManifest.getEntryAttributes().add(modelEntry);
            }
        }

    } catch (IOException e) {
        MoDiscoLogger.logError(e, JavaActivator.getDefault());
    }
}

From source file:org.eclipse.pde.api.tools.internal.model.ProjectComponent.java

License:Open Source License

/**
 * Returns the cached API type container for the given package fragment
 * root, or <code>null</code> if none. The given package fragment has to be
 * a SOURCE package fragment - this method is only used by the project API
 * description to obtain a class file corresponding to a compilation unit
 * when tag scanning (to resolve signatures).
 * //from   w w w . ja va 2s  .  c o m
 * @param root source package fragment root
 * @return API type container associated with the package fragment root, or
 *         <code>null</code> if none
 */
public IApiTypeContainer getTypeContainer(IPackageFragmentRoot root) throws CoreException {
    if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
        getApiTypeContainers(); // ensure initialized
        IResource resource = root.getResource();
        if (resource != null) {
            String location = resource.getProjectRelativePath().toString();
            return getApiTypeContainer(location, this);
        }
    }
    return null;
}

From source file:org.eclipse.recommenders.jdt.JavaElementsFinder.java

License:Open Source License

/**
 * Returns the compilation unit's absolute location on the local hard drive - if it exists.
 *///from   w w  w  . j a  v a  2  s .  c  om
public static Optional<File> findLocation(@Nullable IPackageFragmentRoot root) {
    if (root == null) {
        return absent();
    }
    File res = null;

    final IResource resource = root.getResource();
    if (resource != null) {
        if (resource.getLocation() == null) {
            res = resource.getRawLocation().toFile().getAbsoluteFile();
        } else {
            res = resource.getLocation().toFile().getAbsoluteFile();
        }
    }
    if (root.isExternal()) {
        res = root.getPath().toFile().getAbsoluteFile();
    }

    // if the file (for whatever reasons) does not exist return absent().
    if (res != null && !res.exists()) {
        return absent();
    }
    return fromNullable(res);
}

From source file:org.eclipse.recommenders.rcp.utils.JdtUtils.java

License:Open Source License

public static Optional<File> getLocation(@Nullable final IPackageFragmentRoot packageRoot) {
    if (packageRoot == null) {
        return absent();
    }/* w  w w  .  ja  va 2 s . c  om*/
    File res = null;
    final IResource resource = packageRoot.getResource();
    if (resource != null) {
        if (resource.getLocation() == null) {
            res = resource.getRawLocation().toFile().getAbsoluteFile();
        } else {
            res = resource.getLocation().toFile().getAbsoluteFile();
        }
    }
    if (packageRoot.isExternal()) {
        res = packageRoot.getPath().toFile().getAbsoluteFile();
    }

    // if the file (for whatever reasons) does not exist, skip it.
    if (res != null && !res.exists()) {
        res = null;
    }
    return Optional.fromNullable(res);
}

From source file:org.eclipse.scout.sdk.saml.importer.operation.AbstractSamlElementImportOperation.java

License:Open Source License

protected void deleteClass(IScoutBundle bundle, String pckName, String className) throws CoreException {
    String fileName = className + ".java";
    for (IPackageFragmentRoot r : bundle.getJavaProject().getPackageFragmentRoots()) {
        if (!r.isArchive() && !r.isReadOnly() && !r.isExternal()) {
            IFolder fld = (IFolder) r.getResource();
            if (fld.exists()) {
                IFolder packageFolder = fld.getFolder(new Path(pckName.replace('.', '/')));
                if (packageFolder.exists()) {
                    IFile javaFile = packageFolder.getFile(fileName);
                    if (javaFile.exists()) {
                        javaFile.delete(true, false, getSamlContext().getMonitor());
                        break;
                    }//from ww w .ja va  2  s. c o  m
                }
            }
        }
    }
}