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

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

Introduction

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

Prototype

String getElementName();

Source Link

Document

Returns the name of this element.

Usage

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 {//from   w  w  w  .  jav a 2 s  .  c o  m
        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
    }
}

From source file:org.jboss.tools.pde.sourcelookup.core.internal.jobs.AbstractSourceDownloadJob.java

License:Open Source License

private void findAndAttachSources(IPackageFragmentRoot fragment, IProgressMonitor monitor) {
    monitor.setTaskName("Searching sources for " + fragment.getElementName());
    try {/*w  ww.j  av  a 2s  .  c o  m*/
        if (ClasspathUtils.hasSources(fragment)) {
            return;
        }
        IPath path = findSources(fragment, monitor);
        if (path != null) {
            ClasspathUtils.attachSource(fragment, path, monitor);
        }
    } catch (CoreException e) {
        e.printStackTrace();
    }
}

From source file:org.jboss.tools.pde.sourcelookup.core.internal.jobs.AbstractSourceDownloadJob.java

License:Open Source License

protected IPath findSources(IPackageFragmentRoot fragment, IProgressMonitor monitor) throws CoreException {
    monitor.setTaskName(fragment.getElementName());

    File jar = fragment.getPath().toFile();
    IPath path = sourceLocators.stream().map(sl -> findSource(sl, jar, monitor)).filter(p -> p != null)
            .findFirst().orElse(null);/*ww  w . j  a  v a  2  s .  c  o m*/

    return path;
}

From source file:org.limy.eclipse.qalab.propertypage.SourceDirSelector.java

License:Open Source License

/**
 * pbP?[WfBNgI???s?B/*from  w  w  w. j a  v a 2 s  .  c o m*/
 * @throws JavaModelException 
 */
private void setPackageDir() throws JavaModelException {

    ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(new Shell(),
            new WorkbenchLabelProvider(), new WorkbenchContentProvider());

    dialog.setTitle(Messages.TITLE_SELECT_SOURCE_DIR);
    dialog.setMessage(Messages.MES_SELECT_SOURCE_DIR);
    ViewerFilter filter = new ViewerFilter() {
        @Override
        public boolean select(Viewer viewer, Object parentElement, Object element) {
            if (element instanceof IPackageFragmentRoot) {
                return true;
            }
            return false;
        }
    };
    dialog.addFilter(filter);

    Collection<IPackageFragmentRoot> targetElements = new ArrayList<IPackageFragmentRoot>();
    for (IProject project : projects) {
        IJavaProject javaProject = JavaCore.create(project);
        for (IPackageFragmentRoot root : javaProject.getAllPackageFragmentRoots()) {
            if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
                // jarpbP?[W?O
                targetElements.add(root);
            }
        }
    }

    dialog.setInput(new AdaptableList(targetElements));

    if (dialog.open() == Dialog.OK) {

        Object[] results = dialog.getResult();
        for (Object result : results) {
            IPackageFragmentRoot fragment = (IPackageFragmentRoot) result;
            control.add(fragment.getElementName());
            if (listener != null) {
                PropertyChangeEvent evt = new PropertyChangeEvent(control, "resultPath", null,
                        fragment.getPath());
                listener.propertyChange(evt);
            }
        }
    }
}

From source file:org.melord.pde.explorer.handlers.OpenExplorerHandler.java

License:Apache License

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
    ISelection sel = window.getSelectionService().getSelection();
    if (sel instanceof IStructuredSelection) {
        Object obj = ((IStructuredSelection) sel).getFirstElement();
        IResource resource = null;/*w ww  . j  a  va 2 s . c  om*/
        String path = null;
        String file = null;
        // common resource file
        if (obj instanceof IFile) {
            resource = (IResource) obj;
            file = resource.getLocation().toOSString();
            path = file.substring(0, file.lastIndexOf(File.separator));
        } else if (obj instanceof IProject) {
            IProject prj = (IProject) obj;
            path = prj.getLocation().toOSString();
        }
        // other resource such as folder,project
        else if (obj instanceof IResource) {
            resource = (IResource) obj;
            path = resource.getLocation().toOSString();
        }
        // explorer java element, contain field,method,package
        else if (obj instanceof IJavaElement) {
            // java project.
            if (obj instanceof IJavaProject) {
                path = ((IJavaProject) obj).getProject().getLocation().toOSString();
            }
            // jar resource is null
            else if (obj instanceof JarPackageFragmentRoot) {
                file = ((IPackageFragmentRoot) obj).getPath().toOSString();
                // get folder
                path = file.substring(0, file.lastIndexOf(File.separator));
            } else if (obj instanceof IPackageFragmentRoot) {
                // src folder
                IPackageFragmentRoot src = ((IPackageFragmentRoot) obj);
                IProject p = src.getJavaProject().getProject();
                String prjPath = p.getLocation().toOSString();
                path = new File(prjPath, src.getElementName()).getAbsolutePath();
                // System.out.println(path);
            } else if (obj instanceof IPackageFragment) {// other : package
                resource = ((IPackageFragment) obj).getResource();
                path = resource.getLocation().toOSString();
            } else {// member:filed:
                resource = ((IJavaElement) obj).getResource();
                file = resource.getLocation().toOSString();
                // get folder
                path = file.substring(0, file.lastIndexOf(File.separator));
            }

        }
        // explorer team ui resource
        else if (obj instanceof ISynchronizeModelElement) {
            resource = ((ISynchronizeModelElement) obj).getResource();
        }
        // process
        if (path != null) {
            // System.out.println(path);
            String cmd = null;
            try {
                cmd = Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.EXPLORER_CMD)
                        .trim();
                if (cmd.toLowerCase().startsWith("explorer")) {
                    String winCmd = String.format("%s %s", cmd, path);
                    if (file != null) {
                        winCmd = String.format("%s /select,%s", cmd, file);
                    }
                    Runtime.getRuntime().exec(winCmd);
                    return null;
                }
                Runtime.getRuntime().exec(cmd.trim() + " " + path); //$NON-NLS-1$
            } catch (IOException e) {
                //
                e.printStackTrace();
            }
        }

    }
    return null;
}

