//----------------------------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.set;
import java.util.Iterator;
import java.util.List;

import org.huihoo.god4j.editor.XPDLCollection;
import org.huihoo.god4j.editor.XPDLCollectionComponent;
import org.huihoo.god4j.editor.XPDLComplexComponent;
import org.huihoo.god4j.editor.elements.XPDLProcess;
import org.huihoo.god4j.editor.elements.XPDLTransition;
import org.huihoo.god4j.editor.panels.JavaGroupPanel;
import org.huihoo.god4j.editor.panels.JavaPanel;
import org.huihoo.god4j.editor.panels.JavaTableControlPanel;
import org.huihoo.god4j.editor.panels.JavaTablePanel;
import org.huihoo.workflow.xpdl.WorkflowProcess;
import org.huihoo.workflow.xpdl.WorkflowTransition;
/**
* Represents a WfMC DTD element that has the similar name.
* This class is a collection of class <b>Transition</b> instances.
*
* @see XPDL
*/
public class XPDLTransitions extends XPDLCollection
{
	/**
	* Creates a new instance of the class.
	*
	* @param wpOrAs The workflow process or activiy set that holds transitions,
	* and which member is this class instance.
	*/
	public XPDLTransitions(XPDLComplexComponent parent)
	{
		super(parent);
	}

	/**
	* Generates a new element of the class which instances
	* are members of collection of this class.
	*
	* return The generated instance of class that makes collection.
	*/
	public XPDLCollectionComponent demoComponent()
	{
		XPDLTransition a = new XPDLTransition(this, null);
		a.initComponent();
		a.structComponent();
		//a.loadComponent();
		
		return a;
	}

	/**
	* Returns the transition object with specified ID.
	*
	* @param ID ID of wanted transition
	* @return The transitions that have specified ID if exists,
	*         <tt>null</tt> otherwise.
	*/
	public XPDLTransition getXPDLTransition(String ID)
	{
		return (XPDLTransition) super.findComponent(ID);
	}
	/**
	* Returns <tt>true</tt> if specified data field or formal parameter is
	* used in transition condition expression of any transition.
	*/
	protected boolean canRemoveDataFieldOrFormalParameter(XPDLCollectionComponent toRemove)
	{
		return true;
	}
	public int[] getInvisibleTableIndexes()
	{
		int[] itfo = new int[4];
		itfo[0] = 2;
		itfo[1] = 3;
		itfo[2] = 5;
		itfo[3] = 6;
		return itfo;
	}
	// First, the controlled panel must be created, and then the control panel
	public JavaPanel getJavaPanel()
	{
		isReadOnly = true;
		controlledPanel = new JavaTablePanel(this, "", false, false);
		controlPanel = new JavaTableControlPanel(this, "", true, false);
		return new JavaGroupPanel(
			this,
			new JavaPanel[] { controlledPanel, controlPanel },
			getLabel(),
			JavaPanel.BOX_LAYOUT,
			false,
			true);
	}
	public Object getValue()
	{
		return String.valueOf(component_elements.size());
	}
	//==================================================================
	public void initComponent()
	{
		//	-----------------------------------------------
		super.initComponent();
		//	-----------------------------------------------	
	}

	public void structComponent()
	{
		//	-----------------------------------------------
		super.structComponent();
		//	-----------------------------------------------
	}
	
	public void loadComponent()
	{
		//	-----------------------------------------------
		super.loadComponent();
		//	-----------------------------------------------
		XPDLProcess xprocess = (XPDLProcess) parent_component;
		WorkflowProcess process = (WorkflowProcess) xprocess.getWorkflowComponent();
		if(process == null)
		{
			//	-----------------------------------------------
			collectionLoadComponent();
			//	-----------------------------------------------
			return;
		}
		
		List acts = process.getWorkflowTransitions();

		//1. remove all missing WorkflowTransition
		List element_clones = (List) component_elements.clone();
		Iterator it = element_clones.iterator();
		while (it.hasNext())
		{
			XPDLCollectionComponent el = (XPDLCollectionComponent) it.next();
			if (el instanceof XPDLTransition)
			{
				if (process.findWorkflowTransition(el.getUUID()) == null)
				{
					removeComponent(el);
				}
			}
		}

		//2. add all newly WorkflowTransition

		for (int i = 0; i < acts.size(); ++i)
		{
			WorkflowTransition act = (WorkflowTransition) acts.get(i);
			XPDLCollectionComponent el = findComponent(act.getUUID());
			if (el == null)
			{
				XPDLTransition xpTrans = new XPDLTransition(this, act);
				xpTrans.initComponent();
				xpTrans.structComponent();

				addComponent(xpTrans);
			}
		}
		
		//	-----------------------------------------------
		collectionLoadComponent();
		//	-----------------------------------------------
	}

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

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