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

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import pl.zalewski.mikolaj.symbdev.SymbdevPlugin;
import pl.zalewski.mikolaj.symbdev.SymbianSDKEntry;

/**
 * A dialog with a combo box to choose the Symbian OS version 
 * 
 * @author mikolaj
 */
public class ChooseSymbianVersionDialog extends Dialog {

    /**
     * @param parentShell The parent of the dialog
     * @param label The label to show in the dialog
     */
    public ChooseSymbianVersionDialog(Shell parentShell, String label) {
        super(parentShell);
        fLabelText=label;
    }
    
	protected Control createDialogArea(Composite parent)
	{
	  	GridData data;
	  	Composite panel=new Composite(parent, SWT.NONE);
	  	
		GridLayout layout= new GridLayout();
		layout.numColumns=1;
		panel.setLayout(layout);
		
		Label l=new Label(panel, SWT.NONE);
		l.setText(fLabelText);
		
		fCombo=new Combo(panel, SWT.READ_ONLY|SWT.DROP_DOWN);
		data=new GridData(GridData.FILL_HORIZONTAL);
		fillCombo();
		fCombo.select(mapVersionToPos(fResult));
		
		return panel;
	}
	
	/**
	 * Sets the result - the selection in the combo
	 * @param result The version of Symbian OS - one of the constants
	 * in SymbianSDKEntry
	 */
	public void setResult(int result)
	{
	    if (fCombo!=null)
	        fCombo.select(mapVersionToPos(result));
	    	else fResult=result;
	}

	/**
	 * Returns the result - the selection in the combo
	 * @return The version of Symbian OS - one of the constants
	 * in SymbianSDKEntry
	 */
	public int getResult()
	{
	   return fResult;
	}
	
	protected void okPressed() {
		fResult=mapPosToVersion(fCombo.getSelectionIndex());
		super.okPressed();
	}	
	
	void fillCombo()
	{
	    fCombo.add("(autodetect)");
	    fCombo.add(SymbianSDKEntry.getSymbianVersionName(SymbianSDKEntry.SYMBIAN_6_1));
	    fCombo.add(SymbianSDKEntry.getSymbianVersionName(SymbianSDKEntry.SYMBIAN_7_0));
	    fCombo.add(SymbianSDKEntry.getSymbianVersionName(SymbianSDKEntry.SYMBIAN_7_0S));
	    fCombo.add(SymbianSDKEntry.getSymbianVersionName(SymbianSDKEntry.SYMBIAN_8_0));
	}
	
	int mapVersionToPos(int version)
	{
	    switch (version)
	    {
	    	case SymbianSDKEntry.SYMBIAN_UNKNOWN: return 0;
	    	case SymbianSDKEntry.SYMBIAN_6_1:     return 1;
	    	case SymbianSDKEntry.SYMBIAN_7_0:     return 2;
	    	case SymbianSDKEntry.SYMBIAN_7_0S:    return 3;
	    	case SymbianSDKEntry.SYMBIAN_8_0:     return 4;
	    	default:   							  return 0;
	    }
	}
	
	int mapPosToVersion(int pos)
	{
	    switch (pos)
	    {
	    	case 0: return SymbianSDKEntry.SYMBIAN_UNKNOWN;
    		case 1: return SymbianSDKEntry.SYMBIAN_6_1;
    		case 2: return SymbianSDKEntry.SYMBIAN_7_0;
    		case 3: return SymbianSDKEntry.SYMBIAN_7_0S;
    		case 4: return SymbianSDKEntry.SYMBIAN_8_0;
    		default: throw new IllegalArgumentException();
	    }
	}
	
	protected void configureShell(Shell shell) {
	  	super.configureShell(shell);
 		shell.setText(SymbdevPlugin.MESSAGE_TITLE);
	}	
    
    String fLabelText;
    Combo fCombo;
    int fResult=SymbianSDKEntry.SYMBIAN_UNKNOWN;
}