From source file:org.nuxeo.ide.sdk.ui.SDKClassPathContainerEntryPage.java

License:Open Source License

public void installSources(IPackageFragmentRoot root) {
    NuxeoSDK sdk = NuxeoSDK.getDefault();
    if (sdk == null) {
        UI.showError("No Nuxeo SDK configured. Cannot continue.");
        return;/*w w  w  . j  av a2s. c om*/
    }
    Artifact artifact = Artifact.fromJarName(root.getElementName());
    if (artifact == null) {
        UI.showError("Cannot resolve JAR " + root.getElementName() + " to Maven GAV");
        return;
    }
    try {
        FileRef ref = MavenDownloader.downloadSourceJar(artifact);
        if (ref == null) {
            UI.showError("No sources found for corresponding artifact: " + artifact);
            return;
        }
        ref.installTo(sdk.getBundlesSrcDir());
        Display.getDefault().asyncExec(new Runnable() {
            @Override
            public void run() {
                NuxeoSDK.reload();
                setMessage("Sources are configured");
            }
        });
    } catch (IOException e) {
        UI.showError("Faield to download artifact file", e);
    }
}

From source file:org.org.eclipse.dws.core.internal.jobs.UpdateJavadocAndSourcesJob.java

License:Open Source License

/**
 * Update sources./*w ww  . jav a 2 s .com*/
 * 
 * @param project
 *            the project
 * @param packageFragmentRoot
 *            the package fragment root
 * @param file
 *            the file
 * @param monitor
 *            the monitor
 */
@SuppressWarnings("restriction")
private void updateSources(IJavaProject project, IPackageFragmentRoot packageFragmentRoot, File file,
        IProgressMonitor monitor) {
    try {
        logger.info("updating source attachment of " + packageFragmentRoot.getElementName() + " with " + file);
        IClasspathEntry rawClasspathEntry = packageFragmentRoot.getRawClasspathEntry();
        IPath containerPath = null;
        if (rawClasspathEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            containerPath = rawClasspathEntry.getPath();
            rawClasspathEntry = handleContainerEntry(containerPath, project, packageFragmentRoot.getPath());
        }
        CPListElement cpElem = CPListElement.createFromExisting(rawClasspathEntry, project);
        String loc = file.getAbsolutePath();
        cpElem.setAttribute(CPListElement.SOURCEATTACHMENT, Path.fromOSString(loc).makeAbsolute());
        IClasspathEntry newEntry = cpElem.getClasspathEntry();
        String[] changedAttributes = { CPListElement.SOURCEATTACHMENT };
        BuildPathSupport.modifyClasspathEntry(null, newEntry, changedAttributes, project, containerPath, true,
                monitor);
    } catch (JavaModelException e) {
        throw new MavenRepositoryInteractionException(
                "Impossible to attach source jar:" + file.getAbsolutePath(), e);
    } catch (CoreException e) {
        throw new MavenRepositoryInteractionException(
                "Impossible to attach source jar:" + file.getAbsolutePath(), e);
    }
}

From source file:org.org.eclipse.dws.core.internal.jobs.UpdateJavadocAndSourcesJob.java

License:Open Source License

/**
 * Update javadoc.//from   w w  w.  j  a v  a2s .c o  m
 * 
 * @param project
 *            the project
 * @param packageFragmentRoot
 *            the package fragment root
 * @param file
 *            the file
 * @param monitor
 *            the monitor
 * 
 * @throws CoreException
 *             the core exception
 */
