Example usage for org.eclipse.jdt.core CompletionContext getEnclosingElement

List of usage examples for org.eclipse.jdt.core CompletionContext getEnclosingElement

Introduction

In this page you can find the example usage for org.eclipse.jdt.core CompletionContext getEnclosingElement.

Prototype

public IJavaElement getEnclosingElement() 

Source Link

Document

Returns the innermost enclosing Java element which contains the completion location or null if this element cannot be computed.

Usage

From source file:at.bestsolution.efxclipse.tooling.jdt.ui.internal.FXBeanJavaCompletionProposalComputer.java

License:Open Source License

@Override
public List<ICompletionProposal> computeCompletionProposals(ContentAssistInvocationContext context,
        IProgressMonitor monitor) {/*from   w ww . ja  va2  s. c  o  m*/
    if (context instanceof JavaContentAssistInvocationContext && false) {
        JavaContentAssistInvocationContext javaContext = (JavaContentAssistInvocationContext) context;
        CompletionContext completionContext = javaContext.getCoreContext();
        IJavaElement enclosingElement = null;
        if (completionContext.isExtended()) {
            enclosingElement = completionContext.getEnclosingElement();
        } else {
            try {
                enclosingElement = javaContext.getCompilationUnit()
                        .getElementAt(context.getInvocationOffset() + 1);
            } catch (JavaModelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        List<ICompletionProposal> l = new ArrayList<ICompletionProposal>();

        if (enclosingElement != null) {
            IType type = (IType) enclosingElement.getAncestor(IJavaElement.TYPE);
            if (type == null) {
                return l;
            }

            try {
                IField[] fields = type.getFields();
                IMethod[] methods = type.getMethods();
                int offset = context.getInvocationOffset() - 3;
                if (offset > 0) {
                    String prefix = context.getDocument().get(offset, 3);
                    IType propType = type.getJavaProject().findType("javafx.beans.property.Property");
                    IType writableType = type.getJavaProject().findType("javafx.beans.value.WritableValue");

                    // Primitives
                    IType booleanType = type.getJavaProject()
                            .findType("javafx.beans.property.ReadOnlyBooleanProperty");
                    IType doubleType = type.getJavaProject()
                            .findType("javafx.beans.property.ReadOnlyDoubleProperty");
                    IType floatType = type.getJavaProject()
                            .findType("javafx.beans.property.ReadOnlyFloatProperty");
                    IType intType = type.getJavaProject()
                            .findType("javafx.beans.property.ReadOnlyIntegerProperty");
                    IType longType = type.getJavaProject()
                            .findType("javafx.beans.property.ReadOnlyLongProperty");
                    IType stringType = type.getJavaProject()
                            .findType("javafx.beans.property.ReadOnlyStringProperty");

                    for (int i = 0; i < fields.length; i++) {
                        IField curr = fields[i];
                        if (!Flags.isEnum(curr.getFlags())) {
                            IType fieldType = toType(type, curr.getTypeSignature());
                            if (fieldType != null) {
                                if (assignable(fieldType, propType)) {
                                    if ("set".equals(prefix)) {
                                        if (assignable(fieldType, writableType)) {
                                            String setterName = NamingConventions.suggestSetterName(
                                                    type.getJavaProject(), curr.getElementName(),
                                                    curr.getFlags(), false, null);
                                            if (!hasMethod(methods, setterName)) {
                                                StyledString s = new StyledString(setterName
                                                        + "(" + toValue(fieldType, booleanType, doubleType,
                                                                floatType, intType, longType, stringType)
                                                        + ") : void");
                                                s.append(" - Setter for '" + curr.getElementName() + "'",
                                                        StyledString.QUALIFIER_STYLER);
                                                l.add(new CompletionProposalImpl(setterName, s));
                                            }
                                        }
                                    } else if (Character.isWhitespace(prefix.charAt(0))
                                            && prefix.endsWith("is")) {
                                        if (assignable(fieldType, booleanType)) {
                                            String getterName = NamingConventions.suggestGetterName(
                                                    type.getJavaProject(), curr.getElementName(),
                                                    curr.getFlags(), false, null);
                                            getterName = "is" + getterName.substring(3);
                                            if (!hasMethod(methods, getterName)) {
                                                StyledString s = new StyledString(getterName + "() : boolean");
                                                s.append(" - Getter for '" + curr.getElementName() + "'",
                                                        StyledString.QUALIFIER_STYLER);
                                                l.add(new CompletionProposalImpl(getterName, s));
                                            }
                                        }
                                    } else if ("get".equals(prefix)) {
                                        if (!assignable(fieldType, booleanType)) {
                                            String getterName = NamingConventions.suggestGetterName(
                                                    type.getJavaProject(), curr.getElementName(),
                                                    curr.getFlags(), false, null);
                                            if (!hasMethod(methods, getterName)) {
                                                StyledString s = new StyledString(getterName + "() : "
                                                        + toValue(fieldType, booleanType, doubleType, floatType,
                                                                intType, longType, stringType));
                                                s.append(" - Getter for '" + curr.getElementName() + "'",
                                                        StyledString.QUALIFIER_STYLER);
                                                l.add(new CompletionProposalImpl(getterName, s));
                                            }
                                        }
                                    } else if (Character.isWhitespace(prefix.charAt(2))) {
                                        String propertyName = curr.getElementName() + "Property";
                                        if (!hasMethod(methods, propertyName)) {
                                            StyledString s = new StyledString(propertyName);
                                            l.add(new CompletionProposalImpl(propertyName, s));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            } catch (JavaModelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (BadLocationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return l;
    }
    return Collections.emptyList();
}

From source file:org.eclipse.fx.ide.jdt.ui.internal.FXBeanJavaCompletionProposalComputer.java

License:Open Source License

@Override
public List<ICompletionProposal> computeCompletionProposals(ContentAssistInvocationContext context,
        IProgressMonitor monitor) {//w w  w .j ava  2s. c  o m
    if (context instanceof JavaContentAssistInvocationContext && Boolean.FALSE == true) {
        JavaContentAssistInvocationContext javaContext = (JavaContentAssistInvocationContext) context;
        CompletionContext completionContext = javaContext.getCoreContext();
        IJavaElement enclosingElement = null;
        if (completionContext.isExtended()) {
            enclosingElement = completionContext.getEnclosingElement();
        } else {
            try {
                enclosingElement = javaContext.getCompilationUnit()
                        .getElementAt(context.getInvocationOffset() + 1);
            } catch (JavaModelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        List<ICompletionProposal> l = new ArrayList<ICompletionProposal>();

        if (enclosingElement != null) {
            IType type = (IType) enclosingElement.getAncestor(IJavaElement.TYPE);
            if (type == null) {
                return l;
            }

            try {
                IField[] fields = type.getFields();
                IMethod[] methods = type.getMethods();
                int offset = context.getInvocationOffset() - 3;
                if (offset > 0) {
                    String prefix = context.getDocument().get(offset, 3);
                    IType propType = type.getJavaProject().findType("javafx.beans.property.Property");
                    IType writableType = type.getJavaProject().findType("javafx.beans.value.WritableValue");

                    // Primitives
                    IType booleanType = type.getJavaProject()
                            .findType("javafx.beans.property.ReadOnlyBooleanProperty");
                    IType doubleType = type.getJavaProject()
                            .findType("javafx.beans.property.ReadOnlyDoubleProperty");
                    IType floatType = type.getJavaProject()
                            .findType("javafx.beans.property.ReadOnlyFloatProperty");
                    IType intType = type.getJavaProject()
                            .findType("javafx.beans.property.ReadOnlyIntegerProperty");
                    IType longType = type.getJavaProject()
                            .findType("javafx.beans.property.ReadOnlyLongProperty");
                    IType stringType = type.getJavaProject()
                            .findType("javafx.beans.property.ReadOnlyStringProperty");

                    for (int i = 0; i < fields.length; i++) {
                        IField curr = fields[i];
                        if (!Flags.isEnum(curr.getFlags())) {
                            IType fieldType = toType(type, curr.getTypeSignature());
                            if (fieldType != null) {
                                if (assignable(fieldType, propType)) {
                                    if ("set".equals(prefix)) {
                                        if (assignable(fieldType, writableType)) {
                                            String setterName = NamingConventions.suggestSetterName(
                                                    type.getJavaProject(), curr.getElementName(),
                                                    curr.getFlags(), false, null);
                                            if (!hasMethod(methods, setterName)) {
                                                StyledString s = new StyledString(setterName + "(" //$NON-NLS-1$
                                                        + toValue(fieldType, booleanType, doubleType, floatType,
                                                                intType, longType, stringType)
                                                        + ")" + " : void");
                                                s.append(" - Setter for '" + curr.getElementName() + "'", //$NON-NLS-1$//$NON-NLS-2$
                                                        StyledString.QUALIFIER_STYLER);
                                                l.add(new CompletionProposalImpl(setterName, s, setterName + "("
                                                        + toValue(fieldType, booleanType, doubleType, floatType,
                                                                intType, longType, stringType)
                                                        + " " + curr.getElementName() + ")", curr, Type.SETTER,
                                                        offset));
                                            }
                                        }
                                    } else if (Character.isWhitespace(prefix.charAt(0))
                                            && prefix.endsWith("is")) {
                                        if (assignable(fieldType, booleanType)) {
                                            String getterName = NamingConventions.suggestGetterName(
                                                    type.getJavaProject(), curr.getElementName(),
                                                    curr.getFlags(), false, null);
                                            getterName = "is" + getterName.substring(3);
                                            if (!hasMethod(methods, getterName)) {
                                                StyledString s = new StyledString(getterName + "() : boolean");
                                                s.append(" - Getter for '" + curr.getElementName() + "'",
                                                        StyledString.QUALIFIER_STYLER);
                                                l.add(new CompletionProposalImpl(getterName, s,
                                                        getterName + "()", curr, Type.GETTER, offset));
                                            }
                                        }
                                    } else if ("get".equals(prefix)) {
                                        if (!assignable(fieldType, booleanType)) {
                                            String getterName = NamingConventions.suggestGetterName(
                                                    type.getJavaProject(), curr.getElementName(),
                                                    curr.getFlags(), false, null);
                                            if (!hasMethod(methods, getterName)) {
                                                StyledString s = new StyledString(getterName + "() : "
                                                        + toValue(fieldType, booleanType, doubleType, floatType,
                                                                intType, longType, stringType));
                                                s.append(" - Getter for '" + curr.getElementName() + "'",
                                                        StyledString.QUALIFIER_STYLER);
                                                l.add(new CompletionProposalImpl(getterName, s,
                                                        toValue(fieldType, booleanType, doubleType, floatType,
                                                                intType, longType, stringType) + " "
                                                                + getterName + "()",
                                                        curr, Type.GETTER, offset));
                                            }
                                        }
                                    } else if (Character.isWhitespace(prefix.charAt(2))) {
                                        String propertyName = curr.getElementName() + "Property() : "
                                                + fieldType.getElementName();
                                        if (!hasMethod(methods, propertyName)) {
                                            StyledString s = new StyledString(propertyName);
                                            l.add(new CompletionProposalImpl(
                                                    propertyName, s, fieldType.getElementName() + " "
                                                            + curr.getElementName() + "Property()",
                                                    curr, Type.ACCESSOR, offset));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            } catch (JavaModelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (BadLocationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return l;
    }
    return Collections.emptyList();
}