/*
 * Created on Aug 15, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package hibergui.property;

import hibergui.Edit;
import hibergui.Form;

import java.util.Arrays;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

/**
 * @author espakm
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public abstract class RadioEdit<T> extends Edit<T> {
    
	Composite cpt;
	Button[] rButtons;
    
    T[] values;
    
    public RadioEdit(Form form, Composite cptParent, int style, String prop, String name) {
        super(form, cptParent, style, prop, name);

        for (Button rb: rButtons) {
            rb.addSelectionListener(lsn_WIDGET_MOD);
            rb.addKeyListener(lsn_WIDGET_KEY);
            rb.addFocusListener(lsn_WIDGET_FOCUS);
            rb.addTraverseListener(lsn_TRAVERSE);
        }
    }
    
    protected Control createContents(Composite cptParent) {
        cpt = new Composite(cptParent, SWT.NONE);
        
        RowLayout rowLayout = new RowLayout();
        rowLayout.justify = false;
        rowLayout.pack = true;
        cpt.setLayout(rowLayout);
        
        rButtons = createRadioButtons(cpt);
        
        return cpt;
    }
    
	protected Button[] createRadioButtons(Composite cpt) {
        String[] labels = createLabels();
        Button[] b = new Button[labels.length];
        for (int i = 0; i < labels.length; ++i) {
            b[i] = new Button(cpt, SWT.RADIO);
            b[i].setText(labels[i]);
        }
        return b;
    }
    
    protected abstract String[] createLabels();
    
    public void setInput(T[] values) {
        this.values = values;
    }
    
    private int getSelection() {
        for (int i = 0; i < rButtons.length; ++i)
            if (rButtons[i].getSelection() == true)
                return i;
        return -1;
    }
    
    private void setSelection(int i) {
        int selected = getSelection();
        if (selected == i)
            return;
        if (selected != -1)
            rButtons[selected].setSelection(false);
        if (i != -1)
            rButtons[i].setSelection(true);
    }
    
    @Override
    public void clear() {
        setSelection(-1);
    }
    
    @Override
    public void fill() {
        setSelection(Arrays.asList(values).indexOf(value));
    }
    
    @Override
    public T extract() {
        return getSelection() == -1 ? null : values[getSelection()];
    }
    
    @Override
    public boolean setFocus() {
        return rButtons[0].setFocus();
    }
    
    @Override
    public boolean isEnabled() {
        return rButtons[0].isEnabled();
    }
    
    @Override
    public void setEnabled(boolean b) {
        for (Button rb: rButtons)
            rb.setEnabled(b);
    }
    
	@Override
	public void setBackground(Color clr) {
		for (Button rb: rButtons)
			rb.setBackground(clr);
	}
    
    @Override
    public void setEditable(boolean b) {
    	setEnabled(b);
    }
}
