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

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

Introduction

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

Prototype

IPath getSourceAttachmentPath() throws JavaModelException;

Source Link

Document

Returns the absolute path to the source archive attached to this package fragment root's binary archive.

Usage

From source file:org.jboss.tools.jst.jsp.jspeditor.info.JavaStringELInfoHover.java

License:Open Source License

/**
 * Adds full information to the hover//  w  w  w  . j  a  va 2 s  .c o  m
 * Returns base URL if exists
 * 
 * @param buffer
 * @param element
 * @return
 */
private static String addFullInfo(StringBuffer buffer, IJavaElement element, boolean useFullHTML) {
    String base = null;

    if (element instanceof IMember) {
        IMember member = (IMember) element;
        HTMLPrinter.addSmallHeader(buffer, getInfoText(member, true, useFullHTML));
        Reader reader;
        try {
            String content = JavadocContentAccess2.getHTMLContent(member, true);
            reader = content == null ? null : new StringReader(content);

            // Provide hint why there's no Javadoc
            if (reader == null && member.isBinary()) {
                boolean hasAttachedJavadoc = JavaDocLocations.getJavadocBaseLocation(member) != null;
                IPackageFragmentRoot root = (IPackageFragmentRoot) member
                        .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
                boolean hasAttachedSource = root != null && root.getSourceAttachmentPath() != null;
                IOpenable openable = member.getOpenable();
                boolean hasSource = openable.getBuffer() != null;

                if (!hasAttachedSource && !hasAttachedJavadoc)
                    reader = new StringReader(ELInfoHoverMessages.ELInfoHover_noAttachments);
                else if (!hasAttachedJavadoc && !hasSource)
                    reader = new StringReader(ELInfoHoverMessages.ELInfoHover_noAttachedJavadoc);
                else if (!hasAttachedSource)
                    reader = new StringReader(ELInfoHoverMessages.ELInfoHover_noAttachedJavaSource);
                else if (!hasSource)
                    reader = new StringReader(ELInfoHoverMessages.ELInfoHover_noInformation);

            } else {
                base = JavaDocLocations.getBaseURL(member);
            }

        } catch (JavaModelException ex) {
            reader = new StringReader(ELInfoHoverMessages.ELInfoHover_error_gettingJavadoc);
            JavaPlugin.log(ex);
        }

        if (reader != null) {
            HTMLPrinter.addParagraph(buffer, reader);
        }

    } else if (element.getElementType() == IJavaElement.LOCAL_VARIABLE
            || element.getElementType() == IJavaElement.TYPE_PARAMETER) {
        HTMLPrinter.addSmallHeader(buffer, getInfoText(element, true, useFullHTML));
    }
    return base;
}

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  ww w.  ja  va 2  s . co  m
            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.pde.sourcelookup.core.internal.utils.ClasspathUtils.java

License:Open Source License

public static boolean hasSources(IPackageFragmentRoot fragment) throws CoreException {
    if (fragment == null || fragment.getSourceAttachmentPath() == null) {
        return false;
    }/*from  w  w  w  .j  av a2s. c  o m*/
    IPath filePath = fragment.getPath();
    IPath sourcePath = fragment.getSourceAttachmentPath();
    if (isBinaryFragment(fragment)) {
        if (!Files.isRegularFile(Paths.get(sourcePath.toOSString()))) {
            return false;
        }
        return !Objects.equals(sourcePath, filePath);
    }
    return true;
}

From source file:org.jboss.tools.pde.sourcelookup.core.internal.utils.ClasspathUtils.java

License:Open Source License

public static void attachSource(final IPackageFragmentRoot fragment, final IPath newSourcePath,
        IProgressMonitor monitor) {/*w  w  w .  j  a v  a 2  s.  com*/
    try {
        if (fragment == null || fragment.getKind() != IPackageFragmentRoot.K_BINARY) {
            return;
        }
        if (!Objects.equals(fragment.getSourceAttachmentPath(), newSourcePath)) {
            // would be so cool if it refreshed the UI automatically
            fragment.attachSource(newSourcePath, null, monitor);
            // close the root so that source attachment cache is flushed. Else UI
            // won't update
            fragment.close();
            // we have to manually fire a delta to notify the UI about the source
            // attachment.
            JavaModelManager manager = JavaModelManager.getJavaModelManager();
            JavaElementDelta attachedSourceDelta = new JavaElementDelta(fragment.getJavaModel());
            attachedSourceDelta.sourceAttached(fragment);
            manager.getDeltaProcessor().fire(attachedSourceDelta, ElementChangedEvent.POST_CHANGE);
        }
    } catch (CoreException e) {
        // ignore
    }
}

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

