package org.lamport.tla.toolbox.spec.nature;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.lamport.tla.toolbox.Activator;
import org.lamport.tla.toolbox.spec.Spec;
import org.lamport.tla.toolbox.spec.parser.ModuleParserLauncher;
import org.lamport.tla.toolbox.spec.parser.ParseResult;
import org.lamport.tla.toolbox.spec.parser.SpecificationParserLauncher;
import org.lamport.tla.toolbox.util.AdapterFactory;

/**
 * Encapsulates parser launching methods
 * 
 * @author Simon Zambrovski
 * @version $Id$
 */
public class ParserHelper
{

    private final static ModuleParserLauncher moduleParser = new ModuleParserLauncher();
    private final static SpecificationParserLauncher launcher = new SpecificationParserLauncher(moduleParser);

    /**
     * Runs the parser on the given module
     * @param resource
     * @param monitor
     */
    public static void rebuildModule(final IResource resource, IProgressMonitor monitor)
    {
        if (resource == null)
        {
            return;
        }
        System.out.println("Module build invoked on " + resource.getProjectRelativePath().toString() + " ...");
        IWorkspaceRunnable run = new IWorkspaceRunnable() {

            public void run(IProgressMonitor monitor) throws CoreException
            {

                // markers already removed in the parseModule
                // TLAMarkerHelper.removeProblemMarkers(resource, monitor);
                ParseResult result = moduleParser.parseModule(resource, monitor);
                System.out.println("Resulting status is: " + AdapterFactory.getStatusAsString(result.getStatus()));
            }
        };
        try
        {
            resource.getWorkspace().run(run, monitor);
        } catch (CoreException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("... build invocation finished.");
    }

    /**
     * This will run the parse of the root file of the current spec 
     * @param monitor
     */
    public static void rebuildSpec(IProgressMonitor monitor)
    {
        // TODO improve this...
        // better: lookup in the project property for the spec name
        // and search a spec by name

        final Spec spec = Activator.getSpecManager().getSpecLoaded();
        if (spec == null)
        {
            return;
        }
        System.out.println("Spec build invoked on " + spec.getName() + " ...");
        IWorkspaceRunnable run = new IWorkspaceRunnable() {

            public void run(IProgressMonitor monitor) throws CoreException
            {

                // markers already removed in the parseSpecification 
                // TLAMarkerHelper.removeProblemMarkers(spec.getProject(), monitor);
                launcher.parseSpecification(spec, monitor);
                System.out.println("Resulting status is: " + AdapterFactory.getStatusAsString(spec));
            }
        };
        try
        {
            ResourcesPlugin.getWorkspace().run(run, monitor);
        } catch (CoreException e)
        {
            e.printStackTrace();
        }
        System.out.println("... build invocation finished.");
    }
}

@@ -578,61 +578,8 @@
 er;%0D
-%0Aimport org.lamport.tla.toolbox.util.AdapterFactory;%0D
 %0A%0D%0A/
@@ -1196,32 +1196,35 @@
       %7D%0D%0A       
+ //
  System.out.prin
@@ -1699,32 +1699,35 @@
 %0A               
+ //
  System.out.prin
@@ -1981,74 +1981,54 @@
     
-// TODO Auto-generated catch block%0D%0A            e.printStackTrace(
+Activator.logError(%22Error parsing a module%22, e
 );%0D%0A
@@ -2037,32 +2037,35 @@
       %7D%0D%0A       
+ //
  System.out.prin
@@ -2297,150 +2297,8 @@
  %7B%0D%0A
-        // TODO improve this...%0D%0A        // better: lookup in the project property for the spec name%0D%0A        // and search a spec by name%0D%0A%0D%0A
     
@@ -2433,32 +2433,35 @@
       %7D%0D%0A       
+ //
  System.out.prin
@@ -2749,94 +2749,8 @@
 n %0D%0A
-                // TLAMarkerHelper.removeProblemMarkers(spec.getProject(), monitor);%0D%0A
     
@@ -2817,24 +2817,27 @@
             
+ //
  System.out.
@@ -3088,26 +3088,54 @@
     
-e.printStackTrace(
+Activator.logError(%22Error parsing a module%22, e
 );%0D%0A
@@ -3152,16 +3152,19 @@
 %0A       
+ //
  System.

kill verbosity