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

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

Introduction

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

Prototype

IPath getPath();

Source Link

Document

Returns the path to the innermost resource enclosing this element.

Usage

From source file:org.jboss.tools.arquillian.ui.internal.dialogs.ArquillianResourcesSelectionDialog.java

License:Open Source License

private void getResources() {
    allResources = new ArrayList<IPath>();
    if (javaProject != null && javaProject.isOpen()) {
        IPath testSourcePath = null;// w w w.  j av  a2 s  . c om
        try {
            IProject project = javaProject.getProject();
            if (project.hasNature(IMavenConstants.NATURE_ID)) {
                IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().create(project,
                        new NullProgressMonitor());
                MavenProject mavenProject = facade.getMavenProject(new NullProgressMonitor());
                Build build = mavenProject.getBuild();
                String testSourceDirectory = build.getTestSourceDirectory();
                testSourcePath = Path.fromOSString(testSourceDirectory);
                IPath workspacePath = ResourcesPlugin.getWorkspace().getRoot().getRawLocation();
                testSourcePath = testSourcePath.makeRelativeTo(workspacePath).makeAbsolute();

            }
            IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
            for (IClasspathEntry entry : rawClasspath) {
                if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(entry);
                    if (roots == null) {
                        continue;
                    }
                    for (IPackageFragmentRoot root : roots) {
                        IPath path = root.getPath();
                        String projectLocation = project.getLocation().toOSString();
                        IPath projectPath = Path.fromOSString(projectLocation);
                        IPath workspacePath = ResourcesPlugin.getWorkspace().getRoot().getRawLocation();
                        projectPath = projectPath.makeRelativeTo(workspacePath).makeAbsolute();
                        projectPath = projectPath.removeLastSegments(1);
                        path = projectPath.append(path);
                        if (path != null && path.equals(testSourcePath)) {
                            continue;
                        }

                        Object[] resources = root.getNonJavaResources();
                        for (Object resource : resources) {
                            addResource(allResources, resource, root.getPath());
                        }
                    }
                }
            }
        } catch (Exception e1) {
            ArquillianUIActivator.log(e1);
        }
    }
}

From source file:org.jboss.tools.arquillian.ui.internal.dialogs.ArquillianTypesSelectionDialog.java

License:Open Source License

