/*
 * Created on 2004-12-10
 *
 * Part of the Symbian development plug-in for Eclipse
 * Copyright (C) Mikołaj Zalewski, 2004
 */
package pl.zalewski.mikolaj.symbdev.ui.internal.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 org.eclipse.swt.widgets.Text;

import pl.zalewski.mikolaj.symbdev.core.mmpmodel.ICompilerOptions;

/**
 * A dialog which asks the user for a compiler and it's command line
 * options
 * 
 * @author mikolaj
 */
public class CompilerOptionsDialog extends Dialog {

    /**
     * @param parentShell
     */
    public CompilerOptionsDialog(Shell parentShell) {
        super(parentShell);
    }

    protected Control createDialogArea(Composite parent) {
        Composite panel=new Composite(parent, SWT.NONE);
        GridLayout layout=new GridLayout(2, false);
        panel.setLayout(layout);
        Label l=new Label(panel, SWT.NONE);
        l.setText("Compiler:");
        fCombo=new Combo(panel, SWT.READ_ONLY);
        for (int i=0; i<ICompilerOptions.compilersTab.length; i++)
        {
            ICompilerOptions.CompilerDefinition def=ICompilerOptions.compilersTab[i];
            fCombo.add(def.longName);
        }
        
        GridData gd=new GridData(GridData.FILL_HORIZONTAL);
        fCombo.setLayoutData(gd);
        
        l=new Label(panel, SWT.NONE);
        l.setText("Options:");
        gd=new GridData();
        gd.horizontalSpan=2;
        l.setLayoutData(gd);
        
        fText=new Text(panel, SWT.BORDER);
        gd=new GridData(GridData.FILL_HORIZONTAL);
        gd.widthHint=300;
        gd.horizontalSpan=2;
        fText.setLayoutData(gd);
        return panel;
    }
    
    public boolean execute(ICompilerOptions o)
    {
        create();
        fCombo.select(0);
        if (o!=null)
        {
            boolean hasId=false;
	        for (int i=0; i<ICompilerOptions.compilersTab.length; i++)
	        {
	            ICompilerOptions.CompilerDefinition def=ICompilerOptions.compilersTab[i];
	            if (def.id.equalsIgnoreCase(o.getCompiler()))
	            {
	                fCombo.select(i);
	                hasId=true;
	                break;
	            }
	        }
	        if (!hasId)
	        {
	            fUnknownId=o.getCompiler();
	            fCombo.add(fUnknownId+" (unknown compiler)");
	            fCombo.select(fCombo.getItemCount()-1);
	        }
	        fText.setText(o.getOptions());
        }
        return open()==OK;
    }
    
    public String getId()
    {
        return fResultId;
    }
    
    public String getOptions()
    {
        return fResultOptions;
    }
    
    protected void okPressed() {
        int combosel=fCombo.getSelectionIndex();
        if (combosel==ICompilerOptions.compilersTab.length)
            fResultOptions=fUnknownId;
            else fResultId=ICompilerOptions.compilersTab[combosel].id;
        fResultOptions=fText.getText();
        super.okPressed();
    }
    
    Combo fCombo;
    Text fText;
    
    String fResultId;
    String fResultOptions;
    String fUnknownId;
}
