// ShapeActivityGraph.cpp: implementation of the ShapeActivityGraph class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AG_MDA.h"
#include "ShapeActivityGraph.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

ShapeActivityGraph::ShapeActivityGraph()
{
	m_pActivityGraph = NULL;
	m_pShapeClass = NULL;
	m_pMarkElement = new MarkActivityGraph;
}

ShapeActivityGraph::~ShapeActivityGraph()
{
	//删除泳道
	for(int i = 0 ; i <= m_pShapeSwimlanes.GetUpperBound() ; i ++ )
	{
		ShapeSwimlane* p = m_pShapeSwimlanes.GetAt( i );
		delete p;
	}
	//删除活动
	for( i = 0 ; i <= m_pShapeActivities.GetUpperBound() ; i ++ )
	{
		ShapeActivity* p = m_pShapeActivities.GetAt( i );
		delete p;
	}
	//删除转换边
	for( i = 0 ; i <= m_pShapeTransEdges.GetUpperBound() ; i ++ )
	{
		ShapeTransEdge* p = m_pShapeTransEdges.GetAt( i );
		delete p;
	}
	//删除控制流
	for( i = 0 ; i <= m_pShapeControlFlows.GetUpperBound() ; i ++ )
	{
		ShapeControlFlow *p = m_pShapeControlFlows.GetAt( i );
		delete p;
	}
	delete m_pActivityGraph;
}

void ShapeActivityGraph::Draw(CDC *pDC)
{
	for(int i = 0 ; i <= m_pShapeSwimlanes.GetUpperBound() ; i ++ )
	{
		ShapeSwimlane* p = m_pShapeSwimlanes.GetAt( i );
		p->DrawDefault( pDC );
	}
	for( i = 0 ; i <= m_pShapeActivities.GetUpperBound() ; i ++ )
	{
		ShapeActivity* p = m_pShapeActivities.GetAt( i );
		p->DrawDefault( pDC );
	}
	for( i = 0 ; i <= m_pShapeTransEdges.GetUpperBound() ; i ++ )
	{
		ShapeTransEdge* p = m_pShapeTransEdges.GetAt( i );
		p->DrawDefault( pDC );
	}
}

ShapeType ShapeActivityGraph::GetType()
{
	return SHAPE_ACTIVITYDIAGRAM;
}

ShapeNamespace* ShapeActivityGraph::GetShapeNamespace()
{
	return m_pShapeClass->GetShapeNamespace();
}

ShapeActivity* ShapeActivityGraph::FindShapeActivity(UMLActivity *p)
{
	if( m_pShapeSwimlanes.GetUpperBound() >= 0 )
	{
		for( int i = 0 ; i <= m_pShapeSwimlanes.GetUpperBound() ; i ++ )
		{
			ShapeSwimlane * pSS = m_pShapeSwimlanes.GetAt( i );
			for( int j = 0 ; j <= pSS->m_pActivities.GetUpperBound() ; j ++ )
			{
				ShapeActivity *pSA = pSS->m_pActivities.GetAt( j );
				if( pSA->m_pUMLActivity == p ) return pSA;
			}
		}
	}
	else
	{
		for( int i = 0 ; i <= m_pShapeActivities.GetUpperBound() ; i ++ )
		{
			ShapeActivity *pSA = m_pShapeActivities.GetAt( i );
			if( pSA->m_pUMLActivity == p ) return pSA;
		}
	}
	return NULL;
}

void ShapeActivityGraph::Save(CFile &file, int &tabnum)
{
	CString txt;
	WriteTab( file , tabnum );
	WriteTag( file , "<ACTIVITYGRAPH>" );
	WriteEnter( file );
	tabnum ++;
	//name
	txt = "<NAME>";
	txt += m_pActivityGraph->name;
	txt += "</NAME>";
	WriteTab( file , tabnum );
	WriteContent( file , txt );
	WriteEnter( file );
	//id
	txt.Format("<ID>%d</ID>",m_ID );
	WriteTab( file , tabnum );
	WriteContent( file , txt );
	WriteEnter( file );
	//泳道
	//活动
	if( m_pShapeSwimlanes.GetUpperBound() >= 0 )
	{
		//泳道
		for( int i = 0 ; i <= m_pShapeSwimlanes.GetUpperBound() ; i ++ )
		{
			ShapeSwimlane *pSS = m_pShapeSwimlanes.GetAt( i );
			pSS->Save( file ,tabnum );
		}
	}
	else
	{
		//活动
		for( int i = 0 ; i <= m_pShapeActivities.GetUpperBound() ; i ++ )
		{
			ShapeActivity *pSA = m_pShapeActivities.GetAt( i );
			pSA->Save( file ,tabnum );
		}
	}
	//转换边
	for( int i = 0 ; i <= m_pShapeTransEdges.GetUpperBound(); i ++ )
	{
		ShapeTransEdge *e = m_pShapeTransEdges.GetAt( i );
		e->Save( file , tabnum );
	}
	//mark
	MarkActivityGraph* pMAG = (MarkActivityGraph*)m_pMarkElement;
	WriteTab( file , tabnum );
	WriteTag( file , "<MARK>");
	WriteEnter( file );
	tabnum ++;
	//mark operation
	WriteOperation( file , &(pMAG->m_UMLOperation) , tabnum );
	//
	tabnum --;
	WriteTab( file , tabnum );
	WriteTag( file , "</MARK>");
	WriteEnter( file );
	//
	tabnum --;
	WriteTab( file , tabnum );
	WriteTag( file , "</ACTIVITYGRAPH>" );
	WriteEnter( file );
}