private void getClasses() {
    allTypes = new ArrayList<IType>();
    if (javaProject != null && javaProject.isOpen()) {
        IPath testSourcePath = null;//w ww . jav a  2 s . c om
        try {
            IProject project = javaProject.getProject();
            if (project.hasNature(IMavenConstants.NATURE_ID)) {
                IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().create(project,
                        new NullProgressMonitor());
                MavenProject mavenProject = facade.getMavenProject(new NullProgressMonitor());
                Build build = mavenProject.getBuild();
                String testSourceDirectory = build.getTestSourceDirectory();
                testSourcePath = Path.fromOSString(testSourceDirectory);
                IPath workspacePath = ResourcesPlugin.getWorkspace().getRoot().getRawLocation();
                testSourcePath = testSourcePath.makeRelativeTo(workspacePath).makeAbsolute();

            }
            IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
            for (IClasspathEntry entry : rawClasspath) {
                if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(entry);
                    if (roots == null) {
                        continue;
                    }
                    for (IPackageFragmentRoot root : roots) {
                        IPath path = root.getPath();
                        String projectLocation = project.getLocation().toOSString();
                        IPath projectPath = Path.fromOSString(projectLocation);
                        IPath workspacePath = ResourcesPlugin.getWorkspace().getRoot().getRawLocation();
                        projectPath = projectPath.makeRelativeTo(workspacePath).makeAbsolute();
                        projectPath = projectPath.removeLastSegments(1);
                        path = projectPath.append(path);
                        if (path != null && path.equals(testSourcePath)) {
                            continue;
                        }

                        IJavaElement[] children = root.getChildren();
                        for (IJavaElement child : children) {
                            if (child instanceof IPackageFragment) {
                                IPackageFragment packageFragment = (IPackageFragment) child;
                                IJavaElement[] elements = packageFragment.getChildren();
                                for (IJavaElement element : elements) {
                                    if (element instanceof ICompilationUnit) {
                                        ICompilationUnit cu = (ICompilationUnit) element;
                                        IType[] types = cu.getTypes();
                                        for (IType type : types) {
                                            if (!addedTypes.contains(type)) {
                                                allTypes.add(type);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e1) {
            ArquillianUIActivator.log(e1);
        }
    }
}

From source file:org.jboss.tools.arquillian.ui.internal.utils.ArquillianUIUtil.java

License:Open Source License

private static IFile getFile(IJavaProject javaProject, String fileName) throws JavaModelException {
    IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
    for (IClasspathEntry entry : rawClasspath) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(entry);
            if (roots == null) {
                continue;
            }// w  w  w. j  a va 2s .com
            for (IPackageFragmentRoot root : roots) {
                Object[] resources = root.getNonJavaResources();
                int segments = root.getPath().segmentCount();
                for (Object resource : resources) {
                    if (resource instanceof IFile) {
                        IFile file = (IFile) resource;
                        IPath filePath = file.getProjectRelativePath();
                        IPath relativePath = filePath.removeFirstSegments(segments - 1);
                        if (fileName.equals(relativePath.toString())) {
                            return file;
                        }
                    }
                }

            }
        }
    }
    return null;
}

From source file:org.jboss.tools.arquillian.ui.internal.utils.ArquillianUIUtil.java

License:Open Source License

private static IFile getNewFile(IJavaProject javaProject, String arquillianProperties) throws CoreException {
    IProject project = javaProject.getProject();
    if (project.hasNature(IMavenConstants.NATURE_ID)) {
        IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().create(project,
                new NullProgressMonitor());
        MavenProject mavenProject = facade.getMavenProject(new NullProgressMonitor());
        Build build = mavenProject.getBuild();
        String testDirectory = null;
        List<Resource> testResources = build.getTestResources();
        if (testResources != null && testResources.size() > 0) {
            testDirectory = testResources.get(0).getDirectory();
        } else {//from  w  ww.  j av  a  2 s . c  o  m
            testDirectory = build.getTestSourceDirectory();
        }
        File testDir = new File(testDirectory);
        if (testDir.isDirectory()) {
            File arquillianFile = new File(testDir, arquillianProperties);
            IPath path = new Path(arquillianFile.getAbsolutePath());
            IFile iFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
            if (!iFile.getParent().exists()) {
                IPath projectPath = javaProject.getProject().getLocation();
                IPath iFilePath = iFile.getLocation();
                if (iFilePath.toString().startsWith(projectPath.toString())) {
                    String s = iFilePath.toString().substring(projectPath.toString().length());
                    path = new Path(s);
                    return javaProject.getProject().getFile(path);
                }
            }
            return iFile;
        }
    }
    IPath path = null;
    IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
    for (IClasspathEntry entry : rawClasspath) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(entry);
            if (roots == null) {
                continue;
            }
            for (IPackageFragmentRoot root : roots) {
                path = root.getPath();
                break;
            }
        }
    }
    if (path == null) {
        throw new CoreException(new Status(IStatus.ERROR, ArquillianUIActivator.PLUGIN_ID, "Invalid project"));
    }
    IFolder folder = javaProject.getProject().getFolder(path);
    if (!folder.exists()) {
        IPath projectPath = javaProject.getPath();
        path = path.makeRelativeTo(projectPath);
        folder = javaProject.getProject().getFolder(path);
    }
    return folder.getFile(arquillianProperties);
}

From source file:org.jboss.tools.as.sourcelookup.ui.actions.AttachSourcesActionDelegate.java

License:Open Source License

@Override
public void setActiveEditor(IAction action, IEditorPart targetEditor) {
    if (targetEditor != null) {
        try {/*from  ww w .jav  a2s. c om*/
            boolean isAuto = SourceLookupActivator.getDefault().isAutoAddSourceContainer();
            if (!isAuto) {
                return;
            }
            IClassFileEditorInput input = (IClassFileEditorInput) targetEditor.getEditorInput();
            IJavaElement element = input.getClassFile();
            while (element.getParent() != null) {
                element = element.getParent();
                if (element instanceof IPackageFragmentRoot) {
                    final IPackageFragmentRoot fragment = (IPackageFragmentRoot) element;
                    IPath attachmentPath = fragment.getSourceAttachmentPath();
                    if (attachmentPath != null && !attachmentPath.isEmpty()
                            && attachmentPath.toFile().exists()) {
                        break;
                    }
                    if (fragment.isArchive()) {
                        IFile iFile = ResourcesPlugin.getWorkspace().getRoot().getFile(fragment.getPath());
                        File file = iFile == null || iFile.getLocation() == null ? fragment.getPath().toFile()
                                : iFile.getLocation().toFile();
                        ZipFile jar = new ZipFile(file);
                        final ArtifactKey artifact = JBossSourceContainer.getArtifact(file, jar);
                        if (artifact != null) {
                            IPath sourcePath = JBossSourceContainer.getSourcePath(artifact);
                            if (sourcePath == null || !sourcePath.toFile().exists()) {
                                Job job = JBossSourceContainer.downloadArtifact(file, artifact);
                                job.addJobChangeListener(new IJobChangeListener() {

                                    @Override
                                    public void sleeping(IJobChangeEvent event) {
                                    }

                                    @Override
                                    public void scheduled(IJobChangeEvent event) {
                                    }

                                    @Override
                                    public void running(IJobChangeEvent event) {
                                    }

                                    @Override
                                    public void done(IJobChangeEvent event) {
                                        IPath sourcePath = JBossSourceContainer.getSourcePath(artifact);
                                        if (sourcePath != null && sourcePath.toFile().exists()) {
                                            attachSource(fragment, sourcePath);
                                        }
                                    }

                                    @Override
                                    public void awake(IJobChangeEvent event) {
                                    }

                                    @Override
                                    public void aboutToRun(IJobChangeEvent event) {
                                    }
                                });
                                job.schedule();
                            } else {
                                attachSource(fragment, sourcePath);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            SourceLookupUIActivator.log(e);
        }
    }
}

From source file:org.jboss.tools.as.sourcelookup.ui.actions.AttachSourcesActionDelegate.java

License:Open Source License

private void attachSource(IPackageFragmentRoot fragment, IPath newSourcePath) {
    try {//  w w w .  ja  v  a2 s .  com
        if (fragment == null || fragment.getKind() != IPackageFragmentRoot.K_BINARY) {
            return;
        }
        IPath containerPath = null;
        IJavaProject jproject = fragment.getJavaProject();
        IClasspathEntry entry = fragment.getRawClasspathEntry();
        if (entry == null) {
            entry = JavaCore.newLibraryEntry(fragment.getPath(), null, null);
        } else {
            if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                containerPath = entry.getPath();
                ClasspathContainerInitializer initializer = JavaCore
                        .getClasspathContainerInitializer(containerPath.segment(0));
                IClasspathContainer container = JavaCore.getClasspathContainer(containerPath, jproject);
                if (initializer == null || container == null) {
                    return;
                }
                IStatus status = initializer.getSourceAttachmentStatus(containerPath, jproject);
                if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED) {
                    return;
                }
                if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY) {
                    return;
                }
                entry = JavaModelUtil.findEntryInContainer(container, fragment.getPath());
                if (entry == null) {
                    return;
                }
            }
        }
        IClasspathEntry entry1;
        CPListElement elem = CPListElement.createFromExisting(entry, null);
        elem.setAttribute(CPListElement.SOURCEATTACHMENT, newSourcePath);
        entry1 = elem.getClasspathEntry();
        if (entry1.equals(entry)) {
            return;
        }
        IClasspathEntry newEntry = entry1;
        String[] changedAttributes = { CPListElement.SOURCEATTACHMENT };
        BuildPathSupport.modifyClasspathEntry(null, newEntry, changedAttributes, jproject, containerPath,
                newEntry.getReferencingEntry() != null, new NullProgressMonitor());
    } catch (CoreException e) {
        // ignore
    }
}

From source file:org.jboss.tools.common.model.ui.editor.ModelObjectStorageEditorInput.java

License:Open Source License

IJarEntryResource findJarEntryFile() {
    XModelObject o = object;/*ww  w. j  a  v a  2  s. co  m*/
    JarEntryFile f = null;
    JarEntryResource current = null;
    String packageName = "";
    List<String> parts = new ArrayList<String>();
    List<XModelObject> os = new ArrayList<XModelObject>();
    while (o != null && o.getFileType() != XModelObject.SYSTEM) {
        String part = o.getFileType() == XModelObject.FILE ? FileAnyImpl.toFileName(o)
                : o.getFileType() == XModelObject.FOLDER ? o.getAttributeValue(XModelObjectConstants.ATTR_NAME)
                        : null;
        if (part != null) {
            parts.add(0, part);
            os.add(0, o);
            if (f == null) {
                f = new JarEntryFile(part) {
                    public InputStream getContents() throws CoreException {
                        return storage.getContents();
                    }
                };
                current = f;
            } else {
                if (packageName.length() > 0) {
                    packageName = part + "." + packageName;
                } else {
                    packageName = part;
                }
                JarEntryDirectory d = new JarEntryDirectory(part);
                current.setParent(d);
                current = d;
            }

        }
        o = o.getParent();
    }
    //      if(!(o instanceof JarSystemImpl)) return null;
    String file = Paths.expand(o.get(XModelObjectConstants.ATTR_NAME_LOCATION), o.getModel().getProperties());

    IProject p = EclipseResourceUtil.getProject(o);
    IJavaProject jp = EclipseResourceUtil.getJavaProject(p);
    if (jp == null)
        return null;

    IPackageFragmentRoot root = null;

    try {
        IPackageFragmentRoot[] rs = jp.getAllPackageFragmentRoots();
        for (IPackageFragmentRoot r : rs) {
            if (r.getResource() != null && r.getResource().exists()
                    && r.getResource().getLocation().toString().equals(file)) {
                root = r;
            } else if (r.getPath() != null && r.getPath().toString().equals(file)) {
                root = r;
            }
        }
    } catch (CoreException e) {
        ModelUIPlugin.getDefault().logError(e);
    }

    if (root == null) {
        root = jp.getPackageFragmentRoot(file);
    }

    if (root == null) {
        return null;
    }

    if (current != null && !"META-INF".equalsIgnoreCase(current.getName()) && packageName.length() > 0) {
        IPackageFragment pf = root.getPackageFragment(packageName);
        f.setParent(pf);
    } else {
        current.setParent(root);
        if (!(o instanceof JarSystemImpl)) {
            Object q = root;
            NonJavaResource nj = null;
            for (int i = 0; i < parts.size(); i++) {
                IResource ri = (IResource) os.get(i).getAdapter(IResource.class);
                if (ri == null) {
                    return f;
                }
                nj = new NonJavaResource(q, ri);
                q = nj;
            }
            if (nj != null) {
                return nj;
            }
        }
    }

    return f;
}

From source file:org.jboss.tools.jsf.project.JSF2Util.java

License:Open Source License

/**
 * Returns root object of Java Model for selected jar file;
 * returns null if jar file is not found in the class path.    * 
 * /*from w  ww.ja  va  2  s  . co  m*/
 * @param project
 * @param jarName
 * @return
 * @throws JavaModelException
 */
public static IPackageFragmentRoot findLibrary(IProject project, String jarName) throws JavaModelException {
    if (project == null || !project.isAccessible()) {
        return null;
    }
    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject == null || !javaProject.exists()) {
        return null;
    }
    for (IPackageFragmentRoot fragmentRoot : javaProject.getAllPackageFragmentRoots()) {
        IPath resource = fragmentRoot.getPath();
        if (resource != null && resource.lastSegment().equals(jarName)) {
            return fragmentRoot;
        }
    }
    return null;
}

From source file:org.jboss.tools.maven.sourcelookup.ui.actions.AttachSourcesActionDelegate.java

License:Open Source License

@Override
public void setActiveEditor(IAction action, IEditorPart targetEditor) {
    if (targetEditor != null) {
        try {/*from w  w w. j a  va2 s.c om*/
            boolean isAuto = SourceLookupActivator.getDefault().isAutoAddSourceContainer();
            if (!isAuto) {
                return;
            }
            IClassFileEditorInput input = (IClassFileEditorInput) targetEditor.getEditorInput();
            IJavaElement element = input.getClassFile();
            while (element.getParent() != null) {
                element = element.getParent();
                if (element instanceof IPackageFragmentRoot) {
                    final IPackageFragmentRoot fragment = (IPackageFragmentRoot) element;
                    IPath attachmentPath = fragment.getSourceAttachmentPath();
                    if (attachmentPath != null && !attachmentPath.isEmpty()
                            && attachmentPath.toFile().exists()) {
                        break;
                    }
                    if (fragment.isArchive()) {
                        IFile iFile = ResourcesPlugin.getWorkspace().getRoot().getFile(fragment.getPath());
                        File file = iFile == null || iFile.getLocation() == null ? fragment.getPath().toFile()
                                : iFile.getLocation().toFile();
                        IFileIdentificationManager identificationManager = new FileIdentificationManager();
                        final ArtifactKey artifact = identificationManager.identify(file,
                                new NullProgressMonitor());
                        if (artifact != null) {
                            IPath sourcePath = JBossSourceContainer.getSourcePath(artifact);
                            if (sourcePath == null || !sourcePath.toFile().exists()) {
                                Job job = JBossSourceContainer.downloadArtifact(file, artifact);
                                job.addJobChangeListener(new IJobChangeListener() {

                                    @Override
                                    public void sleeping(IJobChangeEvent event) {
                                    }

                                    @Override
                                    public void scheduled(IJobChangeEvent event) {
                                    }

                                    @Override
                                    public void running(IJobChangeEvent event) {
                                    }

                                    @Override
                                    public void done(IJobChangeEvent event) {
                                        IPath sourcePath = JBossSourceContainer.getSourcePath(artifact);
                                        if (sourcePath != null && sourcePath.toFile().exists()) {
                                            attachSource(fragment, sourcePath);
                                        }
                                    }

                                    @Override
                                    public void awake(IJobChangeEvent event) {
                                    }

                                    @Override
                                    public void aboutToRun(IJobChangeEvent event) {
                                    }
                                });
                                job.schedule();
                            } else {
                                attachSource(fragment, sourcePath);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            SourceLookupUIActivator.log(e);
        }
    }
}

From source file:org.jboss.tools.maven.sourcelookup.ui.internal.util.SourceLookupUtil.java

License:Open Source License

public static void attachSource(final IPackageFragmentRoot fragment, final IPath newSourcePath,
        boolean displayDialog) {
    try {/* ww w  .ja  v a  2s .c  om*/
        if (fragment == null || fragment.getKind() != IPackageFragmentRoot.K_BINARY) {
            return;
        }
        String value = SourceLookupActivator.getDefault().getAutoAddSourceAttachment();
        if (SourceLookupActivator.AUTO_ADD_JBOSS_SOURCE_ATTACHMENT_NEVER.equals(value)) {
            return;
        }
        if (displayDialog && SourceLookupActivator.AUTO_ADD_JBOSS_SOURCE_ATTACHMENT_PROMPT.equals(value)) {
            final boolean[] attach = new boolean[1];
            Display.getDefault().syncExec(new Runnable() {

                @Override
                public void run() {
                    attach[0] = promptToAddSourceAttachment(fragment.getElementName(),
                            newSourcePath.toString());
                }
            });
            if (!attach[0]) {
                return;
            }
            ;
        }
        IPath containerPath = null;
        IJavaProject jproject = fragment.getJavaProject();
        IClasspathEntry entry = fragment.getRawClasspathEntry();
        if (entry == null) {
            entry = JavaCore.newLibraryEntry(fragment.getPath(), null, null);
        } else {
            if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                containerPath = entry.getPath();
                ClasspathContainerInitializer initializer = JavaCore
                        .getClasspathContainerInitializer(containerPath.segment(0));
                IClasspathContainer container = JavaCore.getClasspathContainer(containerPath, jproject);
                if (initializer == null || container == null) {
                    return;
                }
                IStatus status = initializer.getSourceAttachmentStatus(containerPath, jproject);
                if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED) {
                    return;
                }
                if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY) {
                    return;
                }
                entry = JavaModelUtil.findEntryInContainer(container, fragment.getPath());
                if (entry == null) {
                    return;
                }
            }
        }
        IClasspathEntry entry1;
        CPListElement elem = CPListElement.createFromExisting(entry, null);
        elem.setAttribute(CPListElement.SOURCEATTACHMENT, newSourcePath);
        entry1 = elem.getClasspathEntry();
        if (entry1.equals(entry)) {
            return;
        }
        IClasspathEntry newEntry = entry1;
        String[] changedAttributes = { CPListElement.SOURCEATTACHMENT };
        BuildPathSupport.modifyClasspathEntry(null, newEntry, changedAttributes, jproject, containerPath,
                newEntry.getReferencingEntry() != null, new NullProgressMonitor());
    } catch (CoreException e) {
        // ignore
    }
}