// ClassInfoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AG_MDA.h"
#include "ClassInfoDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// ClassInfoDlg dialog


ClassInfoDlg::ClassInfoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(ClassInfoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(ClassInfoDlg)
	m_Name = _T("");
	m_CreateMainFunc = FALSE;
	m_MainFuncExpression = _T("");
	//}}AFX_DATA_INIT
	m_pClass = NULL;
}


void ClassInfoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(ClassInfoDlg)
	DDX_Control(pDX, IDC_LIST_OPERATION, m_OperationListCtrl);
	DDX_Control(pDX, IDC_LIST_ATTRIBUTE, m_AttributeListCtrl);
	DDX_Text(pDX, IDC_EDIT_NAME, m_Name);
	DDX_Check(pDX, IDC_CHECK_MAIN_FUNC, m_CreateMainFunc);
	DDX_Text(pDX, IDC_EDIT_MAIN_OPE, m_MainFuncExpression);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(ClassInfoDlg, CDialog)
	//{{AFX_MSG_MAP(ClassInfoDlg)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_ATTRIBUTE, OnDblclkListAttribute)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_OPERATION, OnDblclkListOperation)
	ON_BN_CLICKED(IDC_BUTTON_NEW_ATTRIBUTE, OnButtonNewAttribute)
	ON_BN_CLICKED(IDC_BUTTON_NEW_OPERATION, OnButtonNewOperation)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// ClassInfoDlg message handlers

void ClassInfoDlg::OnDblclkListAttribute(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	AttributeInfoDlg dlg;
	int iCurSel = m_AttributeListCtrl.GetSelectionMark();
	if( iCurSel < 0)return;

	dlg.m_pClassList = m_pClassList;
	dlg.m_Attribute = m_Attributes.GetAt( iCurSel );
	if( dlg.DoModal() == IDOK )
	{
		int num = iCurSel;
		m_AttributeListCtrl.SetItemText( num , 0 , dlg.m_Attribute.name );
		m_AttributeListCtrl.SetItemText( num , 1 , dlg.m_Attribute.m_pType->UMLGeneralizableElement::name );
		switch( dlg.m_Attribute.m_Visibility )
		{
		case VISIBILITY_PUBLIC:
			{
				m_AttributeListCtrl.SetItemText( num , 2 , "PUBLIC" );
			}
			break;
		case VISIBILITY_PRIVATE:
			{
				m_AttributeListCtrl.SetItemText( num , 2 , "PRIVATE" );
			}
			break;
		case VISIBILITY_PROTECTED:
			{
				m_AttributeListCtrl.SetItemText( num , 2 , "PROTECTED" );
			}
			break;
		default:
			m_AttributeListCtrl.SetItemText( num , 2 , "PACKAGE" );
		};
		m_AttributeListCtrl.SetItemText( num , 3 , dlg.m_Attribute.m_Multiplicity );
		m_AttributeListCtrl.SetItemText( num , 4 , dlg.m_Attribute.m_InitialValue  );
		m_Attributes.SetAt( num , dlg.m_Attribute );
	}	
	*pResult = 0;
}

void ClassInfoDlg::OnDblclkListOperation(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	int iSel = m_OperationListCtrl.GetSelectionMark();
	if( iSel < 0 )return;
	OperationInfoDlg dlg;
	dlg.m_pClassList = m_pClassList;
	dlg.m_Operation = m_Operations.GetAt( iSel );
	if( dlg.DoModal() == IDOK )
	{
		int iNum = iSel;
		m_OperationListCtrl.SetItemText( iNum , 0 , dlg.m_Operation.name );
		switch( dlg.m_Operation.m_Visibility )
		{
		case VISIBILITY_PUBLIC:
			{
				m_OperationListCtrl.SetItemText( iNum , 1 , "PUBLIC" );
			}
			break;
		case VISIBILITY_PRIVATE:
			{
				m_OperationListCtrl.SetItemText( iNum , 1 , "PRIVATE" );
			}
			break;
		case VISIBILITY_PROTECTED:
			{
				m_OperationListCtrl.SetItemText( iNum , 1 , "PROTECTED" );
			}
			break;
		default:
			m_OperationListCtrl.SetItemText( iNum , 1 , "PACKAGE" );
		}
		if( dlg.m_Operation.m_ReturnType.m_pType )
			m_OperationListCtrl.SetItemText( iNum , 2 , dlg.m_Operation.m_ReturnType.m_pType->UMLGeneralizableElement::name );
	}
	*pResult = 0;
}

