Search in sources :

Example 1 with AstEditor

use of org.eclipse.wb.internal.core.utils.ast.AstEditor in project windowbuilder by eclipse.

the class DisplayExpressionPropertyEditor method getText.

// //////////////////////////////////////////////////////////////////////////
// 
// Presentation
// 
// //////////////////////////////////////////////////////////////////////////
@Override
protected String getText(Property property) throws Exception {
    GenericProperty genericProperty = (GenericProperty) property;
    Expression expression = genericProperty.getExpression();
    if (expression != null) {
        AstEditor editor = genericProperty.getJavaInfo().getEditor();
        return editor.getSource(expression);
    }
    return null;
}
Also used : AstEditor(org.eclipse.wb.internal.core.utils.ast.AstEditor) Expression(org.eclipse.jdt.core.dom.Expression) GenericProperty(org.eclipse.wb.internal.core.model.property.GenericProperty)

Example 2 with AstEditor

use of org.eclipse.wb.internal.core.utils.ast.AstEditor in project windowbuilder by eclipse.

the class MethodInvocationArgumentAccessor method setExpression.

@Override
public boolean setExpression(final JavaInfo javaInfo, final String source) throws Exception {
    final List<String> defaultArguments = getMethodDescription(javaInfo).getDefaultArguments();
    // modify expression
    final MethodInvocation invocation = getMethodInvocation(javaInfo);
    if (invocation != null) {
        final AstEditor editor = javaInfo.getEditor();
        final Expression oldArgumentExpression = getArgumentExpression(invocation);
        final String argumentSource = source != null ? source : defaultArguments.get(m_index);
        if (!isSameSource(editor, oldArgumentExpression, argumentSource)) {
            ExecutionUtils.run(javaInfo, new RunnableEx() {

                @Override
                public void run() throws Exception {
                    editor.replaceExpression(oldArgumentExpression, argumentSource);
                    if (isDefaultArguments(javaInfo, invocation)) {
                        editor.removeEnclosingStatement(invocation);
                    }
                }
            });
        }
    } else if (source != null) {
        ExecutionUtils.run(javaInfo, new RunnableEx() {

            @Override
            public void run() throws Exception {
                String argumentsSource = getNewInvocationArguments();
                javaInfo.addMethodInvocation(m_methodSignature, argumentsSource);
            }

            private String getNewInvocationArguments() {
                List<String> arguments = Lists.newArrayList(defaultArguments);
                arguments.set(m_index, source);
                return StringUtils.join(arguments.iterator(), ", ");
            }
        });
    }
    // success
    return true;
}
Also used : AstEditor(org.eclipse.wb.internal.core.utils.ast.AstEditor) CastExpression(org.eclipse.jdt.core.dom.CastExpression) Expression(org.eclipse.jdt.core.dom.Expression) RunnableEx(org.eclipse.wb.internal.core.utils.execution.RunnableEx) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 3 with AstEditor

use of org.eclipse.wb.internal.core.utils.ast.AstEditor in project windowbuilder by eclipse.

the class MethodInvocationArgumentAccessor method isDefaultArguments.

