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

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

Introduction

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

Prototype

IJavaElement getAncestor(int ancestorType);

Source Link

Document

Returns this Java element or the first ancestor of this element that has the given type.

Usage

From source file:com.google.gdt.eclipse.designer.smart.actions.SmartGwtPropertyTester.java

License:Open Source License

/**
 * Tests property, can throw {@link Exception}.
 *//*from   w ww.j  a v  a2 s.  co  m*/
private static boolean testEx(Object receiver, String property) throws Exception {
    // prepare IJavaElement
    final IJavaElement element;
    if (receiver instanceof IAdaptable) {
        element = (IJavaElement) ((IAdaptable) receiver).getAdapter(IJavaElement.class);
    } else {
        element = null;
    }
    // Java tests
    {
        // prepare java element
        if (element == null || !element.exists()) {
            return false;
        }
        // do tests
        if (PROPERTY_IS_CONFIGURED.equals(property)) {
            IJavaProject javaProject = element.getJavaProject();
            boolean hasJar = javaProject.findType("com.smartgwt.client.widgets.BaseWidget") != null;
            if (hasJar) {
                IJavaElement pkg = element.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
                if (pkg != null) {
                    IResource resource = pkg.getUnderlyingResource();
                    ModuleDescription module = Utils.getSingleModule(resource);
                    return module != null && Utils.inheritsModule(module, "com.smartgwt.SmartGwt");
                }
            }
            return false;
        }
    }
    // unknown property
    throw new IllegalArgumentException(
            MessageFormat.format("Illegal property '{0}' for '{1}'.", property, receiver));
}

From source file:com.google.gdt.eclipse.designer.wizards.model.mvp.ViewWizard.java

License:Open Source License

public void init(IWorkbench workbench, IStructuredSelection selection) {
    try {//from  www.  ja v a  2s  .  c  o m
        Object selectedObject = selection.getFirstElement();
        if (selectedObject instanceof IJavaElement) {
            IJavaElement element = (IJavaElement) selectedObject;
            initialPkg = (IPackageFragment) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
        }
    } catch (Throwable e) {
        DesignerPlugin.log(e);
    }
}

From source file:com.google.gdt.eclipse.designer.wizards.model.service.ServiceWizard.java

License:Open Source License

public void init(IWorkbench workbench, IStructuredSelection selection) {
    if (selection != null) {
        Object selectedObject = selection.getFirstElement();
        // convert resource to Java model object
        if (selectedObject instanceof IResource) {
            IResource resource = (IResource) selectedObject;
            selectedObject = JavaCore.create(resource);
        }/*  w  w  w  .j a v a  2s  .c om*/
        // prepare selected package
        if (selectedObject instanceof IJavaElement) {
            IJavaElement selectedJavaElement = (IJavaElement) selectedObject;
            m_selectedPackage = (IPackageFragment) selectedJavaElement
                    .getAncestor(IJavaElement.PACKAGE_FRAGMENT);
        } else {
            m_selectedPackage = null;
        }
    }
}

From source file:com.google.gdt.eclipse.designer.wizards.ui.JUnitWizardPage.java

License:Open Source License

