package ru.artyushov.jmhPlugin.configuration.producer;

import com.intellij.execution.JavaExecutionUtil;
import com.intellij.execution.Location;
import com.intellij.execution.actions.ConfigurationContext;
import com.intellij.execution.configurations.ConfigurationFactory;
import com.intellij.execution.junit.JavaRunConfigurationProducerBase;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.util.Ref;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import ru.artyushov.jmhPlugin.configuration.JmhConfiguration;
import ru.artyushov.jmhPlugin.configuration.JmhConfigurationType;

/**
 * User: nikart
 * Date: 09/04/14
 * Time: 18:45
 */
@SuppressWarnings("UnusedDeclaration")
public abstract class JmhConfigurationProducer extends JavaRunConfigurationProducerBase<JmhConfiguration> implements Cloneable {

    @NotNull
    @Override
    public ConfigurationFactory getConfigurationFactory() {
        return JmhConfigurationType.getInstance().getConfigurationFactories()[0];
    }

    @Override
    protected abstract boolean setupConfigurationFromContext(JmhConfiguration configuration, ConfigurationContext context,
                                                    Ref<PsiElement> sourceElement);

    @Override
    public abstract boolean isConfigurationFromContext(JmhConfiguration jmhConfiguration, ConfigurationContext configurationContext);

    protected boolean isConfigurationFromContext(JmhConfiguration configuration, ConfigurationContext context, String configurationName) {
        if (!configuration.getName().equals(configurationName)) {
            return false;
        }
        Location locationFromContext = context.getLocation();
        if (locationFromContext == null) {
            return false;
        }
        Location location = JavaExecutionUtil.stepIntoSingleClass(locationFromContext);
        final Module originalModule = configuration.getConfigurationModule().getModule();
        if (location.getModule() == null || !location.getModule().equals(originalModule)) {
            return false;
        }
        setupConfigurationModule(context, configuration);
        configuration.restoreOriginalModule(originalModule);

        return true;
    }

    String createProgramParameters(String generatedParams, String defaultParams) {
        return defaultParams != null ? generatedParams + " " + defaultParams : generatedParams;
    }
}

@@ -634,53 +634,134 @@
 %0A * 
-User: nikart%0A * Date: 09/04/14%0A * Time: 18:45
+Supports creating run configurations from context (by right-clicking a code element in the source editor or the project view).
 %0A */
@@ -1102,24 +1102,705 @@
 %5B0%5D;%0A    %7D%0A%0A
+    /**%0A     * Sets up a configuration based on the specified context.%0A     *%0A     * @param configuration a clone of the template run configuration of the specified type%0A     * @param context       contains the information about a location in the source code.%0A     * @param sourceElement a reference to the source element for the run configuration (by default contains the element at caret,%0A     *                      can be updated by the producer to point to a higher-level element in the tree).%0A     * @return true if the context is applicable to this run configuration producer, false if the context is not applicable and the%0A     * configuration should be discarded.%0A     */%0A
     @Overrid
@@ -1854,32 +1854,41 @@
 tionFromContext(
+@NotNull 
 JmhConfiguration
@@ -1894,32 +1894,41 @@
 n configuration,
+ @NotNull
  ConfigurationCo
@@ -1945,93 +1945,401 @@
 ext,
-%0A                       
+ @NotNull Ref%3CPsiElement%3E sourceElement);%0A%0A    /**%0A     * Checks if the specified configuration was created from the specified context.%0A
      
+*%0A
      
-                   Ref%3CPsiElement%3E sourceElement);%0A
+* @param configuration a configuration instance.%0A     * @param context       contains the information about a location in the source code.%0A     * @return true if this configuration was created from the specified context, false otherwise.%0A     */
 %0A   
@@ -2396,32 +2396,41 @@
 tionFromContext(
+@NotNull 
 JmhConfiguration
@@ -2434,12 +2434,9 @@
 ion 
-jmhC
+c
 onfi
@@ -2436,32 +2436,41 @@
 n configuration,
+ @NotNull
  ConfigurationCo
@@ -2478,29 +2478,16 @@
 text con
-figurationCon
 text);%0A%0A

copy JavaDoc from parent class to make code more readable