//----------------------------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.XPDLComplexChoice;
import org.huihoo.god4j.editor.XPDLComponent;
import org.huihoo.god4j.editor.elements.atoms.XPDLDescription;
import org.huihoo.god4j.editor.elements.set.XPDLExtendedAttributes;
import org.huihoo.god4j.editor.elements.set.XPDLFormalParameters;
import org.huihoo.god4j.editor.panels.JavaGroupPanel;
import org.huihoo.god4j.editor.panels.JavaPanel;
import org.huihoo.god4j.editor.panels.JavaTabbedPanel;
import org.huihoo.god4j.util.UUID;
import org.huihoo.workflow.WorkflowComponent;
import org.huihoo.workflow.impl.xpdl.WorkflowApplicationImpl;
import org.huihoo.workflow.xpdl.WorkflowApplication;
/**
* Represents a WfMC DTD element that has the similar name.
*
* @see XPDL
*/
public class XPDLApplication extends XPDLCollectionComponent
{
	private XPDLDescription refDescription = null; // min=0
	private XPDLComplexChoice refChoice = null;
	private XPDLExtendedAttributes refExtendedAttributes = null; // min=0
	private XPDLAttribute attrName = null;

	private XPDLAttribute attrQualifiedClassName = null; //only for willow

	//-------------------------------------------------------------------------

	/** Enables canceling of changes to the extended attributes collection. */
	private XPDLExtendedAttributes clonedEAs;

	/**
	* Creates a new instance of the class.
	*
	* @param aps The reference to collection of applications where
	*            this instance will be put into.
	* @param p   The package where applications are defined.
	*/
	public XPDLApplication(XPDLCollection myCollection, WorkflowApplication app)
	{
		super(myCollection, "Application");
		setWorkflowComponent(app);
	}

	public JavaPanel getJavaPanel()
	{
		JavaPanel p1 =
			new JavaGroupPanel(
				this,
				new XPDLComponent[] { attrId, attrName, refDescription, refChoice },
				sm.getString("GeneralKey"));

		clonedEAs = (XPDLExtendedAttributes) refExtendedAttributes.clone();
		JavaPanel p2 = clonedEAs.getJavaPanel();

		JavaPanel p3 =
			new JavaGroupPanel(this, new XPDLComponent[] { attrQualifiedClassName }, sm.getString("AdvancedKey"));
		return new JavaTabbedPanel(this, new JavaPanel[] { p1, p2, p3 });
	}

	public String toString()
	{
		String disp = attrName.toString();
		if (disp.trim().length() == 0)
		{
			disp = attrId.toString();
		}
		return disp;
	}

	public boolean isInputValid(JavaPanel groupPanel)
	{
		if (clonedEAs != null)
		{
			complexChildren.remove(refExtendedAttributes);

			refExtendedAttributes = clonedEAs;
			complexChildren.add(4, refExtendedAttributes);
		}
		return true;
	}
	//==================================================================
	public void initComponent()
	{
		//	-----------------------------------------------
		super.initComponent();
		//	-----------------------------------------------
		refExtendedAttributes = new XPDLExtendedAttributes(this); // min=0
		refExtendedAttributes.initComponent();

		attrName = new XPDLAttribute(this, "Name");
		attrName.initComponent();
		attrName.setRequired(true);
		
		attrQualifiedClassName = new XPDLAttribute(this, "QualifiedClassName");
		attrQualifiedClassName.initComponent();
		attrQualifiedClassName.setRequired(true);
		
		refDescription = new XPDLDescription(this); // min=0
		refDescription.initComponent();

		XPDLFormalParameters refChoice_fps = new XPDLFormalParameters(this);
		refChoice = new XPDLComplexChoice(this, "Choice", new XPDLComponent[] { refChoice_fps }, 0, true)
		{
			public void setValue(Object v)
			{
				super.setValue(v);
			}
		};
		refChoice.initComponent();
	}

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

		refDescription.structComponent();
		refChoice.structComponent();
		refExtendedAttributes.structComponent();

		complexChildren.add(attrName);
		complexChildren.add(refDescription);
		complexChildren.add(refChoice);
		complexChildren.add(refExtendedAttributes);
		complexChildren.add(attrQualifiedClassName);
		
		attributes.add(attrName);
		attributes.add(attrQualifiedClassName);
	}

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

		refDescription.loadComponent();
		refChoice.loadComponent();
		refExtendedAttributes.loadComponent();

		WorkflowComponent workflow_component = getWorkflowComponent();
		if (workflow_component != null && (workflow_component instanceof WorkflowApplication))
		{
			WorkflowApplicationImpl simple_component = (WorkflowApplicationImpl) workflow_component;
			attrId.setValue(simple_component.getUUID());
			attrName.setValue(simple_component.getName());
			attrQualifiedClassName.setValue(simple_component.getQualifiedClassName());
		}
		else
		{
			attrId.setValue(UUID.generate());
		}
	}

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

		refDescription.saveComponent();
		refChoice.saveComponent();
		refExtendedAttributes.saveComponent();

		WorkflowComponent workflow_component = getWorkflowComponent();
		if (workflow_component != null && (workflow_component instanceof WorkflowApplication))
		{
			WorkflowApplicationImpl simple_component = (WorkflowApplicationImpl) workflow_component;
			simple_component.setUUID(attrId.toString());
			simple_component.setName(attrName.toString());
			simple_component.setQualifiedClassName(attrQualifiedClassName.toString());
		}

		//System.err.println("XPDLAPP []: "+this.attrId.getValue());
	}

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

		refDescription.saveComponent(workflow_component);
		refChoice.saveComponent(workflow_component);
		refExtendedAttributes.saveComponent(workflow_component);

		if (workflow_component != null && (workflow_component instanceof WorkflowApplication))
		{
			WorkflowApplicationImpl simple_component = (WorkflowApplicationImpl) workflow_component;
			simple_component.setUUID(attrId.toString());
			simple_component.setName(attrName.toString());
			simple_component.setQualifiedClassName(attrQualifiedClassName.toString());
		}

		//System.err.println("XPDLAPP [1-c]: "+this.attrId.getValue());
	}
	//==================================================================
}
