Example usage for org.eclipse.jdt.core IJavaElement getParent

List of usage examples for org.eclipse.jdt.core IJavaElement getParent

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaElement getParent.

Prototype

IJavaElement getParent();

Source Link

Document

Returns the element directly containing this element, or null if this element has no parent.

Usage

From source file:org.eclipse.ltk.internal.ui.refactoring.CompilationUnitChangeNode.java

License:Open Source License

private IJavaElement getModifiedJavaElement(TextEditBasedChangeGroup edit, ICompilationUnit cunit)
        throws JavaModelException {
    IRegion range = edit.getRegion();/* w  ww.j  av a 2  s.c  om*/
    if (range.getOffset() == 0 && range.getLength() == 0)
        return cunit;
    IJavaElement result = cunit.getElementAt(range.getOffset());
    if (result == null)
        return cunit;

    try {
        while (true) {
            ISourceReference ref = (ISourceReference) result;
            IRegion sRange = new Region(ref.getSourceRange().getOffset(), ref.getSourceRange().getLength());
            if (result.getElementType() == IJavaElement.COMPILATION_UNIT || result.getParent() == null
                    || coveredBy(edit, sRange))
                break;
            result = result.getParent();
        }
    } catch (JavaModelException e) {
        // Do nothing, use old value.
    } catch (ClassCastException e) {
        // Do nothing, use old value.
    }
    return result;
}

From source file:org.eclipse.ltk.internal.ui.refactoring.CompilationUnitChangeNode.java

License:Open Source License

private JavaLanguageNode getChangeElement(Map<IJavaElement, JavaLanguageNode> map, IJavaElement element,
        List<ChildNode> children, TextEditChangeNode cunitChange) {
    JavaLanguageNode result = map.get(element);
    if (result != null)
        return result;
    IJavaElement parent = element.getParent();
    if (parent instanceof ICompilationUnit) {
        result = new JavaLanguageNode(cunitChange, element);
        children.add(result);//from w  w  w.  j  av a 2s. c  om
        map.put(element, result);
    } else {
        JavaLanguageNode parentChange = getChangeElement(map, parent, children, cunitChange);
        result = new JavaLanguageNode(parentChange, element);
        parentChange.addChild(result);
        map.put(element, result);
    }
    return result;
}

From source file:org.eclipse.m2e.jdt.internal.DownloadSourcesActionDelegate.java

License:Open Source License

public void setActiveEditor(IAction action, IEditorPart part) {

    if (part != null) {
        try {/*w  w  w .  ja v  a 2s .co  m*/
            IClasspathManager buildpathManager = MavenJdtPlugin.getDefault().getBuildpathManager();

            IClassFileEditorInput input = (IClassFileEditorInput) part.getEditorInput();
            IJavaElement element = input.getClassFile();
            while (element.getParent() != null) {
                element = element.getParent();
                if (element instanceof IPackageFragmentRoot) {
                    IPackageFragmentRoot root = (IPackageFragmentRoot) element;

                    if (root.getSourceAttachmentPath() != null) {
                        // do nothing if sources attached already
                        break;
                    }

                    buildpathManager.scheduleDownload(root, true/*sources*/, false/*javadoc*/);
                }
            }
        } catch (Exception ex) {
            log.error("Could not schedule source download", ex); //$NON-NLS-1$
        }
    }
}

From source file:org.eclipse.m2e.jdt.ui.internal.DownloadSourcesActionDelegate.java

License:Open Source License

public void setActiveEditor(IAction action, IEditorPart part) {

    if (part != null && part.getEditorInput() instanceof IClassFileEditorInput) {
        try {//w  ww .j  a v  a 2s .  c  om
            IClasspathManager buildpathManager = MavenJdtPlugin.getDefault().getBuildpathManager();
            IClassFileEditorInput input = (IClassFileEditorInput) part.getEditorInput();
            IJavaElement element = input.getClassFile();
            while (element.getParent() != null) {
                element = element.getParent();
                if (element instanceof IPackageFragmentRoot) {
                    IPackageFragmentRoot root = (IPackageFragmentRoot) element;

                    if (root.getSourceAttachmentPath() != null) {
                        // do nothing if sources attached already
                        break;
                    }

                    buildpathManager.scheduleDownload(root, true/*sources*/, false/*javadoc*/);
                }
            }
        } catch (Exception ex) {
            log.error("Could not schedule source download", ex); //$NON-NLS-1$
        }
    }
}

From source file:org.eclipse.m2e.refactoring.exclude.DependencyExcludeAction.java

License:Open Source License

