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.objectteams.otdt.internal.ui.wizards.typecreation.RoleCreator.java

License:Open Source License

private boolean hasCommonEnclosingTeam(IType type, ITypeBinding binding) {
    IJavaElement currentElement = type;
    while ((currentElement = currentElement.getParent()) instanceof IType) {
        IType currentType = (IType) currentElement;
        if (OTModelManager.isTeam(currentType)) {
            ITypeBinding currentBinding = binding;
            while ((currentBinding = currentBinding.getDeclaringClass()) != null) {
                if (currentBinding.getQualifiedName().equals(currentType.getFullyQualifiedName('.')))
                    return true;
            }//w  w  w  .  j a  v a  2 s . c o  m
        }
    }
    return false;
}

From source file:org.eclipse.objectteams.otdt.tests.AbstractJavaModelTests.java

License:Open Source License

protected ICompilationUnit getCompilationUnitFor(IJavaElement element) {

    if (element instanceof ICompilationUnit) {
        return (ICompilationUnit) element;
    }/*from  w w w  .j  a  v  a 2s.  c o  m*/

    if (element instanceof IMember) {
        return ((IMember) element).getCompilationUnit();
    }

    if (element instanceof IPackageDeclaration || element instanceof IImportDeclaration) {
        return (ICompilationUnit) element.getParent();
    }

    return null;

}

From source file:org.eclipse.objectteams.otdt.ui.tests.hierarchy.contentprovider.TreeNode.java

License:Open Source License

private void toString(StringBuffer buf, int indent) {
    for (int i = 0; i < indent; i++)
        buf.append(' ');
    IJavaElement element = (IJavaElement) this._element;
    buf.append(element.getParent().getElementName()).append('.').append(element.getElementName()).append('\n');
    for (Entry<Object, TreeNode> e : this._children.entrySet())
        e.getValue().toString(buf, 4);//from   w  ww.  jav a  2  s.  c o  m
}

From source file:org.eclipse.oomph.manifests.OpenManifestHandler.java

License:Open Source License

@Override
@SuppressWarnings("restriction")
protected void executeWithElement(IWorkbenchPage page, Object element) throws Exception {
    if (element == null) {
        IEditorPart editor = page.getActiveEditor();
        if (editor != null) {
            element = editor.getEditorInput();
        }/*from   w  w  w. ja  v  a  2  s  .c om*/
    }

    IPluginModelBase pluginModelBase = ObjectUtil.adapt(element, IPluginModelBase.class);
    if (pluginModelBase != null) {
        org.eclipse.pde.internal.ui.editor.plugin.ManifestEditor.openPluginEditor(pluginModelBase);
        return;
    }

    BundleDescription bundleDescription = ObjectUtil.adapt(element, BundleDescription.class);
    if (bundleDescription != null) {
        org.eclipse.pde.internal.ui.editor.plugin.ManifestEditor.openPluginEditor(bundleDescription);
        return;
    }

    IJavaElement javaElement = ObjectUtil.adapt(element, IJavaElement.class);
    while (javaElement != null && javaElement.getElementType() != IJavaElement.PACKAGE_FRAGMENT_ROOT) {
        javaElement = javaElement.getParent();
    }

    if (javaElement != null) {
        IPackageFragmentRoot root = (IPackageFragmentRoot) javaElement;
        IEditorInput editorInput = getManifestEditorInputStorage(root);
        if (editorInput != null) {
            org.eclipse.pde.internal.ui.editor.plugin.ManifestEditor.openEditor(editorInput);
        }
    }
}

From source file:org.eclipse.pde.api.tools.ui.internal.completion.APIToolsJavadocCompletionProposalComputer.java

License:Open Source License

/**
 * Returns the type of the enclosing type.
 * //from w ww . j  a  va  2 s.c  o  m
 * @param element java element
 * @return TYPE_INTERFACE, TYPE_CLASS, TYPE_ENUM, TYPE_ANNOTATION or -1
 * @throws JavaModelException
 */
private int getType(IJavaElement element) throws JavaModelException {
    IJavaElement lelement = element;
    while (lelement != null && lelement.getElementType() != IJavaElement.TYPE) {
        lelement = lelement.getParent();
    }
    if (lelement instanceof IType) {
        IType type = (IType) lelement;
        if (type.isAnnotation()) {
            return IApiJavadocTag.TYPE_ANNOTATION;
        } else if (type.isInterface()) {
            return IApiJavadocTag.TYPE_INTERFACE;
        } else if (type.isEnum()) {
            return IApiJavadocTag.TYPE_ENUM;
        }
    }
    return IApiJavadocTag.TYPE_CLASS;
}

From source file:org.eclipse.pde.api.tools.ui.internal.JavaElementActionFilter.java

License:Open Source License

/**
 * @see org.eclipse.ui.IActionFilter#testAttribute(Object, String, String)
 *//*from   w w w .j  ava2s .c om*/
