/**
 * Copyright (c) 2011 committers of YAKINDU and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * Contributors:
 * 	committers of YAKINDU - initial API and implementation
 * 
 */
package org.yakindu.sct.ui.simulation.launch;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
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.ILaunchShortcut;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorPart;
import org.yakindu.sct.core.simulation.launch.IStatechartLaunchConfigurationType;
import org.yakindu.sct.core.simulation.launch.IStatechartLaunchParameters;

/**
 * 
 * @author andreas muelder
 * 
 */
public class StatechartLaunchShortcut implements ILaunchShortcut,
		IStatechartLaunchConfigurationType, IStatechartLaunchParameters {

	public void launch(ISelection selection, String mode) {
		if (selection instanceof IStructuredSelection) {
			final IStructuredSelection structuredSelection = (IStructuredSelection) selection;
			final Object object = structuredSelection.getFirstElement();
			if (object instanceof IAdaptable) {
				IFile file = (IFile) ((IAdaptable) object)
						.getAdapter(IResource.class);
				launch(file, mode);
			}
		}
	}

	public void launch(IEditorPart editor, String mode) {
		IResource resource = (IResource) editor.getEditorInput().getAdapter(
				IResource.class);
		launch((IFile) resource, mode);
	}

	protected void launch(IFile file, String mode) {
		// TODO: Check for existing launch configuration
		ILaunchConfiguration launchConfiguration = createNewLaunchConfiguration(file);
		DebugUITools.launch(launchConfiguration, mode);
	}

	private ILaunchConfiguration createNewLaunchConfiguration(IFile file) {
		final ILaunchManager launchManager = DebugPlugin.getDefault()
				.getLaunchManager();
		final ILaunchConfigurationType configType = launchManager
				.getLaunchConfigurationType(CONFIG_TYPE);
		try {
			ILaunchConfigurationWorkingCopy newConfig = configType.newInstance(
					null, launchManager.generateLaunchConfigurationName(file
							.getName()));

			newConfig.setAttribute(FILE_NAME, file.getLocation().toString());
			return newConfig.doSave();
			
		} catch (CoreException e) {
			e.printStackTrace();
		}
		throw new IllegalStateException();
	}

}

@@ -2140,58 +2140,381 @@
 %0D%0A%09%09
-// TODO: Check for existing launch configuration%0D%0A
+final ILaunchManager launchManager = DebugPlugin.getDefault()%0D%0A%09%09%09%09.getLaunchManager();%0D%0A%09%09final ILaunchConfigurationType configType = launchManager%0D%0A%09%09%09%09.getLaunchConfigurationType(CONFIG_TYPE);%0D%0A%09%09ILaunchConfiguration launchConfig = findLaunchConfiguration(configType,%0D%0A%09%09%09%09file);%0D%0A%09%09if (launchConfig != null) %7B%0D%0A%09%09%09DebugUITools.launch(launchConfig, mode);%0D%0A%09%09%7D else %7B%0D%0A%09
 %09%09IL
@@ -2593,16 +2593,17 @@
 le);%0D%0A%09%09
+%09
 DebugUIT
@@ -2635,32 +2635,37 @@
 ration, mode);%0D%0A
+%09%09%7D%0D%0A
 %09%7D%0D%0A%0D%0A%09private I
@@ -3144,16 +3144,16 @@
 .get
-Location
+FullPath
 ().t
@@ -3196,19 +3196,584 @@
 ave();%0D%0A
-%09%09%09
+%0D%0A%09%09%7D catch (CoreException e) %7B%0D%0A%09%09%09e.printStackTrace();%0D%0A%09%09%7D%0D%0A%09%09throw new IllegalStateException();%0D%0A%09%7D%0D%0A%0D%0A%09protected ILaunchConfiguration findLaunchConfiguration(%0D%0A%09%09%09ILaunchConfigurationType configType, IFile file) %7B%0D%0A%09%09ILaunchConfiguration%5B%5D configs;%0D%0A%09%09try %7B%0D%0A%09%09%09configs = DebugPlugin.getDefault().getLaunchManager()%0D%0A%09%09%09%09%09.getLaunchConfigurations(configType);%0D%0A%09%09%09for (ILaunchConfiguration config : configs) %7B%0D%0A%09%09%09%09String attribute = config.getAttribute(FILE_NAME, %22%22);%0D%0A%09%09%09%09if (attribute.equals(file.getFullPath().toString())) %7B%0D%0A%09%09%09%09%09return config;%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D
 %0D%0A%09%09%7D ca
@@ -3833,41 +3833,19 @@
 %0D%0A%09%09
-throw new IllegalStateException()
+return null
 ;%0D%0A%09

Use existing launch configuration for file if executed via Launch Shortcut