//----------------------------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.ArrayList;
import java.util.Collection;
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.XPDLComponent;
import org.huihoo.god4j.editor.elements.XPDLPackage;
import org.huihoo.god4j.editor.elements.XPDLProcess;
import org.huihoo.god4j.editor.panels.JavaPanel;
import org.huihoo.workflow.xpdl.WorkflowPackage;
import org.huihoo.workflow.xpdl.WorkflowProcess;

/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class XPDLProcesses extends XPDLCollection
{
	/**
	* Creates a new instance of the class.
	*
	* @param p The container for all processes.
	*/
	public XPDLProcesses(XPDLComplexComponent parent)
	{
		super(parent,"WorkflowProcesses");
	}

	public boolean contains(XPDLProcess wp)
	{
		return component_elements.contains(wp);
	}

	// min=0, max=unbounded
	/**
	* 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()
	{
		XPDLProcess a = new XPDLProcess(this, null);
		a.initComponent();
		a.structComponent();
		//a.loadComponent();
		
		return a;
	}
	/**
	* Returns a workflow process with specified ID.
	*
	* @param  ID The 'Id' attribute of wanted instance of
	*         XPDLProcess element defined within a
	*         XPDL file.
	* @return Returns the instance of XPDLProcess element
	*         that have the specified ID as an 'Id' attribute
	*         if it exist among all defined processes within
	*         package, if it doesn't exist, <tt>null</tt> is
	*         returned.
	*/
	public XPDLProcess getXPDLProcess(String ID)
	{
		XPDLProcess wp = (XPDLProcess) super.findComponent(ID);
		return wp;
	}
	/**
		* Returns the collection of processes defined in the current package. It
		* doesn't include processes from referenced external packages.
		*/
	public Collection getTableComponents()
	{
		ArrayList chos = new ArrayList(component_elements);
		return chos;
	}
	/**
		* Returns collection of all workflow processes from current package and
		* processes from referenced external packages, but leaving out the
		* processes from external packages which Id is equal to any of current
		* package processes Ids.
		*/
	public Collection getComboxComponents()
	{
		ArrayList chos = new ArrayList(component_elements);
		return chos;
	}
	/**
	* Checks if specified workflow process can be removed. Workflow process
	* can be removed only if it is not referenced within any subflow.
	*/
	public boolean canRemoveElement(XPDLComponent el)
	{
		boolean remove = true;
		XPDLProcess toRemove = (XPDLProcess) el;
		Iterator it = component_elements.iterator();
		while (it.hasNext())
		{
			XPDLProcess wp = (XPDLProcess) it.next();
			// do not check itself and external workflows
			if (wp != toRemove)
			{
				XPDLActivities acts = (XPDLActivities) wp.get("Activities");
				remove = acts.canRemoveProcess(toRemove);
				if (!remove)
				{
					break;
				}

			}
		}
		return remove;
	}

	public int[] getInvisibleTableIndexes()
	{
		int[] itfo = new int[2];
		itfo[0] = 2;
		itfo[1] = 5;
		return itfo;
	}
	/**
	* @return XPDLPanel to be shown.
	*/
	public JavaPanel getJavaPanel()
	{
		return super.getJavaPanel();
	}
	//==================================================================
	public void initComponent()
	{
		//	-----------------------------------------------
		super.initComponent();
		//	-----------------------------------------------	
		setReadOnly(true);
	}

	public void structComponent()
	{
		//	-----------------------------------------------
		super.structComponent();
		//	-----------------------------------------------
	}
	
	public void loadComponent()
	{
		//	-----------------------------------------------
		super.loadComponent();
		//	-----------------------------------------------

		XPDLPackage pkg = (XPDLPackage) parent_component;
		WorkflowPackage wpkg = (WorkflowPackage) pkg.getWorkflowComponent();
		List processes = wpkg.getWorkflowProcesses();

		//1. remove all missing WorkflowProcess
		List element_clones = (List) component_elements.clone();
		Iterator it = element_clones.iterator();
		while (it.hasNext())
		{
			XPDLCollectionComponent el = (XPDLCollectionComponent) it.next();
			if (el instanceof XPDLProcess)
			{
				if (wpkg.findWorkflowProcess(el.getUUID()) == null)
				{
					removeComponent(el);
				}
			}
		}

		//2. add all newly WorkflowProcess
		for (int i = 0; i < processes.size(); ++i)
		{
			WorkflowProcess process = (WorkflowProcess) processes.get(i);
			XPDLCollectionComponent el = findComponent(process.getUUID());
			if (el == null)
			{
				XPDLProcess xpProcess = new XPDLProcess(this, process);
				xpProcess.initComponent();
				xpProcess.structComponent();

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

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

	}

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