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

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.XPDLExtendedAttribute;
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.WorkflowComplexComponent;
import org.huihoo.workflow.WorkflowComponent;
import org.huihoo.workflow.xpdl.ExtendedAttribute;
/**
* Represents a WfMC DTD element that has the similar name.
* This class is a collection of class <b>ExtendedAttribute</b> instances.
*
* @see XPDLCollection
*/
public class XPDLExtendedAttributes extends XPDLCollection
{
	/** The collection of elements for hidden. */
	public transient ArrayList hidden_elements = new ArrayList();
	
	public final static String HIDDEN_LIST =
		"|ID_PROCESS_MAIN|ID_ACTIVITY_BEGIN|ID_ACTIVITY_END|ACTIVITY_TYPE|PERFORMER_TYPE|GOD4J.ACTIVITY_BOUNDS|GOD4J.TRANSITION_LABEL_LOCATION|GOD4J.TRANSITION_POINT_LIST|";

	/**
	* Creates a new instance of the class.
	*
	* @param parent Could be any class derived from XPDLComplexComp
	*                class (e.g. Activity, Transition, Package, ...).
	*/
	public XPDLExtendedAttributes(XPDLComplexComponent parent)
	{
		super(parent,"ExtendedAttributes");
		
	}
	/**
	* 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()
	{
		XPDLExtendedAttribute a = new XPDLExtendedAttribute(this, null);
		a.initComponent();
		a.structComponent();
		a.loadComponent();
		
		return a;
	}

	public XPDLExtendedAttribute getXPDLExtendedAttribute(String name)
	{
		return (XPDLExtendedAttribute) super.findComponent(name,false);
	}

	public int[] getInvisibleTableIndexes()
	{
		int[] itfo = new int[3];
		itfo[0] = 0;
		itfo[1] = 3;
		itfo[2] = 4;
		return itfo;
	}
	// First, the controlled panel must be created, and then the control panel
	public JavaPanel getJavaPanel()
	{
		controlledPanel = new JavaTablePanel(this, "", false, false, true);
		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();
		//	-----------------------------------------------
		
		WorkflowComponent workflow_component =getOwnerComponent().getWorkflowComponent();
		
		if(workflow_component == null)
		{
			//	-----------------------------------------------
			collectionLoadComponent();
			//	-----------------------------------------------
			return;
		}
		
		WorkflowComplexComponent xpdlElement=(WorkflowComplexComponent)workflow_component;
		Map params = xpdlElement.getExtendedAttributes();

		//1. remove all missing ExtendedAttribute
		List element_clones = (List) component_elements.clone();
		Iterator it = element_clones.iterator();
		while (it.hasNext())
		{
			XPDLCollectionComponent el = (XPDLCollectionComponent) it.next();
			if (el instanceof XPDLExtendedAttribute)
			{
				if (xpdlElement.findExtendedAttribute(el.getUUID()) == null)
				{
					removeComponent(el);
				}
			}
		}

		//2. add all newly ExtendedAttribute
		//System.err.println("------------check list----------------"+parent.getClass()+","+xpdlElement.getInfo());
		it = params.keySet().iterator();
		hidden_elements.clear();
		
		while (it.hasNext())
		{
			ExtendedAttribute param = (ExtendedAttribute) params.get((String) it.next());
			//System.err.println("----->"+"|"+param.getName()+"|");
			XPDLCollectionComponent el = findComponent(param.getName());
			if (el == null)
			{
				XPDLExtendedAttribute xpdl_attr = new XPDLExtendedAttribute(this, param);
				xpdl_attr.initComponent();
				xpdl_attr.structComponent();
				
				if (HIDDEN_LIST.indexOf("|" + param.getName() + "|") != -1)
				{
					xpdl_attr.loadComponent();
					hidden_elements.add(xpdl_attr);					
				}
				else
				{
					xpdl_attr.loadComponent();
				  addComponent(xpdl_attr);
				}
			}
		}
		
		//	-----------------------------------------------
		collectionLoadComponent();
		//	-----------------------------------------------
	}


	public void saveComponent()
	{
    if(isReadOnly())
    {
      return;
    }
    
		//	-----------------------------------------------
		super.saveComponent();
		//	-----------------------------------------------
		
		WorkflowComponent workflow_component = getOwnerComponent().getWorkflowComponent();
		if(workflow_component == null)
		{
			return ;
		}
		saveComponent(workflow_component);		
	}

	public void saveComponent(WorkflowComponent workflow_component)
	{
		//	-----------------------------------------------
		super.saveComponent(workflow_component);
		//	-----------------------------------------------
		
		WorkflowComplexComponent xpdlElement=(WorkflowComplexComponent)workflow_component;
		if(xpdlElement == null)
		{
			return;
		}
		xpdlElement.getExtendedAttributes().clear();
		
		Iterator it = component_elements.iterator();
		//System.err.println(this.getClass()+"---- saveComponent-----["+elements.size()+"]");
		while (it.hasNext())
		{
			XPDLCollectionComponent el = (XPDLCollectionComponent) it.next();
			if (el instanceof XPDLExtendedAttribute)
			{
				ExtendedAttribute attr = new ExtendedAttribute();
				XPDLExtendedAttribute xpdl_attr = (XPDLExtendedAttribute) el;
				xpdl_attr.saveComponent(attr);
				xpdlElement.addExtendedAttribute(attr);
			}
		}

		it = hidden_elements.iterator();
		while (it.hasNext())
		{
			XPDLCollectionComponent el = (XPDLCollectionComponent) it.next();
			if (el instanceof XPDLExtendedAttribute)
			{
				ExtendedAttribute attr = new ExtendedAttribute();
				XPDLExtendedAttribute xpdl_attr = (XPDLExtendedAttribute) el;
				xpdl_attr.saveComponent(attr);
				xpdl_attr.setWorkflowComponent(attr);
				xpdlElement.addExtendedAttribute(attr);
			}
		}
	}
	
	//==================================================================
	
	// must make new collection that consists of the same elements as
	// previous one
	public Object clone()
	{
		XPDLExtendedAttributes d = (XPDLExtendedAttributes) super.clone();
		d.hidden_elements = new ArrayList();
		Iterator it = this.hidden_elements.iterator();
		while (it.hasNext())
		{
			XPDLComponent el = (XPDLComponent) it.next();
			d.hidden_elements.add(el.clone());
		}
		return d;
	}
}
