/*
 * User: anna
 * Date: 23-May-2007
 */
package com.theoryinpractice.testng.configuration;

import com.intellij.execution.Location;
import com.intellij.execution.PsiLocation;
import com.intellij.execution.RunnerAndConfigurationSettings;
import com.intellij.execution.actions.ConfigurationContext;
import com.intellij.execution.impl.RunnerAndConfigurationSettingsImpl;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.util.PsiClassUtil;
import com.intellij.psi.util.PsiTreeUtil;
import com.theoryinpractice.testng.util.TestNGUtil;
import org.jetbrains.annotations.Nullable;

public class TestNGInClassConfigurationProducer extends TestNGConfigurationProducer{
  private PsiElement myPsiElement = null;


  public PsiElement getSourceElement() {
    return myPsiElement;
  }

  @Nullable
  protected RunnerAndConfigurationSettingsImpl createConfigurationByElement(Location location, ConfigurationContext context) {
    PsiElement element = location.getPsiElement();
    PsiClass psiClass;
    if (element instanceof PsiClass) {
      psiClass = (PsiClass)element;
    }
    else {
      psiClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
    }
    if (psiClass == null) return null;
    if (!PsiClassUtil.isRunnableClass(psiClass, true)) return null;
    if (!TestNGUtil.hasTest(psiClass)) return null;
    myPsiElement = psiClass;
    final Project project = location.getProject();
    RunnerAndConfigurationSettings settings = cloneTemplateConfiguration(project, context);
    final TestNGConfiguration configuration = (TestNGConfiguration)settings.getConfiguration();
    final Module originalModule = configuration.getConfigurationModule().getModule();
    configuration.setClassConfiguration(psiClass);
    PsiMethod method = PsiTreeUtil.getParentOfType(element, PsiMethod.class, false);
    if (method != null && TestNGUtil.hasTest(method)) {
      configuration.setMethodConfiguration(PsiLocation.fromPsiElement(project, method));
      myPsiElement = method;
    }
    configuration.restoreOriginalModule(originalModule);
    settings.setName(configuration.getName());
    copyStepsBeforeRun(project, configuration);
    return (RunnerAndConfigurationSettingsImpl)settings;
  }

  public int compareTo(Object o) {
    return PREFERED;
  }
}
@@ -521,16 +521,53 @@
 lement;%0A
+import com.intellij.psi.PsiJavaFile;%0A
 import c
@@ -774,16 +774,44 @@
 lable;%0A%0A
+import java.util.Iterator;%0A%0A
 public c
@@ -1217,99 +1217,307 @@
 lass
-;%0A    if (element instanceof PsiClass) %7B%0A      psiClass = (PsiClass)element;%0A    %7D%0A    else
+ = null;%0A    for (Iterator%3CLocation%3CPsiClass%3E%3E iterator = location.getAncestors(PsiClass.class, false); iterator.hasNext();) %7B%0A      psiClass = iterator.next().getPsiElement();%0A      if (TestNGUtil.hasTest(psiClass)) break;%0A    %7D%0A    if (psiClass == null) %7B%0A      if (element instanceof PsiJavaFile)
  %7B%0A 
@@ -1525,72 +1525,131 @@
     
+  
-p
+P
 siClass
+%5B%5D classes
  = 
+((
 Psi
-TreeUtil.getParentOfType(element, P
+JavaFile)element).getClasses();%0A        if (classes.length == 1) p
 siClass
-.
+ = 
 class
-);
+es%5B0%5D;%0A      %7D
 %0A   

enable run/debug action on editor tabs