//----------------------------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 XPDLSplit extends XPDLComplexComponent
{
	private XPDLAttribute attrType = null;
	/**
	* Creates a new instance of the class.
	*/
	public XPDLSplit(XPDLComplexComponent parent)
	{
		super(parent, "Split");
	}

	/**
	* The split 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 split 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()
	{
		XPDLSplit s = (XPDLSplit) super.clone();
		s.attrType = (XPDLAttribute) this.attrType.clone();

		s.structComponent();

		return s;
	}

	//==================================================================
	public void initComponent()
	{
		//	-----------------------------------------------
		super.initComponent();
		//	-----------------------------------------------	
		attrType = new XPDLAttribute(this, "Type", new String[] { "", "OR","AND", "XOR" }, 1);
		attrType.initComponent();
		attrType.setLabel(sm.getString("SplitTypeKey"));

	}

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

		complexChildren.add(attrType);

		attributes.add(attrType);
	}

	/**
	* 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();

		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.getSplitType().getType());
			}
		}
	}

	/**
	* 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();

		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.setSplitType(attrType.toString());
			}
		}
	}

	//==================================================================
}
