//----------------------------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.XPDLComplexComponent;
import org.huihoo.god4j.editor.XPDLComponent;
import org.huihoo.god4j.editor.elements.atoms.XPDLDescription;
import org.huihoo.god4j.editor.elements.set.XPDLExtendedAttributes;
import org.huihoo.god4j.editor.panels.JavaGroupPanel;
import org.huihoo.god4j.editor.panels.JavaMultiLineTextPanel;
import org.huihoo.god4j.editor.panels.JavaPanel;
import org.huihoo.workflow.WorkflowComponent;
import org.huihoo.workflow.xpdl.WorkflowTransition;
/**
* Represents a WfMC DTD element that has the similar name.
*/
public class XPDLTransition extends XPDLCollectionComponent
{
	//-----------------------------------------------------------------------------
	private XPDLAttribute attrFrom = null; //required
	private XPDLAttribute attrTo = null; //required
	private XPDLAttribute attrName = null;

	private XPDLCondition refCondition = null; // min==0
	private XPDLDescription refDescription = null; // min=0
	private XPDLExtendedAttributes refExtendedAttributes = null; // min=0

	//-----------------------------------------------------------------------------

	/** The workflow element which is source for transition */
	private transient XPDLActivity from = null;
	/** The workflow element which is tareget for transition */
	private transient XPDLActivity to = null;

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

	/**
	* Creates a new instance of the class.
	*
	* @param ts The reference to collection of Transitions where
	*           this instance will be put into.
	*/
	public XPDLTransition(XPDLCollection myCollection, WorkflowTransition trans)
	{
		super(myCollection);
		setWorkflowComponent(trans);
	}

	/**
	* Sets the source element for transition.
	*
	* @param from The source element for the transition.
	*/
	public void setFromActivity(XPDLActivity from)
	{
		this.from = from;
		attrFrom.setValue(this.from.get("Id").getValue());
	}
	/**
	* Sets the target element for transition.
	*
	* @param to The target element for the transition.
	*/
	public void setToActivity(XPDLActivity to)
	{
		this.to = to;
		attrTo.setValue(this.to.get("Id").getValue());
	}

	/**
		* Gets the source element for transition.
		*
		* @return The source element for the transition.
		*/
	public XPDLActivity getFromActivity()
	{
		return from;
	}

	/**
		* Gets the target element for transition.
		*
		* @return The target element for the transition.
		*/
	public XPDLActivity getToActivity()
	{
		return to;
	}

	public JavaPanel getJavaPanel()
	{

		try
		{
			XPDLAttribute f = new XPDLAttribute(this, "From");
			f.setValue(from.toString());
			f.setReadOnly(true);
			XPDLAttribute t = new XPDLAttribute(this, "To");
			t.setValue(to.toString());
			t.setReadOnly(true);
			clonedEAs = (XPDLExtendedAttributes) refExtendedAttributes.clone();
			return new JavaGroupPanel(
				this,
				new XPDLComponent[] { attrId, attrName, f, t, refDescription, refCondition, clonedEAs },
				getLabel());
		}
		catch (Exception ex)
		{
			XPDLComponent el = new XPDLComponent(null, "ErrorMessage")
			{
				public JavaPanel getJavaPanel()
				{
					setReadOnly(true);
					value = sm.getString("ErrorGraphObjectIsNotDefined");
					return new JavaMultiLineTextPanel(this);
				}
			};
			return el.getJavaPanel();
		}
	}

	public String toString()
	{
		if (to != null)
		{
			return to.toString();
		}
		else
		{
			return "";
		}
	}

	public Object clone()
	{
		XPDLTransition a = (XPDLTransition) super.clone();
		a.attrName = (XPDLAttribute) this.attrName.clone();
		a.attrFrom = (XPDLAttribute) this.attrFrom.clone();
		a.attrTo = (XPDLAttribute) this.attrTo.clone();
		a.refCondition = (XPDLCondition) this.refCondition.clone();
		a.refDescription = (XPDLDescription) this.refDescription.clone();
		a.refExtendedAttributes = (XPDLExtendedAttributes) this.refExtendedAttributes.clone();

		a.from = this.from;
		a.to = this.to;
		a.clonedEAs = null;

		a.structComponent();

		return a;
	}

	public boolean isInputValid(JavaPanel groupPanel)
	{
		if (clonedEAs != null)
		{
			complexChildren.remove(refExtendedAttributes);
			refExtendedAttributes = clonedEAs;
			complexChildren.add(6, refExtendedAttributes);
		}
		return true;
	}

	//==================================================================
	public void initComponent()
	{
		//	-----------------------------------------------
		super.initComponent();
		//	-----------------------------------------------	
		attrFrom = new XPDLAttribute(this, "From"); //required
		attrFrom.initComponent();
		attrFrom.setReadOnly(true);

		attrTo = new XPDLAttribute(this, "To"); //required
		attrTo.initComponent();
		attrTo.setReadOnly(true);

		attrName = new XPDLAttribute(this, "Name");
		attrName.initComponent();

		refCondition = new XPDLCondition(this); // min==0
		refCondition.initComponent();

		refDescription = new XPDLDescription(this); // min=0
		refDescription.initComponent();

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

	}

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

		refCondition.structComponent();
		refDescription.structComponent();
		refExtendedAttributes.structComponent();

		complexChildren.add(attrName);
		complexChildren.add(attrFrom);
		complexChildren.add(attrTo);
		complexChildren.add(refCondition);
		complexChildren.add(refDescription);
		complexChildren.add(refExtendedAttributes);

		attributes.add(attrName);
		attributes.add(attrFrom);
		attributes.add(attrTo);
	}

	public void loadComponent()
	{
		//	-----------------------------------------------
		super.loadComponent();
		//	-----------------------------------------------
		attrName.loadComponent();
		attrFrom.loadComponent();
		attrTo.loadComponent();
		refCondition.loadComponent();
		refDescription.loadComponent();
		refExtendedAttributes.loadComponent();

		WorkflowComponent workflow_component = getWorkflowComponent();

		if (workflow_component != null && (workflow_component instanceof WorkflowTransition))
		{
			WorkflowTransition tran = (WorkflowTransition) workflow_component;
			attrId.setValue(tran.getUUID());
			attrName.setValue(tran.getName());

			attrFrom.setValue(tran.getFromWorkflowActivity().getUUID());
			attrTo.setValue(tran.getToWorkflowActivity().getUUID());

			XPDLComplexComponent xprocess = getParentComponent();
			XPDLCollection activities = (XPDLCollection) xprocess.get("Activities");
			this.from = (XPDLActivity) activities.findComponent(tran.getFromWorkflowActivity().getUUID());
			this.to = (XPDLActivity) activities.findComponent(tran.getToWorkflowActivity().getUUID());
		}
	}

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

		refCondition.saveComponent();
		refDescription.saveComponent();
		refExtendedAttributes.saveComponent();

		WorkflowComponent workflow_component = getWorkflowComponent();
		if (workflow_component != null && (workflow_component instanceof WorkflowTransition))
		{
			WorkflowTransition tran = (WorkflowTransition) workflow_component;
			tran.setUUID(attrId.toString());
			tran.setName(attrName.toString());
		}
	}

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

		refCondition.saveComponent(workflow_component);
		refDescription.saveComponent(workflow_component);
		refExtendedAttributes.saveComponent(workflow_component);

		if (workflow_component != null && (workflow_component instanceof WorkflowTransition))
		{
			WorkflowTransition tran = (WorkflowTransition) workflow_component;
			tran.setUUID(attrId.toString());
			tran.setName(attrName.toString());
		}
	}
	//==================================================================
}