License:Open Source License

@Override
protected Control createContents(Composite parent) {
    final IPackageFragmentRoot root = (IPackageFragmentRoot) getElement()
            .getAdapter(IPackageFragmentRoot.class);
    if (root == null) {
        Label label = new Label(parent, SWT.NONE);
        label.setText("Input si not a classpath entry!");
        return label;
    }/*from   w ww . j  av  a  2  s .  c o m*/

    String msg = null;
    try {
        IPath path = root.getSourceAttachmentPath();
        if (path != null) {
            msg = "Sources are already configured";
        } else {
            msg = "Souces are not yet configured";
        }
    } catch (JavaModelException e) {
        msg = "Failed to get source status";
    }
    setMessage(msg);
    Composite panel = new Composite(parent, SWT.NONE);
    panel.setLayout(new RowLayout(SWT.VERTICAL));
    Label label = new Label(panel, SWT.NONE);
    label.setText("You can download and configure sources by clicking on 'Download' button");
    Button dwn = new Button(panel, SWT.BORDER);
    dwn.setText("Download");
    dwn.addSelectionListener(new SelectionListener() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            Job job = new Job("Download Sources") {
                @Override
                protected IStatus run(IProgressMonitor monitor) {
                    monitor.beginTask("Downloading", 1);
                    installSources(root);
                    monitor.worked(1);
                    monitor.done();
                    return Status.OK_STATUS;
                }
            };
            job.setUser(true);
            job.schedule();
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
            widgetSelected(e);
        }
    });

    return panel;
}

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

License:Open Source License

/**
 * Misses sources./*from   w w w .ja  va  2 s .c  o  m*/
 * 
 * @param packageFragmentRoot
 *            the package fragment root
 * 
 * @return true, if successful
 * 
 * @throws JavaModelException
 *             the java model exception
 */
private boolean missesSources(IPackageFragmentRoot packageFragmentRoot) throws JavaModelException {
    return packageFragmentRoot.getSourceAttachmentPath() == null;
}

From source file:org.seasar.resource.synchronizer.servlet.SelectionServlet.java

License:Apache License

private IDocument toDocument(final IType type) throws JavaModelException {
    IDocument document = null;/*from   w  w  w.  ja v a  2 s.  c o m*/
    if (type.isBinary()) {
        IPackageFragmentRoot root = (IPackageFragmentRoot) type.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
        IPath path = root.getSourceAttachmentPath();
        if (path != null) {
            IOpenable o = type.getOpenable();
            IBuffer buf = o.getBuffer();
            if (buf != null) {
                document = new Document(buf.getContents());
            }
        }
    } else {
        document = new Document(type.getCompilationUnit().getSource());
    }
    return document;
}

From source file:org.teavm.eclipse.debugger.TeaVMSourceLookupParticipant.java

License:Apache License

@Override
public void sourceContainersChanged(ISourceLookupDirector director) {
    delegateContainers.clear();/*  w  w w .  jav a 2  s .c  o  m*/
    ISourceContainer[] containers = director.getSourceContainers();
    for (int i = 0; i < containers.length; i++) {
        ISourceContainer container = containers[i];
        if (container.getType().getId().equals(ArchiveSourceContainer.TYPE_ID)) {
            IFile file = ((ArchiveSourceContainer) container).getFile();
            IProject project = file.getProject();
            IJavaProject javaProject = JavaCore.create(project);
            if (javaProject.exists()) {
                try {
                    IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
                    for (int j = 0; j < roots.length; j++) {
                        IPackageFragmentRoot root = roots[j];
                        if (file.equals(root.getUnderlyingResource())) {
                            delegateContainers.put(container, new PackageFragmentRootSourceContainer(root));
                        } else {
                            IPath path = root.getSourceAttachmentPath();
                            if (path != null) {
                                if (file.getFullPath().equals(path)) {
                                    delegateContainers.put(container,
                                            new PackageFragmentRootSourceContainer(root));
                                }
                            }
                        }
                    }
                } catch (JavaModelException e) {
                }
            }
        }
    }
}