private void handleSelectClassUnderTest(IJavaElement element) {
    try {//from w  ww. j a  v a2s . com
        if (element == null || !element.exists() || element.getJavaProject() == null) {
            setErrorState();
        } else {
            IJavaProject javaProject = element.getJavaProject();
            if (Utils.isGWTProject(javaProject)) {
                IPackageFragmentRoot testSourceFragmentRoot = handleTestSourceFolder(javaProject);
                IPackageFragment elementPackage = handleTestPackage(element, testSourceFragmentRoot);
                // handle class under test
                IType classUnderTestType = (IType) element.getAncestor(IJavaElement.TYPE);
                if (classUnderTestType == null) {
                    ICompilationUnit compilationUnit = (ICompilationUnit) element
                            .getAncestor(IJavaElement.COMPILATION_UNIT);
                    if (compilationUnit != null) {
                        classUnderTestType = compilationUnit.findPrimaryType();
                    }
                }
                if (classUnderTestType == null) {
                    setErrorState();
                } else {
                    m_classUnderTestField.setText(classUnderTestType.getFullyQualifiedName());
                    setTypeName(classUnderTestType.getElementName() + "Test", true);
                    m_classUnderTestStatus = new Status(IStatus.OK, Activator.PLUGIN_ID, IStatus.OK, null,
                            null);
                    //
                    ModuleDescription module = Utils.getSingleModule(elementPackage);
                    if (module == null) {
                        setErrorState(
                                "GWT module for " + classUnderTestType.getFullyQualifiedName() + " not found.");
                    } else {
                        m_moduleId = module.getId();
                    }
                }
            } else {
                setErrorState();
            }
        }
    } catch (Throwable e) {
        DesignerPlugin.log(e);
        setErrorState("InternalError: " + e.getMessage());
    } finally {
        doStatusUpdate();
    }
}

From source file:com.google.gdt.eclipse.designer.wizards.ui.JUnitWizardPage.java

License:Open Source License

private IPackageFragment handleTestPackage(IJavaElement element, IPackageFragmentRoot testSourceFragmentRoot)
        throws Exception {
    IPackageFragment testPackage = null;
    IPackageFragment elementPackage = (IPackageFragment) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
    if (elementPackage != null) {
        testPackage = testSourceFragmentRoot.getPackageFragment(elementPackage.getElementName());
        if (testPackage == null || !testPackage.exists()) {
            testPackage = testSourceFragmentRoot.createPackageFragment(elementPackage.getElementName(), true,
                    null);/* w w w .  ja va2s .co m*/
        }
    }
    //
    setPackageFragment(testPackage, true);
    return elementPackage;
}

From source file:com.google.gwt.eclipse.core.wizards.NewModuleWizardPage.java

License:Open Source License

protected void initModulePage(IStructuredSelection selection) {
    IJavaElement jelem = getInitialJavaElement(selection);

    initContainerPage(jelem);//from  w ww  . j  a  v  a  2  s. com

    String pName = "";
    if (jelem != null) {
        IPackageFragment pf = (IPackageFragment) jelem.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
        if (pf != null) {
            pName = pf.getElementName();
        }
    }

    modulePackageField.setText(pName);

    modulePackageCompletionProcessor.setPackageFragmentRoot(getPackageFragmentRoot());

    moduleCreateElementsCheckboxes.setSelection(CLIENT_PACKAGE_CHECKBOX_GROUP_INDEX, true);
    moduleCreateElementsCheckboxes.setSelection(PUBLIC_PATH_CHECKBOX_GROUP_INDEX, true);

    doFieldChange(CONTAINER, null);
}

From source file:com.google.inject.tools.ideplugin.eclipse.EclipseJavaElement.java

License:Apache License

private String getType(IJavaElement element, String signature) {
    try {/*from ww w  . j a v a  2 s  .c o  m*/
        IType type = null;
        ICompilationUnit compilationUnit = (ICompilationUnit) element
                .getAncestor(IJavaElement.COMPILATION_UNIT);
        if (compilationUnit != null) {
            type = compilationUnit.getAllTypes()[0];

        } else {
            IClassFile classFile = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE);
            if (classFile != null) {
                type = classFile.getType();
            }
        }
        String resolvedSignature = TypeUtil.resolveTypeSignature(type, signature, false);
        String className = getClassNameFromResolvedSignature(resolvedSignature);
        return className;
    } catch (Throwable e) {
        return null;
    }
}

From source file:com.iw.plugins.spindle.ui.wizards.factories.ClassFactory.java

License:Mozilla Public License

/**
 * Examines a string and returns the first line delimiter found.
 *//*from   w  ww.  j  a  va  2s  . co m*/
