/*
 * Created on 2004-11-03
 *
 * Part of the Symbian development plug-in for Eclipse
 * Copyright (C) Mikołaj Zalewski, 2004
 */
package pl.zalewski.mikolaj.symbdev.ui.mmpedit;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.util.Assert;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.editor.FormEditor;

import pl.zalewski.mikolaj.symbdev.SymbdevPlugin;
import pl.zalewski.mikolaj.symbdev.SymbianPrjNature;
import pl.zalewski.mikolaj.symbdev.SymbianSDKEntry;
import pl.zalewski.mikolaj.symbdev.core.mmpmodel.BldInfModel;
import pl.zalewski.mikolaj.symbdev.core.mmpmodel.IMMPModel;
import pl.zalewski.mikolaj.symbdev.properties.ProjectProperties;

/**
 * The editor for MMP files 
 * 
 * @author mikolaj
 */
public class MMPEditor extends FormEditor {

    /**
     *  Default constructor
     */
    public MMPEditor() {
        super();
    }

    public void doSave(IProgressMonitor monitor) {
        fEditor.doSave(monitor);
    }
        
    /* (non-Javadoc)
     * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
     */
    public boolean isSaveAsAllowed() {
        return false;
    }    

    /* (non-Javadoc)
     * @see org.eclipse.ui.ISaveablePart#doSaveAs()
     */
    public void doSaveAs() {
        // Save As never allowed
    }
    
    protected void addPages() {
        IEditorInput input=getEditorInput();
        Assert.isTrue(input instanceof IFileEditorInput);
		setPartName(input.getName());
        IFile file=((IFileEditorInput)input).getFile();
        IPath dirPath=file.getParent().getLocation();
        fProject=file.getProject();
        try {
	        if (!fProject.hasNature(SymbianPrjNature.NATURE_ID))
	            fProject=null;
	            else fSDK=ProjectProperties.getSymbianSDK(fProject); 
        }
        catch (CoreException e)
        {
            SymbdevPlugin.showError(e, "Error querying project's SDK",
                    getSite().getShell());
            fProject=null;
        }

        if (fProject!=null)
	        try {
	            fModel=BldInfModel.getMMPModelForFile(file);
	        } catch (CoreException e1) {
	            SymbdevPlugin.showError(e1, "Error reading MMP file", getSite().getShell());
	            return;
	        }        
        
        try {
			fEditor = new MMPTextEditor();

			if (fProject!=null)
			{
				fOverview = new OverviewPage(this, fModel);
				addPage(fOverview);
	
				fBuild = new BuildPage(this, fModel, file.getProject(), fSDK, dirPath);
				addPage(fBuild);
				
				fOptions = new OptionsPage(this, fModel, fSDK, dirPath);
				addPage(fOptions);
				
				fExtra = new ExtraPage(this, fModel, fSDK, dirPath, file.getProject());
				addPage(fExtra);
			}
			
			int index=addPage(fEditor, input);
			setPageText(index, fEditor.getTitle());			
        }
        catch (PartInitException e)
        {
            SymbdevPlugin.showError(e,
                    "Error creating MMP editor subviews",
                    getSite().getShell());
        }
    }
    
	private MMPTextEditor fEditor;
    private OverviewPage fOverview;
    private BuildPage fBuild;
    private OptionsPage fOptions;
    private ExtraPage fExtra;
	
	protected IMMPModel fModel;
	IProject fProject;
	SymbianSDKEntry fSDK;
}
