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

import org.eclipse.cdt.ui.wizards.NewCCProjectWizard;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SubProgressMonitor;

import pl.zalewski.mikolaj.symbdev.SymbdevPlugin;
import pl.zalewski.mikolaj.symbdev.SymbianPrjNature;
import pl.zalewski.mikolaj.symbdev.properties.ProjectProperties;
import pl.zalewski.mikolaj.symbdev.templates.IProjectTemplate;

/**
 * @author mikolaj
 *
 * This class implements the File > New > Project > C++ > Symbian OS
 * Wizard
 */
public class NewSymbianProject extends NewCCProjectWizard {

    public NewSymbianProject() {
        super();
    }

    public NewSymbianProject(String title, String description)
    {
        super(title, description);
    }
    
    /**
     * @return The path to the project location or <code>null</code>
     * it default
     */
    public IPath getProjectLocation()
    {
        IPath p=fMainPage.getLocationPath();
        if (p.equals(Platform.getLocation()))
            return null;
        return p;
    }
    
    public char getProjectDrive()
    {
        IPath p=fMainPage.getLocationPath();
        return p.toOSString().charAt(0);
    }
    
    public String getProjectName()
    {
        return fMainPage.getProjectName();
    }

    /* (non-Javadoc)
     * @see org.eclipse.cdt.ui.wizards.NewCProjectWizard#doRunPrologue(org.eclipse.core.runtime.IProgressMonitor)
     */
    protected void doRunPrologue(IProgressMonitor monitor) {
    }
    
	protected void doRun(IProgressMonitor monitor) throws CoreException {
		if (monitor == null) {
			monitor = new NullProgressMonitor();
		}
		
		monitor.beginTask("Creating Project", 10);
		
		// super.doRun() just creates the project and does not assign a builder to it.
		super.doRun(new SubProgressMonitor(monitor, 5));

		// Add the Symbian build nature and builder
		monitor.subTask("Adding Symbian nature");
		SymbianPrjNature.addNature(newProject, new SubProgressMonitor(monitor, 1));
		monitor.subTask("Adding builders");
		SymbianPrjNature.setupBuilder(newProject);
		monitor.worked(1);
		
		monitor.subTask("Configuring user settings");
		ProjectProperties.setSymbianSDKName(newProject, fOptionsPage.getSDKName());
		ProjectProperties.setWin32ToolId(newProject, fOptionsPage.getWin32ToolId());
		SymbianPrjNature.applyPaths(newProject);
		monitor.worked(2);
		
		monitor.subTask("Applying template");
		IProjectTemplate template=fOptionsPage.getTemplate();
		template.applyToProject(newProject, new SubProgressMonitor(monitor, 1));
		monitor.done();
	}    

    /* (non-Javadoc)
     * @see org.eclipse.cdt.ui.wizards.NewCProjectWizard#doRunEpilogue(org.eclipse.core.runtime.IProgressMonitor)
     */
    protected void doRunEpilogue(IProgressMonitor monitor) {
    }

    /* (non-Javadoc)
     * @see org.eclipse.cdt.ui.wizards.NewCProjectWizard#getProjectID()
     */
    public String getProjectID() {
        return SymbdevPlugin.CPROJECT_ID;
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.wizard.IWizard#addPages()
     */
    public void addPages() {
       super.addPages();
       fOptionsPage=new SymbianOptionsPage("Symbian Options", this);
       fTemplatePage=new TemplateOptionsPage("Template Options");
       addPage(fOptionsPage);
       addPage(fTemplatePage);
    }

    void setupTemplatePage() {
        fTemplatePage.setWizardContentProvider(
                fOptionsPage.getTemplate().getWizardContentProvider());
        getContainer().updateButtons();
    }    
    
    SymbianOptionsPage fOptionsPage;
    TemplateOptionsPage fTemplatePage;
}
