//----------------------------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;
import org.huihoo.god4j.editor.XPDLCollection;
import org.huihoo.god4j.editor.XPDLCollectionComponent;
import org.huihoo.god4j.editor.elements.set.XPDLActivities;
import org.huihoo.god4j.editor.elements.set.XPDLActualParameters;
import org.huihoo.god4j.editor.elements.set.XPDLDataFields;
import org.huihoo.god4j.editor.elements.set.XPDLProcesses;
import org.huihoo.god4j.editor.elements.set.XPDLTools;
import org.huihoo.god4j.editor.panels.JavaComboxButtonPanel;
import org.huihoo.god4j.editor.panels.JavaPanel;
import org.huihoo.workflow.WorkflowComponent;
import org.huihoo.workflow.WorkflowSimpleComponent;
import org.huihoo.workflow.xpdl.parameter.ActualParameter;
/**
* Represents a WfMC DTD element that has the similar name.
*
* @see XPDL
*/
public class XPDLActualParameter extends XPDLCollectionComponent
{
	/**
	* Reference to all defined DataFields, within workflow process
	* that element which member is instance of this element belongs
	* to, plus all defined DataFields within Package.
	* These DataFields are used as ActualParameters.
	*/
	private transient XPDLDataFields definedDataFields=null;
	
	/**
	* Creates a new instance of the class.
	*
	* @param   wp   workflow process that defines DataField objects
	*               which are used as actual parameters of an
	*               application or of a process itself.
	*/
	public XPDLActualParameter(XPDLCollection myCollection, ActualParameter aPram)
	{
		super(myCollection,"ActualParameter");
		setWorkflowComponent(aPram);
	}
	/**
	* Prepares the panel with a combo box to choose the one of
	* defined DataFields that will be used as an actual parameter.
	*
	* @return XPDLPanel to be shown.
	*/
	public JavaPanel getJavaPanel()
	{		
		Object[] choices = null;
		if (definedDataFields != null)
		{
			choices = definedDataFields.getComboxComponents().toArray();
		}
		else
		{
			choices = new Object[0];
		}
		return new JavaComboxButtonPanel(this, choices, definedDataFields, true);
	}
	/**
	* Used to create exact copy of instance of this class.
	* The newly created instance will have all the properties
	* same as the copied one.
	*
	* @return The newly created instance of this class.
	*/
	public Object clone()
	{
		XPDLActualParameter a = (XPDLActualParameter) super.clone();
		a.definedDataFields=this.definedDataFields;
		
		a.structComponent();
		return a;
	}
	
	//==================================================================
	public void initComponent()
	{
		//	-----------------------------------------------
		super.initComponent();
		//	-----------------------------------------------	
		
		XPDLActualParameters xpACPs = (XPDLActualParameters)getOwnerCollection();
		XPDLTool  xpTool = (XPDLTool)xpACPs.getParentComponent();
		XPDLTools xpTools = (XPDLTools)xpTool.getOwnerCollection();

		XPDLImplementation xpdlImpl = (XPDLImplementation) xpTools.getParentComponent();
		XPDLActivity xpAct = (XPDLActivity) xpdlImpl.getParentComponent();
		XPDLActivities xpActs = (XPDLActivities) xpAct.getOwnerCollection();

		XPDLProcess   xpProcess = (XPDLProcess) xpActs.getParentComponent();
		XPDLProcesses xpProcesses = (XPDLProcesses) xpProcess.getOwnerCollection();
		XPDLPackage   xpPackage = (XPDLPackage) xpProcesses.getParentComponent();
		definedDataFields = (XPDLDataFields) xpPackage.get("DataFields");
	}

	public void structComponent()
	{
		//	-----------------------------------------------
		super.structComponent();
		//	-----------------------------------------------;
	}
	
	public void loadComponent()
	{	 
		//	-----------------------------------------------
		super.loadComponent();
		//	-----------------------------------------------
		
		WorkflowComponent workflow_component = getWorkflowComponent();
		if (workflow_component != null && (workflow_component instanceof WorkflowSimpleComponent))
		{
			WorkflowSimpleComponent simple_component = (WorkflowSimpleComponent) workflow_component;
			attrId.setValue(simple_component.getUUID());
			
			setValue(definedDataFields.findComponent(simple_component.getUUID()));		
		}
	}

	public void saveComponent()
	{	
		if(isReadOnly())
		{
			return;
		}
		
		//	-----------------------------------------------
		super.saveComponent();
		//	-----------------------------------------------
		Object value=getValue();
		if(value!=null && (value instanceof XPDLDataField))
		{
			//System.out.println("---saveComponent----->getUUID:"+((XPDLDataField)value).getUUID());
			attrId.setValue(((XPDLDataField)value).getUUID());
		}
	}
	
	public void saveComponent(WorkflowComponent workflow_component)
	{
		if(isReadOnly())
		{
			return;
		}
		
		//	-----------------------------------------------
		super.saveComponent(workflow_component);
		//	-----------------------------------------------
		
		if (workflow_component != null && (workflow_component instanceof WorkflowSimpleComponent))
		{
			WorkflowSimpleComponent simple_component = (WorkflowSimpleComponent) workflow_component;
			simple_component.setUUID(attrId.toString());
		}
	}
}
