/*
 * User: anna
 * Date: 07-May-2008
 */
package com.intellij.refactoring.replaceConstructorWithBuilder;

import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.HelpID;
import com.intellij.refactoring.RefactoringActionHandler;
import com.intellij.refactoring.util.CommonRefactoringUtil;
import org.jetbrains.annotations.NotNull;

public class ReplaceConstructorWithBuilderHandler implements RefactoringActionHandler {
  public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file, final DataContext dataContext) {
    final int offset = editor.getCaretModel().getOffset();
    final PsiElement element = file.findElementAt(offset);
    final PsiClass psiClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
    if (psiClass == null) {
      showErrorMessage("The caret should be positioned inside a class which constructors are to be replaced with builder.", project);
      return;
    }

    final PsiMethod[] constructors = psiClass.getConstructors();
    if (constructors.length == 0) {
      showErrorMessage("Current class doesn't have constructors to replace with builder.", project);
      return;
    }

    new ReplaceConstructorWithBuilderDialog(project, constructors).show();
  }

  public void invoke(@NotNull final Project project, @NotNull final PsiElement[] elements, final DataContext dataContext) {
    throw new UnsupportedOperationException();
  }

  private static void showErrorMessage(String message, Project project) {
    CommonRefactoringUtil.showErrorMessage(ReplaceConstructorWithBuilderProcessor.REFACTORING_NAME, message, HelpID.REPLACE_CONSTRUCTOR_WITH_BUILDER, project);
  }
}

@@ -270,120 +270,9 @@
 psi.
-PsiClass;%0Aimport com.intellij.psi.PsiElement;%0Aimport com.intellij.psi.PsiFile;%0Aimport com.intellij.psi.PsiMethod
+*
 ;%0Aim
@@ -510,16 +510,59 @@
 NotNull;
+%0Aimport org.jetbrains.annotations.Nullable;
 %0A%0Apublic
@@ -922,59 +922,35 @@
 s = 
-PsiTreeUtil.getParentOfType(element, PsiClass.class
+getParentNamedClass(element
 );%0A 
@@ -1099,32 +1099,40 @@
 ilder.%22, project
+, editor
 );%0A      return;
@@ -1334,24 +1334,32 @@
 r.%22, project
+, editor
 );%0A      ret
@@ -1450,16 +1450,305 @@
 );%0A  %7D%0A%0A
+  @Nullable%0A  private static PsiClass getParentNamedClass(PsiElement element) %7B%0A    final PsiClass psiClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);%0A    if (psiClass instanceof PsiAnonymousClass) %7B%0A      return getParentNamedClass(psiClass);%0A    %7D%0A    return psiClass;%0A  %7D%0A%0A
   public
@@ -1985,16 +1985,31 @@
  project
+, Editor editor
 ) %7B%0A    
@@ -2039,24 +2039,47 @@
 howError
-M
+Hint(project, editor, m
 essage
-(
+, 
 ReplaceC
@@ -2127,24 +2127,16 @@
 G_NAME, 
-message,
  HelpID.
@@ -2167,25 +2167,16 @@
 _BUILDER
-, project
 );%0A  %7D%0A%7D

replace constructor with builder: modal dialog -> hint; skip anonymous classes