//----------------------------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.jgraph;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.huihoo.workflow.WorkflowComplexComponent;
import org.huihoo.workflow.xpdl.WorkflowActivity;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultPort;
import org.jgraph.graph.Port;
/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public abstract class  JGraphActivity extends DefaultGraphCell implements XPDLLinkable,XPDLJGraphCell
{
	protected DefaultPort port;
	public JGraphActivity(WorkflowActivity activity)
	{
		super(activity);
		port = new DefaultPort();
		add(port);
	}
	public Port getDefaultPort()
	{
		return port;
	}
	public boolean acceptsSource()
	{
		return true;
	}
	public boolean acceptsTarget()
	{
		return true;
	}

	public List getIncomingTransitions()
	{
		List inputTransitions = new ArrayList();
		for (Iterator i = getDefaultPort().edges(); i.hasNext();)
		{
			JGraphTransition l = (JGraphTransition) i.next();
			Object target = ((DefaultPort) (l.getTarget())).getParent();
			if (this == target)
			{
				inputTransitions.add(l);
			}
		}
		return inputTransitions;
	}

	public List getOutgoingTransitions()
	{
		List outputTransitions = new ArrayList();
		for (Iterator i = getDefaultPort().edges(); i.hasNext();)
		{
			JGraphTransition l = (JGraphTransition) i.next();
			Object source = ((DefaultPort) (l.getSource())).getParent();
			if (this == source)
			{
				outputTransitions.add(l);
			}
		}
		return outputTransitions;
	}
	
	public WorkflowComplexComponent getWorkflowComponent()
	{
		return (WorkflowComplexComponent)getUserObject();
	}
	
	public void                     setWorkflowComponent(WorkflowComplexComponent comp)
	{
		setUserObject(comp);
	}
	
	public String toString()
	{
		if (userObject == null)
		{
			return null;
		}
		else
		{
			return ((WorkflowComplexComponent) userObject).getName();
		}
	}
}