/*
 * Copyright 2007-2008 Dave Griffith
 *
 * 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 org.jetbrains.plugins.groovy.codeInspection.naming;

import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.refactoring.RefactoringActionHandler;
import com.intellij.refactoring.RefactoringActionHandlerFactory;
import com.intellij.refactoring.RefactoringFactory;
import com.intellij.refactoring.RenameRefactoring;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.codeInspection.GroovyFix;

public class RenameFix extends GroovyFix {

  private final String targetName;

  public RenameFix() {
    super();
    targetName = null;
  }

  public RenameFix(@NonNls String targetName) {
    super();
    this.targetName = targetName;
  }

  @Override
  @NotNull
  public String getName() {
    if (targetName == null) {
      return "Rename";
    } else {
      return "Rename to " + targetName;
    }
  }

  @Override
  public void doFix(@NotNull final Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement nameIdentifier = descriptor.getPsiElement();
    final PsiElement elementToRename = nameIdentifier.getParent();
    if (targetName == null) {
      final RefactoringActionHandlerFactory factory =
          RefactoringActionHandlerFactory.getInstance();
      final RefactoringActionHandler renameHandler =
          factory.createRenameHandler();
      final DataManager dataManager = DataManager.getInstance();
      final DataContext dataContext = dataManager.getDataContext();
      renameHandler.invoke(project, new PsiElement[]{elementToRename}, dataContext); }
    else {
      final RefactoringFactory factory =
          RefactoringFactory.getInstance(project);
      final RenameRefactoring renameRefactoring =
          factory.createRename(elementToRename, targetName);
      renameRefactoring.run();
    }
  }

  @Override
  public boolean startInWriteAction() {
    return false;
  }
}
@@ -730,77 +730,42 @@
 lij.
-ide.DataManager;%0Aimport com.intellij.openapi.actionSystem.DataContext
+codeInspection.RefactoringQuickFix
 ;%0Aim
@@ -1022,59 +1022,8 @@
 ry;%0A
-import com.intellij.refactoring.RenameRefactoring;%0A
 impo
@@ -1205,16 +1205,47 @@
 oovyFix 
+implements RefactoringQuickFix 
 %7B%0A%0A  pri
@@ -1733,30 +1733,23 @@
 Element 
-nameIdentifier
+element
  = descr
@@ -1779,478 +1779,84 @@
     
-final PsiElement elementToRename = nameIdentifier.getParent();%0A    if (targetName == null) %7B%0A      final RefactoringActionHandlerFactory factory =%0A          RefactoringActionHandlerFactory.getInstance();%0A      final RefactoringActionHandler renameHandler =%0A          factory.createRenameHandler();%0A      final DataManager dataManager = DataManager.getInstance();%0A      final DataContext dataContext = dataManager.getDataContext();%0A      renameHandler.invoke(project, new
+if (targetName == null) %7B%0A      doFix(element);%0A    %7D%0A    else %7B%0A      final
  Psi
@@ -1866,11 +1866,9 @@
 ment
-%5B%5D%7B
+ 
 elem
@@ -1882,197 +1882,77 @@
 name
-%7D, dataContext); %7D%0A    else %7B%0A      final RefactoringFactory factory =%0A          RefactoringFactory.getInstance(project);%0A      final RenameRefactoring renameRefactoring =%0A          factory
+ = element.getParent();%0A      RefactoringFactory.getInstance(project)
 .cre
@@ -1993,33 +1993,8 @@
 ame)
-;%0A      renameRefactoring
 .run
@@ -2000,32 +2000,43 @@
 n();%0A    %7D%0A  %7D%0A%0A
+  @NotNull%0A
   @Override%0A  pu
@@ -2044,55 +2044,126 @@
 lic 
-boolean startInWriteAction() %7B%0A    return false
+RefactoringActionHandler getHandler() %7B%0A    return RefactoringActionHandlerFactory.getInstance().createRenameHandler()
 ;%0A  

use RefactoringQuickFix in Groovy