/*
 * Created by IntelliJ IDEA.
 * User: mike
 * Date: Aug 19, 2002
 * Time: 8:28:43 PM
 * To change template for new class use
 * Code Style | Class Templates options (Tools | IDE Options).
 */
package com.intellij.codeInsight.daemon.impl.quickfix;

import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.generation.surroundWith.JavaWithTryCatchSurrounder;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInsight.CodeInsightUtil;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.LogicalPosition;
import com.intellij.openapi.editor.ScrollType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;

public class SurroundWithTryCatchAction implements IntentionAction {
  private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.SurroundWithTryCatchAction");
  private PsiStatement myStatement;

  public SurroundWithTryCatchAction(PsiElement element) {
    myStatement = PsiTreeUtil.getNonStrictParentOfType(element, PsiStatement.class);
  }

  public String getText() {
    return QuickFixBundle.message("surround.with.try.catch.fix");
  }

  public String getFamilyName() {
    return QuickFixBundle.message("surround.with.try.catch.fix");
  }

  public boolean isAvailable(Project project, Editor editor, PsiFile file) {
    if(myStatement instanceof PsiExpressionStatement && ((PsiExpressionStatement) myStatement).getExpression() instanceof PsiMethodCallExpression) {
      PsiMethodCallExpression callExpr = (PsiMethodCallExpression) ((PsiExpressionStatement) myStatement).getExpression();
      PsiElement referenceName = callExpr.getMethodExpression().getReferenceNameElement();
      if (referenceName != null && referenceName.getText().equals(PsiKeyword.SUPER)) return false;
    }
    return myStatement != null && myStatement.isValid();
  }

  public void invoke(Project project, Editor editor, PsiFile file) {
    if (!CodeInsightUtil.prepareFileForWrite(file)) return;

    int col = editor.getCaretModel().getLogicalPosition().column;
    int line = editor.getCaretModel().getLogicalPosition().line;
    LogicalPosition pos = new LogicalPosition(0, 0);
    editor.getCaretModel().moveToLogicalPosition(pos);
    TextRange range = null;

    if (myStatement.getParent() instanceof PsiForStatement) {
      PsiForStatement forStatement = (PsiForStatement)myStatement.getParent();
      if (myStatement.equals(forStatement.getInitialization()) || myStatement.equals(forStatement.getUpdate())) {
        myStatement = forStatement;
      }
    }

    try{
      JavaWithTryCatchSurrounder handler = new JavaWithTryCatchSurrounder();
      range = handler.surroundElements(project, editor, new PsiElement[] {myStatement});
    }
    catch(IncorrectOperationException e){
      LOG.error(e);
    }
    LogicalPosition pos1 = new LogicalPosition(line, col);
    editor.getCaretModel().moveToLogicalPosition(pos1);
    if (range != null) {
      int offset = range.getStartOffset();
      editor.getCaretModel().moveToOffset(offset);
      editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
      editor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset());
    }
  }

  public boolean startInWriteAction() {
    return true;
  }

}

@@ -299,16 +299,84 @@
 Bundle;%0A
+import com.intellij.codeInsight.daemon.impl.analysis.HighlightUtil;%0A
 import c
@@ -1635,16 +1635,17 @@
 %7B%0A    if
+ 
 (myState
@@ -1653,205 +1653,184 @@
 ent 
-instanceof PsiExpressionStatement && ((PsiExpressionStatement) myStatement).getExpression() instanceof PsiMethodCallExpression) %7B%0A      PsiMethodCallExpression callExpr = (PsiMethodCallExpression) 
+== null %7C%7C !myStatement.isValid()) %7B%0A      return false;%0A    %7D%0A    return !(myStatement instanceof PsiExpressionStatement) %7C%7C%0A           !HighlightUtil.isSuperOrThisMethodCall(
 ((Ps
@@ -1850,17 +1850,16 @@
 atement)
- 
 myStatem
@@ -1882,260 +1882,8 @@
 on()
-;%0A      PsiElement referenceName = callExpr.getMethodExpression().getReferenceNameElement();%0A      if (referenceName != null && referenceName.getText().equals(PsiKeyword.SUPER)) return false;%0A    %7D%0A    return myStatement != null && myStatement.isValid(
 );%0A 
@@ -2151,61 +2151,8 @@
 ne;%0A
-    LogicalPosition pos = new LogicalPosition(0, 0);%0A
     
@@ -2200,40 +2200,34 @@
 ion(
-pos);%0A    TextRange range = null
+new LogicalPosition(0, 0))
 ;%0A%0A 
@@ -2529,16 +2529,44 @@
  %7D%0A    %7D
+%0A    TextRange range = null;
 %0A%0A    tr
@@ -2831,17 +2831,16 @@
 tion pos
-1
  = new L
@@ -2918,17 +2918,16 @@
 tion(pos
-1
 );%0A    i

fix test