/*
 * Created on 2005-04-26
 *
 * Part of the Symbian development plug-in for Eclipse
 * Copyright (C) Mikołaj Zalewski, 2005
 */
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 pl.zalewski.mikolaj.symbdev.core.mmpmodel.MMPBitmapReference;

/**
 * A dialog to choose the bitmap color depth
 * 
 * @author mikolaj
 */
public class ChooseDepthDialog extends Dialog {

    protected final int POS_NEW_ITEM=9;
    
    /**
     * @param parentShell The parent of the dialog
     */
    public ChooseDepthDialog(Shell parentShell) {
        super(parentShell);
    }
    
    protected void setNewItem(String item) {
        if (fHaveNewItem)
            fCombo.setItem(POS_NEW_ITEM, item);
        	else fCombo.add(item);
        fHaveNewItem=true;
        fAdditionalItem=item;
    }
    
	/**
	 * Sets the result - the selection in the combo
	 * @param result The bitmap depth - usually one of the DEPTH_*
	 * constanst in MMPBitmapReference
	 */
	public void setResult(String result)
	{
	    if (fCombo!=null)
	    {
	        int pos=mapDepthToPos(result);
	        if (pos==POS_NEW_ITEM)
	            setNewItem(result);
	        fCombo.select(pos);
	    } else fResult=result;
	}
	
	public String getResult() {
	    return fResult;
	}
	
	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("Choose bitmap depth");
		
		fCombo=new Combo(panel, SWT.READ_ONLY|SWT.DROP_DOWN);
		data=new GridData(GridData.FILL_HORIZONTAL);
		fillCombo();
		int pos=mapDepthToPos(fResult);
		if (pos==POS_NEW_ITEM)
		    setNewItem(fResult);
		fCombo.select(pos);
		
		return panel;
	}

	protected void okPressed() {
		fResult=mapPosToDepth(fCombo.getSelectionIndex());
		super.okPressed();
	}	
	
	void fillCombo()
	{
	    fCombo.add(MMPBitmapReference.getDepthName(MMPBitmapReference.DEPTH_1));
	    fCombo.add(MMPBitmapReference.getDepthName(MMPBitmapReference.DEPTH_2));
	    fCombo.add(MMPBitmapReference.getDepthName(MMPBitmapReference.DEPTH_4));
	    fCombo.add(MMPBitmapReference.getDepthName(MMPBitmapReference.DEPTH_8));
	    fCombo.add(MMPBitmapReference.getDepthName(MMPBitmapReference.DEPTH_C4));
	    fCombo.add(MMPBitmapReference.getDepthName(MMPBitmapReference.DEPTH_C8));
	    fCombo.add(MMPBitmapReference.getDepthName(MMPBitmapReference.DEPTH_C12));
	    fCombo.add(MMPBitmapReference.getDepthName(MMPBitmapReference.DEPTH_C16));
	    fCombo.add(MMPBitmapReference.getDepthName(MMPBitmapReference.DEPTH_C24));
	}
	
	protected int mapDepthToPos(String depth) {
	    if (MMPBitmapReference.DEPTH_1.equalsIgnoreCase(depth))
	        return 0;
	    if (MMPBitmapReference.DEPTH_2.equalsIgnoreCase(depth))
	        return 1;
	    if (MMPBitmapReference.DEPTH_4.equalsIgnoreCase(depth))
	        return 2;
	    if (MMPBitmapReference.DEPTH_8.equalsIgnoreCase(depth))
	        return 3;
	    if (MMPBitmapReference.DEPTH_C4.equalsIgnoreCase(depth))
	        return 4;
	    if (MMPBitmapReference.DEPTH_C8.equalsIgnoreCase(depth))
	        return 5;
	    if (MMPBitmapReference.DEPTH_C12.equalsIgnoreCase(depth))
	        return 6;
	    if (MMPBitmapReference.DEPTH_C16.equalsIgnoreCase(depth))
	        return 7;
	    if (MMPBitmapReference.DEPTH_C24.equalsIgnoreCase(depth))
	        return 8;
	    return POS_NEW_ITEM;
	}
	
	protected String mapPosToDepth(int pos) {
	    if (pos==0) return MMPBitmapReference.DEPTH_1;
	    if (pos==1) return MMPBitmapReference.DEPTH_2;
	    if (pos==2) return MMPBitmapReference.DEPTH_4;
	    if (pos==3) return MMPBitmapReference.DEPTH_8;
	    if (pos==4) return MMPBitmapReference.DEPTH_C4;
	    if (pos==5) return MMPBitmapReference.DEPTH_C8;
	    if (pos==6) return MMPBitmapReference.DEPTH_C12;
	    if (pos==7) return MMPBitmapReference.DEPTH_C16;
	    if (pos==8) return MMPBitmapReference.DEPTH_C24;
	    if (pos==9) return fAdditionalItem;
	    throw new IllegalArgumentException();
	}

	Combo fCombo;
	String fResult="1";
	String fAdditionalItem;
	boolean fHaveNewItem;
}