private boolean isDefaultArguments(JavaInfo javaInfo, MethodInvocation invocation) {
    AstEditor editor = javaInfo.getEditor();
    boolean allDefault = true;
    List<ParameterDescription> parameters = getMethodDescription(javaInfo).getParameters();
    List<Expression> arguments = DomGenerics.arguments(invocation);
    for (ParameterDescription parameter : parameters) {
        int index = parameter.getIndex();
        Expression argument = arguments.get(index);
        allDefault &= isDefaultArgument(editor, argument, parameter);
    }
    return allDefault;
}
Also used : AstEditor(org.eclipse.wb.internal.core.utils.ast.AstEditor) CastExpression(org.eclipse.jdt.core.dom.CastExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParameterDescription(org.eclipse.wb.internal.core.model.description.ParameterDescription)

Example 4 with AstEditor

use of org.eclipse.wb.internal.core.utils.ast.AstEditor in project windowbuilder by eclipse.

the class FieldAccessor method splitAssignmentsSequence.

private static Assignment splitAssignmentsSequence(JavaInfo javaInfo, Assignment targetAssignment, AssignmentsSequence sequence) throws Exception {
    Assignment newTargetAssignment = null;
    {
        AstEditor editor = javaInfo.getEditor();
        String assignedSource = editor.getSource(sequence.getAssignedExpression());
        Statement targetStatement = AstNodeUtils.getEnclosingStatement(targetAssignment);
        for (Assignment assignment : sequence.getAssignments()) {
            String source = editor.getSource(assignment.getLeftHandSide()) + " = " + assignedSource;
            // add new assignment
            StatementTarget target = new StatementTarget(targetStatement, false);
            Assignment newAssignment = (Assignment) javaInfo.addExpressionStatement(target, source);
            if (assignment == targetAssignment) {
                newTargetAssignment = newAssignment;
            }
            // prepare new statement target
            targetStatement = AstNodeUtils.getEnclosingStatement(newAssignment);
        }
        // remove old statement
        editor.removeEnclosingStatement(targetAssignment);
    }
    // use new target assignment
    Assert.isNotNull(newTargetAssignment);
    return newTargetAssignment;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) AstEditor(org.eclipse.wb.internal.core.utils.ast.AstEditor) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) Statement(org.eclipse.jdt.core.dom.Statement) StatementTarget(org.eclipse.wb.internal.core.utils.ast.StatementTarget)

Example 5 with AstEditor

use of org.eclipse.wb.internal.core.utils.ast.AstEditor in project windowbuilder by eclipse.

the class InvocationChildAssociationAccessor method setExpression.

@Override
public boolean setExpression(JavaInfo javaInfo, String source) throws Exception {
    // if given source is "null", use default source (but it also can be "null")
    final String newSource;
    if (source != null) {
        newSource = source;
    } else {
        newSource = m_defaultSource;
    }
    // if we have source to replace current, do this
    if (newSource != null) {
        final AstEditor editor = javaInfo.getEditor();
        final Expression oldExpression = getExpression(javaInfo);
        if (!editor.getSource(oldExpression).equals(source)) {
            ExecutionUtils.run(javaInfo, new RunnableEx() {

                @Override
                public void run() throws Exception {
                    editor.replaceExpression(oldExpression, newSource);
                }
            });
            return true;
        }
    }
    // no changes
    return false;
}
Also used : AstEditor(org.eclipse.wb.internal.core.utils.ast.AstEditor) Expression(org.eclipse.jdt.core.dom.Expression) RunnableEx(org.eclipse.wb.internal.core.utils.execution.RunnableEx)

Aggregations

AstEditor (org.eclipse.wb.internal.core.utils.ast.AstEditor)132 Expression (org.eclipse.jdt.core.dom.Expression)28 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)24 Statement (org.eclipse.jdt.core.dom.Statement)18 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)18 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)15 JavaInfo (org.eclipse.wb.core.model.JavaInfo)15 StatementTarget (org.eclipse.wb.internal.core.utils.ast.StatementTarget)15 BodyDeclarationTarget (org.eclipse.wb.internal.core.utils.ast.BodyDeclarationTarget)13 List (java.util.List)12 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)11 ConstructorCreationSupport (org.eclipse.wb.internal.core.model.creation.ConstructorCreationSupport)11 CreationSupport (org.eclipse.wb.internal.core.model.creation.CreationSupport)11 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)10 ASTNode (org.eclipse.jdt.core.dom.ASTNode)9 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)9 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)8 RunnableEx (org.eclipse.wb.internal.core.utils.execution.RunnableEx)8 Block (org.eclipse.jdt.core.dom.Block)7 SimpleName (org.eclipse.jdt.core.dom.SimpleName)7