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

import javax.swing.Box;
import javax.swing.JComboBox;
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 JavaComboxChoicePanel extends JavaPanel
{
	protected JavaPanel prevPanel = null;
	protected JavaComboxPanel pCombo;
	protected JScrollPane jsp;
	public JavaComboxChoicePanel(XPDLChoice xpdlComponent)
	{
		this(xpdlComponent, "");
	}
	public JavaComboxChoicePanel(XPDLChoice xpdlComponent, String title)
	{
		this(xpdlComponent, title, JavaPanel.BOX_LAYOUT);
	}
	public JavaComboxChoicePanel(XPDLChoice xpdlComponent, String title, int layout)
	{
		this(xpdlComponent, title, layout, true);
	}
	public JavaComboxChoicePanel(XPDLChoice xpdlComponent, String title, int layout, boolean isVertical)
	{
		this(xpdlComponent, title, layout, isVertical, false);
	}
	public JavaComboxChoicePanel(
		XPDLChoice xpdlComponent,
		String title,
		int layout,
		boolean isVertical,
		boolean isChoiceVertical)
	{
		this(xpdlComponent, title, layout, isVertical, isChoiceVertical, true);
	}
	public JavaComboxChoicePanel(
		XPDLChoice xpdlComponent,
		String title,
		int layout,
		boolean isVertical,
		boolean isChoiceVertical,
		boolean hasBorder)
	{
		super(xpdlComponent, (xpdlComponent instanceof XPDLAttribute) ? 2 : 3, title, layout, isVertical, hasBorder);
		int noOfPanels = 2;
		if (xpdlComponent instanceof XPDLAttribute)
		{
			noOfPanels = 1;
		}
		pCombo = new JavaComboxPanel(xpdlComponent, JavaPanel.BOX_LAYOUT, isChoiceVertical);
		add(pCombo);
		
		if (noOfPanels == 2)
		{
			jsp = new JScrollPane();
			jsp.setAlignmentX(Component.LEFT_ALIGNMENT);
			jsp.setAlignmentY(Component.TOP_ALIGNMENT);
			//jsp.setMinimumSize(new Dimension(250,200));
			add(jsp);
			final JComboBox jcb = pCombo.getComboBox();
			jcb.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent ae)
				{
					if (prevPanel != null)
					{
						prevPanel.applyChanges();
					}
					XPDLComponent choosen;
					choosen = (XPDLComponent) jcb.getSelectedItem();
					if (choosen != null)
					{
						prevPanel = choosen.getJavaPanel();
						jsp.setViewportView(prevPanel);
					}
				}
			});
			jcb.setSelectedItem(xpdlComponent.getChoosen());
		}
		if (layout == JavaPanel.BOX_LAYOUT)
		{
			if (isVertical)
			{
				add(Box.createVerticalGlue());
			}
			else
			{
				add(Box.createHorizontalGlue());
			}
		}
	}
	public boolean checkRequired()
	{
		// checking Combo panel
		boolean isOK = ((JavaPanel) getComponent(0)).checkRequired();
		// checking implementation panel
		if (!(getXPDLComponent() instanceof XPDLAttribute))
		{
			JScrollPane jsp = (JScrollPane) getComponent(1);
			JViewport jvp = (JViewport) jsp.getComponent(0);
			if (jvp.getComponentCount() > 0)
			{
				JavaPanel p = (JavaPanel) jvp.getComponent(0);
				isOK = isOK && p.checkRequired();
			}
		}
		return isOK;
	}

	public void applyChanges()
	{
		// setting Combo panel
		 ((JavaPanel) getComponent(0)).applyChanges();
		// setting implementation panel
		if (!(getXPDLComponent() instanceof XPDLAttribute))
		{
			JScrollPane jsp = (JScrollPane) getComponent(1);
			JViewport jvp = jsp.getViewport();
			if (jvp.getComponentCount() > 0)
			{
				JavaPanel p = (JavaPanel) jvp.getComponent(0);
				p.applyChanges();
			}
		}
	}
}
