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

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 JavaGroupPanel extends JavaPanel
{
	public JavaGroupPanel(XPDLComponent myOwner, Object[] elements)
	{
		this(myOwner, elements, "");
	}
	public JavaGroupPanel(XPDLComponent myOwner, Object[] elements, String title)
	{
		this(myOwner, elements, title, JavaPanel.BOX_LAYOUT);
	}
	public JavaGroupPanel(XPDLComponent myOwner, Object[] elements, String title, int layout)
	{
		this(myOwner, elements, title, layout, true);
	}
	public JavaGroupPanel(XPDLComponent myOwner, Object[] elements, String title, int layout, boolean isVertical)
	{
		this(myOwner, elements, title, layout, isVertical, true);
	}
	public JavaGroupPanel(
		XPDLComponent myOwner,
		Object[] elements,
		String title,
		int layout,
		boolean isVertical,
		boolean hasBorder)
	{
		super(myOwner, elements.length + 1, title, layout, isVertical, hasBorder);
		JavaPanel dtdp = null;
		for (int i = 0; i < elements.length; i++)
		{
			if (elements[i] instanceof XPDLComponent)
			{
				dtdp = ((XPDLComponent) elements[i]).getJavaPanel();
			}
			else if (elements[i] instanceof JavaPanel)
			{
				dtdp = (JavaPanel) elements[i];
			}
			dtdp.setEnabled(!myOwner.isReadOnly());
			add(dtdp);
		}
		if (isVertical)
		{
			add(Box.createVerticalGlue());
		}
		else
		{
			add(Box.createHorizontalGlue());
		}
	}
	public JavaPanel getPanel(int no)
	{
		if (no >= getComponentCount() - 1)
		{
			return null;
		}
		return (JavaPanel) getComponent(no);
	}
	public boolean checkRequired()
	{
		if (isEmpty() && !getXPDLComponent().isRequired())
		{
			return true;
		}
		boolean isOK = true;
		for (int i = 0; i < getComponentCount(); i++)
		{
			Component c = getComponent(i);
			if (c instanceof JavaPanel)
			{
				isOK = isOK && ((JavaPanel) c).checkRequired();
			}
		}
		return isOK;
	}
	public boolean isEmpty()
	{
		boolean isEmpty = true;
		for (int i = 0; i < getComponentCount(); i++)
		{
			Component c = getComponent(i);
			if (c instanceof JavaPanel)
			{
				isEmpty = isEmpty && ((JavaPanel) c).isEmpty();
			}
		}
		return isEmpty;
	}

	public void applyChanges()
	{
		boolean isOK = true;
		for (int i = 0; i < getComponentCount(); i++)
		{
			Component c = getComponent(i);
			if (c instanceof JavaPanel)
			{
				//System.out.println("Comp no "+(i+1)+" = "+getComponent(i));
				 ((JavaPanel) c).applyChanges();
			}
		}
	}
}
