//----------------------------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.elements.set;
import java.util.Iterator;
import java.util.List;

import org.huihoo.god4j.GOD4JFactory;
import org.huihoo.god4j.editor.XPDLCollection;
import org.huihoo.god4j.editor.XPDLCollectionComponent;
import org.huihoo.god4j.editor.XPDLComplexComponent;
import org.huihoo.god4j.editor.elements.XPDLActualParameter;
import org.huihoo.god4j.editor.elements.XPDLTool;
import org.huihoo.god4j.editor.panels.JavaActualParametersListControlPanel;
import org.huihoo.god4j.editor.panels.JavaGroupPanel;
import org.huihoo.god4j.editor.panels.JavaListPanel;
import org.huihoo.god4j.editor.panels.JavaPanel;
import org.huihoo.workflow.WorkflowComponent;
import org.huihoo.workflow.xpdl.WorkflowParameter;
import org.huihoo.workflow.xpdl.activity.ToolImplementation;
import org.huihoo.workflow.xpdl.parameter.ActualParameter;
/**
* Represents a WfMC DTD element that has the similar name.
* This class is a collection of class <b>ActualParameter</b> instances.
*
* @see XPDL
*/
public class XPDLActualParameters extends XPDLCollection
{
	/**
	* Creates a new instance of the class.
	*
	* @param   wp   The workflow process which defines DataField
	*               elements to use as a actual parameters.
	*/
	public XPDLActualParameters(XPDLComplexComponent parent)
	{
		super( parent,"ActualParameters");
	}
	/**
	* Generates a new element of the class which instances
	* are members of collection of this class.
	*
	* return The generated instance of class that makes collection.
	*/
	public XPDLCollectionComponent demoComponent()
	{
		XPDLActualParameter a = new XPDLActualParameter(this, null);
		a.initComponent();
		a.structComponent();
		a.loadComponent();
		return a;
	}
	/**
	* Creates panel which allows user to manipulate with instances
	* of classes that makes a collection that this class represents.
	* The panel consists of two sections: XPDLListPanel - shows
	* collections' elements, and XPDLListControlPanel - display buttons
	* for manipulating with that elements.
	* <p>NOTE: The order of creation is relevant, first, the controlled
	*           panel must be created, and then the control panel
	*
	* @return DTDPanel to be shown
	*/
	public JavaPanel getJavaPanel()
	{
		controlledPanel = new JavaListPanel(this, "", false, true, false);
		//controlPanel=new JavaListControlPanel(this,"",true,false,true);
		controlPanel = new JavaActualParametersListControlPanel(this, "", true, false);
		return new JavaGroupPanel(
			this,
			new JavaPanel[] { controlledPanel, controlPanel },
			getLabel(),
			JavaPanel.BOX_LAYOUT,
			false,
			true);
	}
	
	public Object clone()
	{
		XPDLActualParameters ap = (XPDLActualParameters) super.clone();
		return ap;
	}
	//==================================================================

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

	public void structComponent()
	{
		//	-----------------------------------------------
		super.structComponent();
		//	-----------------------------------------------
	}
	
	/**
	* Supposed to be overriden by derived classes to implement
	* element specific reading from an XPDL Model. This implementation
	* satisfies only "simple" elements.
	*/
	public void loadComponent()
	{
		//	-----------------------------------------------
		super.loadComponent();
		//	-----------------------------------------------

		if (parent_component==null || !(parent_component instanceof XPDLTool))
		{
			//	-----------------------------------------------
			collectionLoadComponent();
			//	-----------------------------------------------
			return;
		}
		
		WorkflowComponent workflow_component= parent_component.getWorkflowComponent();
		
		if(workflow_component ==null || !(workflow_component instanceof ToolImplementation))
		{
			return;
		}
		
		ToolImplementation toolImpl = (ToolImplementation)workflow_component;
		
		List acts = toolImpl.getActualParameters();

		//1. remove all missing ActualParameter
		List element_clones = (List) component_elements.clone();
		Iterator it = element_clones.iterator();
		while (it.hasNext())
		{
			XPDLCollectionComponent el = (XPDLCollectionComponent) it.next();
			if (el instanceof XPDLActualParameter)
			{								
				if (toolImpl.findActualParameter(el.getUUID()) == null)
				{
					removeComponent(el);
				}
			}
		}

		//2. add all newly ActualParameter
		for (int i = 0; i < acts.size(); ++i)
		{
			ActualParameter act = (ActualParameter) acts.get(i);
			WorkflowParameter workflow_P=GOD4JFactory.getGOD4J().getDocument().findWorkflowParameter(act.getUUID());
			if(workflow_P!=null)
			{
				act.setName(workflow_P.getName());
			}
			XPDLCollectionComponent el = findComponent(act.getUUID());
			if (el == null)
			{
				XPDLActualParameter xpACP = new XPDLActualParameter(this, act);
				xpACP.initComponent();
				xpACP.structComponent();

				addComponent(xpACP);
			}
		}
		
		//	-----------------------------------------------
		collectionLoadComponent();
		//	-----------------------------------------------
	}

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

		WorkflowComponent workflow_component = getOwnerComponent().getWorkflowComponent();
		
		if(workflow_component ==null || !(workflow_component instanceof ToolImplementation))
		{
			return;
		}
		
		ToolImplementation toolImpl = (ToolImplementation)workflow_component;
		
		if (toolImpl == null)
		{
			return;
		}
		
		saveComponent(toolImpl);

	}

	public void saveComponent(WorkflowComponent workflow_component)
	{
		//System.out.println("-------------ACPs --------saveComponent----------");
		//	-----------------------------------------------
		super.saveComponent(workflow_component);
		//	-----------------------------------------------
		
		if ((workflow_component == null) || (!(workflow_component instanceof ToolImplementation)))
		{
			return;
		}

		ToolImplementation toolImpl = (ToolImplementation) workflow_component;
		toolImpl.getActualParameters().clear();
		Iterator it = component_elements.iterator();
		while (it.hasNext())
		{
			XPDLCollectionComponent el = (XPDLCollectionComponent) it.next();
			
			if (el instanceof XPDLActualParameter)
			{
				ActualParameter actual_P = new ActualParameter();
				XPDLActualParameter xactual_P = (XPDLActualParameter) el;
				xactual_P.saveComponent(actual_P);
				xactual_P.setWorkflowComponent(actual_P);
				
				WorkflowParameter workflow_P=GOD4JFactory.getGOD4J().getDocument().findWorkflowParameter(actual_P.getUUID());
				if(workflow_P!=null)
				{
					actual_P.setName(workflow_P.getName());
				}
				
				//System.out.println("-------------ACPs --------saveComponent-----actual_P-----"+actual_P.getUUID());
				
				toolImpl.addActualParameter(actual_P);
			}
		}
	}
	//==================================================================
}
