/*
 * 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.widgets.*;

import pl.zalewski.mikolaj.symbdev.Win32ToolEntry;
import pl.zalewski.mikolaj.symbdev.Win32ToolsList;

/**
 * @author mikolaj
 *
 * This class warps a ComboBox with a list of Win32 tools
 */
public class Win32ToolCombo 
{
	/**
	 * Creates a drop-down combobox with the Win32 tools. 
	 * @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 Win32ToolsList is empty)
	 */
  public Win32ToolCombo(Composite parent, int style, boolean allowNone)
  {
  	indexToId=new HashMap();
  	idToIndex=new HashMap();
  	comboBox=new Combo(parent, SWT.DROP_DOWN|SWT.READ_ONLY|style);
  	
  	if (allowNone)
  	{
  		comboBox.add("(none)");
  		indexToId.put(new Integer(0), new Integer(-1));  		
  		idToIndex.put(new Integer(-1), new Integer(0));  		
  	}
  	Win32ToolsList toollist=Win32ToolsList.getInstance();
  	Iterator it=toollist.getEntries();
  	if (!allowNone && !it.hasNext())
  		throw new IllegalArgumentException();
  	
  	while (it.hasNext())
  	{
  		Win32ToolEntry entry=(Win32ToolEntry)it.next();
  		comboBox.add(entry.name);
  		indexToId.put(new Integer(comboBox.getItemCount()-1),
		              new Integer(entry.id));
  		idToIndex.put(new Integer(entry.id),
            		  new Integer(comboBox.getItemCount()-1));
  	}
  	
  	comboBox.select(0);
  }

  public int getResultId()
  {
  	int index=getComboBox().getSelectionIndex();
  	return ((Integer)indexToId.get(new Integer(index))).intValue();
  }
  
  public void setResultId(int id)
  {
    Integer iid=(Integer)idToIndex.get(new Integer(id));
    if (iid!=null)
        getComboBox().select(iid.intValue());
  }
  
   /**
	 * @param comboBox The comboBox to set.
	 */
	public void setComboBox(Combo comboBox) {
		this.comboBox = comboBox;
	}

	/**
	 * @return Returns the comboBox.
	 */
	public Combo getComboBox() {
		return comboBox;
	}

	private Combo comboBox;
	HashMap indexToId;
	HashMap idToIndex;
}
