//----------------------------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.XPDLImplementation;
import org.huihoo.god4j.editor.elements.XPDLTool;
import org.huihoo.workflow.impl.xpdl.WorkflowActivityImpl;
import org.huihoo.workflow.xpdl.WorkflowActivity;
import org.huihoo.workflow.xpdl.activity.Implementation;
import org.huihoo.workflow.xpdl.activity.ToolImplementation;
import org.huihoo.workflow.xpdl.activity.ToolSet;
/**
* This is not a real WfMC DTD element, but it is introduced to help.
* This class is a collection of class <b>Tool</b> instances.
*/
public class XPDLTools extends XPDLCollection
{
	/**
	* Creates a new instance of the class.
	*
	* @param act The activity which owns this tools.
	*/
	public XPDLTools(XPDLComplexComponent parent)
	{
		super(parent);
	}
	// 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()
	{
		XPDLTool a = new XPDLTool(this, null);
		a.initComponent();
		a.structComponent();
		//a.loadComponent();
		
		return a;
	}

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

	/**
	* 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()
	{
		XPDLTools t = (XPDLTools) super.clone();
		return t;
	}
	
	public XPDLTool getXPDLTool(String ID)
	{
		return (XPDLTool) super.findComponent(ID);
	}
	//==================================================================
	
	public void initComponent()
	{
		//	-----------------------------------------------
		super.initComponent();
		//	-----------------------------------------------	
	}

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

	public void loadComponent()
	{
		//	-----------------------------------------------
		super.loadComponent();
		//	-----------------------------------------------
		XPDLImplementation xpImpl = (XPDLImplementation) getParentComponent();
		XPDLActivity xpAct = (XPDLActivity) xpImpl.getParentComponent();
		WorkflowActivity wfAct = (WorkflowActivity) xpAct.getWorkflowComponent();
		//System.err.println("XPDLTOOLS [wfAct]--1: "+wfAct);
		if(wfAct ==null)
		{
			//	-----------------------------------------------
			collectionLoadComponent();
			//	-----------------------------------------------
			return;
		}
		
		Implementation wfImpl = wfAct.getImplementation();
		//System.err.println("XPDLTOOLS [wfImpl]--2: "+wfImpl);
		
		if (wfImpl != null)
		{
			if (wfImpl instanceof ToolImplementation)
			{
				this.component_elements.clear();
				XPDLTool tool = new XPDLTool(this, (ToolImplementation) wfImpl);
				tool.initComponent();
				tool.structComponent();
				tool.loadComponent();
				addComponent(tool);
			}
			else if (wfImpl instanceof ToolSet)
			{
				this.component_elements.clear();
				ToolSet toolSet = (ToolSet) wfImpl;
				List tools = toolSet.getToolKits();
				for (int i = 0; i < tools.size(); ++i)
				{
					XPDLTool tool = new XPDLTool(this, (ToolImplementation) tools.get(i));
					tool.initComponent();
					tool.structComponent();

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

	/**
	* Supposed to be overriden by derived classes to implement
	* element specific writting to an XPDL Model.
	*
	*/
	public void saveComponent()
	{
    if(isReadOnly())
    {
      return;
    }
    
		//	-----------------------------------------------
		super.saveComponent();
		//	-----------------------------------------------
		XPDLImplementation xpImpl = (XPDLImplementation)getOwnerComponent();
		XPDLActivity xpAct = (XPDLActivity) xpImpl.getParentComponent();
		WorkflowActivity wfAct = (WorkflowActivity) xpAct.getWorkflowComponent();
		wfAct.setImplementation(null);
		
		Iterator it = component_elements.iterator();
		while (it.hasNext())
		{			
			XPDLCollectionComponent el = (XPDLCollectionComponent) it.next();
			if ((el instanceof XPDLTool))
			{
				XPDLTool xpTool = (XPDLTool) el;
				ToolImplementation wfImpl = new ToolImplementation();
				xpTool.saveComponent(wfImpl);
				xpTool.setWorkflowComponent(wfImpl);
				((WorkflowActivityImpl)wfAct).addToolImplementation(wfImpl);
			}
		}
		
		//System.err.println("XPDLTOOLS : "+wfAct.getImplementation());
	}

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