package tool.launching;
import java.util.ArrayList;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.IDebugModelPresentation;
import org.eclipse.debug.ui.ILaunchShortcut;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.pde.ui.launcher.EclipseLaunchShortcut;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;
import tool.ToolProjectSupport;
import tool.model.IProjectComponent;
import tool.model.ToolClass;
import tool.model.ToolComponent;
import tool.model.ToolPlan;
import tool.model.grammar.IErrorReporter;
public class LocalLaunchShortcut implements ILaunchShortcut{
private String fMode;
private String planName;
private ToolPlan plan;
@Override
public void launch(ISelection selection, String mode) {
this.fMode = mode;
if (mode.equalsIgnoreCase("debug"))
return;
if (selection instanceof IStructuredSelection) {
plan = findToolPlan(((IStructuredSelection)selection));
if (plan != null){
planName = plan.getToolName();
launch(plan, mode);
}
}
}
protected ToolPlan findToolPlan(IStructuredSelection selection){
ToolPlan plan = null;
Object firstElement = selection.getFirstElement();
if (firstElement instanceof IFolder){
if (ToolProjectSupport.isPlanFolder((IFolder)firstElement)){
plan = ToolProjectSupport.getPlanFromFolder((IFolder)firstElement);
} else if (firstElement instanceof IFile){
//TODO
} else if (firstElement instanceof ToolPlan){
plan = (ToolPlan)firstElement;
} else if (firstElement instanceof ToolClass){
plan = ((IProjectComponent)firstElement).getPlan();
}
}
return plan;
}
@Override
public void launch(IEditorPart editor, String mode) {
this.fMode = mode;
if (mode.equalsIgnoreCase("debug"))
return;
IEditorInput input = editor.getEditorInput();
ToolComponent toolElement =
(ToolComponent) input.getAdapter(ToolComponent.class);
if (toolElement != null) {
//TODO launch from method or class editor
}
}
protected void launch(IErrorReporter plan, String mode) {
try {
ILaunchConfiguration config = findLaunchConfiguration();
if (config != null) {
config.launch(mode, null);
}
} catch (CoreException e) {
/* Handle exceptions*/
}
}
private ILaunchConfiguration findLaunchConfiguration() throws CoreException {
ILaunchConfiguration[] configs = getLaunchConfigurations();
if (configs.length == 0)
return createConfiguration();
ILaunchConfiguration config = null;
if (configs.length == 1) {
config = configs[0];
} else {
// Prompt the user to choose a config.
config = chooseConfiguration(configs);
}
if (config != null) {
config = refreshConfiguration(config.getWorkingCopy());
}
return config;
}
private ILaunchConfiguration chooseConfiguration(ILaunchConfiguration[] configs) {
IDebugModelPresentation labelProvider = DebugUITools.newDebugModelPresentation();
ElementListSelectionDialog dialog = new ElementListSelectionDialog(getShell(), labelProvider);
dialog.setElements(configs);
dialog.setTitle("Choose configuration");
if (fMode.equals(ILaunchManager.DEBUG_MODE)) {
dialog.setMessage("Debug...");
} else {
dialog.setMessage("Run...");
}
dialog.setMultipleSelection(false);
int result = dialog.open();
labelProvider.dispose();
if (result == Window.OK) {
return (ILaunchConfiguration) dialog.getFirstResult();
}
return null;
}
private ILaunchConfiguration refreshConfiguration(ILaunchConfigurationWorkingCopy wc) throws CoreException {
IProject project = this.plan.getProject();
setAttributes(wc, project);
return wc.doSave();
}
private void setAttributes(ILaunchConfigurationWorkingCopy wc, IProject project){
String projectName = project.getName();
String forteRoot = ToolProjectSupport.getForteRoot(project);
String logFlags = ToolProjectSupport.getLogFlags(project);
String repository = ToolProjectSupport.getRepositoryName(project);
wc.setAttribute(IToolLaunchConstants.FORTE_ROOT, forteRoot);
wc.setAttribute(IToolLaunchConstants.PLAN_NAME, this.planName);
wc.setAttribute(IToolLaunchConstants.LOG_FLAGS, logFlags);
wc.setAttribute(IToolLaunchConstants.REPOSITORY_NAME, repository);
wc.setAttribute(IToolLaunchConstants.PROJECT_NAME, projectName);
}
private ILaunchConfiguration[] getLaunchConfigurations() throws CoreException {
ArrayList<ILaunchConfiguration> result = new ArrayList<ILaunchConfiguration>();
ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
ILaunchConfigurationType type = manager.getLaunchConfigurationType(EclipseLaunchShortcut.CONFIGURATION_TYPE);
ILaunchConfiguration[] configs = manager.getLaunchConfigurations(type);
for (int i = 0; i < configs.length; i++) {
if (!DebugUITools.isPrivate(configs[i])) {
String foundName = configs[i].getAttribute(IToolLaunchConstants.PLAN_NAME, ""); //$NON-NLS-1$
if (this.planName.equalsIgnoreCase(foundName)) {
result.add(configs[i]);
}
}
}
return (ILaunchConfiguration[]) result.toArray(new ILaunchConfiguration[result.size()]);
}
private ILaunchConfiguration createConfiguration() throws CoreException {
ILaunchConfigurationType configType = getWorkbenchLaunchConfigType();
String computedName = getComputedName(this.planName);
ILaunchConfigurationWorkingCopy wc = configType.newInstance(null, computedName);
IProject project = this.plan.getProject();
setAttributes(wc, project);
return refreshConfiguration(wc);
}
private String getComputedName(String prefix) {
ILaunchManager lm = DebugPlugin.getDefault().getLaunchManager();
return lm.generateUniqueLaunchConfigurationNameFrom(prefix);
}
protected ILaunchConfigurationType getWorkbenchLaunchConfigType() {
ILaunchManager lm = DebugPlugin.getDefault().getLaunchManager();
return lm.getLaunchConfigurationType("tool.launching.localLaunch");
}
protected Shell getShell(){
return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
}
}