//----------------------------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.XPDLActivity;
import org.huihoo.god4j.editor.elements.XPDLActualParameter;
import org.huihoo.god4j.editor.elements.XPDLApplication;
import org.huihoo.god4j.editor.elements.XPDLProcess;
import org.huihoo.god4j.editor.elements.XPDLSubFlow;
import org.huihoo.god4j.editor.elements.XPDLTool;
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.WorkflowActivity;
import org.huihoo.workflow.xpdl.WorkflowProcess;
/**
* Represents a WfMC DTD element that has the similar name.
* This class is a collection of class {@link Activity}
* instances.
*/
public class XPDLActivities extends XPDLCollection
{
	/**
	* Creates a new instance of the class.
	* @param wpOrAs WorkflowProcess or ActivitySet that holds Activities class.
	*/
	public XPDLActivities(XPDLComplexComponent parent)
	{
		super(parent, "Activities");
	}

	/**
	* 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()
	{
		XPDLActivity a = new XPDLActivity(this, null);
		a.initComponent();
		a.structComponent();

		return a;
	}

	public int[] getInvisibleTableIndexes()
	{
		int[] itfo = new int[4];
		itfo[0] = 2;
		itfo[1] = 3;
		itfo[2] = 4;
		itfo[3] = 8;
		return itfo;
	}

	public XPDLActivity getXPDLActivity(String ID)
	{
		return (XPDLActivity) super.findComponent(ID);
	}

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

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

		//2. add all newly WorkflowActivity

		for (int i = 0; i < acts.size(); ++i)
		{
			WorkflowActivity act = (WorkflowActivity) acts.get(i);
			XPDLCollectionComponent el = findComponent(act.getUUID());
			if (el == null)
			{
				XPDLActivity xpAct = new XPDLActivity(this, act);
				xpAct.initComponent();
				xpAct.structComponent();
				addComponent(xpAct);
			}
		}

		//	-----------------------------------------------
		collectionLoadComponent();
		//	-----------------------------------------------
	}

	public void saveComponent()
	{
		if (isReadOnly())
		{
			return;
		}

		//	-----------------------------------------------
		super.saveComponent();
		//	-----------------------------------------------
	}

	//==================================================================
	/**
		* Returns <tt>true</tt> if specified workflow process isn't referenced from
		* any activity of subflow type.
		*/
	protected boolean canRemoveProcess(XPDLProcess toRemove)
	{
		Iterator it = component_elements.iterator();
		while (it.hasNext())
		{
			XPDLActivity a = (XPDLActivity) it.next();
			if (a.getType() == XPDLActivity.IMPLEMENTATION_SUBFLOW)
			{
				XPDLSubFlow xpSubFlow = (XPDLSubFlow) a.getImplementation();
				if (xpSubFlow.getChoosenProcess() == toRemove)
				{
					return false;
				}
			}
		}

		return true;
	}

	/**
	* Returns <tt>true</tt> if specified application isn't used as a tool
	* of activity which type is Tool.
	*/
	protected boolean canRemoveApplication(XPDLApplication toRemove)
	{
		Iterator it = component_elements.iterator();
		while (it.hasNext())
		{
			XPDLActivity a = (XPDLActivity) it.next();
			if (a.getType() == XPDLActivity.IMPLEMENTATION_TOOLS)
			{
				XPDLTools xpTools = (XPDLTools) a.getImplementation();
				Iterator itTools = xpTools.toCollection().iterator();
				while (itTools.hasNext())
				{
					XPDLTool t = (XPDLTool) itTools.next();
					if (t.getChoosenApplication() == toRemove)
					{
						return false;
					}
				}
			}
		}
		return true;
	}
	/**
	* Returns <tt>true</tt> if specified data field isn't used as
	* actual parameter of activity which type is Tool or Subflow.
	*/
	protected boolean canRemoveDataFieldOrFormalParameter(XPDLCollectionComponent toRemove)
	{
		Iterator it = component_elements.iterator();
		while (it.hasNext())
		{
			XPDLActivity a = (XPDLActivity) it.next();
			if (a.getType() == XPDLActivity.IMPLEMENTATION_TOOLS)
			{
				XPDLTools xpTools = (XPDLTools) a.getImplementation();
				Iterator itTools = xpTools.toCollection().iterator();
				while (itTools.hasNext())
				{
					XPDLTool t = (XPDLTool) itTools.next();
					XPDLActualParameters aps = (XPDLActualParameters) t.get("ActualParameters");
					Iterator itAPs = aps.toCollection().iterator();
					while (itAPs.hasNext())
					{
						XPDLActualParameter ap = (XPDLActualParameter) itAPs.next();
						if (ap.getValue() == toRemove)
						{
							return false;
						}
					}
				}
			}
		}
		return true;
	}
	//==================================================================
}
