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

import java.util.HashMap;
import java.util.Iterator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;

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

/**
 * This class warps a ComboBox with a list of Symbian SDKs
 *
 * @author mikolaj
 */
public class SymbianSDKCombo {

	/**
	 * Creates a drop-down combobox with the Symbian SDKs. 
	 * @param parent the parent of the widget
	 * @param style the SWT style. SWT.DROP_DOWN|SWT.READ_ONLY is already set
	 * @param allowNone adds a (none) option in the combo box
	 * 
	 * @throws IllegalArgumentException if the combo box would have no data
	 * (allowNone==false and the SymbianSDKsList is empty)
	 */
	public SymbianSDKCombo(Composite parent, int style, boolean allowNone)
	{
		nameToIndex=new HashMap();
	  	comboBox=new Combo(parent, SWT.DROP_DOWN|SWT.READ_ONLY|style);
	  	this.allowNone=allowNone;
	  	
	  	if (allowNone)
	  	{
	  		comboBox.add("(none)");
	  		nameToIndex.put("(none)", new Integer(0));
	  	}
	  	
	  	SymbianSDKsList sdklist=SymbianSDKsList.getInstance();
	  	Iterator it=sdklist.getEntries();
	  	if (!allowNone && !it.hasNext())
	  		throw new IllegalArgumentException();
	  	
	  	while (it.hasNext())
	  	{
	  		String name=(String)it.next();
	  		comboBox.add(name);
	  		nameToIndex.put(name, new Integer(comboBox.getItemCount()-1));
	  	}
	  	
	  	comboBox.select(0);
	  	comboBox.addSelectionListener(new SelectionListener() {
            public void widgetDefaultSelected(SelectionEvent e) { event(); }
            public void widgetSelected(SelectionEvent e) { event(); }
            void event() { sdkSelected(); }
        });
	}
	
    public String getResultName()
	{
	  int index=getComboBox().getSelectionIndex();
	  if (index==0 && allowNone) return null;
	  return comboBox.getItem(index);
	}
    
    public SymbianSDKEntry getResultSDK()
    {
        return SymbianSDKsList.getInstance().getEntry(getResultName());
    }
	  
	public void setResultName(String name)
	{
	  getComboBox().select(((Integer)nameToIndex.get(name)).intValue());
	  sdkSelected();
	}	

    /**
     * The selection in the associated combo is changed when
     * an SDK is selected and it has a default Win32 tool
     * @return Returns the associatedCombo.
     */
    public Win32ToolCombo getAssociatedCombo() {
        return associatedCombo;
    }
    /**
     * The selection in the associated combo is changed when
     * an SDK is selected and it has a default Win32 tool
     * @param associatedCombo The associatedCombo to set.
     */
    public void setAssociatedCombo(Win32ToolCombo associatedCombo) {
        this.associatedCombo = associatedCombo;
        if (associatedCombo!=null)
            sdkSelected();
    }
	
   /**
	 * @param comboBox The comboBox to set.
	 */
	public void setComboBox(Combo comboBox) {
		this.comboBox = comboBox;
	}

	/**
	 * @return Returns the comboBox.
	 */
	public Combo getComboBox() {
		return comboBox;
	}
	
	protected void sdkSelected()
	{
	    if (associatedCombo==null) return;
	    String id=getResultName();
	    SymbianSDKEntry entry=SymbianSDKsList.getInstance().getEntry(id);
	    if (entry==null)
	        return;
	    if (entry.win32ToolId!=-1)
	        associatedCombo.setResultId(entry.win32ToolId);
	}

	Win32ToolCombo associatedCombo;
	private Combo comboBox;
	boolean allowNone;
	HashMap nameToIndex;
	
}
