|
//------------------------------------------------------------------------------
// $Workfile: ModelAction.cpp $
// $Header: /SbjDev/SbjCore/ModelAction.cpp 3 11/12/08 2:22p Steve $
//
// Copyright � 2008 SbjCat
// All rights reserved.
//
//
// *** Authors ***
// Steve Johnson
//
// $Revision: 3 $
//
//-----------------------------------------------------------------------------
#include "StdAfx.h"
#include "ModelAction.h"
#include "ModelController.h"
#include "ModelDataTypes.h"
#include "ModelActionHandler.h"
namespace SbjCore
{
namespace Mvc
{
namespace Model
{
struct ActionImpl
{
Action::HandlerMap* pTheMap;
CRuntimeClass* pTheDefaultHandler;
const Controller* pTheModel;
ActionImpl(const Controller* pCtrlr, Action::HandlerMap* pMap, CRuntimeClass* pDefaultHandler) :
pTheModel(pCtrlr),
pTheMap(pMap),
pTheDefaultHandler(pDefaultHandler)
{
}
virtual ~ActionImpl()
{
}
int Apply(Action* pAction, HANDLE hItem)
{
Action::Results eRslt = Action::kContinue;
ActionHandler* pTheHandler = NULL;
CString sClassName;
CString s(pTheModel->GetItemTypeName(hItem));
if (pTheMap != NULL)
{
Action::HandlerMap::iterator iter = pTheMap->find((LPCTSTR)s);
if (iter != pTheMap->end())
{
try
{
CRuntimeClass* pClass = iter->second;
pTheHandler = dynamic_cast<ActionHandler*>(pClass->CreateObject());
ASSERT(pTheHandler != NULL);
sClassName = pClass->m_lpszClassName;
//TRACE(_T("Create %s : 0x%X\n"), pClass->m_lpszClassName, hItem);
}
catch (...)
{
ASSERT(FALSE);
}
}
}
if (NULL == pTheHandler)
{
if (pTheDefaultHandler != NULL)
{
pTheHandler = dynamic_cast<ActionHandler*>(pTheDefaultHandler->CreateObject());
sClassName = pTheDefaultHandler->m_lpszClassName;
//TRACE(_T("Create %s : 0x%X\n\n"), pTheDefaultHandler->m_lpszClassName, hItem);
}
else
{
CRuntimeClass* pClass = RUNTIME_CLASS(ActionHandler);
pTheHandler = dynamic_cast<ActionHandler*>(pClass->CreateObject());
sClassName = pClass->m_lpszClassName;
//TRACE(_T("Create %s : 0x%X\n\n"), pClass->m_lpszClassName, hItem);
}
}
if (pTheHandler != NULL)
{
pTheHandler->Initialize(pAction, hItem);
eRslt = (Action::Results)pTheHandler->BeginHandling();
}
if (Action::kContinue == eRslt)
{
Model::ItemHandles theChildren;
pTheModel->GetItemChildren(hItem, theChildren);
for (Model::ItemHandlesIter iter = theChildren.begin(); iter != theChildren.end(); iter++)
{
HANDLE hChild = *iter;
if (hChild != NULL)
{
eRslt = (Action::Results)Apply(pAction, hChild);
}
if (eRslt != Action::kContinue)
{
break;
}
}
}
if (pTheHandler != NULL)
{
if (Action::kContinue == eRslt)
{
eRslt = (Action::Results)pTheHandler->EndHandling();
}
delete pTheHandler;
//TRACE(_T("Delete %s : 0x%X\n"), sClassName, hItem);
}
return eRslt;
}
};
/////////////////////////////////////////////////////////
Action::Action(const Controller* pCtrlr, HandlerMap* pMap /*= NULL*/, CRuntimeClass* pDefaultHandler /*= NULL*/) :
m_pImpl(new ActionImpl(pCtrlr, pMap, pDefaultHandler))
{
}
Action::~Action()
{
try
{
delete m_pImpl;
}
catch(...)
{
ASSERT(FALSE);
}
}
void Action::SetDefaultHandler(CRuntimeClass* pDefault)
{
m_pImpl->pTheDefaultHandler = pDefault;
}
int Action::Apply(HANDLE hItem)
{
return m_pImpl->Apply(this, hItem);
}
const Controller* Action::GetModelController()
{
return m_pImpl->pTheModel;
}
}
}
}
//*** Modification History ***
// $Log: /SbjDev/SbjCore/ModelAction.cpp $
//
// 3 11/12/08 2:22p Steve
// Finished Generalization of Model v2.0.1
//
// 2 10/26/08 9:19a Steve
//
// 1 10/24/08 9:03a Steve
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Real name is Steve Johnson. Programming since 1979. Started on a Heathkit Micro with a DEC LSI-11 and UCSD Pascal. Moved to PCs & DOS as soon as Turbo Pascal became available. Did some Assembly, ISR, TSR etc. All this while working for a Manufacturing Co. for 8 years. Had my own solo Co. doing barcode labeling software for 4 years (terrible business man, all I wanted to do was code). Since then working for various software companies. Moved to Windows around the time of 3.1 with Borland C then C++. Then on to VC++ and MFC, and just about anything I could get my hands on or had to learn for my job, and been at it ever since. Of course recently I've been playing with .NET, ASP, C#, WPF etc.