GoTo Test: do not show 'Create Test' action if not applicable.
authorAnton Makeev <Anton.Makeev@jetbrains.com>
Mon, 9 Aug 2010 08:39:29 +0000 (12:39 +0400)
committerAnton Makeev <Anton.Makeev@jetbrains.com>
Mon, 9 Aug 2010 12:57:49 +0000 (16:57 +0400)
java/java-impl/src/com/intellij/testIntegration/JavaTestCreator.java
platform/lang-api/src/com/intellij/testIntegration/TestCreator.java
platform/lang-impl/src/com/intellij/testIntegration/GotoTestOrCodeHandler.java

index 3edb3d5bdc5c330701cf5fd2e977c87b57069fec..d85ac9212f16fe4c423413ceffdc062cd0977813 100644 (file)
  */
 package com.intellij.testIntegration;
 
  */
 package com.intellij.testIntegration;
 
+import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.project.Project;
-import com.intellij.psi.PsiFile;
 import com.intellij.psi.PsiElement;
 import com.intellij.psi.PsiElement;
+import com.intellij.psi.PsiFile;
 import com.intellij.testIntegration.createTest.CreateTestAction;
 import com.intellij.util.IncorrectOperationException;
 
 public class JavaTestCreator implements TestCreator {
 import com.intellij.testIntegration.createTest.CreateTestAction;
 import com.intellij.util.IncorrectOperationException;
 
 public class JavaTestCreator implements TestCreator {
+  private static final Logger LOG = Logger.getInstance("com.intellij.testIntegration.JavaTestCreator");
+
+
+  @Override
+  public boolean isAvailable(Project project, Editor editor, PsiFile file) {
+    CreateTestAction action = new CreateTestAction();
+    PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
+
+    return action.isAvailableForElement(element);
+  }
+
   public void createTest(Project project, Editor editor, PsiFile file) {
     try {
       CreateTestAction action = new CreateTestAction();
   public void createTest(Project project, Editor editor, PsiFile file) {
     try {
       CreateTestAction action = new CreateTestAction();
@@ -32,7 +44,7 @@ public class JavaTestCreator implements TestCreator {
       }
     }
     catch (IncorrectOperationException e) {
       }
     }
     catch (IncorrectOperationException e) {
-      throw new RuntimeException(e);
+      LOG.warn(e);
     }
   }
 }
     }
   }
 }
index bb426123e500d2009bf604914ed27e51db65541b..8cb85c1ed3aa35a5d7829ebc2fde7e0ee75ea1e9 100644 (file)
@@ -21,5 +21,6 @@ import com.intellij.openapi.project.Project;
 import com.intellij.psi.PsiFile;
 
 public interface TestCreator {
 import com.intellij.psi.PsiFile;
 
 public interface TestCreator {
+  boolean isAvailable(Project project, Editor editor, PsiFile file);
   void createTest(Project project, Editor editor, PsiFile file);
 }
   void createTest(Project project, Editor editor, PsiFile file);
 }
index 285a1b33ebe2db312a87f8963552c8164f68152a..48900b1271c56276f336f825227aca75dff8ed0e 100644 (file)
@@ -53,23 +53,25 @@ public class GotoTestOrCodeHandler extends GotoTargetHandler {
     }
     else {
       candidates = TestFinderHelper.findTestsForClass(selectedElement);
     }
     else {
       candidates = TestFinderHelper.findTestsForClass(selectedElement);
-      actions.add(new AdditionalAction() {
-        @Override
-        public String getText() {
-          return "Create New Test...";
-        }
+      final TestCreator creator = LanguageTestCreators.INSTANCE.forLanguage(file.getLanguage());
+      if (creator != null && creator.isAvailable(file.getProject(), editor, file)) {
+        actions.add(new AdditionalAction() {
+          @Override
+          public String getText() {
+            return "Create New Test...";
+          }
 
 
-        @Override
-        public Icon getIcon() {
-          return IconLoader.getIcon("/actions/intentionBulb.png");
-        }
+          @Override
+          public Icon getIcon() {
+            return IconLoader.getIcon("/actions/intentionBulb.png");
+          }
 
 
-        @Override
-        public void execute() {
-          final TestCreator creator = LanguageTestCreators.INSTANCE.forLanguage(file.getLanguage());
-          if (creator != null) creator.createTest(file.getProject(), editor, file);
-        }
-      });
+          @Override
+          public void execute() {
+            creator.createTest(file.getProject(), editor, file);
+          }
+        });
+      }
     }
 
     return new GotoData(sourceElement, candidates.toArray(new PsiElement[candidates.size()]), actions);
     }
 
     return new GotoData(sourceElement, candidates.toArray(new PsiElement[candidates.size()]), actions);