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

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.SectionPart;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;

import pl.zalewski.mikolaj.symbdev.SymbdevPlugin;

/**
 * @author mikolaj
 *
 * This is a base class for MMP sections that uses grid layout
 */
public class MMPSectionPart extends SectionPart {

    /**
     * Constructs a section with the default layout (TableWrapLayout)
     * @param parent The parent control
     * @param toolkit The form toolkit
     * @param title The title of the section
     * @param description The description (shown below the title)
     * @param numColumns The number of columns of the grid layout
     */
    public MMPSectionPart(Composite parent, FormToolkit toolkit, String title, String description, int numColumns)
    {
        super(parent, toolkit, Section.TITLE_BAR | Section.DESCRIPTION);
        fToolkit=toolkit;
		getSection().clientVerticalSpacing = 4;
		getSection().setText(title);
		getSection().setDescription(description);
		
		fContainer = toolkit.createComposite(getSection());
		toolkit.paintBordersFor(fContainer);
		TableWrapLayout layout=new TableWrapLayout();
		layout.numColumns=numColumns;
		layout.horizontalSpacing = 15;
		layout.verticalSpacing = 5;
		layout.leftMargin=layout.rightMargin=layout.bottomMargin=layout.topMargin=3;
		fContainer.setLayout(layout);		    
		fNumColumns=numColumns;		
    }

    /**
     * Constructs a section with the default layout and two columns
     * (TableWrapLayout)
     * @param parent The parent control
     * @param toolkit The form toolkit
     * @param title The title of the section
     * @param description The description (shown below the title)
     */
    public MMPSectionPart(Composite parent, FormToolkit toolkit, String title, String description)
    {
        this(parent, toolkit, title, description, 2);
    }
    
    Text addTextWithLabel(Composite container, String label, String initText, String tooltip)
    {
        Label l=fToolkit.createLabel(container, label);
        if (tooltip!=null)
            l.setToolTipText(tooltip);
        Text text=fToolkit.createText(container, initText);
        TableWrapData td;
		td = new TableWrapData();
		td.colspan = fNumColumns-1;
		td.grabHorizontal = true;
		td.align=TableWrapData.FILL;
		text.setLayoutData(td);
		return text;
    }

    Text addTextWithLabel(String label, String initText, String tooltip)
    {
        return addTextWithLabel(fContainer, label, initText, tooltip);
    }
    
    Text addTextWithLabel(Composite container, String label, String initText)
    {
        return addTextWithLabel(container, label, initText, null);
    }    

    Text addTextWithLabel(String label, String initText)
    {
        return addTextWithLabel(fContainer, label, initText, null);
    }
    
    Combo addComboWithLabel(Composite container,
            String label, String[] choices, int initSelect)
    {
        fToolkit.createLabel(container, label);
        Combo combo=new Combo(container, SWT.BORDER|SWT.DROP_DOWN|SWT.READ_ONLY);
        combo.setItems(choices);
        fToolkit.adapt(combo);
		TableWrapData td = new TableWrapData();
		td.align = TableWrapData.FILL;
		td.grabHorizontal= true;
		combo.setLayoutData(td);
        combo.select(initSelect);
        return combo;
    }    

    Combo addComboWithLabel(String label, String[] choices, int initSelect)
    {
        return addComboWithLabel(fContainer, label, choices, initSelect);
    }
    
    public static int stringToInt(String str) throws CoreException
    {
        try {
	        str=str.trim();
	        if (str.equals(""))
	            return 0;
	        return Integer.decode(str).intValue();
        }
        catch (NumberFormatException e)
        {
            throw new CoreException(new Status(IStatus.ERROR, SymbdevPlugin.getUniqueIdentifier(),
                    SymbdevPlugin.ERROR_BAD_INPUT, "The specified value is not a string", e));
        }
    }
    
    public void contentCreated()
    {
		getSection().setClient(fContainer);        
    }
    
    /**
     * The main section's container
     */
    public Composite fContainer;
    
    /**
     * The number of columns in the grid layout
     */
    public int fNumColumns;
    
    protected FormToolkit fToolkit;
}
