// ClassInfoDlg_Cardinality.cpp : implementation file
//

#include "stdafx.h"
#include "XmlClassCardinality.h"
#include "XmlClassInfo.h"
#include "ClassInfoDlg_Cardinality.h"

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

/////////////////////////////////////////////////////////////////////////////
// CClassInfoDlg_Cardinality dialog


CClassInfoDlg_Cardinality::CClassInfoDlg_Cardinality(CWnd* pParent /*=NULL*/)
	: CPropertyEditorDlg(CClassInfoDlg_Cardinality::IDD, pParent),
	classInfo_(NULL),
	cardinality_(enCardinality_SingleInstance)
{
	//{{AFX_DATA_INIT(CClassInfoDlg_Cardinality)
	//}}AFX_DATA_INIT
}


void CClassInfoDlg_Cardinality::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CClassInfoDlg_Cardinality)
	DDX_Control(pDX, IDC_TXT_MAP_MIN_SIZE, txtMapMinSize_);
	DDX_Control(pDX, IDC_TXT_MAP_MAX_SIZE, txtMapMaxSize_);
	DDX_Control(pDX, IDC_TXT_ARRAY_MIN_SIZE, txtArrayMinSize_);
	DDX_Control(pDX, IDC_TXT_ARRAY_MAX_SIZE, txtArrayMaxSize_);
	DDX_Control(pDX, IDC_LBL_MAP_SORT_KEY, lblMapSortKey_);
	DDX_Control(pDX, IDC_CHK_MAP_MIN_SIZE, chkMapMinSize_);
	DDX_Control(pDX, IDC_CHK_MAP_MAX_SIZE, chkMapMaxSize_);
	DDX_Control(pDX, IDC_CHK_ARRAY_MIN_SIZE, chkArrayMinSize_);
	DDX_Control(pDX, IDC_CHK_ARRAY_MAX_SIZE, chkArrayMaxSize_);
	DDX_Control(pDX, IDC_CBO_MAP_SORT_KEY, cboMapSortKey_);
	DDX_Control(pDX, IDC_CHK_DISPLAY_TOOLBAR, chkDisplayToolbar_);
	DDX_Control(pDX, IDC_CHK_CREATE_CONTAINER_NODE, chkCreateContainerNode_);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CClassInfoDlg_Cardinality, CPropertyEditorDlg)
	//{{AFX_MSG_MAP(CClassInfoDlg_Cardinality)
	ON_BN_CLICKED(IDC_OPT_SINGLE_INSTANCE, OnOptSingleInstance)
	ON_BN_CLICKED(IDC_OPT_ARRAY, OnOptArray)
	ON_BN_CLICKED(IDC_OPT_MAP, OnOptMap)
	ON_BN_CLICKED(IDC_CHK_ARRAY_MAX_SIZE, OnChkArrayMaxSize)
	ON_BN_CLICKED(IDC_CHK_ARRAY_MIN_SIZE, OnChkArrayMinSize)
	ON_BN_CLICKED(IDC_CHK_MAP_MAX_SIZE, OnChkMapMaxSize)
	ON_BN_CLICKED(IDC_CHK_MAP_MIN_SIZE, OnChkMapMinSize)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClassInfoDlg_Cardinality message handlers

void CClassInfoDlg_Cardinality::StoreData()
{
	if (classInfo_ == NULL)
		return;

	CXmlClassCardinality* tmp;

	tmp = classInfo_->GetCardinalityInfo();

	if (chkArrayMinSize_.GetCheck())
		tmp->SetArrayMinSize(txtArrayMinSize_.GetInt());
	else
		tmp->SetArrayMinSize(0);

	if (chkArrayMaxSize_.GetCheck())
		tmp->SetArrayMaxSize(txtArrayMaxSize_.GetInt());
	else
		tmp->SetArrayMaxSize(-1);

	if (chkMapMinSize_.GetCheck())
		tmp->SetMapMinSize(txtMapMinSize_.GetInt());
	else
		tmp->SetMapMinSize(0);

	if (chkMapMaxSize_.GetCheck())
		tmp->SetMapMaxSize(txtMapMaxSize_.GetInt());
	else
		tmp->SetMapMaxSize(-1);

	cmString cboText;
	
	if (cboMapSortKey_.GetCurSel() != CB_ERR) 
		cboMapSortKey_.GetLBText(cboMapSortKey_.GetCurSel(), cboText);

	tmp->SetMapSortKey(cboText);

	if (((CButton*)GetDlgItem(IDC_OPT_SINGLE_INSTANCE))->GetCheck())
		tmp->SetCardinality(enCardinality_SingleInstance);
	else if (((CButton*)GetDlgItem(IDC_OPT_ARRAY))->GetCheck())
		tmp->SetCardinality(enCardinality_Array);
	else
		tmp->SetCardinality(enCardinality_Map);

	tmp->SetDisplayToolbar(chkDisplayToolbar_.GetCheck() == TRUE);
	tmp->SetUseContainerNode(chkCreateContainerNode_.GetCheck() == TRUE);
}

