//----------------------------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;
import org.huihoo.god4j.editor.panels.JavaComboxPanel;
import org.huihoo.god4j.editor.panels.JavaPanel;
import org.huihoo.workflow.WorkflowComponent;
/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class XPDLChoice extends XPDLComponent
{
	/** The currently choosen element. */
	protected Object choosen;
	/** The possible choices. */
	protected Object[] choices;
	/**
	* Creates a new instance of element with the specified name, and
	* specifies the values of choices for the element. Assumes that
	* the first choice is choosen when object is created.
	*
	* @param name    The name of the 'choice' XML element
	*                that this class represents.
	* @param choices The possible choices for this element.
	*/
	public XPDLChoice(XPDLComplexComponent parent,String name,  Object[] choices)
	{
		this(parent, name, choices, 0);
	}
	/**
	* Creates a new instance of element with the specified name,
	* specified choices for the element, and with <b>chooseIndex</b>'th
	* value choosen.
	*
	* @param name         The name of the 'choice' XML element
	*                     that this class represents.
	* @param choices      The possible choices for this element.
	* @param choosenIndex The index of element to be choosen right after creation.
	*/
	public XPDLChoice(XPDLComplexComponent parent,String name,  Object[] choices, int choosenIndex)
	{
		super(parent,name);

		this.choices = choices;
		if(choices!=null && (choosenIndex>=0 & choosenIndex<choices.length))
		{
			this.choosen = choices[choosenIndex];
			super.setValue(choosen);
		}
	}
	/**
	* Overrides super-method to set this element and all it's choice elements
	* (if choices are instances of XPDLComp class) read only value to the
	* one specified.
	*
	* @param ro <tt>true</tt> if element should be <i>read only</i>,
	*           <tt>false</tt> otherwise.
	*/
	public void setReadOnly(boolean ro)
	{
		super.setReadOnly(ro);
		if (choices != null)
		{
			for (int i = 0; i < choices.length; i++)
			{
				if (choices[i] instanceof XPDLComponent)
				{
					((XPDLComponent) choices[i]).setReadOnly(ro);
				}
			}
		}
	}
	/**
	* Overrides the super-method to set the value field and the choosen field
	* to the specified value. The value is, in fact, one of possible choices.
	*
	* @param v the choice to be set.
	*/
	public void setValue(Object v)
	{
		super.setValue(v);
		choosen =  v;
	}
	
	/**
	* The possible choices could be instances of XPDLComp class, or
	* can be instances of String.
	*
	* @return the possible choices for this element.
	*/
	public Object[] getChoices()
	{
		return choices;
	}
	/**
	* Choosen element is one of the choices.
	*
	* @return the choosen element.
	* @see #getChoices
	*/
	public Object getChoosen()
	{
		return choosen;
	}
	/**
	* The default panel that represents this class visually
	* is instance of {@link XMLComboPanel} class. The panel
	* has the combo box that is filled with content of
	* <code>choices</code> field.
	*
	* @return the XMLComboPanel which owner is this class instance.
	*/
	public JavaPanel getJavaPanel()
	{
		return new JavaComboxPanel(this);
	}
	/**
	* Overrides super-method to apply cloning of <code>choices</code>
	* field content, if it's content are instances of <code>XPDLComp
	* </code> class.
	*
	* @return the new instance that has all similar attributes as the
	*         cloned one.
	*/
	public Object clone()
	{
		XPDLChoice d = (XPDLChoice) super.clone();
		d.choosen = null;
		d.choices = null;
		if (this.choices != null && this.choices.length > 0)
		{
			if (this.choices[0] instanceof String)
			{
				d.choices = this.choices;
				d.choosen = this.choosen;
			}
			else
			{
				d.choices = new Object[this.choices.length];
				for (int i = 0; i < this.choices.length; i++)
				{
					XPDLComponent ch = (XPDLComponent) this.choices[i];
					d.choices[i] = ch.clone();
					if (this.choosen == ch)
					{
						d.choosen = d.choices[i];
					}
				}
			}
		}
				
		return d;
	}

	public String toString()
	{
		if(choosen!=null)
		{
			return choosen.toString();
		}
		
		return super.toString();
	}
	
	//==================================================================

	public void initComponent()
	{
		//	-----------------------------------------------
		super.initComponent();
		//	-----------------------------------------------
		if (choices != null)
		{
			for (int i = 0; i < choices.length; i++)
			{
				if (choices[i] instanceof XPDLComponent)
				{
					((XPDLComponent) choices[i]).initComponent();
				}
			}
		}
	}

	public void structComponent()
	{
		//	-----------------------------------------------
		super.structComponent();
		//	-----------------------------------------------	
		if (choices != null)
		{
			for (int i = 0; i < choices.length; i++)
			{
				if (choices[i] instanceof XPDLComponent)
				{
					((XPDLComponent) choices[i]).structComponent();
				}
			}
		}
	}
	

	public void loadComponent()
	{
		//	-----------------------------------------------
		super.loadComponent();
		//	-----------------------------------------------
		if (choices != null)
		{
			for (int i = 0; i < choices.length; i++)
			{
				if (choices[i] instanceof XPDLComponent)
				{
					((XPDLComponent) choices[i]).loadComponent();
				}
			}
		}
	}


	public void saveComponent()
	{
    if(isReadOnly())
    {
      return;
    }
    
		//	-----------------------------------------------
		super.saveComponent();
		//	-----------------------------------------------
		if(choosen !=null && (choosen instanceof XPDLComponent ))
		{
			((XPDLComponent) choosen).saveComponent();
		}
	}

	public void saveComponent(WorkflowComponent workflow_component)
	{ 
		//	-----------------------------------------------
		super.saveComponent(workflow_component);
		//	-----------------------------------------------
		if(choosen !=null && (choosen instanceof XPDLComponent ))
		{
			((XPDLComponent) choosen).saveComponent(workflow_component);
		}
	}

	//==================================================================

}