
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.refactoring.util;

import com.intellij.java.refactoring.JavaRefactoringBundle;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.JavaDirectoryService;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiMethod;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.usageView.UsageViewUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;

public final class RefactoringMessageUtil {

  @Nls
  public static String getIncorrectIdentifierMessage(String identifierName) {
    return JavaRefactoringBundle.message("0.is.not.a.legal.java.identifier", identifierName);
  }

  /**
   * @return null, if can create a class
   *         an error message, if cannot create a class
   *
   */
  public static String checkCanCreateClass(PsiDirectory destinationDirectory, String className) {
    PsiClass[] classes = JavaDirectoryService.getInstance().getClasses(destinationDirectory);
    VirtualFile file = destinationDirectory.getVirtualFile();
    for (PsiClass aClass : classes) {
      if (className.equals(aClass.getName())) {
        return JavaRefactoringBundle.message("directory.0.already.contains.1.named.2",
                                         file.getPresentableUrl(), UsageViewUtil.getType(aClass), className);
      }
    }
    @NonNls String fileName = className+".java";
    return checkCanCreateFile(destinationDirectory, fileName);
  }
  public static String checkCanCreateFile(PsiDirectory destinationDirectory, String fileName) {
    VirtualFile file = destinationDirectory.getVirtualFile();
    VirtualFile child = file.findChild(fileName);
    if (child != null) {
      return JavaRefactoringBundle.message("directory.0.already.contains.a.file.named.1",
                                       file.getPresentableUrl(), fileName);
    }
    return null;
  }

  public static String getGetterSetterMessage(String newName, String action, PsiMethod getter, PsiMethod setter) {
    String text;
    if (getter != null && setter != null) {
      text = JavaRefactoringBundle.message("getter.and.setter.methods.found.for.the.field.0", newName, action);
    }
    else if (getter != null) {
      text = JavaRefactoringBundle.message("getter.method.found.for.the.field.0", newName, action);
    }
    else {
      text = JavaRefactoringBundle.message("setter.method.found.for.the.field.0", newName, action);
    }
    return text;
  }

  public static void showNotSupportedForJspClassesError(final Project project, Editor editor, final @NlsContexts.DialogTitle String refactoringName, final String helpId) {
    String message = RefactoringBundle.getCannotRefactorMessage(JavaRefactoringBundle.message("refactoring.is.not.supported.for.jsp.classes"));
    CommonRefactoringUtil.showErrorHint(project, editor, message, refactoringName, helpId);
  }
}
@@ -741,16 +741,129 @@
 .NonNls;
+%0Aimport org.jetbrains.annotations.Nullable;%0A%0Aimport static org.jetbrains.annotations.Nls.Capitalization.Sentence;
 %0A%0Apublic
@@ -1136,24 +1136,16 @@
 ass%0A   *
-        
  an erro
@@ -1184,13 +1184,8 @@
 ass%0A
-   *%0A
    *
@@ -1194,32 +1194,74 @@
   public static 
+@Nls(capitalization = Sentence) @Nullable 
 String checkCanC
@@ -1688,32 +1688,36 @@
                 
+    
 file.getPresenta
@@ -1826,9 +1826,11 @@
 Name
-+
+ + 
 %22.ja
@@ -1901,16 +1901,17 @@
 e);%0A  %7D%0A
+%0A
   public
@@ -1914,24 +1914,66 @@
 blic static 
+@Nls(capitalization = Sentence) @Nullable 
 String check
@@ -2267,16 +2267,20 @@
 med.1%22,%0A
+    
         

annotate RefactoringMessageUtil#checkCanCreateClass and #checkCanCreateFile