//----------------------------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;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.huihoo.god4j.editor.panels.JavaGroupPanel;
import org.huihoo.god4j.editor.panels.JavaPanel;
import org.huihoo.workflow.WorkflowComponent;
import org.huihoo.workflow.WorkflowSimpleComponent;
/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class XPDLComplexComponent extends XPDLComponent
{
	//---------------------------------------------------------------
	/**
	*  The list of all elements (attributes, and other elements) that
	*  complex element is consisted of.
	*/
	protected java.util.List complexChildren = null;
	/** The list of attributes that complex element contains. */
	protected java.util.List attributes = null;
	
	protected  WorkflowComponent workflow_component = null;
	
	//---------------------------------------------------------------

	public XPDLComplexComponent(XPDLComplexComponent parent)
	{
		this(parent,null);
	}

	public XPDLComplexComponent(XPDLComplexComponent parent,String name)
	{
		super(parent,name);
	}

	public WorkflowComponent getWorkflowComponent()
	{			
		return workflow_component;
	}

	public void setWorkflowComponent(WorkflowComponent eleWorkflow)
	{
		workflow_component=eleWorkflow;
		//setValue(eleWorkflow);
	}

	/**
	* Returns the collection of elements this element is made of.
	*/
	public Collection findChildren()
	{
		return complexChildren;
	}
	/**
	* Returns the collection of strings that represents elements
	* that this element is made of.
	*/
	public Collection findChildrenValues()
	{
		java.util.List l = new ArrayList();
		Iterator it = complexChildren.iterator();
		while (it.hasNext())
		{
			XPDLComponent el = (XPDLComponent) it.next();
			l.add(el.getValue());	
		}
		return l;
	}
	/**
	* Sets the element and all elements it is made of to be
	* read only or not.
	*/
	public void setReadOnly(boolean ro)
	{
		super.setReadOnly(ro);
		Iterator it = complexChildren.iterator();
		while (it.hasNext())
		{
			XPDLComponent el = (XPDLComponent) it.next();
			el.setReadOnly(ro);
		}
	}
	public boolean isEmpty()
	{
		boolean isEmpty = true;
		Iterator it = complexChildren.iterator();
		while (it.hasNext())
		{
			XPDLComponent el = (XPDLComponent) it.next();
			isEmpty = isEmpty && el.isEmpty();
		}
		return isEmpty;
	}

	public JavaPanel getJavaPanel()
	{
		XPDLComponent[] c = new XPDLComponent[complexChildren.size()];
		complexChildren.toArray(c);
		return new JavaGroupPanel(this, c, getLabel());
	}

	/** Gets the element with specified name from stucture. */
	public XPDLComponent get(String name)
	{
		Iterator it = complexChildren.iterator();
		while (it.hasNext())
		{
			XPDLComponent el = (XPDLComponent) it.next();
			if (el.name.equals(name))
			{
				return el;
			}
		}
		return null;
	}
	
	/**
	* Sets the element with specified name from stucture to
	* the specified value.
	*/
	public void set(String name, Object value)
	{
		XPDLComponent el = get(name);
		if (el != null)
		{
			el.setValue(value);
		}
	}
	
	public String toString()
	{
		String to_string = null;
		
		WorkflowComponent workflow_component = getWorkflowComponent();
		if (workflow_component != null && (workflow_component instanceof WorkflowSimpleComponent))
		{
			WorkflowSimpleComponent simple_component = (WorkflowSimpleComponent) workflow_component;

			to_string=simple_component.getName();
		
			if(to_string == null )
			{
				to_string=simple_component.getUUID();
			}
		}
		
		if(to_string !=null)
		{
			return to_string;
		}
		
		return super.toString();
	}

	// prepeares cloning for extended classes
	public Object clone()
	{
		XPDLComplexComponent d = (XPDLComplexComponent) super.clone();
		d.complexChildren = new ArrayList();
		d.attributes = new ArrayList();
		
		d.workflow_component = this.workflow_component;
		
		return d;
	}
	//==================================================================
	public void initComponent()
	{
		//	-----------------------------------------------
		super.initComponent();
		//	-----------------------------------------------
		complexChildren = new ArrayList();
		attributes = new ArrayList();
	}

	public void structComponent()
	{
//	-----------------------------------------------
		super.structComponent();
//	-----------------------------------------------
	}
	

	public void loadComponent()
	{
		//	-----------------------------------------------
		super.loadComponent();
		//	-----------------------------------------------
	}

	public void saveComponent()
	{
    if(isReadOnly())
    {
      return;
    }
		//	-----------------------------------------------
		super.saveComponent();
		//	-----------------------------------------------
	}

	public void saveComponent(WorkflowComponent workflow_component)
	{
		//	-----------------------------------------------
		super.saveComponent(workflow_component);
		//	-----------------------------------------------
	}
	//==================================================================

}