public void selectionChanged(IAction action, ISelection selection) {
    file = null;//w w  w. jav  a2 s.  co m
    keys = null;

    // TODO move logic into adapters
    if (selection instanceof IStructuredSelection) {
        IStructuredSelection structuredSelection = (IStructuredSelection) selection;

        List<ArtifactKey> keys = new ArrayList<ArtifactKey>(structuredSelection.size());
        for (Object selected : structuredSelection.toArray()) {
            if (selected instanceof Artifact) {
                file = getFileFromEditor();
                keys.add(new ArtifactKey((Artifact) selected));
            } else if (selected instanceof org.sonatype.aether.graph.DependencyNode) {
                file = getFileFromEditor();
                keys.add(new ArtifactKey(
                        ((org.sonatype.aether.graph.DependencyNode) selected).getDependency().getArtifact()));
            } else if (selected instanceof RequiredProjectWrapper) {
                RequiredProjectWrapper w = (RequiredProjectWrapper) selected;
                file = getFileFromProject(w.getParentClassPathContainer().getJavaProject());
                keys.add(SelectionUtil.getType(selected, ArtifactKey.class));
            } else {
                keys.add(SelectionUtil.getType(selected, ArtifactKey.class));
                if (selected instanceof IJavaElement) {
                    IJavaElement el = (IJavaElement) selected;
                    file = getFileFromProject(el.getParent().getJavaProject());
                }
            }
        }
        this.keys = keys.toArray(new ArtifactKey[keys.size()]);
    }
    if (keys != null && keys.length > 0 && file != null) {
        action.setEnabled(true);
    } else {
        action.setEnabled(false);
    }
}

From source file:org.eclipse.mylyn.internal.java.ui.JavaEditingMonitor.java

License:Open Source License

/**
 * @return null for elements that aren't modeled
 *///from   w ww.jav  a2  s . com
protected IJavaElement checkIfAcceptedAndPromoteIfNecessary(IJavaElement element) {
    // if (element instanceof IPackageDeclaration) return null;
    if (element instanceof IImportContainer) {
        return element.getParent();
    } else if (element instanceof IImportDeclaration) {
        return element.getParent().getParent();
    } else {
        return element;
    }
}

From source file:org.eclipse.mylyn.internal.java.ui.JavaStructureBridge.java

License:Open Source License

@Override
public String getParentHandle(String handle) {
    IJavaElement javaElement = (IJavaElement) getObjectForHandle(handle);
    if (javaElement != null && javaElement.getParent() != null) {
        return getHandleIdentifier(javaElement.getParent());
    } else {/*w ww.ja va2  s  .  co m*/
        return null;
    }
}

From source file:org.eclipse.mylyn.internal.java.ui.JavaStructureBridge.java

License:Open Source License

@Override
public String getHandleForOffsetInObject(Object object, int offset) {
    IMarker marker;/*www  .j av  a 2 s  .c om*/
    if (object instanceof ConcreteMarker) {
        marker = ((ConcreteMarker) object).getMarker();
    } else if (object instanceof Marker) {
        marker = (Marker) object;
    } else {
        return null;
    }

    try {
        ICompilationUnit compilationUnit = null;
        IResource resource = marker.getResource();
        if (resource instanceof IFile) {
            IFile file = (IFile) resource;
            // TODO: get rid of file extension check
            if (file.getFileExtension().equals("java")) { //$NON-NLS-1$
                compilationUnit = JavaCore.createCompilationUnitFrom(file);
            } else {
                return null;
            }
        }
        if (compilationUnit != null) {
            // first try to resolve the character start, then the line number if not present
            int charStart = 0;
            Object attribute = marker.getAttribute(IMarker.CHAR_START, 0);
            if (attribute instanceof Integer) {
                charStart = ((Integer) attribute).intValue();
            }
            IJavaElement javaElement = null;
            if (charStart != -1) {
                javaElement = compilationUnit.getElementAt(charStart);
            } else {
                int lineNumber = 0;
                Object lineNumberAttribute = marker.getAttribute(IMarker.LINE_NUMBER, 0);
                if (lineNumberAttribute instanceof Integer) {
                    lineNumber = ((Integer) lineNumberAttribute).intValue();
                }
                if (lineNumber != -1) {
                    // could do finer granularity by uncommenting what's below, see bug 132092
                    //                  Document document = new Document(compilationUnit.getSource());
                    //                  IRegion region = document.getLineInformation(lineNumber);
                    //                  javaElement = compilationUnit.getElementAt(region.getOffset());
                    javaElement = compilationUnit;
                }
            }

            if (javaElement != null) {
                if (javaElement instanceof IImportDeclaration) {
                    javaElement = javaElement.getParent().getParent();
                }
                return javaElement.getHandleIdentifier();
            } else {
                return null;
            }
        } else {
            return null;
        }
    } catch (JavaModelException ex) {
        if (!ex.isDoesNotExist()) {
            ExceptionHandler.handle(ex, "error", "could not find java element"); //$NON-NLS-1$ //$NON-NLS-2$
        }
        return null;
    } catch (Throwable t) {
        StatusHandler.log(new Status(IStatus.ERROR, JavaUiBridgePlugin.ID_PLUGIN, "Could not find element for: " //$NON-NLS-1$
                + marker, t));
        return null;
    }
}

