//----------------------------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.god4j.editor.panels.JavaPanel;
import org.huihoo.workflow.WorkflowComponent;
import org.huihoo.workflow.xpdl.WorkflowActivity;
/**
* Represents a WfMC DTD element that has the similar name.
*/
public class XPDLJoin extends XPDLComplexComponent
{
	protected XPDLAttribute attrType = null;
	/**
	* Creates a new instance of the class.
	*/
	public XPDLJoin(XPDLComplexComponent parent)
	{
		super(parent, "Join");
	}

	/**
	* The join is empty if user didn't select AND or XOR.
	*/
	public boolean isEmpty()
	{
		return (attrType.getChoosen().toString().length() == 0);
	}
	/**
	* Prepares the panel with a combo box to choose the one of
	* allowed join types.
	*
	* @return XPDLPanel to be shown.
	*/
	public JavaPanel getJavaPanel()
	{
		return attrType.getJavaPanel();
	}

	public Object getValue()
	{
		return attrType.getValue();
	}

	/**
	* 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()
	{
		XPDLJoin j = (XPDLJoin) super.clone();
		j.attrType = (XPDLAttribute) this.attrType.clone();
		j.structComponent();
		return j;
	}

	//==================================================================

	public void initComponent()
	{
		//	-----------------------------------------------
		super.initComponent();
		//	-----------------------------------------------	
		//		attrType =
		//			new XPDLAttribute(
		//				this,
		//				"Type",
		//				new String[] { "", "AND", "XOR", "OR", "ROUTE", "WEIGHT", "INSTANCE_AND", "INSTANCE_XOR", "INSTANCE_WEIGHT" },
		//				0);

		attrType = new XPDLAttribute(this, "Type", new String[] { "", "AND", "XOR", "OR", "ROUTE","INSTANCE_AND","INSTANCE_XOR" }, 0);
		attrType.initComponent();
		attrType.setLabel(sm.getString("JoinTypeKey"));
	}

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

		attributes.add(attrType);
		complexChildren.add(attrType);
	}


	public void loadComponent()
	{
		//	-----------------------------------------------
		super.loadComponent();
		//	-----------------------------------------------
		XPDLComplexComponent parent_component = getParentComponent();
		if (parent_component != null)
		{
			WorkflowComponent workflow_component = parent_component.getWorkflowComponent();
			if (workflow_component != null && (workflow_component instanceof WorkflowActivity))
			{
				WorkflowActivity activity = (WorkflowActivity) workflow_component;
				attrType.setValue(activity.getJoinType().getType());
			}
		}
	}

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

		XPDLComplexComponent parent_component = getParentComponent();
		if (parent_component != null)
		{
			WorkflowComponent workflow_component = parent_component.getWorkflowComponent();

			if (workflow_component != null && (workflow_component instanceof WorkflowActivity))
			{
				WorkflowActivity activity = (WorkflowActivity) workflow_component;
				activity.setJoinType(attrType.toString());
			}
		}
	}
	//==================================================================
}
