//----------------------------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.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

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

import org.huihoo.god4j.editor.XPDLAttribute;
import org.huihoo.god4j.editor.XPDLChoice;
import org.huihoo.god4j.editor.XPDLComponent;
/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class JavaRadioChoicePanel extends JavaPanel
{
	JavaPanel prevPanel = null;
	public JavaRadioChoicePanel(XPDLChoice myOwner)
	{
		this(myOwner, "");
	}
	public JavaRadioChoicePanel(XPDLChoice myOwner, String title)
	{
		this(myOwner, title, JavaPanel.BOX_LAYOUT);
	}
	public JavaRadioChoicePanel(XPDLChoice myOwner, String title, int layout)
	{
		this(myOwner, title, layout, true);
	}
	public JavaRadioChoicePanel(XPDLChoice myOwner, String title, int layout, boolean isVertical)
	{
		this(myOwner, title, layout, isVertical, true);
	}
	public JavaRadioChoicePanel(XPDLChoice myOwner, String title, int layout, boolean isVertical, boolean isChoiceVertical)
	{
		this(myOwner, title, layout, isVertical, isChoiceVertical, true);
	}
	public JavaRadioChoicePanel(XPDLChoice myOwner, String title, int layout, boolean isVertical, boolean isChoiceVertical, boolean hasBorder)
	{
		super(myOwner, (myOwner instanceof XPDLAttribute) ? 2 : 3, title, layout, isVertical, hasBorder);
		int noOfPanels = 2;
		if (myOwner instanceof XPDLAttribute)
		{
			noOfPanels = 1;
		}
		JavaRadioPanel pRadio = new JavaRadioPanel(myOwner, "", JavaPanel.BOX_LAYOUT, isChoiceVertical, false);
		add(pRadio);
		if (noOfPanels == 2)
		{
			final JScrollPane jsp = new JScrollPane();
			jsp.setPreferredSize(new Dimension(600, 400));
			jsp.setAlignmentX(Component.LEFT_ALIGNMENT);
			jsp.setAlignmentY(Component.TOP_ALIGNMENT);
			add(jsp);
			ButtonGroup bg = pRadio.getButtonGroup();
			Enumeration e = bg.getElements();
			int i = 0;
			Object[] choices = myOwner.getChoices();
			while (e.hasMoreElements())
			{
				final Object choosen = choices[i++];
				JRadioButton jr = (JRadioButton) e.nextElement();
				jr.addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent ae)
					{
						if (prevPanel != null)
						{
							prevPanel.applyChanges();
							//((XPDLCompDialog)prevPanel.getDialog()).notifyListeners(prevPanel.getOwner());
						}
						if (choosen != null)
						{
							prevPanel = ((XPDLComponent) choosen).getJavaPanel();
							jsp.setViewportView(prevPanel);
						}
					}
				});
				if (choosen == myOwner.getChoosen())
				{
					jsp.setViewportView(((XPDLComponent) choosen).getJavaPanel());
					jr.setSelected(true);
				}
			}
		}
		if (layout == JavaPanel.BOX_LAYOUT)
		{
			if (isVertical)
			{
				add(Box.createVerticalGlue());
			}
			else
			{
				add(Box.createHorizontalGlue());
			}
		}
	}
	public boolean checkRequired()
	{
		// check Radio panel
		boolean isOK = ((JavaPanel) getComponent(0)).checkRequired();
		// check implementation panel
		if (!(getXPDLComponent() instanceof XPDLAttribute))
		{
			JScrollPane jsp = (JScrollPane) getComponent(1);
			JViewport jvp = (JViewport) jsp.getComponent(0);
			isOK = isOK && ((JavaPanel) jvp.getComponent(0)).checkRequired();
		}
		return isOK;
	}
	
	public void applyChanges()
	{
		// setting Radio panel
		 ((JavaPanel) getComponent(0)).applyChanges();
		// setting implementation panel
		if (!(getXPDLComponent() instanceof XPDLAttribute))
		{
			JScrollPane jsp = (JScrollPane) getComponent(1);
			JViewport jvp = (JViewport) jsp.getComponent(0);
			((JavaPanel) jvp.getComponent(0)).applyChanges();
		}
	}
}