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

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;

import pl.zalewski.mikolaj.symbdev.SymbianSDKEntry;
import pl.zalewski.mikolaj.symbdev.SymbianSDKsList;
import pl.zalewski.mikolaj.symbdev.Win32ToolsList;
import pl.zalewski.mikolaj.symbdev.templates.IProjectTemplate;
import pl.zalewski.mikolaj.symbdev.templates.TemplatesList;
import pl.zalewski.mikolaj.symbdev.widgets.SymbianSDKCombo;
import pl.zalewski.mikolaj.symbdev.widgets.Win32ToolCombo;

/**
 * The second page of the New Project Wizard with Symbian-specific options
 * 
 * @author mikolaj
 */
public class SymbianOptionsPage extends WizardPage {

    /**
     * @param pageName
     */
    public SymbianOptionsPage(String pageName, NewSymbianProject wizard) {
        super(pageName);
        fSymbWizard=wizard;
        setTitle("Symbian Options");
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
     */
    public void createControl(Composite parent) {
        Composite panel=new Composite(parent, SWT.NONE);
        GridLayout layout=new GridLayout(2, false);
        panel.setLayout(layout);
        setControl(panel);
        GridData data;
        
        if (SymbianSDKsList.getInstance().isEmpty())
        {
            Label l=new Label(panel, SWT.WRAP);
            l.setText("No Symbian SDKs found. A Symbian SDK is needed to build a Symbian project.");
            setErrorMessage("No Symbian SDKs");
            setPageComplete(false);
            return;
        }

        if (Win32ToolsList.getInstance().isEmpty())
        {
            Label l=new Label(panel, SWT.WRAP);
            l.setText("No Win32 compilation tools found. Go to Window > Properties > Symbian OS > Win32 Compilation Tools to configure one.");
            setErrorMessage("No Win32 compilation tools");
            setPageComplete(false);
            return;
        }
  
        Label l=new Label(panel, SWT.NONE);
        l.setText("Symbian SDK:");
        fSDKCombo=new SymbianSDKCombo(panel, SWT.NONE, false);
        data=new GridData(GridData.FILL_HORIZONTAL);
        fSDKCombo.getComboBox().setLayoutData(data);
        fSDKCombo.getComboBox().addListener(SWT.Selection, new Listener() {
            public void handleEvent(org.eclipse.swt.widgets.Event e) {
                sdkSelected();
            }			
        });
        l=new Label(panel, SWT.NONE);
        l.setText("Win32 compilation tool:");
        fToolCombo=new Win32ToolCombo(panel, SWT.NONE, false);
        data=new GridData(GridData.FILL_HORIZONTAL);
        fToolCombo.getComboBox().setLayoutData(data);
        fSDKCombo.setAssociatedCombo(fToolCombo);
        
        addSeparator(panel);
        
        l=new Label(panel, SWT.NONE);
        l.setText("Select template");
        data=new GridData();
        data.horizontalSpan=2;
        l.setLayoutData(data);
        
        fTemplateList=new List(panel, SWT.BORDER);
        data=new GridData(GridData.FILL_BOTH);
        fTemplateList.setLayoutData(data);
        fTemplateDescription=new Text(panel, SWT.BORDER|SWT.READ_ONLY|SWT.WRAP);
        data=new GridData(GridData.FILL_BOTH);
        fTemplateDescription.setLayoutData(data);
        fTemplateList.addSelectionListener(new SelectionListener() {
            public void widgetDefaultSelected(SelectionEvent e) { event(); }
            public void widgetSelected(SelectionEvent e) { event(); }
            void event() { templateSelect(); }
        });
        fillList();
        
        setPageComplete(false);
    }
    
    protected void fillList()
    {
        fTemplArray=TemplatesList.getDefault().getTemplates();
        int i;
        for (i=0; i<fTemplArray.length; i++)
        {
            fTemplateList.add(fTemplArray[i].getName());
        }
    }
    
    protected void templateSelect()
    {
        setPageComplete(true);
        int idx=fTemplateList.getSelectionIndex();
        fTemplateDescription.setText(fTemplArray[idx].getDescription());
        fSymbWizard.setupTemplatePage();
    }
    
    protected void sdkSelected()
    {
        checkSDKDrive(fSDKCombo.getResultSDK());
    }
    
	protected void addSeparator(Composite parent) {
		Label separator = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.horizontalSpan=2;
		gridData.grabExcessHorizontalSpace = true;
		separator.setLayoutData(gridData);
	}
	
	void updateMessage()
	{
	    if (fDriveError)
	    {
	        setMessage("SDK on a different drive than the project. " +
	        		"SDK tools won't work", ERROR);
	    } else if (fSpacesWarning)
	    {
	        setMessage("Path to the project directory contains a space. " +
	        		"SDK tools probably won't work", WARNING);
	    } else setMessage(null);
	}
	
	void checkSDKDrive(SymbianSDKEntry e)
	{
	    fDriveError=Character.toUpperCase(e.epocRoot.charAt(0))!=
	        Character.toUpperCase(fSymbWizard.getProjectDrive());
	    updateMessage();
	}
	
    public void setVisible(boolean visible) {
        super.setVisible(visible);
        if (visible)
        {
            IPath p=fSymbWizard.getProjectLocation();
            if (p!=null)
                fSpacesWarning=(p.toString().indexOf(' ')!=-1);
            else {
                // default directory
                fSpacesWarning=(Platform.getLocation().toString().indexOf(' ')!=-1)
                	|| (fSymbWizard.getProjectName().indexOf(' ')!=-1);
            }
            checkSDKDrive(fSDKCombo.getResultSDK());  // calls updateMessage()
        }
    }
    /* (non-Javadoc)
     * @see org.eclipse.jface.wizard.IWizardPage#canFlipToNextPage()
     */
    public boolean canFlipToNextPage() {
        return super.canFlipToNextPage();
    }
	String getSDKName()
	{
	    return fSDKCombo.getResultName();
	}
	
	int getWin32ToolId()
	{
	    return fToolCombo.getResultId();
	}
	
	IProjectTemplate getTemplate()
	{
	    int idx=fTemplateList.getSelectionIndex();
	    return fTemplArray[idx];
	}

    SymbianSDKCombo fSDKCombo;
    Win32ToolCombo fToolCombo;
    List fTemplateList;
    Text fTemplateDescription;
    IProjectTemplate[] fTemplArray;
    NewSymbianProject fSymbWizard;
    
    boolean fSpacesWarning;
    boolean fDriveError;
}
