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

import org.huihoo.god4j.editor.XPDLAttribute;
import org.huihoo.god4j.editor.XPDLCollection;
import org.huihoo.god4j.editor.XPDLCollectionComponent;
import org.huihoo.god4j.editor.XPDLComponent;
import org.huihoo.god4j.editor.elements.atoms.XPDLBasicType;
import org.huihoo.god4j.editor.elements.atoms.XPDLDataType;
import org.huihoo.god4j.editor.elements.atoms.XPDLDataTypes;
import org.huihoo.god4j.editor.elements.atoms.XPDLDescription;
import org.huihoo.god4j.editor.elements.atoms.XPDLInitialValue;
import org.huihoo.god4j.editor.elements.set.XPDLExtendedAttributes;
import org.huihoo.god4j.editor.panels.JavaGroupPanel;
import org.huihoo.god4j.editor.panels.JavaPanel;
import org.huihoo.god4j.util.UUID;
import org.huihoo.workflow.WorkflowComponent;
import org.huihoo.workflow.xpdl.WorkflowParameter;
import org.huihoo.workflow.xpdl.util.TypeDefinition;
/**
* Represents a WfMC DTD element that has the similar name.
*
* @see XPDL
*/
public class XPDLDataField extends XPDLCollectionComponent
{
	private XPDLDataType refDataType=null;
	private XPDLInitialValue refInitialValue = null; 
	private XPDLDescription refDescription = null; 
	private XPDLExtendedAttributes refExtendedAttributes = null; 
	private XPDLAttribute attrName = null;
	
	/**
	* Creates a new instance of the class.
	*
	* @param dfs  The reference to collection of  data fields where
	*             this instance will be put into.
	* @param porw The class instance which member is this class instance.
	*/
	public XPDLDataField(XPDLCollection myCollection, WorkflowParameter param)
	{
		super(myCollection,"DataField");
		setWorkflowComponent(param);
	}

	public JavaPanel getJavaPanel()
	{
		JavaPanel p =
			new JavaGroupPanel(
				this,
				new XPDLComponent[] { attrId, attrName, refDataType, refInitialValue, refDescription, refExtendedAttributes },
				getLabel());
		return p;
	}
	
	/**
	  * This method is called only if user doesn't press Cancel button
	  * within the dialog for editing DataField properties, so
	  * the changes to the real collection of DataField are applied here.
	  * @param groupPanel The panel for editing parameters.
	  * @return always returns <tt>true</tt>.
	  */
	public boolean isInputValid(JavaPanel p)
	{
		return true;
	}
	/**
	* Overrides parent method to display data type details for BasicType and
	* for DeclaredType.
	*/
	public Collection findChildrenValues()
	{
		java.util.List l = new ArrayList();
		Iterator it = complexChildren.iterator();
		while (it.hasNext())
		{
			XPDLComponent el = (XPDLComponent) it.next();
			if (el instanceof XPDLAttribute)
			{
				l.add(el.toString());
			}
			else if (el instanceof XPDLDataType)
			{
				XPDLDataType dt = (XPDLDataType) el;
				XPDLDataTypes dtt = dt.getDataTypes();
				Object type = dtt.getValue();
				if (type instanceof XPDLBasicType)
				{
					l.add(el.getValue() + " - " + ((XPDLBasicType) type).getValue());
				}
				
				else
				{
					l.add(el.getValue());
				}
			}
			else
			{
				l.add(el.getValue());
			}
		}
		return l;
	}
//==================================================================
	public void initComponent()
	{
//	-----------------------------------------------
		super.initComponent();
//	-----------------------------------------------			
		
		refDataType = new XPDLDataType(this); // must be defined
		refDataType.initComponent();		
		refDataType.setRequired(true);
		
		refInitialValue = new XPDLInitialValue(this); // min=0
		refInitialValue.initComponent();
		
		refDescription = new XPDLDescription(this); // min=0
		refDescription.initComponent();
		
		refExtendedAttributes = new XPDLExtendedAttributes(this); // min=0
		refExtendedAttributes.initComponent();
		
		attrName = new XPDLAttribute( this,"Name");
		attrName.initComponent();
	}

	public void structComponent()
	{
		//	-----------------------------------------------
		super.structComponent();
		//	-----------------------------------------------		
		attrName.structComponent();
		refDataType.structComponent();
		refInitialValue.structComponent();
		refDescription.structComponent();
		refExtendedAttributes.structComponent();
		
		complexChildren.add(attrName);
		complexChildren.add(refDataType);
		complexChildren.add(refInitialValue);
		complexChildren.add(refDescription);
		complexChildren.add(refExtendedAttributes);
		
		attributes.add(attrName);
	}

	public void loadComponent() 
	{
//	-----------------------------------------------
		super.loadComponent();
//	-----------------------------------------------
		attrName.loadComponent();
		
		refDataType.loadComponent();
		refInitialValue.loadComponent();
		refDescription.loadComponent();
		refExtendedAttributes.loadComponent();
	
		WorkflowComponent workflow_component = getWorkflowComponent();
		if (workflow_component != null && (workflow_component instanceof WorkflowParameter))
		{
			WorkflowParameter param_component = (WorkflowParameter) workflow_component;
			attrId.setValue(param_component.getUUID());
			attrName.setValue(param_component.getName());
			XPDLBasicType basicType=new XPDLBasicType(refDataType.getDataTypes());
			basicType.initComponent();
			basicType.loadComponent();
			basicType.setValue(TypeDefinition.javaToXPDL(((WorkflowParameter)getWorkflowComponent()).getType()));
			refDataType.setValue(basicType);
		}
		else
		{
			attrId.setValue(UUID.generate());
		}		
	}


	public void saveComponent() 
	{
    if(isReadOnly())
    {
      return;
    }
    
    
//	-----------------------------------------------
		super.saveComponent();
//	-----------------------------------------------
		attrName.saveComponent();
		
		refDataType.saveComponent();
		refInitialValue.saveComponent();
		refDescription.saveComponent();
		refExtendedAttributes.saveComponent();
		
		WorkflowComponent workflow_component = getWorkflowComponent();
		if (workflow_component != null && (workflow_component instanceof WorkflowParameter))
		{
			WorkflowParameter param_component = (WorkflowParameter) workflow_component;
			param_component.setUUID(attrId.toString());
			param_component.setName(attrName.toString());
			
			Object obj=refDataType.getValue();
			if((obj!=null) && ( obj instanceof XPDLBasicType))
			{
				((WorkflowParameter)getWorkflowComponent()).setType(TypeDefinition.XPDLToJava(((XPDLBasicType)obj).getValue().toString()));
			}
		}
	}
	
	public void saveComponent(WorkflowComponent workflow_component) 
	{
//	-----------------------------------------------
		super.saveComponent(workflow_component);
//	-----------------------------------------------
		attrName.saveComponent(workflow_component);
		
		refDataType.saveComponent(workflow_component);
		refInitialValue.saveComponent(workflow_component);
		refDescription.saveComponent(workflow_component);
		refExtendedAttributes.saveComponent(workflow_component);

		if (workflow_component != null && (workflow_component instanceof WorkflowParameter))
		{		
			WorkflowParameter param_component = (WorkflowParameter) workflow_component;
			param_component.setUUID(attrId.toString());
			param_component.setName(attrName.toString());
			
			Object obj=refDataType.getValue();
			if((obj!=null) && ( obj instanceof XPDLBasicType))
			{
				param_component.setType(TypeDefinition.XPDLToJava(((XPDLBasicType)obj).getValue().toString()));
			}
		}
	}	
//==================================================================
}