public static String getLineDelimiterUsed(IJavaElement elem) throws JavaModelException {
    ICompilationUnit cu = (ICompilationUnit) elem.getAncestor(IJavaElement.COMPILATION_UNIT);
    if (cu != null && cu.exists()) {
        IBuffer buf = cu.getBuffer();
        int length = buf.getLength();
        for (int i = 0; i < length; i++) {
            char ch = buf.getChar(i);
            if (ch == SWT.CR) {
                if (i + 1 < length) {
                    if (buf.getChar(i + 1) == SWT.LF) {
                        return "\r\n"; //$NON-NLS-1$
                    }
                }
                return "\r"; //$NON-NLS-1$
            } else if (ch == SWT.LF) {
                return "\n"; //$NON-NLS-1$
            }
        }
    }
    return System.getProperty("line.separator", "\n");
}

From source file:com.microsoft.javapkgsrv.JavaElementLabelComposer.java

License:Open Source License

/**
 * Appends the label for a Java element with the flags as defined by this class.
 *
 * @param element the element to render//www  .ja  v a 2 s.c o  m
 * @param flags the rendering flags.
 */
public void appendElementLabel(IJavaElement element, long flags) {
    int type = element.getElementType();
    IPackageFragmentRoot root = null;

    if (type != IJavaElement.JAVA_MODEL && type != IJavaElement.JAVA_PROJECT
            && type != IJavaElement.PACKAGE_FRAGMENT_ROOT)
        root = (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
    if (root != null && getFlag(flags, PREPEND_ROOT_PATH)) {
        appendPackageFragmentRootLabel(root, ROOT_QUALIFIED);
        fBuffer.append(CONCAT_STRING);
    }

    switch (type) {
    case IJavaElement.METHOD:
        appendMethodLabel((IMethod) element, flags);
        break;
    case IJavaElement.FIELD:
        appendFieldLabel((IField) element, flags);
        break;
    case IJavaElement.LOCAL_VARIABLE:
        appendLocalVariableLabel((ILocalVariable) element, flags);
        break;
    case IJavaElement.TYPE_PARAMETER:
        appendTypeParameterLabel((ITypeParameter) element, flags);
        break;
    case IJavaElement.INITIALIZER:
        appendInitializerLabel((IInitializer) element, flags);
        break;
    case IJavaElement.TYPE:
        appendTypeLabel((IType) element, flags);
        break;
    case IJavaElement.CLASS_FILE:
        appendClassFileLabel((IClassFile) element, flags);
        break;
    case IJavaElement.COMPILATION_UNIT:
        appendCompilationUnitLabel((ICompilationUnit) element, flags);
        break;
    case IJavaElement.PACKAGE_FRAGMENT:
        appendPackageFragmentLabel((IPackageFragment) element, flags);
        break;
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
        appendPackageFragmentRootLabel((IPackageFragmentRoot) element, flags);
        break;
    case IJavaElement.IMPORT_CONTAINER:
    case IJavaElement.IMPORT_DECLARATION:
    case IJavaElement.PACKAGE_DECLARATION:
        appendDeclarationLabel(element, flags);
        break;
    case IJavaElement.JAVA_PROJECT:
    case IJavaElement.JAVA_MODEL:
        fBuffer.append(element.getElementName());
        break;
    default:
        fBuffer.append(element.getElementName());
    }

    if (root != null && getFlag(flags, APPEND_ROOT_PATH)) {
        int offset = fBuffer.length();
        fBuffer.append(CONCAT_STRING);
        appendPackageFragmentRootLabel(root, ROOT_QUALIFIED);
    }
}

From source file:com.siteview.mde.internal.core.JavaElementChangeListener.java

License:Open Source License

private void updateTable(IJavaElement element) {
    IJavaProject jProject = (IJavaProject) element.getAncestor(IJavaElement.JAVA_PROJECT);
    if (jProject != null) {
        IProject project = jProject.getProject();
        IMonitorModelBase model = MonitorRegistry.findModel(project);
        if (model != null) {
            String id = model.getMonitorBase().getId();
            if (id != null)
                fTable.put(id, Long.toString(System.currentTimeMillis()));
        }/*from   w w w.j  a  va 2  s  .  c o m*/
    }
}