/**
 * Copyright (c) 2013 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.simulation.core.launch;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.model.ILaunchConfigurationDelegate;
import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
import org.yakindu.sct.model.sgraph.Statechart;
import org.yakindu.sct.simulation.core.debugmodel.SCTDebugTarget;
import org.yakindu.sct.simulation.core.engine.ISimulationEngine;
import org.yakindu.sct.simulation.core.util.ResourceUtil;

/**
 * 
 * @author andreas muelder - Initial contribution and API
 * 
 */
public abstract class AbstractSCTLaunchConfigurationDelegate extends LaunchConfigurationDelegate implements
		ILaunchConfigurationDelegate {

	public String FILE_NAME = "filename";
	public String DEFAULT_FILE_NAME = "";

	protected abstract ISimulationEngine createExecutionContainer(ILaunch launch, Statechart statechart);

	public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor)
			throws CoreException {
		String filename = configuration.getAttribute(FILE_NAME, DEFAULT_FILE_NAME);
		Statechart statechart = loadStatechart(filename);
		SCTDebugTarget target = createDebugTarget(launch, statechart);
		launch.addDebugTarget(target);
		target.init();
		target.start();
	}

	protected SCTDebugTarget createDebugTarget(ILaunch launch, Statechart statechart) throws CoreException {
		Assert.isNotNull(statechart);
		return new SCTDebugTarget(launch, statechart, createExecutionContainer(launch, statechart));
	}

	protected Statechart loadStatechart(String filename) {
		return ResourceUtil.loadStatechart(filename);
	}

	@Override
	protected IProject[] getProjectsForProblemSearch(ILaunchConfiguration configuration, String mode)
			throws CoreException {
		String filename = configuration.getAttribute(FILE_NAME, "");
		IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(filename);
		return new IProject[] { resource.getProject() };

	}
	
	@Override
	public boolean buildForLaunch(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor)
			throws CoreException {
		//Never build the workspace before simulation
		return false;
	}

}

@@ -444,16 +444,49 @@
 nch;%0D%0A%0D%0A
+import java.util.Collections;%0D%0A%0D%0A
 import o
@@ -518,24 +518,24 @@
 .IProject;%0D%0A
-
 import org.e
@@ -978,32 +978,126 @@
 ationDelegate;%0D%0A
+import org.eclipse.emf.common.util.URI;%0D%0Aimport org.eclipse.emf.ecore.resource.URIConverter;%0D%0A
 import org.yakin
@@ -1731,24 +1731,463 @@
 echart);%0D%0A%0D%0A
+%09@Override%0D%0A%09public boolean preLaunchCheck(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor)%0D%0A%09%09%09throws CoreException %7B%0D%0A%09%09String filename = configuration.getAttribute(FILE_NAME, DEFAULT_FILE_NAME);%0D%0A%09%09URI uri = URI.createPlatformResourceURI(filename, true);%0D%0A%09%09if (!URIConverter.INSTANCE.exists(uri, Collections.emptyMap()))%0D%0A%09%09%09return false;%0D%0A%09%09return super.preLaunchCheck(configuration, mode, monitor);%0D%0A%09%7D%0D%0A%09%0D%0A
 %09public void

check if file exists prior launch #509