//----------------------------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.JavaLocationPanel;
import org.huihoo.god4j.editor.panels.JavaPanel;
import org.huihoo.god4j.editor.panels.JavaRadioPanel;
import org.huihoo.god4j.editor.panels.JavaTextPanel;
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 XPDLAttribute extends XPDLChoice
{
	//-----------------------------------------------------------
	public final static int PANEL_LOCATION = 1;
	public final static int PANEL_TEXT = 2;
	//-----------------------------------------------------------
	/**
	* The type of textual XPDLPanel
	*/
	protected int panelType = PANEL_TEXT;

	/** Language depended names of choices-read from property file. */
	protected String[] choiceNames = null;
	protected String choosenName = null;

	/** Indicates if attribut uses XPDLComboPanel or XPDLRadioPanel. */
	protected boolean isComboPanel = true;
	/**
	Indicates if element's label and edit box will be shown
	one above other, or one beside other.
	*/
	protected boolean isVertical = false;

	public XPDLAttribute(XPDLComplexComponent parent,String name)
	{
		super(parent,name, null);
	}

	public XPDLAttribute(XPDLComplexComponent parent, String name, int tpType)
	{
		super(parent, name,null);
		this.panelType = tpType;
	}

	public XPDLAttribute(XPDLComplexComponent parent,String name,  String[] choices, int choosenIndex)
	{
		this(parent, name, choices, choosenIndex, true, false);
	}

	public XPDLAttribute(
		XPDLComplexComponent parent,
		String name,
		String[] choices,
		int choosenIndex,
		boolean isComboPanel,
		boolean isVertical)
	{
		super( parent,name, (Object[]) choices, choosenIndex);
		this.choiceNames = new String[choices.length];
		String nm;
		for (int i = 0; i < choices.length; i++)
		{
			nm = sm.getString(choices[i] + "Key");
			if (nm != null)
			{
				this.choiceNames[i] = nm;
			}
			else
			{
				this.choiceNames[i] = choices[i];
			}
		}

		choosen = choices[choosenIndex];
		choosenName = choiceNames[choosenIndex];

		this.isComboPanel = isComboPanel;
		this.isVertical = isVertical;
	}

	public String[] getChoiceNames()
	{
		return choiceNames;
	}

	public String getChoosenName()
	{
		return choosenName;
	}

	public boolean isEmpty()
	{
		if (choices == null)
		{
			return super.isEmpty();
		}
		else
		{
			if (choosen == null || ((choosen instanceof String) && ((String) choosen).length() == 0))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
	/**
	* Overrides super-class method.
	*
	* @return The same as the super-class if this is not an
	*         attribute with restricted set of values,
	*         string representation of choosen value otherwise.
	*/
	public String toString()
	{
		if (choosen != null)
		{
			return choosen.toString();
		}
		
		if(value != null)
		{
			return value.toString();
		}
		return "";
	}

	public JavaPanel getJavaPanel()
	{
		if (choices == null)
		{
			if (panelType == PANEL_LOCATION)
			{
				return new JavaLocationPanel(this);
			}
			else
			{
				return new JavaTextPanel(this, JavaPanel.BOX_LAYOUT, isVertical, false);
			}
		}
		else
		{
			if (isComboPanel)
			{
				return new JavaComboxPanel(this, JavaPanel.BOX_LAYOUT, isVertical);
			}
			else
			{
				return new JavaRadioPanel(this, getLabel(), JavaPanel.BOX_LAYOUT, isVertical);
			}
		}
	}

	public void setValue(Object v)
	{
		if (choices == null)
		{
			super.setValue(v);
		}
		else
		{
			if (v != null)
			{
				for (int i = 0; i < choices.length; i++)
				{
					if (choices[i].equals(v))
					{
						value = choices[i];
						choosen = choices[i];
						choosenName = choiceNames[i];
						break;
					}
				}
			}
		}
	}
	/**
	* 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()
	{
		XPDLAttribute d = (XPDLAttribute) super.clone();
		d.panelType = this.panelType;
		d.choiceNames = this.choiceNames;
		d.choosenName = this.choosenName;
		d.isComboPanel = this.isComboPanel;
		return d;
	}

	//==================================================================
	public void initComponent()
	{
		//	-----------------------------------------------
		super.initComponent();
		//	-----------------------------------------------
	}

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

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

	public void saveComponent()
	{
    if(isReadOnly())
    {
      return;
    }	
    
		//	-----------------------------------------------
		super.saveComponent();
		//	-----------------------------------------------
	}

	public void saveComponent(WorkflowComponent workflow_component)
	{	
		//	-----------------------------------------------
		super.saveComponent(workflow_component);
		//	-----------------------------------------------
	}
	//==================================================================
}