/*
 * Copyright 2006 Bas Leijdekkers
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.siyeh.ipp.commutative;

import com.siyeh.ipp.base.MutablyNamedIntention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.IntentionPowerPackBundle;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;

public class SwapMethodCallArgumentsIntention extends MutablyNamedIntention {

    @NotNull
    protected PsiElementPredicate getElementPredicate() {
        return new SwapMethodCallArgumentsPredicate();
    }

    protected String getTextForElement(PsiElement element) {
        final PsiExpressionList expressionList = (PsiExpressionList)element;
        final PsiExpression[] expressions = expressionList.getExpressions();
        final PsiExpression firstExpression = expressions[0];
        final PsiExpression secondExpression = expressions[1];
        return IntentionPowerPackBundle.message(
                "swap.method.call.arguments.intention.name",
                firstExpression.getText(), secondExpression.getText());
    }

    protected void processIntention(@NotNull PsiElement element)
            throws IncorrectOperationException {
        final PsiExpressionList argumentList = (PsiExpressionList)element;
        final PsiExpression[] arguments = argumentList.getExpressions();
        final PsiExpression firstArgument = arguments[0];
        final PsiExpression secondArgument = arguments[1];
        final String firstArgumentText = firstArgument.getText();
        final String secondArgumentText = secondArgument.getText();
        final PsiCallExpression callExpression =
                (PsiCallExpression)argumentList.getParent();
        final String callText;
        if (callExpression instanceof PsiMethodCallExpression) {
            final PsiMethodCallExpression methodCallExpression =
                    (PsiMethodCallExpression)callExpression;
            final PsiReferenceExpression methodExpression =
                    methodCallExpression.getMethodExpression();
            callText = methodExpression.getText();
        } else if (callExpression instanceof PsiNewExpression) {
            final PsiNewExpression newExpression =
                    (PsiNewExpression)callExpression;
            final PsiJavaCodeReferenceElement classReference =
                    newExpression.getClassReference();
            assert classReference != null;
            callText = "new " + classReference.getText();
        } else {
            return;
        }
        final String newExpression = callText + '(' + secondArgumentText +
                ", " + firstArgumentText + ')';
        replaceExpression(newExpression, callExpression);
    }
}

@@ -855,41 +855,132 @@
 ort 
-org.jetbrains.annotations.NotNull
+com.intellij.openapi.util.text.StringUtil;%0Aimport org.jetbrains.annotations.NotNull;%0Aimport org.jetbrains.annotations.NonNls
 ;%0A%0Ap
@@ -1656,16 +1656,33 @@
         
+StringUtil.first(
 firstExp
@@ -1700,16 +1700,44 @@
 Text(), 
+20, true), StringUtil.first(
 secondEx
@@ -1754,16 +1754,27 @@
 etText()
+, 20, true)
 );%0A    %7D
@@ -2394,32 +2394,32 @@
 etParent();%0A
-
         
 final String
@@ -2398,32 +2398,40 @@
 rent();%0A        
+@NonNls 
 final String cal

truncate too long intention text