From source file:org.eclipse.mylyn.java.tests.InteractionContextManagerTest.java

License:Open Source License

public void testIncremenOfParentDoi() throws JavaModelException, Exception {
    IWorkbenchPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart();
    IMethod m1 = type1.createMethod("void m1() { }", null, true, null);
    IInteractionElement node = ContextCore.getContextManager().getElement(m1.getHandleIdentifier());
    assertFalse(node.getInterest().isInteresting());

    StructuredSelection sm1 = new StructuredSelection(m1);
    monitor.selectionChanged(part, sm1);
    node = ContextCore.getContextManager().getElement(m1.getHandleIdentifier());
    assertTrue(node.getInterest().isInteresting());

    project.build();//from  w w  w  .  jav  a  2  s.c o  m
    IJavaElement parent = m1.getParent();
    @SuppressWarnings("unused")
    int level = 1;
    do {
        level++;
        IInteractionElement parentNode = ContextCore.getContextManager()
                .getElement(parent.getHandleIdentifier());
        if (!(parent instanceof JavaModel)) {
            assertEquals("failed on: " + parent.getClass(), node.getInterest().getValue(),
                    parentNode.getInterest().getValue());
        }
        parent = parent.getParent();
    } while (parent != null);
}

From source file:org.eclipse.mylyn.java.tests.InteractionContextManagerTest.java

License:Open Source License

public void testIncremenOfParentDoiAfterForcedDecay() throws JavaModelException, Exception {
    IWorkbenchPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart();
    IMethod m1 = type1.createMethod("void m1() { }", null, true, null);
    IMethod m2 = type1.createMethod("void m2() { }", null, true, null);
    IInteractionElement node = ContextCore.getContextManager().getElement(m1.getHandleIdentifier());
    assertFalse(node.getInterest().isInteresting());

    monitor.selectionChanged(part, new StructuredSelection(m1));
    node = ContextCore.getContextManager().getElement(m1.getHandleIdentifier());
    assertTrue(node.getInterest().isInteresting());

    // make all the parents interest propated to have negative interest
    IJavaElement parent = m1.getParent();
    @SuppressWarnings("unused")
    int level = 1;
    do {/*from  w w w  .j  a v a  2  s.  c  om*/
        level++;
        IInteractionElement parentNode = ContextCore.getContextManager()
                .getElement(parent.getHandleIdentifier());
        if (!(parent instanceof JavaModel)) {
            assertTrue(parentNode.getInterest().isInteresting());
            ContextCore.getContextManager().processInteractionEvent(mockInterestContribution(
                    parentNode.getHandleIdentifier(), -2 * parentNode.getInterest().getValue()));
            IInteractionElement updatedParent = ContextCore.getContextManager()
                    .getElement(parent.getHandleIdentifier());
            assertFalse(updatedParent.getInterest().isInteresting());
        }
        parent = parent.getParent();
    } while (parent != null);

    //      assertFalse(node.getInterest().isInteresting());

    // select the element, should propagate up
    monitor.selectionChanged(part, new StructuredSelection(m2));
    monitor.selectionChanged(part, new StructuredSelection(m1));
    node = ContextCore.getContextManager().getElement(m1.getHandleIdentifier());
    assertTrue(node.getInterest().isInteresting());

    project.build();
    parent = m1.getParent();
    level = 1;
    do {
        level++;
        IInteractionElement parentNode = ContextCore.getContextManager()
                .getElement(parent.getHandleIdentifier());
        if (!(parent instanceof JavaModel)) {
            assertTrue(parentNode.getInterest().isInteresting());
            //            assertEquals("failed on: " + parent.getClass(), node.getInterest().getValue(), parentNode.getInterest()
            //                  .getValue());
        }
        parent = parent.getParent();
    } while (parent != null);
}