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:navclus.userinterface.classdiagram.java.analyzer.RootModel.java

License:Open Source License

public void addJavaFile(IJavaElement _element) throws JavaModelException {
    TypeHistory.setCurElement(_element);
    if (_element instanceof ICompilationUnit) {
        this.openCU((ICompilationUnit) _element);
    } else if (_element instanceof IClassFile) {
        this.openClass((IClassFile) _element);
    } else if (_element instanceof IType) {
        this.createType((IType) _element);
    } else if (_element instanceof IMember) {
        _element = _element.getAncestor(IJavaElement.TYPE);
        this.createType((IType) _element);
    } else {/*w w w. j  a va 2  s  . com*/
        MessageDialog.openInformation(PlugIn.getDefaultShell(), "Error",
                "Cannot open this kind of Java Element:" + _element);
    }
}

From source file:navclus.userinterface.classdiagram.java.analyzer.RootModel.java

License:Open Source License

private TypeNode getTypeNode(IJavaElement locElement) {
    IType locType = (IType) locElement.getAncestor(IJavaElement.TYPE);

    TypeNode locNode = rootNode.findNode(locType);
    if (locNode == null) {
        locNode = createType(locType);//  w ww.  ja  v a  2  s . c om

        // drawing?
        //         rootNode.synchronizeNodesinView();
    }

    return locNode;
}

From source file:navclus.userinterface.classdiagram.java.analyzer.RootModel.java

License:Open Source License

public boolean addElement(IType topType, IJavaElement locElement) {

    boolean bExist = false;

    if (topType == null)
        return false;
    if (locElement == null)
        return false;
    if (topType.getHandleIdentifier().equals(locElement.getHandleIdentifier()))
        return false;

    // finding the node having itypeTop
    // if finding the node, add the javaelement methods
    IType locType = (IType) locElement.getAncestor(IJavaElement.TYPE);
    TypeNode topNode = rootNode.findNode(topType);
    TypeNode locNode = rootNode.findNode(locType);
    if (topNode == null)
        return false;

    boolean bCreateType = false;
    if (locNode == null) {
        locNode = createType(locType);/*from   w  w  w  . j  a va  2  s.co  m*/
        //         rootNode.synchronizeNodesinView();
        bCreateType = true;
    }

    if (locNode != null) {
        if (!topType.getHandleIdentifier().equals(locType.getHandleIdentifier())) {
            topNode.embeddedTypes.add(locType);
            TypeHistory.setPreType(locType);
        }

        if (locElement instanceof IMethod) {
            bExist = locNode.addMethod((IMethod) locElement);
        } else if (locElement instanceof IField) {
            bExist = locNode.addField((IField) locElement);
        } else if (bCreateType)
            return true;
        else
            return false;
    }

    if (bExist) {
        return true;
    } else
        return false;
}

From source file:navclus.userinterface.classdiagram.java.analyzer.TypeModel.java

License:Open Source License

public boolean removeElement(IType topType, IJavaElement locElement) {
    if (topType == null)
        return false;
    if (locElement == null)
        return false;
    if (topType.getHandleIdentifier().equals(locElement.getHandleIdentifier()))
        return false;

    // finding the node having itypeTop
    // if finding the node, add the javaelement methods
    IType locType = (IType) locElement.getAncestor(IJavaElement.TYPE);
    TypeNode topNode = rootNode.findNode(topType);
    TypeNode locNode = rootNode.findNode(locType);
    if (topNode == null)
        return false;
    if (locNode == null)
        return false;

    if (IsRemoved(locNode, locElement)) {
        //         rootNode.synchronizeNodesinView();
        return true;
    } else/*w  w w .j a  va 2s . co m*/
        return false;
}

From source file:navclus.userinterface.classdiagram.utils.JavaEditorUtil.java

License:Open Source License

public static IType getType(IJavaElement javaelement) {
    if (javaelement instanceof IType) {
        return (IType) javaelement;
    } else if (javaelement instanceof IMethod) {
        return (IType) javaelement.getAncestor(IJavaElement.TYPE);
    } else if (javaelement instanceof IField) {
        return (IType) javaelement.getAncestor(IJavaElement.TYPE);
    } else/*  w ww  .  j ava  2s . c o m*/
        return null;
}

From source file:navclus.userinterface.classdiagram.utils.TypeHistory.java

License:Open Source License

