package org.lamport.tla.toolbox.ui.handler;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.resources.IFile;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IPropertyListener;
import org.eclipse.ui.IWorkbenchPartConstants;
import org.eclipse.ui.part.FileEditorInput;
import org.lamport.tla.toolbox.Activator;
import org.lamport.tla.toolbox.spec.Spec;
import org.lamport.tla.toolbox.spec.parser.IParseConstants;
import org.lamport.tla.toolbox.ui.perspective.InitialPerspective;
import org.lamport.tla.toolbox.ui.perspective.SpecLoadedPerspective;
import org.lamport.tla.toolbox.util.PreferenceStoreHelper;
import org.lamport.tla.toolbox.util.ResourceHelper;
import org.lamport.tla.toolbox.util.UIHelper;

/**
 * Handles the open-spec command
 * @author Simon Zambrovski
 * @version $Id$
 */
public class OpenSpecHandler extends AbstractHandler implements IHandler
{
    public static final String TLA_EDITOR = "org.lamport.tla.toolbox.editor.basic.TLAEditor";
    public static final String COMMAND_ID = "toolbox.command.openSpec";
    public static final String PARAM_SPEC = "toolbox.command.openSpec.param";

    public Object execute(ExecutionEvent event) throws ExecutionException
    {
        String specName = event.getParameter(PARAM_SPEC);
        if (specName == null)
        {
            return null;
        }

        final Spec spec = Activator.getSpecManager().getSpecByName(specName);
        if (spec == null)
        {
            return null;
        }

        // spec the perspective
        UIHelper.switchPerspective(SpecLoadedPerspective.ID);
        // close the initial perspective
        UIHelper.closeWindow(InitialPerspective.ID);

        String[] editors = PreferenceStoreHelper.getOpenedEditors(spec.getProject());
        IEditorPart part = null;
        if (editors.length != 0)
        {

            for (int i = 0; i < editors.length; i++)
            {
                // IEditorInput input = new FileEditorInput(this.spec.getRootFile());

                IFile file = ResourceHelper.getLinkedFile(spec.getProject(), editors[i]);

                // open the editor
                part = UIHelper.openEditor(TLA_EDITOR, new FileEditorInput(file));

            }
        } else
        {
            // open the editor
            part = UIHelper.openEditor(TLA_EDITOR, new FileEditorInput(spec.getRootFile()));
            part.addPropertyListener(new IPropertyListener() {

                public void propertyChanged(Object source, int propId)
                {
                    if (IWorkbenchPartConstants.PROP_DIRTY == propId)
                    {
                        spec.setStatus(IParseConstants.MODIFIED);
                        Activator.getParserRegistry().parseResultChanged(IParseConstants.MODIFIED);
                    }
                }
            });
        }

        // store information about opened spec in the spec manager
        Activator.getSpecManager().setSpecLoaded(spec);

        return null;
    }

}

@@ -542,69 +542,8 @@
 c;%0D%0A
-import org.lamport.tla.toolbox.spec.parser.IParseConstants;%0D%0A
 impo
@@ -711,30 +711,23 @@
 ox.util.
-PreferenceStor
+Resourc
 eHelper;
@@ -760,32 +760,26 @@
 oolbox.util.
-Resource
+UI
 Helper;%0D%0Aimp
@@ -803,34 +803,52 @@
 la.toolbox.util.
-UI
+pref.PreferenceStore
 Helper;%0D%0A%0D%0A/**%0D%0A
@@ -2406,32 +2406,136 @@
 lse%0D%0A        %7B%0D%0A
+            System.out.println(%22No editor information found. If the spec is reopened this is a BUG.%22);%0D%0A
             // o
@@ -2542,32 +2542,32 @@
 pen the editor%0D%0A
-
             part
@@ -2640,24 +2640,66 @@
 tFile()));%0D%0A
+            %0D%0A            %0D%0A            %0D%0A
             
@@ -2965,150 +2965,56 @@
     
-spec.setStatus(IParseConstants.MODIFIED);%0D%0A                        Activator.getParserRegistry().parseResultChanged(IParseConstants.MODIFIED);
+// here the listeners to editor changes go into 
 %0D%0A  

change listener removed