Example usage for org.eclipse.jdt.core IClasspathEntry getReferencingEntry

List of usage examples for org.eclipse.jdt.core IClasspathEntry getReferencingEntry

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IClasspathEntry getReferencingEntry.

Prototype

IClasspathEntry getReferencingEntry();

Source Link

Document

Returns the classpath entry that is making a reference to this classpath entry.

Usage

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  .  j  a  v  a2s  .c  o  m*/
        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.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 www.  jav  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
    }
}