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

import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.IncrementalProjectBuilder;
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.IParserLauncher;
import org.lamport.tla.toolbox.spec.parser.StreamInterpretingParserLauncher;
import org.lamport.tla.toolbox.util.AdapterFactory;
import org.lamport.tla.toolbox.util.TLAMarkerHelper;
import org.lamport.tla.toolbox.util.pref.IPreferenceConstants;
import org.lamport.tla.toolbox.util.pref.PreferenceStoreHelper;

/**
 * The parsing builder
 * @author Simon Zambrovski
 * @version $Id$
 */
public class TLAParsingBuilder extends IncrementalProjectBuilder
{
    public static final String BUILDER_ID = "toolbox.builder.TLAParserBuilder";

    private final IParserLauncher launcher = new StreamInterpretingParserLauncher();

    protected void clean(IProgressMonitor monitor) throws CoreException
    {
        System.out.println("Clean has been invoked");
        Spec spec = Activator.getSpecManager().getSpecLoaded();
        // TLAMarkerHelper.deleteMarkers(spec, monitor);
    }

    /**
     * @see org.eclipse.core.resources.IncrementalProjectBuilder#build(int, java.util.Map, org.eclipse.core.runtime.IProgressMonitor)
     */
    protected IProject[] build(int kind, Map args, IProgressMonitor monitor) throws CoreException
    {

        if (PreferenceStoreHelper.getInstancePreferenceStore().getBoolean(
                IPreferenceConstants.P_PARSER_RUN_ON_MODIFICATION))
        {
            System.out.println("Running a Build (on): " + kind);
        } else
        {
            System.out.println("Running a Build (off): " + kind);
        }

        runBuild(monitor);

        forgetLastBuiltState();

        // since no deltas should be recalculated, return null.
        // this could change in the future
        return null;
    }

    /**
     * @param monitor
     */
    private void runBuild(IProgressMonitor monitor)
    {
        // TODO handle the monitor

        try
        {
            getProject().accept(new SpecificationRootFileParsingVisitor());

        } catch (CoreException e)
        {
            // TODO
            e.printStackTrace();
        }
    }

    /**
     * Visits the project and parses the associated specification root module
     * @author Simon Zambrovski
     * @version $Id$
     */
    public class SpecificationRootFileParsingVisitor implements IResourceVisitor
    {
        IProgressMonitor monitor = null;
        
        /**
         * @see org.eclipse.core.resources.IResourceVisitor#visit(org.eclipse.core.resources.IResource)
         */
        public boolean visit(IResource resource) throws CoreException
        {
            
            if (resource instanceof IProject && ((IProject) resource).hasNature(TLANature.ID))
            {
                System.out.println("Build Invoked");
                // 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();
                IWorkspaceRunnable run = new IWorkspaceRunnable(){

                    public void run(IProgressMonitor monitor) throws CoreException
                    {
                        TLAMarkerHelper.deleteMarkers(spec, monitor);
                        launcher.parseSpecification(spec);
                        TLAMarkerHelper.setupProblemInformation(spec, monitor);
                        System.out.println("Resulting status is: " + AdapterFactory.getStatusAsString(spec));
                    }
                };
                resource.getWorkspace().run(run, monitor);

            }

            // invoke only on the IProject, never on the single module files
            return false;
        }
    }

}

@@ -323,24 +323,76 @@
 ctBuilder;%0D%0A
+import org.eclipse.core.resources.ResourcesPlugin;%0D%0A
 import org.e
@@ -759,191 +759,8 @@
 ry;%0D
-%0Aimport org.lamport.tla.toolbox.util.TLAMarkerHelper;%0D%0Aimport org.lamport.tla.toolbox.util.pref.IPreferenceConstants;%0D%0Aimport org.lamport.tla.toolbox.util.pref.PreferenceStoreHelper;%0D
 %0A%0D%0A/
@@ -1291,41 +1291,33 @@
     
-// TLAMarkerHelper.delete
+spec.cleanProblem
 Markers(
 spec
@@ -1304,38 +1304,32 @@
 nProblemMarkers(
-spec, 
 monitor);%0D%0A    %7D
@@ -1609,137 +1609,54 @@
 if (
-PreferenceStoreHelper.getInstancePreferenceStore().getBoolean(%0D%0A                IPreferenceConstants.P_PARSER_RUN_ON_MODIFICATION
+ResourcesPlugin.getWorkspace().isAutoBuilding(
 ))%0D%0A
@@ -3329,32 +3329,90 @@
 tSpecLoaded();%0D%0A
+                if (spec != null)%0D%0A                %7B%0D%0A    
                 
@@ -3481,24 +3481,28 @@
             
+ 
+   
 public void 
@@ -3565,32 +3565,36 @@
                 
+    
 %7B%0D%0A             
@@ -3608,173 +3608,128 @@
     
-TLAMarkerHelper.deleteMarkers(spec, monitor);%0D%0A                        launcher.parseSpecification(spec);%0D%0A                        TLAMarkerHelper.setupProblemInform
+    %0D%0A                            spec.cleanProblemMarkers(monitor);%0D%0A                            launcher.parseSpecific
 atio
@@ -3739,32 +3739,36 @@
 pec, monitor);%0D%0A
+    
                 
@@ -3878,27 +3878,35 @@
             
-%7D%0D%0A
+    %7D%0D%0A    
             
@@ -3913,16 +3913,20 @@
     %7D;%0D%0A
+    
         
@@ -3969,32 +3969,49 @@
 run, monitor);%0D%0A
+                %7D
 %0D%0A            %7D%0D

switch to IMarkers, changed the property controlling the auto-build