package edu.kit.iti.formal.pse.worthwhile.ui.launching;

import static edu.kit.iti.formal.pse.worthwhile.debugger.launching.WorthwhileLaunchConfigurationConstants.ATTR_PATH;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.eclipse.xtext.ui.resource.IResourceSetProvider;

import com.google.inject.Inject;

import edu.kit.iti.formal.pse.worthwhile.debugger.DebugHelper;
import edu.kit.iti.formal.pse.worthwhile.model.ast.Program;

/**
 * This delegate is responsible for launching a Worthwhile program from a given launch configuration.
 * 
 * @author Joachim
 * 
 */
public abstract class WorthwhileLaunchConfigurationDelegate extends LaunchConfigurationDelegate {

	/**
	 * A resource set provider for loading the program.
	 */
	@Inject
	private IResourceSetProvider resourceSetProvider;

	/**
	 * Gets a program from the specified launch configuration.
	 * 
	 * @param configuration
	 *                The launch configuration.
	 * @return The root AST node of the program launched by this configuration, or {@code null} if the file contains
	 *         errors.
	 * @throws CoreException
	 *                 if getting the program from the file fails
	 */
	protected final Program getProgram(final ILaunchConfiguration configuration) throws CoreException {
		// Get the file to execute from the launch configuration.
		String path = configuration.getAttribute(ATTR_PATH, "");
		IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(path));

		if (file == null) {
			DebugHelper.throwError("The specified source file cannot be found.", null);
		}

		// Load the model from the file
		XtextResourceSet resourceSet = (XtextResourceSet) resourceSetProvider.get(file.getProject());
		resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, true);
		Resource resource = resourceSet.getResource(URI.createURI(path), true);

		// Check if there are no parser errors
		// TODO: Validate?!
		if (!resource.getErrors().isEmpty()) {
			DebugHelper.throwError("There are errors in the source file.", null);
		}

		return (Program) resource.getContents().get(0);
	}
}

@@ -168,16 +168,40 @@
 _PATH;%0A%0A
+import java.util.List;%0A%0A
 import o
@@ -566,32 +566,91 @@
 ource.Resource;%0A
+import org.eclipse.emf.ecore.resource.Resource.Diagnostic;%0A
 import org.eclip
@@ -792,16 +792,162 @@
 rovider;
+%0Aimport org.eclipse.xtext.validation.CheckMode;%0Aimport org.eclipse.xtext.validation.IResourceValidator;%0Aimport org.eclipse.xtext.validation.Issue;
 %0A%0Aimport
@@ -1456,16 +1456,95 @@
 rovider;
+%0A%09%0A%09/**%0A%09 * The validator.%0A%09 */%0A%09@Inject%0A%09private IResourceValidator validator;
 %0A%0A%09/**%0A%09
@@ -2625,36 +2625,27 @@
 ser 
-errors%0A%09%09// TODO: Validate?!
+or validator errors
 %0A%09%09i
@@ -2689,69 +2689,775 @@
 %0A%09%09%09
-DebugHelper.throwError(%22There are errors in the source file.%22
+StringBuilder errorStringBuilder = new StringBuilder(%22There are syntax errors in the source file:%22);%0A%09%09%09%0A%09%09%09for (Diagnostic diag : resource.getErrors()) %7B%0A%09%09%09%09errorStringBuilder.append(%22%5CnLine %22 + diag.getLine() + %22: %22 + diag.getMessage());%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09DebugHelper.throwError(errorStringBuilder.toString(), null);%0A%09%09%7D else %7B%0A%09%09%09List%3CIssue%3E validationErrors = validator.validate(resource, CheckMode.ALL, null);%0A%09%09%09if (validationErrors.size() %3E 0) %7B%0A%09%09%09%09StringBuilder errorStringBuilder = new StringBuilder(%22There are validation errors in the source file:%22);%0A%09%09%09%09%0A%09%09%09%09for (Issue issue : validationErrors) %7B%0A%09%09%09%09%09errorStringBuilder.append(%22%5CnLine %22 + issue.getLineNumber() + %22: %22 + issue.getMessage());%0A%09%09%09%09%7D%0A%09%09%09%09%0A%09%09%09%09DebugHelper.throwError(errorStringBuilder.toString()
 , nu
@@ -3457,24 +3457,29 @@
 g(), null);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%0A%09%09retur

Fix #40: Invoke validator before launching