//----------------------------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;
import org.huihoo.god4j.editor.XPDLAttribute;
import org.huihoo.god4j.editor.XPDLCollection;
import org.huihoo.god4j.editor.XPDLCollectionComponent;
import org.huihoo.god4j.editor.XPDLComponent;
import org.huihoo.god4j.editor.panels.JavaGroupPanel;
import org.huihoo.god4j.editor.panels.JavaPanel;
import org.huihoo.god4j.editor.panels.JavaTextPanel;
import org.huihoo.workflow.WorkflowComponent;
import org.huihoo.workflow.xpdl.ExtendedAttribute;
/**
* Represents a WfMC DTD element that has the similar name.
*
* @see XPDL
*/
public class XPDLExtendedAttribute extends XPDLCollectionComponent
{
	private XPDLAttribute attrName = null; // required
	private XPDLAttribute attrValue = null;

	/**
	* Creates a new instance of the class.
	*/
	public XPDLExtendedAttribute(XPDLCollection myCollection, ExtendedAttribute attrParam)
	{
		super(myCollection, "ExtendedAttribute");
		setWorkflowComponent(attrParam);
	}

	/**
	* Prepares the panel for showing extended attribute of
	* some XPDL element.
	*
	* @return XPDLPanel to be shown.
	*/
	public JavaPanel getJavaPanel()
	{
		return new JavaGroupPanel(this, new XPDLComponent[] { attrName, attrValue }, getLabel());
	}
	public String toString()
	{
		String toRet =
			sm.getString("NameKey")
				+ "="
				+ attrName.toString()
				+ ", "
				+ sm.getString("ValueKey")
				+ "="
				+ attrValue.toString();
		if (toRet == null)
		{
			return "";
		}
		else
		{
			return toRet;
		}
	}

	public boolean nameIsUniqueAndValid (JavaPanel p) 
	{
		JavaGroupPanel gp = (JavaGroupPanel) p;
		try
		{
			JavaTextPanel tp = (JavaTextPanel) gp.getPanel(0);
			String nameToCheck = tp.getText();
			
		}
		catch (Exception ex)
		{
		}
		return true;
	}
	
	public boolean isInputValid(JavaPanel groupPanel)
	{
		if(!nameIsUniqueAndValid(groupPanel))
		{
			return false;
		}
		return true;
	}

	/**
	* 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()
	{
		XPDLExtendedAttribute a = (XPDLExtendedAttribute) super.clone();
		a.attrName = (XPDLAttribute) this.attrName.clone();
		a.attrValue = (XPDLAttribute) this.attrValue.clone();

		a.structComponent();

		return a;
	}

	//==================================================================
	public void initComponent()
	{
		//	-----------------------------------------------
		super.initComponent();
		//	-----------------------------------------------	
		attrName = new XPDLAttribute(this, "Name"); // required
		attrName.initComponent();
		attrName.setRequired(true);

		attrValue = new XPDLAttribute(this, "Value");
		attrValue.initComponent();

	}

	public void structComponent()
	{
		//	-----------------------------------------------
		super.structComponent();
		//	-----------------------------------------------
		attrName.structComponent();
		attrValue.structComponent();

		complexChildren.add(attrName);
		complexChildren.add(attrValue);

		attributes.add(attrName);
		attributes.add(attrValue);
	}

	public void loadComponent()
	{
		//	-----------------------------------------------
		super.loadComponent();
		//	-----------------------------------------------			
		attrName.loadComponent();
		attrValue.loadComponent();

		if (getWorkflowComponent() != null && (getWorkflowComponent() instanceof ExtendedAttribute))
		{
			ExtendedAttribute exa = (ExtendedAttribute) getWorkflowComponent();
			attrName.setValue(exa.getName());
			attrValue.setValue(exa.getValue());
		}
	}

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

		//	-----------------------------------------------
		super.saveComponent();
		//	-----------------------------------------------
		attrName.saveComponent();
		attrValue.saveComponent();

		if (getWorkflowComponent() != null && (getWorkflowComponent() instanceof ExtendedAttribute))
		{
			ExtendedAttribute exa = (ExtendedAttribute) getWorkflowComponent();
			exa.setName(attrName.toString());
			exa.setValue(attrValue.toString());
		}
	}

	public void saveComponent(WorkflowComponent workflow_component)
	{
		//	-----------------------------------------------
		super.saveComponent(workflow_component);
		//	-----------------------------------------------
		attrName.saveComponent(workflow_component);
		attrValue.saveComponent(workflow_component);

		if (workflow_component != null && (workflow_component instanceof ExtendedAttribute))
		{
			ExtendedAttribute exta = (ExtendedAttribute) workflow_component;
			exta.setName(attrName.toString());
			exta.setValue(attrValue.toString());
		}
	}
	//==================================================================
}