@SuppressWarnings("restriction")
private void updateJavadoc(IJavaProject project, IPackageFragmentRoot packageFragmentRoot, File file,
        IProgressMonitor monitor) throws CoreException {
    try {
        logger.info("updating javadoc location of " + packageFragmentRoot.getElementName() + " with " + file);
        IClasspathEntry rawClasspathEntry = packageFragmentRoot.getRawClasspathEntry();
        IPath containerPath = null;
        if (rawClasspathEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            containerPath = rawClasspathEntry.getPath();
            rawClasspathEntry = handleContainerEntry(containerPath, project, packageFragmentRoot.getPath());
        }
        CPListElement cpElem = CPListElement.createFromExisting(rawClasspathEntry, project);
        String loc = "jar:file:/" + file.getAbsolutePath() + "!/";
        cpElem.setAttribute(CPListElement.JAVADOC, loc);
        IClasspathEntry newEntry = cpElem.getClasspathEntry();
        String[] changedAttributes = { CPListElement.JAVADOC };
        BuildPathSupport.modifyClasspathEntry(null, newEntry, changedAttributes, project, containerPath, true,
                monitor);
    } catch (JavaModelException e) {
        throw new MavenRepositoryInteractionException(
                "Impossible to set javadoc location:" + file.getAbsolutePath(), e);
    } catch (CoreException e) {
        throw new MavenRepositoryInteractionException(
                "Impossible to set javadoc location:" + file.getAbsolutePath(), e);
    }

}

From source file:org.org.eclipse.dws.ui.internal.wizards.pages.DependenciesFromClasspathPage.java

License:Open Source License

/**
 * Gets the possible dependency wrappers.
 * // w  w w .  j  a  v a2  s  .c om
 * @param javaProject
 *            the java project
 * 
 * @return the possible dependency wrappers
 * 
 * @throws JavaModelException
 *             the java model exception
 */
private Set<PossibleDependencyWrapper> getPossibleDependencyWrappers(IJavaProject javaProject)
        throws JavaModelException {
    Set<IPackageFragmentRoot> packageFragmentRoots = new TreeSet<IPackageFragmentRoot>(
            new Comparator<IPackageFragmentRoot>() {
                public int compare(IPackageFragmentRoot o1, IPackageFragmentRoot o2) {
                    return o1.getElementName().compareTo(o2.getElementName());
                }

            });
    for (IPackageFragmentRoot packageFragmentRoot : javaProject.getPackageFragmentRoots()) {
        if (isPossibleDependency(packageFragmentRoot)) {
            packageFragmentRoots.add(packageFragmentRoot);
        }
    }
    Set<PossibleDependencyWrapper> possibleDependencyWrappers = new TreeSet<PossibleDependencyWrapper>(
            new Comparator<PossibleDependencyWrapper>() {

                public int compare(PossibleDependencyWrapper o1, PossibleDependencyWrapper o2) {
                    int result = o1.getLibraryId().compareTo(o2.getLibraryId());
                    if (o1.getExactMatch() != o2.getExactMatch()) {
                        result = -(o1.getExactMatch().compareTo(o2.getExactMatch()));
                    }
                    return result;
                }

            });
    for (IPackageFragmentRoot packageFragmentRoot : packageFragmentRoots) {
        String libraryPath = packageFragmentRoot.getPath().toOSString();
        final String libraryId = packageFragmentRoot.getElementName();
        Set<ArtifactVersion> possibleDependencies = findPossibleDependencies(libraryId);
        if (possibleDependencies.size() > 0) {
            Set<ArtifactVersionWrapper> artifactVersionWrappers = new TreeSet<ArtifactVersionWrapper>(
                    new ArtifactVersionWrappersComparator(libraryId));
            wrapArtifactVersions(possibleDependencies, artifactVersionWrappers, libraryId);
            boolean hasExactMatch = lookForExactMatch(artifactVersionWrappers);
            possibleDependencyWrappers.add(new PossibleDependencyWrapper(libraryPath, libraryId,
                    Collections.unmodifiableSet(artifactVersionWrappers), hasExactMatch));
        }
    }
    possibleDependencyWrappers = Collections.unmodifiableSet(possibleDependencyWrappers);
    return possibleDependencyWrappers;
}

From source file:org.org.eclipse.dws.ui.internal.wizards.pages.DependenciesFromClasspathPage.java

License:Open Source License

/**
 * Checks if is filtered./*from   w w  w.j  a  va  2s .co m*/
 * 
 * @param packageFragmentRoot
 *            the package fragment root
 * 
 * @return true, if is filtered
 */
private boolean isFiltered(IPackageFragmentRoot packageFragmentRoot) {
    Boolean result = false;
    for (String filteredEntry : filteredLibraries) {
        if (packageFragmentRoot.getElementName().endsWith(filteredEntry)) {
            result = true;
            break;
        }
    }
    return result;
}