void CClassInfoDlg_Cardinality::SetData(CXmlBaseElement* element)
{
	CXmlClassCardinality* tmpCardinality;

	classInfo_ = dynamic_cast<CXmlClassInfo*>(element);

	tmpCardinality = classInfo_->GetCardinalityInfo();
	
	cardinality_ = tmpCardinality->GetCardinality();

	if (tmpCardinality->GetArrayMinSize() == -1) {
		chkArrayMinSize_.SetCheck(FALSE);
	} else {
		chkArrayMinSize_.SetCheck(TRUE);
		txtArrayMinSize_.SetInt(tmpCardinality->GetArrayMinSize());
	}

	if (tmpCardinality->GetArrayMaxSize() == -1) {
		chkArrayMaxSize_.SetCheck(FALSE);
	} else {
		chkArrayMaxSize_.SetCheck(TRUE);
		txtArrayMaxSize_.SetInt(tmpCardinality->GetArrayMaxSize());
	}
	
	if (tmpCardinality->GetMapMinSize() == -1) {
		chkMapMinSize_.SetCheck(FALSE);
	} else {
		chkMapMinSize_.SetCheck(TRUE);
		txtMapMinSize_.SetInt(tmpCardinality->GetMapMinSize());
	}

	if (tmpCardinality->GetMapMaxSize() == -1) {
		chkMapMaxSize_.SetCheck(FALSE);
	} else {
		chkMapMaxSize_.SetCheck(TRUE);
		txtMapMaxSize_.SetInt(tmpCardinality->GetMapMaxSize());
	}

	// setup combo box
	//
	ClassMemberMap* members;
	ClassMemberMap::iterator it;
	CXmlBaseElement* member;

	cboMapSortKey_.ResetContent();
	members = classInfo_->GetMembers();
	for (it = members->begin(); it != members->end(); it++) {
		member = it->second;

		if (member->GetDataType() != enDataType_Class) {
			cboMapSortKey_.AddString(member->GetName());
		}
	}
	
	cboMapSortKey_.SelectString(-1, tmpCardinality->GetMapSortKey());

	chkDisplayToolbar_.SetCheck(tmpCardinality->GetDisplayToolbar());
	chkCreateContainerNode_.SetCheck(tmpCardinality->GetUseContainerNode());

	UpdateRadioButtons();
	UpdateControls(cardinality_);
}

void CClassInfoDlg_Cardinality::OnOptSingleInstance() 
{
	cardinality_ = enCardinality_SingleInstance;

	UpdateControls(cardinality_);
}

void CClassInfoDlg_Cardinality::OnOptArray() 
{
	cardinality_ = enCardinality_Array;
	UpdateControls(cardinality_);
}

void CClassInfoDlg_Cardinality::OnOptMap() 
{
	cardinality_ = enCardinality_Map;
	UpdateControls(cardinality_);
}

void CClassInfoDlg_Cardinality::UpdateControls(enCardinality cardinality)
{
	bool isArray;

	isArray = (cardinality == enCardinality_Array);
	chkArrayMinSize_.EnableWindow(isArray);
	chkArrayMaxSize_.EnableWindow(isArray);
	txtArrayMinSize_.EnableWindow(isArray && chkArrayMinSize_.GetCheck());
	txtArrayMaxSize_.EnableWindow(isArray && chkArrayMaxSize_.GetCheck());

	bool isMap;
	
	isMap = (cardinality == enCardinality_Map);
	chkMapMinSize_.EnableWindow(isMap);
	chkMapMaxSize_.EnableWindow(isMap);
	txtMapMinSize_.EnableWindow(isMap  && chkMapMinSize_.GetCheck());
	txtMapMaxSize_.EnableWindow(isMap  && chkMapMaxSize_.GetCheck());
	lblMapSortKey_.EnableWindow(isMap);
	cboMapSortKey_.EnableWindow(isMap);

	chkDisplayToolbar_.EnableWindow(isMap || isArray);
	chkCreateContainerNode_.EnableWindow(isMap || isArray);

}

void CClassInfoDlg_Cardinality::OnChkArrayMaxSize() 
{
	UpdateControls(cardinality_);
}

void CClassInfoDlg_Cardinality::OnChkArrayMinSize() 
{
	UpdateControls(cardinality_);
}

void CClassInfoDlg_Cardinality::OnChkMapMaxSize() 
{
	UpdateControls(cardinality_);
}

void CClassInfoDlg_Cardinality::OnChkMapMinSize() 
{
	UpdateControls(cardinality_);
}

BOOL CClassInfoDlg_Cardinality::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	txtMapMinSize_.AllowNegative(false);
	txtMapMaxSize_.AllowNegative(false);
	txtArrayMinSize_.AllowNegative(false);
	txtArrayMaxSize_.AllowNegative(false);
	
	UpdateRadioButtons();
	UpdateControls(cardinality_);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CClassInfoDlg_Cardinality::OnDataUpdate()
{
	cardinality_ = classInfo_->GetCardinalityInfo()->GetCardinality();
	UpdateRadioButtons();
	UpdateControls(cardinality_);
}

void CClassInfoDlg_Cardinality::UpdateRadioButtons()
{
	((CButton*)GetDlgItem(IDC_OPT_SINGLE_INSTANCE))->SetCheck(cardinality_ == enCardinality_SingleInstance);
	((CButton*)GetDlgItem(IDC_OPT_ARRAY))->SetCheck(cardinality_ == enCardinality_Array);
	((CButton*)GetDlgItem(IDC_OPT_MAP))->SetCheck(cardinality_ == enCardinality_Map);
	
}