@Override
public boolean testAttribute(Object target, String name, String value) {
    if (name.equals("JavaElementActionFilter")) { //$NON-NLS-1$
        if (target instanceof IJavaElement) {
            IJavaElement javaElement = (IJavaElement) target;
            if (value.equals("isEnabled")) { //$NON-NLS-1$
                while (javaElement != null) {
                    switch (javaElement.getElementType()) {
                    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
                        IPackageFragmentRoot root = (IPackageFragmentRoot) javaElement;
                        return !root.isArchive();
                    case IJavaElement.PACKAGE_FRAGMENT:
                    case IJavaElement.COMPILATION_UNIT:
                    case IJavaElement.CLASS_FILE:
                    case IJavaElement.TYPE:
                        javaElement = javaElement.getParent();
                        break;
                    case IJavaElement.ANNOTATION:
                    case IJavaElement.FIELD:
                    case IJavaElement.IMPORT_CONTAINER:
                    case IJavaElement.IMPORT_DECLARATION:
                    case IJavaElement.INITIALIZER:
                    case IJavaElement.JAVA_MODEL:
                    case IJavaElement.LOCAL_VARIABLE:
                    case IJavaElement.METHOD:
                    case IJavaElement.PACKAGE_DECLARATION:
                    case IJavaElement.TYPE_PARAMETER:
                        return false;
                    case IJavaElement.JAVA_PROJECT:
                        return true;
                    default:
                        break;
                    }
                }
                return true;
            }
        }
    }
    return false;
}

From source file:org.eclipse.vjet.eclipse.javalaunch.utils.EclipseResourceUtils.java

License:Open Source License

public static File getSourceRoot(IJavaProject project, IJavaElement e) {
    try {/*from   w  ww  . ja v a  2  s.co  m*/
        IPackageFragment frag = project.findPackageFragment(e.getPath());
        while (frag.isDefaultPackage() == false) {
            e = e.getParent();
            frag = project.findPackageFragment(e.getPath());
        }
        File f = e.getResource().getLocation().toFile();
        return f;
    } catch (JavaModelException e1) {
        throw new RuntimeException(e1);
    }
}

From source file:org.eclipse.wb.internal.core.databinding.wizards.autobindings.AutomaticDatabindingWizard.java

License:Open Source License

/**
 * @return the fully qualified name of class if given {@link IStructuredSelection} contains .java
 *         file./*from   w ww  . j av  a  2 s  .c  o  m*/
 */
protected static String getSelectionBeanClass(IStructuredSelection selection) {
    try {
        // check no selection
        if (UiUtils.isEmpty(selection)) {
            return null;
        }
        // prepare selection object
        Object object = selection.getFirstElement();
        // check java selection
        if (object instanceof IJavaElement) {
            IJavaElement element = (IJavaElement) object;
            // find compilation unit
            while (element != null) {
                if (element instanceof ICompilationUnit) {
                    ICompilationUnit compilationUnit = (ICompilationUnit) element;
                    IType[] types = compilationUnit.getTypes();
                    // find main type
                    if (!ArrayUtils.isEmpty(types)) {
                        return StringUtils.defaultIfEmpty(types[0].getFullyQualifiedName(), null);
                    }
                    // wrong selection
                    return null;
                }
                // lookup to parent
                element = element.getParent();
            }
        }
    } catch (Throwable e) {
        DesignerPlugin.log(e);
    }
    // wrong selection
    return null;
}

From source file:org.eclipse.xtend.ide.javaconverter.ConvertJavaCodeHandler.java

License:Open Source License

private void collectCompilationUnits(IJavaElement elem, Set<ICompilationUnit> result) {
    try {//from w  ww.ja  va 2 s.  c o  m
        switch (elem.getElementType()) {
        case IJavaElement.TYPE:
            if (elem.getParent().getElementType() == IJavaElement.COMPILATION_UNIT) {
                result.add((ICompilationUnit) elem.getParent());
            }
            break;
        case IJavaElement.COMPILATION_UNIT:
            result.add((ICompilationUnit) elem);
            break;
        case IJavaElement.PACKAGE_FRAGMENT:
            Collections.addAll(result, ((IPackageFragment) elem).getCompilationUnits());
            break;
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            for (IJavaElement child : ((IPackageFragmentRoot) elem).getChildren()) {
                collectCompilationUnits(child, result);
            }
            break;
        }
    } catch (JavaModelException e) {
    }
}

From source file:org.eclipse.xtext.common.types.ui.notification.JavaBuilderState.java

License:Open Source License

private IPackageFragmentRoot getPackageFragmentRoot(final IJavaElement it) {
    IPackageFragmentRoot _switchResult = null;
    IJavaElement _parent = it.getParent();
    final IJavaElement parent = _parent;
    boolean _matched = false;
    if (parent instanceof IPackageFragmentRoot) {
        _matched = true;/*w w  w .j  a v  a2s.co m*/
        _switchResult = ((IPackageFragmentRoot) parent);
    }
    if (!_matched) {
        _switchResult = this.getPackageFragmentRoot(parent);
    }
    return _switchResult;
}