//----------------------------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.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.set.XPDLActivities;
import org.huihoo.god4j.editor.elements.set.XPDLExtendedAttributes;
import org.huihoo.god4j.editor.elements.set.XPDLTransitions;
import org.huihoo.god4j.editor.panels.JavaGroupPanel;
import org.huihoo.god4j.editor.panels.JavaPanel;
import org.huihoo.god4j.editor.panels.JavaTabbedPanel;
import org.huihoo.workflow.WorkflowComponent;
import org.huihoo.workflow.impl.xpdl.WorkflowProcessImpl;
import org.huihoo.workflow.xpdl.WorkflowProcess;
/**
* Represents a WfMC DTD element that has the similar name.
*/
public class XPDLProcess extends XPDLCollectionComponent
{
	private XPDLProcessHeader refProcessHeader = null;
	private XPDLActivities refActivities = null; // min=0
	private XPDLTransitions refTransitions = null; // min=0
	private XPDLExtendedAttributes refExtendedAttributes = null; // min=0
	private XPDLAttribute attrName = null;

	private XPDLAttribute attrListenerClassName = null; //only for willow

	//-----------------------------------------------------------
	/** Enables canceling of changes to the extended attributes collection. */
	private XPDLExtendedAttributes clonedEAs;

	/**
	* Creates a new instance of the class.
	*
	* @param wps The reference to collection of workflow processes where
	*            this instance will be put into.
	* @param p   The container for all processes.
	*/
	public XPDLProcess(XPDLCollection myCollection, WorkflowProcess workflowProcess)
	{
		super(myCollection);
		setWorkflowComponent(workflowProcess);
	}

	/**
	* Overrides super-class method to retreive the value
	* of this class "Name" attribute, or if it does not exist,
	* the value of "Id" attribute.
	* This is used when displaying instance of this class
	* within dialog.
	*
	* @return The "Name" attribute value of this class.
	*/
	public String toString()
	{
		String disp = attrName.toString();
		if (disp.trim().length() == 0)
		{
			disp = attrId.toString();
		}
		return disp;
	}

	/**
	* Prepares the tabbed panel to show editable fields of WorkflowProcess.
	* Panel consists of seven tabs that logically comprises the
	* XPDLProcesses elements to be edited.
	* <p>NOTE: This method is never used, panels are retrieved one by one
	*          calling the method with the same name but with ordinal
	*          number of wanted panel as an argument.
	*
	* @return XPDLPanel to be shown.
	*/
	public JavaPanel getJavaPanel()
	{
		clonedEAs = (XPDLExtendedAttributes) refExtendedAttributes.clone();
		JavaPanel p1 =
			new JavaGroupPanel(
				this,
				new XPDLComponent[] { attrId, attrName, refExtendedAttributes },
				sm.getString("GeneralKey"));
		JavaPanel p2 = refProcessHeader.getJavaPanel();

		JavaPanel p3 = refActivities.getJavaPanel();

		JavaPanel p4 = refTransitions.getJavaPanel();

		JavaPanel p5 = new JavaGroupPanel(this, new XPDLComponent[] { attrListenerClassName }, sm.getString("AdvancedKey"));

		return new JavaTabbedPanel(this, new JavaPanel[] { p1, p2, p3, p4, p5 });
	}

	//==================================================================
	public void initComponent()
	{
		//	-----------------------------------------------
		super.initComponent();
		//	-----------------------------------------------
		attrName = new XPDLAttribute(this, "Name");
		attrName.initComponent();

		attrListenerClassName = new XPDLAttribute(this, "ListenerClassName");
		attrListenerClassName.initComponent();

		refProcessHeader = new XPDLProcessHeader(this);
		refProcessHeader.initComponent();
		refProcessHeader.setRequired(true);

		refActivities = new XPDLActivities(this); // min=0
		refActivities.initComponent();

		refTransitions = new XPDLTransitions(this); // min=0
		refTransitions.initComponent();

		refExtendedAttributes = new XPDLExtendedAttributes(this); // min=0
		refExtendedAttributes.initComponent();
	}

	public void structComponent()
	{
		//	-----------------------------------------------
		super.structComponent();
		//	-----------------------------------------------
		attrName.structComponent();
		attrListenerClassName.structComponent();

		refProcessHeader.structComponent();
		refActivities.structComponent();
		refTransitions.structComponent();
		refExtendedAttributes.structComponent();

		complexChildren.add(attrName);
		complexChildren.add(refProcessHeader);
		complexChildren.add(refActivities);
		complexChildren.add(refTransitions);
		complexChildren.add(refExtendedAttributes);
		complexChildren.add(attrListenerClassName);

		attributes.add(attrName);
		attributes.add(attrListenerClassName);
	}

	public void loadComponent()
	{
		//	-----------------------------------------------
		super.loadComponent();
		//	-----------------------------------------------
		attrName.loadComponent();
		attrListenerClassName.loadComponent();

		refProcessHeader.loadComponent();
		refActivities.loadComponent();
		refTransitions.loadComponent();
		refExtendedAttributes.loadComponent();

		WorkflowComponent workflow_component = getWorkflowComponent();
		if (workflow_component != null && (workflow_component instanceof WorkflowProcessImpl))
		{
			WorkflowProcessImpl simple_component = (WorkflowProcessImpl) workflow_component;
			attrId.setValue(simple_component.getUUID());
			attrName.setValue(simple_component.getName());
			attrListenerClassName.setValue(simple_component.getListenerClassName());
		}
	}

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

		refProcessHeader.saveComponent();
		refActivities.saveComponent();
		refTransitions.saveComponent();
		refExtendedAttributes.saveComponent();

		WorkflowComponent workflow_component = getWorkflowComponent();
		if (workflow_component != null && (workflow_component instanceof WorkflowProcessImpl))
		{
			WorkflowProcessImpl simple_component = (WorkflowProcessImpl) workflow_component;
			simple_component.setUUID(attrId.toString());
			simple_component.setName(attrName.toString());
			simple_component.setListenerClassName(attrListenerClassName.toString());
		}
	}

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

		attrName.saveComponent(workflow_component);
		attrListenerClassName.saveComponent(workflow_component);

		refProcessHeader.saveComponent(workflow_component);
		refActivities.saveComponent(workflow_component);
		refTransitions.saveComponent(workflow_component);
		refExtendedAttributes.saveComponent(workflow_component);

		if (workflow_component != null && (workflow_component instanceof WorkflowProcessImpl))
		{
			WorkflowProcessImpl simple_component = (WorkflowProcessImpl) workflow_component;
			simple_component.setUUID(attrId.toString());
			simple_component.setName(attrName.toString());
			simple_component.setListenerClassName(attrListenerClassName.toString());
		}
	}
	//==================================================================
}
