//----------------------------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.XPDLComplexComponent;
import org.huihoo.god4j.editor.XPDLComponent;
import org.huihoo.god4j.editor.elements.atoms.XPDLScript;
import org.huihoo.god4j.editor.elements.set.XPDLApplications;
import org.huihoo.god4j.editor.elements.set.XPDLDataFields;
import org.huihoo.god4j.editor.elements.set.XPDLExtendedAttributes;
import org.huihoo.god4j.editor.elements.set.XPDLProcesses;
import org.huihoo.god4j.editor.panels.JavaGroupPanel;
import org.huihoo.god4j.editor.panels.JavaPanel;
import org.huihoo.god4j.editor.panels.JavaTabbedPanel;
import org.huihoo.workflow.WorkflowComponent;
import org.huihoo.workflow.WorkflowSimpleComponent;
import org.huihoo.workflow.xpdl.WorkflowPackage;
/**
* Represents the main element of WfMC schema.
*/
public class XPDLPackage extends XPDLComplexComponent
{
	private final static String XPDLNS = "http://www.wfmc.org/2002/XPDL1.0";
	private final static String XPDLNS_XPDL = "http://www.wfmc.org/2002/XPDL1.0";
	private final static String XPDLNS_XSI = "http://www.w3.org/2001/XPDLSchema-instance";
	private final static String XSI_SCHEMA_LOCATION =
		"http://www.wfmc.org/2002/XPDL1.0 http://wfmc.org/standards/docs/TC-1025_schema_10_xpdl.xsd";

	private XPDLPackageHeader refPackageHeader;
	private XPDLScript refScript; // min=0
	private XPDLApplications refApplications; // min=0
	private XPDLDataFields refDataFields; // min=0
	private XPDLProcesses refProcesses; // min=0
	private XPDLExtendedAttributes refExtendedAttributes;

	private XPDLAttribute attrId; // required
	private XPDLAttribute attrName; // required

	/**
	* Creates a new instance of the class.
	*
	* @param pe Instance of main class of this application.
	*/
	public XPDLPackage(WorkflowPackage workflowPackage)
	{
		super( null,"Package");
		setWorkflowComponent(workflowPackage);
	}

	/**
	* Overrides super-class method to retreive the value
	* of this class "Id" attribute.
	* This is used when displaying instance of this class
	* within dialog.
	*
	* @return The "Id" attribute value of this class.
	*/
	public String toString()
	{
		return attrId.toString();
	}
	
	public XPDLProcess getMainProcess()
	{
		WorkflowPackage pkg=(WorkflowPackage)getWorkflowComponent();
		return refProcesses.getXPDLProcess(pkg.getMainProcess().getUUID());
	}
	/**
	* Prepares the tabbed panel to show editable fields of Package.
	* Panel consists of nine tabs that logically comprises the
	* Package elements to be edited.
	* <p>NOTE: This method is never used, panels are retrieved one by one
	*          calling the method with the same name but with ordinal
	*          number of wanted panel as an argument.
	*
	* @return XPDLPanel to be shown.
	*/
	public JavaPanel getJavaPanel()
	{
		JavaPanel p1 =
			new JavaGroupPanel(
				this,
				new XPDLComponent[] { attrId, attrName, refScript, refExtendedAttributes },
				sm.getString("GeneralKey"));

		JavaPanel p2 = refPackageHeader.getJavaPanel();

		JavaPanel p3 = refApplications.getJavaPanel();

		JavaPanel p4 = refDataFields.getJavaPanel();

		JavaPanel p5 = refProcesses.getJavaPanel();

		return new JavaTabbedPanel(this, new JavaPanel[] { p1, p2, p3, p4, p5 });
	}
	//==================================================================
	public void initComponent()
	{
		//	-----------------------------------------------
		super.initComponent();
		//	-----------------------------------------------	

		refPackageHeader = new XPDLPackageHeader(this);
		refPackageHeader.initComponent();
		refPackageHeader.setRequired(true);

		refScript = new XPDLScript(this); // min=0
		refScript.initComponent();

		refApplications = new XPDLApplications(this); // min=0
		refApplications.initComponent();
		//refApplications.setReadOnly(true);

		refDataFields = new XPDLDataFields(this); // min=0
		refDataFields.initComponent();
		//refDataFields.setReadOnly(true);

		refProcesses = new XPDLProcesses(this); // min=0
		refProcesses.initComponent();
		refProcesses.setReadOnly(true);

		refExtendedAttributes = new XPDLExtendedAttributes(this);
		refExtendedAttributes.initComponent();

		attrId = new XPDLAttribute( this,"Id"); // required
		attrId.initComponent();				
		attrId.setReadOnly(false);
		
		attrName = new XPDLAttribute( this,"Name"); // required
		attrName.initComponent();
		attrName.setRequired(true);

	}

	public void structComponent()
	{
		//	-----------------------------------------------
		super.structComponent();
		//	-----------------------------------------------
		attrId.structComponent();
		attrName.structComponent();
		
		refPackageHeader.structComponent();
		refScript.structComponent();
		refApplications.structComponent();
		refDataFields.structComponent();
		refProcesses.structComponent();
		refExtendedAttributes.structComponent();
				
		complexChildren.add(attrName);
		complexChildren.add(refPackageHeader);
		complexChildren.add(refScript);
		complexChildren.add(refApplications);
		complexChildren.add(refDataFields);
		complexChildren.add(refProcesses);
		complexChildren.add(refExtendedAttributes);

		//------------------------------------------
		attributes.add(attrId);
		attributes.add(attrName);
		//------------------------------------------	
	}
	
	public void loadComponent()
	{
		//	-----------------------------------------------
		super.loadComponent();
		//	-----------------------------------------------
		attrId.loadComponent();
		attrName.loadComponent();
		
		refPackageHeader.loadComponent();
		refScript.loadComponent();
		refApplications.loadComponent();
		refDataFields.loadComponent();
		refProcesses.loadComponent();
		refExtendedAttributes.loadComponent();

		WorkflowComponent workflow_component = getWorkflowComponent();
		if (workflow_component != null && (workflow_component instanceof WorkflowSimpleComponent))
		{
			WorkflowSimpleComponent simple_component = (WorkflowSimpleComponent) workflow_component;
			attrId.setValue(simple_component.getUUID());
			attrName.setValue(simple_component.getName());
		}
	}

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

		refPackageHeader.saveComponent();
		refScript.saveComponent();
		refApplications.saveComponent();
		refDataFields.saveComponent();
		refProcesses.saveComponent();
		refExtendedAttributes.saveComponent();

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

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

		refPackageHeader.saveComponent(workflow_component);
		refScript.saveComponent(workflow_component);
		refApplications.saveComponent(workflow_component);
		refDataFields.saveComponent(workflow_component);
		refProcesses.saveComponent(workflow_component);
		refExtendedAttributes.saveComponent(workflow_component);

		if (workflow_component != null && (workflow_component instanceof WorkflowSimpleComponent))
		{
			WorkflowSimpleComponent simple_component = (WorkflowSimpleComponent) workflow_component;
			simple_component.setName(attrName.toString());
			simple_component.setUUID(attrId.toString());
		}
	}
	
	//==================================================================
}
