package org.lamport.tla.toolbox.editor.basic;

import org.eclipse.core.runtime.IPath;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.contexts.IContextActivation;
import org.eclipse.ui.contexts.IContextService;
import org.eclipse.ui.editors.text.FileDocumentProvider;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.part.FileEditorInput;
import org.lamport.tla.toolbox.editor.basic.util.ElementStateAdapter;
import org.lamport.tla.toolbox.util.ResourceHelper;

/**
 * Basic editor without any additional features
 *
 * @author zambrovski
 */
public class TLAEditor extends TextEditor
{
    private IContextService contextService = null;
    private IContextActivation contextActivation = null;

    private Image rootImage = TLAEditorActivator.imageDescriptorFromPlugin(TLAEditorActivator.PLUGIN_ID, "/icons/root_file.gif").createImage();
    
    /**
     * Constructor
     */
    public TLAEditor()
    {
        super();
        setDocumentProvider(new FileDocumentProvider());
        setHelpContextId("org.lamport.tla.toolbox.editor.basic.main_editor_window");

        getDocumentProvider().addElementStateListener(new ElementStateAdapter() {
            public void elementDirtyStateChanged(Object element, boolean isDirty)
            {
                // System.out.println("elementDirtyStateChanged " + element);
                if (isDirty)
                {
                    contextService.deactivateContext(contextActivation);
                } else
                {
                    contextActivation = contextService.activateContext("toolbox.contexts.cleaneditor");
                }
            }
        });
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.ui.texteditor.AbstractTextEditor#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
     */
    public void init(IEditorSite site, IEditorInput input) throws PartInitException
    {
        super.init(site, input);
        if (input instanceof FileEditorInput)
        {
            FileEditorInput finput = (FileEditorInput) input;
            if (finput != null)
            {
                IPath path = finput.getPath();
                setContentDescription(path.toString());

                if (ResourceHelper.isRoot(finput.getFile()))
                {
                    setTitleImage(rootImage);
                }
            }
        }
        // grab context service and activate the context on editor load
        this.contextService = (IContextService) getSite().getService(IContextService.class);
        this.contextActivation = contextService.activateContext("toolbox.contexts.cleaneditor");
    }

    
    public void dispose()
    {
        super.dispose();
        rootImage.dispose();
    }

}

@@ -69,20 +69,199 @@
 re.r
-untime.IPath
+esources.IFile;%0Aimport org.eclipse.core.runtime.IPath;%0Aimport org.eclipse.core.runtime.IProgressMonitor;%0Aimport org.eclipse.jface.dialogs.MessageDialog;%0Aimport org.eclipse.jface.window.Window
 ;%0Aim
@@ -297,16 +297,54 @@
 .Image;%0A
+import org.eclipse.swt.widgets.Shell;%0A
 import o
@@ -546,16 +546,60 @@
 ervice;%0A
+import org.eclipse.ui.dialogs.SaveAsDialog;%0A
 import o
@@ -859,16 +859,62 @@
 eHelper;
+%0Aimport org.lamport.tla.toolbox.util.UIHelper;
 %0A%0A/**%0A *
@@ -1245,16 +1245,28 @@
 UGIN_ID,
+%0A           
  %22/icons
@@ -1297,20 +1297,16 @@
 mage();%0A
-    
 %0A    /**
@@ -3080,13 +3080,8 @@
  %7D%0A%0A
-    %0A
     
@@ -3129,24 +3129,24 @@
 .dispose();%0A
-
         root
@@ -3169,10 +3169,1442 @@
 %0A    %7D%0A%0A
+    protected void performSaveAs(IProgressMonitor progressMonitor)%0A    %7B%0A        IFile file = ((FileEditorInput) getEditorInput()).getFile();%0A        Shell shell = UIHelper.getShellProvider().getShell();%0A        SaveAsDialog saveAsDialog = null;%0A        while (true)%0A        %7B%0A            saveAsDialog = new SaveAsDialog(shell);%0A            saveAsDialog.setHelpAvailable(true);%0A            saveAsDialog.setBlockOnOpen(true);%0A            saveAsDialog.setOriginalFile(file);%0A            int result = saveAsDialog.open();%0A            if (result == Window.OK)%0A            %7B%0A                IPath newPath = saveAsDialog.getResult();%0A                if (newPath.toFile().exists())%0A                %7B%0A                    boolean confirmOverride = MessageDialog.openQuestion(shell, %22Override file?%22,%0A                            %22The provided filename already exists. The existing file will be overriden.%5CnDo you want to override the file %22%0A                                    + newPath.toOSString() + %22 ?%22);%0A                    if (!confirmOverride)%0A                    %7B%0A                        continue;%0A                    %7D%0A                %7D%0A                %0A                System.out.println(%22TODO: Save %22 + file + %22 as %22 + newPath);%0A                // break out on save%0A                break;%0A            %7D else %0A            %7B%0A                // break out on cancel of the dialog%0A                break;%0A            %7D%0A        %7D%0A    %7D%0A%0A
 %7D%0A

save as support