package com.googlecode.grinderstone.debug.ui.launching;
import java.io.File;
import java.util.List;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.variables.IStringVariableManager;
import org.eclipse.core.variables.VariablesPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.ui.DebugUITools;
import org.python.pydev.core.IInterpreterManager;
import org.python.pydev.debug.core.PydevDebugPlugin;
import org.python.pydev.debug.ui.launching.AbstractLaunchShortcut;
import org.python.pydev.debug.ui.launching.FileOrResource;
import org.python.pydev.plugin.PydevPlugin;
import com.googlecode.grinderstone.core.GrinderConstants;
/**
* Class represents shortcut for GrinderStone running.
*
* @author Borislav Andruschuk
* @since GrinderStone 1.0
*/
public class GrinderLaunchShortcut extends AbstractLaunchShortcut {
/** Show dialog? */
private boolean propertiesShouldBeSetManually = false;
@Override
protected String getLaunchConfigurationType() {
return GrinderConstants.ID_GRINDER_LAUNCH_CONFIGURATION_TYPE;
}
// @Override
protected IInterpreterManager getInterpreterManager() {
return PydevPlugin.getJythonInterpreterManager();
}
/**
* {@inheritDoc}
*/
// @Override
protected IInterpreterManager getInterpreterManager(IProject project) {
return this.getInterpreterManager();
}
@Override
protected void launch(FileOrResource[] resource, String mode) {
ILaunchConfiguration conf = null;
List<ILaunchConfiguration> configurations = super
.findExistingLaunchConfigurations(resource);
if (configurations.isEmpty()) {
FileOrResource fileOrResource = resource[0];
if (fileOrResource.resource != null) {
IProject project = fileOrResource.resource.getProject();
IFile file = project.getFile(GrinderConstants.GRINDER_PROPERTIES_FILE_NAME);
propertiesShouldBeSetManually = file == null ? true : !file.exists();
conf = createDefaultLaunchConfiguration(resource);
}
} else {
if (configurations.size() == 1) {
conf = configurations.get(0);
} else {
conf = chooseConfig(configurations);
if (conf == null)
// User cancelled selection
return;
}
}
if (conf != null) {
if (propertiesShouldBeSetManually) {
propertiesShouldBeSetManually = false;
String groupID = GrinderConstants.DEBUG_LAUNCH_GROUP + "." + mode;
DebugUITools.openLaunchConfigurationDialog(PydevDebugPlugin
.getActiveWorkbenchWindow().getShell(), conf, groupID, null);
} else {
DebugUITools.launch(conf, mode);
}
return;
}
fileNotFound();
}
/**
* Customize launch configuration for setting grinder.properties attributes.
*/
@Override
public ILaunchConfiguration createDefaultLaunchConfiguration(FileOrResource[] resource) {
ILaunchConfiguration conf = super.createDefaultLaunchConfiguration(resource);
if (!propertiesShouldBeSetManually) {
FileOrResource fileOrResource = resource[0];
if (fileOrResource.resource == null) {
reportError("The grinder.properties cannot be set for a run. "
+ "Project cannot be located.", null);
}
String projName = fileOrResource.resource.getProject().getName();
IStringVariableManager varManager = VariablesPlugin.getDefault()
.getStringVariableManager();
String location = varManager.generateVariableExpression(GrinderConstants.WORKSPACE_LOC,
projName + File.separator
+ GrinderConstants.GRINDER_PROPERTIES_FILE_NAME);
try {
ILaunchConfigurationWorkingCopy workingCopy = conf.copy(conf.getName());
workingCopy.setAttribute(GrinderConstants.ATTR_GRINDER_PROPERTIES_LOCATION,
location);
conf.delete();
conf = workingCopy.doSave();
} catch (CoreException e) {
reportError("The grinder.properties cannot be set for a run.", e);
}
}
return conf;
}
}