BOOL ClassInfoDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_AttributeListCtrl.SetExtendedStyle( 33 );
	m_OperationListCtrl.SetExtendedStyle( 33 );
	m_AttributeListCtrl.InsertColumn( 0 , "Name" , LVCFMT_LEFT , 100 );
	m_AttributeListCtrl.InsertColumn( 1 , "Type" , LVCFMT_LEFT , 100 );
	m_AttributeListCtrl.InsertColumn( 2 , "Visibility" , LVCFMT_LEFT , 100 );
	m_AttributeListCtrl.InsertColumn( 3 , "Multiplicity" , LVCFMT_LEFT , 100 );
	m_AttributeListCtrl.InsertColumn( 4 , "InitialValue" , LVCFMT_LEFT , 100 );

	m_OperationListCtrl.InsertColumn( 0 , "Name" , LVCFMT_LEFT , 100 );
	m_OperationListCtrl.InsertColumn( 1 , "Visibility" , LVCFMT_LEFT , 100 );
	m_OperationListCtrl.InsertColumn( 2 , "ReturnValue" , LVCFMT_LEFT , 100 );

	if( m_pClass )
	{
		m_Name = m_pClass->UMLGeneralizableElement::name ;
		m_Operations = m_pClass->m_Operations;
		m_Attributes = m_pClass->m_Attributes;
		for( int i = 0 ; i < m_pClass->m_Attributes.GetSize() ; i ++ )
		{
			UMLAttribute attr = m_pClass->m_Attributes.GetAt( i );
			m_AttributeListCtrl.InsertItem( i , attr.name );
			m_AttributeListCtrl.SetItemText( i , 1 , attr.m_pType->UMLGeneralizableElement::name );
			switch( attr.m_Visibility )
			{
			case VISIBILITY_PUBLIC:
				{
					m_AttributeListCtrl.SetItemText( i , 2 , "PUBLIC" );
				}
				break;
			case VISIBILITY_PRIVATE:
				{
					m_AttributeListCtrl.SetItemText( i , 2 , "PRIVATE" );
				}
				break;
			case VISIBILITY_PROTECTED:
				{
					m_AttributeListCtrl.SetItemText( i , 2 , "PROTECTED" );
				}
				break;
			default:
					m_AttributeListCtrl.SetItemText( i , 2 , "PACKAGE" );
			}
			m_AttributeListCtrl.SetItemText( i , 3 , attr.m_Multiplicity );
			m_AttributeListCtrl.SetItemText( i , 4 , attr.m_InitialValue );
		}
		for( i = 0 ; i < m_Operations.GetSize() ; i ++ )
		{
			UMLOperation ope = m_Operations.GetAt( i );
			m_OperationListCtrl.InsertItem( i , ope.name );
			switch( ope.m_Visibility )
			{
			case VISIBILITY_PUBLIC:
				{
					m_OperationListCtrl.SetItemText( i , 1 , "PUBLIC" );
				}
				break;
			case VISIBILITY_PRIVATE:
				{
					m_OperationListCtrl.SetItemText( i , 1 , "PRIVATE" );
				}
				break;
			case VISIBILITY_PROTECTED:
				{
					m_OperationListCtrl.SetItemText( i , 1 , "PROTECTED" );
				}
				break;
			default:
					m_OperationListCtrl.SetItemText( i , 1 , "PACKAGE" );
			}
			if( ope.m_ReturnType.m_pType )
				m_OperationListCtrl.SetItemText( i , 2 , ope.m_ReturnType.m_pType->UMLGeneralizableElement::name );
		}
		UpdateData( FALSE );
	}
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void ClassInfoDlg::OnButtonNewAttribute() 
{
	// TODO: Add your control notification handler code here
	AttributeInfoDlg dlg;
	dlg.m_pClassList = m_pClassList;
	if( dlg.DoModal() == IDOK )
	{
		int num = m_AttributeListCtrl.GetItemCount();
		m_Attributes.Add( dlg.m_Attribute );
		m_AttributeListCtrl.InsertItem( num , dlg.m_Attribute.name );
		m_AttributeListCtrl.SetItemText( num , 1 , dlg.m_Attribute.m_pType->UMLGeneralizableElement::name );
		switch( dlg.m_Attribute.m_Visibility )
		{
		case VISIBILITY_PUBLIC:
			{
				m_AttributeListCtrl.SetItemText( num , 2 , "PUBLIC" );
			}
			break;
		case VISIBILITY_PRIVATE:
			{
				m_AttributeListCtrl.SetItemText( num , 2 , "PRIVATE" );
			}
			break;
		case VISIBILITY_PROTECTED:
			{
				m_AttributeListCtrl.SetItemText( num , 2 , "PROTECTED" );
			}
			break;
		default:
			m_AttributeListCtrl.SetItemText( num , 2 , "PACKAGE" );
		};
		m_AttributeListCtrl.SetItemText( num , 3 , dlg.m_Attribute.m_Multiplicity );
		m_AttributeListCtrl.SetItemText( num , 4 , dlg.m_Attribute.m_InitialValue  );
	}
}

void ClassInfoDlg::OnButtonNewOperation() 
{
	// TODO: Add your control notification handler code here
	OperationInfoDlg dlg;
	dlg.m_pClassList = m_pClassList;
	if( dlg.DoModal() == IDOK )
	{
		int iNum = m_OperationListCtrl.GetItemCount();
		m_Operations.Add( dlg.m_Operation );
		m_OperationListCtrl.InsertItem( iNum , dlg.m_Operation.name );
		switch( dlg.m_Operation.m_Visibility )
		{
		case VISIBILITY_PUBLIC:
			{
				m_OperationListCtrl.SetItemText( iNum , 1 , "PUBLIC" );
			}
			break;
		case VISIBILITY_PRIVATE:
			{
				m_OperationListCtrl.SetItemText( iNum , 1 , "PRIVATE" );
			}
			break;
		case VISIBILITY_PROTECTED:
			{
				m_OperationListCtrl.SetItemText( iNum , 1 , "PROTECTED" );
			}
			break;
		default:
			m_OperationListCtrl.SetItemText( iNum , 1 , "PACKAGE" );
		}
		if( dlg.m_Operation.m_ReturnType.m_pType )
			m_OperationListCtrl.SetItemText( iNum , 2 , dlg.m_Operation.m_ReturnType.m_pType->UMLGeneralizableElement::name );
	}
}
