//----------------------------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 javax.swing.Box;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;

import org.huihoo.god4j.editor.XPDLComponent;
import org.huihoo.god4j.editor.elements.atoms.XPDLDataTypes;
/**
* Creates a combo panel and a panel which type is determined by the
* choosen element from combo panel.
*/
public class JavaDataTypesPanel extends JavaPanel
{
	private JavaPanel prevPanel = null;
	private JavaPanel emptyPanel;
	
	private JScrollPane jsp;
	private JComboBox jcb;
	
	public JavaDataTypesPanel(XPDLDataTypes xpdlComponent, String title)
	{
		super(xpdlComponent, 3, title, JavaPanel.BOX_LAYOUT, true, true);
		
		jsp = new JScrollPane();
		jsp.setAlignmentX(Component.LEFT_ALIGNMENT);
		jsp.setAlignmentY(Component.TOP_ALIGNMENT);
		int w = 550;
		int h = 250;

		if (w < 330)
		{
			w = 330;
		}
		
		jsp.setPreferredSize(new Dimension(w, h));
		jsp.setMinimumSize(new Dimension(w, h));
		add(jsp);
		
		emptyPanel = new JavaPanel(xpdlComponent, 2, "", JavaPanel.BOX_LAYOUT, true, false);
		jsp.setViewportView(emptyPanel);
		JavaComboxPanel pCombo = new JavaComboxPanel(xpdlComponent);
		emptyPanel.add(pCombo);
		
		XPDLComponent choosen=(XPDLComponent)xpdlComponent.getChoosen();
		if(choosen !=null)
		{
			prevPanel = choosen.getJavaPanel();
			emptyPanel.add(prevPanel);
		}
		
		final JComboBox jcb = pCombo.getComboBox();
		jcb.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent ae)
			{
				if (prevPanel != null)
				{
					prevPanel.applyChanges();
					emptyPanel.remove(prevPanel);
				}
				XPDLComponent choosen;
				choosen = (XPDLComponent) jcb.getSelectedItem();
				if (choosen != null)
				{
					prevPanel = choosen.getJavaPanel();
					emptyPanel.add(prevPanel);
				}
				jsp.paintAll(jsp.getGraphics());
			}
		});
		jcb.setSelectedItem(xpdlComponent.getChoosen());
		add(Box.createVerticalGlue());
	}
	public boolean checkRequired()
	{
		boolean isOK = true;
		for (int i = 0; i < emptyPanel.getComponentCount(); i++)
		{
			JavaPanel p = (JavaPanel) emptyPanel.getComponent(i);
			isOK = isOK && p.checkRequired();
		}
		return isOK;
	}

	public void applyChanges()
	{
		for (int i = 0; i < emptyPanel.getComponentCount(); i++)
		{
			JavaPanel p = (JavaPanel) emptyPanel.getComponent(i);
			p.applyChanges();
		}
	}
}