public static void setPreElement(IJavaElement preElement) {
    if (preElement == null)
        return;/*  w w w .ja  v a2  s.  c om*/

    TypeHistory.preElement = preElement;

    if (preElement instanceof ICompilationUnit) {
        IType[] types;

        try {
            types = ((ICompilationUnit) preElement).getTypes();

            if (types.length > 0)
                setPreType(types[0]);
            else
                return;

        } catch (JavaModelException e) {
            e.printStackTrace();
        }
        return;
    } else if (preElement instanceof IClassFile) {
        setPreType(((IClassFile) preElement).getType());
    } else if (preElement instanceof IType) {
        setPreType((IType) preElement);
    } else if (preElement instanceof IMember) {
        setPreType((IType) preElement.getAncestor(IJavaElement.TYPE));
    } else {
        MessageDialog.openInformation(PlugIn.getDefaultShell(), "Error",
                "Cannot open this kind of Java Element:" + preElement);
    }
}

From source file:navclus.userinterface.classdiagram.utils.TypeHistory.java

License:Open Source License

public static void setCurElement(IJavaElement curElement) {
    if (curElement == null)
        return;//from   ww  w . j  av a2 s  . c o  m

    TypeHistory.curElement = curElement;

    if (curElement instanceof ICompilationUnit) {
        IType[] types;

        try {
            types = ((ICompilationUnit) curElement).getTypes();

            if (types.length > 0)
                setCurType(types[0]);
            else
                return;

        } catch (JavaModelException e) {
            e.printStackTrace();
        }
        return;
    } else if (curElement instanceof IClassFile) {
        setCurType(((IClassFile) curElement).getType());
    } else if (curElement instanceof IType) {
        setCurType((IType) curElement);
    } else if (curElement instanceof IMember) {
        setCurType((IType) curElement.getAncestor(IJavaElement.TYPE));
    } else {
        MessageDialog.openInformation(PlugIn.getDefaultShell(), "Error",
                "Cannot open this kind of Java Element:" + curElement);
    }
}

From source file:net.hillsdon.testlink.model.impl.JavaSelection.java

License:Open Source License

/**
 * Find the type best associated with the java element.
 *
 * @param javaElement A java element.//from w  ww  .j  a  va  2  s.  c o  m
 * @return A type, or null if we can't figure one out.
 * @throws JavaModelException If we fail to read from the java model.
 */
public IType getSelectedType() throws JavaModelException {
    final IJavaElement javaElement = getSelectedJavaElement();
    IType type = null;
    if (javaElement != null) {
        type = getOutermostType(javaElement);
        if (type == null) {
            ICompilationUnit cu = (ICompilationUnit) javaElement.getAncestor(IJavaElement.COMPILATION_UNIT);
            if (cu != null) {
                IType[] types = cu.getTypes();
                if (types.length > 0) {
                    type = types[0];
                }
            }
        }
    }
    return type;
}

From source file:net.officefloor.eclipse.classpath.ClasspathUtil.java

License:Open Source License

/**
 * Obtains the class name of the input {@link IJavaElement}.
 * //from w  w  w .  jav  a2  s .  c o m
 * @param javaElement
 *            {@link IJavaElement}.
 * @return Class name or <code>null</code> if {@link IJavaElement} not a
 *         class or contained within a class.
 */
public static String getClassName(IJavaElement javaElement) {

    // Find the type
    IType type;
    if (javaElement instanceof IType) {
        type = (IType) javaElement;
    } else if (javaElement instanceof IClassFile) {
        type = ((IClassFile) javaElement).getType();
    } else if (javaElement instanceof ICompilationUnit) {
        ICompilationUnit unit = (ICompilationUnit) javaElement;

        // Strip extension from name
        String name = javaElement.getElementName().split("\\.")[0];

        // Obtain the type
        type = unit.getType(name);
    } else {
        // Look upwards for type
        type = (IType) javaElement.getAncestor(IJavaElement.TYPE);
    }

    // Ensure have type
    if (type == null) {
        return null;
    }

    // Determine the fully qualified name
    return type.getFullyQualifiedName();
}

From source file:net.sf.commonclipse.popup.actions.JavaTypeViewerAction.java

License:Apache License

/**
 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
 *//*w  ww .ja  v  a  2 s . co  m*/
public void run(IAction action) {
    IType type = null;

    if (this.editor != null) {
        if (this.editor instanceof JavaEditor) {
            try {
                IJavaElement element = SelectionConverter.getElementAtOffset((JavaEditor) this.editor);
                if (element != null) {
                    type = (IType) element.getAncestor(IJavaElement.TYPE);
                }
            } catch (JavaModelException e) {
                // ignore, simply get the primary type
            }
        }

        if (type == null) {
            IWorkingCopyManager manager = JavaUI.getWorkingCopyManager();
            ICompilationUnit unit = manager.getWorkingCopy(this.editor.getEditorInput());

            type = unit.findPrimaryType();
        }
    }

    if (type != null) {
        runAction(action, type, new Shell());
    }
}