//----------------------------BEGIN LICENSE----------------------------
/* 
 * Willow : the Open Source WorkFlow Project
 * Distributable under GNU LGPL license by gun.org
 *
 * Copyright (C) 2004-2010 huihoo.org 
 * Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com> 
 * 
 * ====================================================================
 * Project Homepage : http://www.huihoo.org/god4j
 * Source Forge     : http://sourceforge.net/projects/huihoo
 * Mailing list     : god4j@lists.sourceforge.net 
 */
//----------------------------END  LICENSE-----------------------------
package org.huihoo.god4j.editor.panels;
import java.awt.GridLayout;
import java.util.Enumeration;

import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;

import org.huihoo.god4j.editor.XPDLChoice;
/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class JavaRadioPanel extends JavaPanel
{
	private ButtonGroup rGroup;
	public JavaRadioPanel(XPDLChoice myOwner)
	{
		this(myOwner, "");
	}
	public JavaRadioPanel(XPDLChoice myOwner, String title)
	{
		this(myOwner, title, JavaPanel.BOX_LAYOUT);
	}
	public JavaRadioPanel(XPDLChoice myOwner, String title, int layout)
	{
		this(myOwner, title, layout, true);
	}
	public JavaRadioPanel(XPDLChoice myOwner, String title, int layout, boolean isVertical)
	{
		this(myOwner, title, layout, isVertical, true);
	}
	public JavaRadioPanel(XPDLChoice myOwner, String title, int layout, boolean isVertical, boolean hasBorder)
	{
		super(myOwner, 2, title, layout, false, hasBorder);
		JavaPanel bgPanel = new JavaPanel(myOwner, myOwner.getChoices().length, "", layout, isVertical, false);
		int noOfElements = myOwner.getChoices().length;
		if (noOfElements > 2)
		{
			int half = (int) noOfElements / 2;
			if ((double) half != noOfElements / 2)
				half++;
			if (half > 1)
			{
				bgPanel.setLayout(new GridLayout(half, half));
			}
			else
			{
				int m, n;
				if (isVertical)
				{
					m = 2 * half;
					n = 1;
				}
				else
				{
					m = 1;
					n = 2 * half;
				}
				bgPanel.setLayout(new GridLayout(m, n));
			}
		}
		rGroup = new ButtonGroup();
		Object[] c = myOwner.getChoices();
		for (int i = 0; i < c.length; i++)
		{
			JRadioButton jr = new JRadioButton(c[i].toString());
			if (c[i] == myOwner.getChoosen())
			{
				jr.setSelected(true);
			}
			jr.setEnabled(!myOwner.isReadOnly());
			// Group the radio buttons.
			rGroup.add(jr);
			bgPanel.add(jr);
		}
		add(Box.createHorizontalGlue());
		add(bgPanel);
	}

	public Object getSelectedItem()
	{
		Enumeration e = rGroup.getElements();
		int i = 0;
		while (e.hasMoreElements())
		{
			JRadioButton jr = (JRadioButton) e.nextElement();
			if (jr.isSelected())
			{
				return ((XPDLChoice) getXPDLComponent()).getChoices()[i];
			}
			i++;
		}
		return null;
	}
	public ButtonGroup getButtonGroup()
	{
		return rGroup;
	}
	
	public void applyChanges()
	{
		getXPDLComponent().setValue(getSelectedItem());
	}
}