// OperationInfoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AG_MDA.h"
#include "OperationInfoDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// OperationInfoDlg dialog


OperationInfoDlg::OperationInfoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(OperationInfoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(OperationInfoDlg)
	m_Name = _T("");
	//}}AFX_DATA_INIT
}


void OperationInfoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(OperationInfoDlg)
	DDX_Control(pDX, IDC_LIST_PARAMETER, m_ParameterListCtrl);
	DDX_Control(pDX, IDC_COMBO_VISIBILITY, m_VisibilityCombo);
	DDX_Control(pDX, IDC_COMBO_RETURN, m_ReturnCombo);
	DDX_Text(pDX, IDC_EDIT_NAME, m_Name);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(OperationInfoDlg, CDialog)
	//{{AFX_MSG_MAP(OperationInfoDlg)
	ON_BN_CLICKED(IDC_BUTTON_ADD_PARAMETER, OnButtonAddParameter)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_PARAMETER, OnDblclkListParameter)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// OperationInfoDlg message handlers

BOOL OperationInfoDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	int iSelType = -1;
	
	// TODO: Add extra initialization here
	m_VisibilityCombo.InsertString( 0 , "PUBLIC" );
	m_VisibilityCombo.InsertString( 1 , "PRIVATE" );
	m_VisibilityCombo.InsertString( 2 , "PROTECTED" );
	m_VisibilityCombo.InsertString( 3 , "PACKAGE" );

	for( int i = 0 ; i < m_pClassList->GetSize() ; i ++ )
	{
		UMLClass *p = m_pClassList->GetAt( i );
		m_ReturnCombo.InsertString( i , p->UMLGeneralizableElement::name );
		if( p == m_Operation.m_ReturnType.m_pType ) iSelType = i ;
	}

	m_ParameterListCtrl.InsertColumn( 0 , "Name" , LVCFMT_LEFT, 100 );
	m_ParameterListCtrl.InsertColumn( 1 , "DirKind" , LVCFMT_LEFT, 100 );
	m_ParameterListCtrl.InsertColumn( 2 , "Type" , LVCFMT_LEFT, 100 );
	m_ParameterListCtrl.InsertColumn( 3 , "DefaultValue" , LVCFMT_LEFT, 100 );

	m_ParameterListCtrl.SetExtendedStyle( 33 );

	m_Name = m_Operation.name;
	m_VisibilityCombo.SetCurSel( int(m_Operation.m_Visibility) );
	m_ReturnCombo.SetCurSel( iSelType );
	for( i = 0 ; i < m_Operation.m_TypedParameter.GetSize() ; i ++ )
	{
		UMLParameter param = m_Operation.m_TypedParameter.GetAt( i );
		m_ParameterListCtrl.InsertItem( i , param.name );
		switch( param.m_DirKind )
		{
		case PARAMETER_DIR_IN:
			{
				m_ParameterListCtrl.SetItemText( i , 1 , "IN" );
			}
			break;
		case PARAMETER_DIR_OUT:
			{
				m_ParameterListCtrl.SetItemText( i , 1 , "OUT" );
			}
			break;
		default:
			m_ParameterListCtrl.SetItemText( i , 1 , "INOUT" );
		}
		m_ParameterListCtrl.SetItemText( i , 2 , param.m_pType->UMLGeneralizableElement::name );
		m_ParameterListCtrl.SetItemText( i , 3 , param.m_DefaultValue );
	}
	
	UpdateData( FALSE );
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void OperationInfoDlg::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData( );
	m_Operation.name = m_Name;
	if( m_ReturnCombo.GetCurSel() >= 0 )
		m_Operation.m_ReturnType.m_pType = m_pClassList->GetAt( m_ReturnCombo.GetCurSel() );
	else
		m_Operation.m_ReturnType.m_pType = NULL;
	m_Operation.m_Visibility = UMLVisibility( m_VisibilityCombo.GetCurSel() );
	CDialog::OnOK();
}

void OperationInfoDlg::OnButtonAddParameter() 
{
	// TODO: Add your control notification handler code here
	ParameterInfoDlg dlg;
	dlg.m_pClassList = m_pClassList;
	if( dlg.DoModal() == IDOK )
	{
		int num = m_ParameterListCtrl.GetItemCount();
		m_Operation.m_TypedParameter.Add( dlg.m_Parameter );
		m_ParameterListCtrl.InsertItem( num , dlg.m_Parameter.name );
		switch( dlg.m_Parameter.m_DirKind )
		{
		case PARAMETER_DIR_IN:
			{
				m_ParameterListCtrl.SetItemText( num , 1 , "IN" );
			}
			break;
		case PARAMETER_DIR_OUT:
			{
				m_ParameterListCtrl.SetItemText( num , 1 , "OUT" );
			}
			break;
		default:
			m_ParameterListCtrl.SetItemText( num , 1 , "INOUT" );

		}
		m_ParameterListCtrl.SetItemText( num , 2 , dlg.m_Parameter.m_pType->UMLGeneralizableElement::name );
		m_ParameterListCtrl.SetItemText( num , 3 , dlg.m_Parameter.m_DefaultValue);
	}
}

void OperationInfoDlg::OnDblclkListParameter(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	int iSel = m_ParameterListCtrl.GetSelectionMark();
	if( iSel < 0 )return;
	ParameterInfoDlg dlg;
	dlg.m_pClassList = m_pClassList;
	dlg.m_Parameter = m_Operation.m_TypedParameter.GetAt( iSel );
	if( dlg.DoModal() == IDOK )
	{
		int num = iSel;
		m_ParameterListCtrl.SetItemText( num , 0 , dlg.m_Parameter.name );
		switch( dlg.m_Parameter.m_DirKind )
		{
		case PARAMETER_DIR_IN:
			{
				m_ParameterListCtrl.SetItemText( num , 1 , "IN" );
			}
			break;
		case PARAMETER_DIR_OUT:
			{
				m_ParameterListCtrl.SetItemText( num , 1 , "OUT" );
			}
			break;
		default:
			m_ParameterListCtrl.SetItemText( num , 1 , "INOUT" );

		}
		m_ParameterListCtrl.SetItemText( num , 2 , dlg.m_Parameter.m_pType->UMLGeneralizableElement::name );
		m_ParameterListCtrl.SetItemText( num , 3 , dlg.m_Parameter.m_DefaultValue);
	}
	*pResult = 0;
}
