/*
 * Created on 2004-09-04
 *
 * 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.jface.util.Assert;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;

import pl.zalewski.mikolaj.symbdev.skins.SkinEntry;
import pl.zalewski.mikolaj.symbdev.skins.SkinManager;

/**
 * This class warps a ComboBox with a list of Emulator skins
 * 
 * @author mikolaj
 */
public class SkinCombo {

	/**
	 * Creates a drop-down combobox with the emulator skins. 
	 * @param parent the parent of the widget
	 * @param style the SWT style. SWT.DROP_DOWN|SWT.READ_ONLY is already set
	 */
    public SkinCombo(Composite parent, int style) {
		fSkinToIndex=new HashMap();
		fIndexToSkin=new HashMap();
	  	setComboBox(new Combo(parent, SWT.DROP_DOWN|SWT.READ_ONLY|style));

  		getComboBox().add(SkinEntry.getDefaultString());
  		fSkinToIndex.put(SkinEntry.getDefault(), new Integer(0));
  		fIndexToSkin.put(new Integer(0), SkinEntry.getDefault());
	  	
	  	SkinManager skinsMgs=SkinManager.getDefault();
	  	Iterator it=skinsMgs.getEntries();

	  	int items=1;
	  	while (it.hasNext())
	  	{
	  	    SkinEntry entry=(SkinEntry)it.next();
	  		String name=entry.getName();
	  		getComboBox().add(name);
	  		fSkinToIndex.put(entry, new Integer(items));
	  		fIndexToSkin.put(new Integer(items), entry);
	  		items++;
	  	}
	  	
	  	getComboBox().select(0);
    }
    
    /**
     * @return The Skin selected by the user
     */
    public SkinEntry getResult()
	{
        int index=getComboBox().getSelectionIndex();
        return (SkinEntry)fIndexToSkin.get(new Integer(index));
	}
	  
    /**
     * Sets the current skin to the parameter
     * @param skin The skin
     */
	public void setResult(SkinEntry skin)
	{
	    Integer index=(Integer)fSkinToIndex.get(skin);
	    Assert.isNotNull(index);
	    getComboBox().select(index.intValue());
	}

    /**
     * @param fComboBox The fComboBox to set.
     */
    public void setComboBox(Combo comboBox) {
        this.fComboBox = comboBox;
    }
    /**
     * @return Returns the fComboBox.
     */
    public Combo getComboBox() {
        return fComboBox;
    }

    private Combo fComboBox;
    HashMap fIndexToSkin;
    HashMap fSkinToIndex;
}
