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

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

Introduction

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

Prototype

void attachSource(IPath sourcePath, IPath rootPath, IProgressMonitor monitor) throws JavaModelException;

Source Link

Document

Attaches the source archive identified by the given absolute path to this binary package fragment root.

Usage

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) {//from w ww  .j a v a 2 s.  c  om
    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.jboss.tools.pde.sourcelookup.ui.tests.IntegrationTest.java

License:Open Source License

@Test
public void basicSingleJarTest() throws Exception {
    final String id = "org.foo.bar";
    final String version = "1.0.0";
    final String jarName = id + "_" + version + ".jar";
    final String sourceJarName = id + ".source" + "_" + version + ".jar";
    final String resourceDir = "/resources/";
    final String sourceRepoRelPath = resourceDir + "valid-source-repo";

    IJavaProject jProject = createJavaProject("integration-test");

    // binary jar file location
    String jarPath = FileLocator.toFileURL(getClass().getResource(resourceDir + jarName)).getPath();
    IPath binJarPath = org.eclipse.core.runtime.Path.fromOSString(jarPath);

    // Add the binary jar to the classpath of the java project
    addBinaries(jProject, binJarPath);/* w ww .  ja  va 2 s.c o  m*/

    // Set the default source attachment as the binary jar
    // This is necessary to trigger our PDE source lookup
    IPackageFragmentRoot pfr = jProject.getPackageFragmentRoot(jarPath);
    pfr.attachSource(binJarPath, null, new NullProgressMonitor());

    // source jar artifact repository location
    URI sourceLoc = getSourceRepoURL(sourceRepoRelPath);

    // Inform the provisioning agent of the artifact repository containing
    // the source bundle
    addP2Repositories(getSourceRepoURL(resourceDir + "invalid-source-repo"), sourceLoc);

    // Simulate opening the classfile in the editor
    // TODO: Could we ever get JavaUI.openInEditor working with Tycho ?
    //      IJavaElement jElement = jProject.findType("org.foo.bar.Main");
    //      JavaUI.openInEditor(jElement);
    DownloadSourcesActionDelegate delegate = new DownloadSourcesActionDelegate();
    delegate.selectionChanged(null, new StructuredSelection(jProject.getPackageFragmentRoot(jarPath)));
    delegate.run(null);

    // Expect to find the downloaded jar at this location
    File downloadedSourceJarFile = sourcesDirectory.resolve(sourceJarName).toFile();
    while (!downloadedSourceJarFile.exists()) {
        Thread.sleep(1000);
    }
}