//----------------------------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.atoms;
import org.huihoo.god4j.editor.XPDLAttribute;
import org.huihoo.god4j.editor.XPDLComplexComponent;
import org.huihoo.workflow.WorkflowComponent;
import org.huihoo.workflow.xpdl.WorkflowPackage;
import org.huihoo.workflow.xpdl.WorkflowScript;
/**
* Represents a WfMC DTD element that has the similar name.
*/
public class XPDLScript extends XPDLComplexComponent
{
	private XPDLAttribute attrType = null;
	private XPDLAttribute attrVersion =null;
	private XPDLAttribute attrGrammar = null;
	
	/**
	* Creates a new instance of the class.
	*/
	public XPDLScript(XPDLComplexComponent parent)
	{
		super(parent,"Script");
	}

//==================================================================
	public void initComponent()
	{
//	-----------------------------------------------
		super.initComponent();
//	-----------------------------------------------
		attrType = new XPDLAttribute( this,"Type"); // required
		attrType.initComponent();
		attrType.setRequired(true);
		
		attrVersion = new XPDLAttribute( this,"Version"); // required
		attrVersion.initComponent();
		
		attrGrammar = new XPDLAttribute( this,"Grammar", 1); // required
		attrGrammar.initComponent();
	}


	public void structComponent()
	{
		//	-----------------------------------------------
		super.structComponent();
		//	-----------------------------------------------
		attrType.structComponent();
		attrVersion.structComponent();
		attrGrammar.structComponent();

		complexChildren.add(attrType);
		complexChildren.add(attrVersion);
		complexChildren.add(attrGrammar);
				
		attributes.add(attrType);
		attributes.add(attrVersion);
		attributes.add(attrGrammar);
	}
	
	/**
	* 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();
//	-----------------------------------------------
		attrType.loadComponent();
		attrVersion.loadComponent();
		attrGrammar.loadComponent();
		
		XPDLComplexComponent parent_component = getParentComponent();
		if (parent_component !=null )
		{
			WorkflowComponent workflow_component =  parent_component.getWorkflowComponent();

			if (workflow_component == null)
			{
				return;
			}

			if (workflow_component instanceof WorkflowPackage)
			{
				WorkflowScript script=((WorkflowPackage)workflow_component).getWorkflowScript();
				
				if(script!=null)
				{
					attrType.setValue(script.getType());
					attrVersion.setValue(script.getVersion());
					attrGrammar.setValue(script.getGrammar());
				}
			}
		}
	}

	/**
	* Supposed to be overriden by derived classes to implement
	* element specific writting to an XPDL Model.
	*
	*/
	public void saveComponent() 
	{
    if(isReadOnly())
    {
      return;
    }
    
//	-----------------------------------------------
		super.saveComponent();
//	-----------------------------------------------
		attrType.saveComponent();
		attrVersion.saveComponent();
		attrGrammar.saveComponent();
		
		XPDLComplexComponent parent_component = getParentComponent();
		if (parent_component !=null )
		{
			WorkflowComponent workflow_component =  parent_component.getWorkflowComponent();

			if (workflow_component == null)
			{
				return;
			}

			if (workflow_component instanceof WorkflowPackage)
			{				
				WorkflowScript script=new WorkflowScript();
				script.setType(attrType.toString());
				script.setVersion(attrVersion.toString());
				script.setGrammar(attrGrammar.toString());
				
				((WorkflowPackage)workflow_component).setWorkflowScript(script);
			}
		}
	}
	
//==================================================================
}
