/*
 * Copyright 2000-2010 JetBrains s.r.o.
 *
 * 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.intentions.conversions;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.refactoring.util.CommonRefactoringUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.actions.GroovyTemplatesFactory;
import org.jetbrains.plugins.groovy.annotator.intentions.CreateClassActionBase;
import org.jetbrains.plugins.groovy.intentions.GroovyIntentionsBundle;
import org.jetbrains.plugins.groovy.intentions.base.Intention;
import org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate;
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition;

/**
 * @author Maxim.Medvedev
 */
public class MoveClassToNewFileIntention extends Intention {
  @Override
  protected void processIntention(@NotNull PsiElement element, Project project, Editor editor) throws IncorrectOperationException {
    final GrTypeDefinition psiClass = (GrTypeDefinition)element;
    final String name = psiClass.getName();

    final PsiFile file = psiClass.getContainingFile();
    final String fileExtension = FileUtil.getExtension(file.getName());
    final String newFileName = name + "." + fileExtension;
    final PsiDirectory dir = file.getParent();
    if (dir != null) {
      if (dir.findFile(newFileName) != null) {
        if (!ApplicationManager.getApplication().isUnitTestMode()) {
          CommonRefactoringUtil
            .showErrorHint(project, editor, GroovyIntentionsBundle.message("file.exists", newFileName, dir.getName()), getFamilyName(), null);
        }
        return;
      }
    }

    final GroovyFile newFile = (GroovyFile)GroovyTemplatesFactory.createFromTemplate(dir, name, newFileName, "GroovyClass.groovy");
    final GrTypeDefinition template = newFile.getTypeDefinitions()[0];
    final PsiElement newClass = template.replace(psiClass);
    psiClass.delete();
    CreateClassActionBase.putCursor(project, newClass.getContainingFile(), newClass.getNavigationElement());
  }


  @NotNull
  @Override
  protected PsiElementPredicate getElementPredicate() {
    return new ClassNameDiffersFromFileNamePredicate(true);
  }
}

@@ -1468,16 +1468,154 @@
 dicate;%0A
+import org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment;%0Aimport org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes;%0A
 import o
@@ -1659,24 +1659,24 @@
 GroovyFile;%0A
-
 import org.j
@@ -2485,73 +2485,30 @@
     
-CommonRefactoringUtil%0A            .showErrorHint(project, editor,
+final String message =
  Gro
@@ -2577,16 +2577,88 @@
 tName())
+;%0A          CommonRefactoringUtil.showErrorHint(project, editor, message
 , getFam
@@ -2919,16 +2919,16 @@
 s()%5B0%5D;%0A
-
     fina
@@ -2979,16 +2979,541 @@
 Class);%0A
+    final GrDocComment docComment = psiClass.getDocComment();%0A    if (newClass instanceof GrTypeDefinition && docComment != null) %7B%0A      final GrDocComment newDoc = ((GrTypeDefinition)newClass).getDocComment();%0A      if (newDoc != null) %7B%0A        newDoc.replace(docComment);%0A      %7D%0A      else %7B%0A        final PsiElement parent = newClass.getParent();%0A        parent.addBefore(docComment, psiClass);%0A        parent.getNode().addLeaf(GroovyTokenTypes.mNLS, %22%5Cn%22, psiClass.getNode());%0A      %7D%0A      docComment.delete();%0A    %7D%0A
     psiC

move doc comment to new file with moved class