Click here to Skip to main content
15,885,115 members
Articles / Desktop Programming / MFC

The SBJ MVC Framework - The Model, from Abstraction to Realization

Rate me:
Please Sign up or sign in to vote.
5.00/5 (19 votes)
20 Mar 2009CPOL19 min read 109.2K   1.3K   51  
A Model-View-Controller Framework that integrates with the MFC Doc/View architecture
//------------------------------------------------------------------------------
// $Workfile: ExplorerPane.cpp $
// $Header: /SbjDev/Shapes/ExplorerPane.cpp 8     11/12/08 2:22p Steve $
//
//	Copyright � 2008 SbjCat
// All rights reserved.
//
//
// *** Authors ***
//	 Steve Johnson
//
// $Revision: 8 $
//
//-----------------------------------------------------------------------------

#include "stdafx.h"
#include "ExplorerPane.h"
#include "Resource.h"


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

namespace localNS
{
	// TreeView::ItemControllers /////////////////////////////////////////////////////////////////////////

	class ExplorerTreeItemController : public SbjCore::Mvc::TreeView::ItemController
	{
		DECLARE_DYNCREATE(ExplorerTreeItemController)
		SbjCore::Mvc::TreeView::ItemDisplayModel theIDM;
		int nImageID;		
		CString sAttrName;
		
	public:
	
		virtual void Create(const SbjCore::Mvc::Model::Controller* pModelCtrlr, const SbjCore::Mvc::TreeView::Controller* pTreeController, HANDLE hItem)
		{
			SbjCore::Mvc::TreeView::ItemController::Create(pModelCtrlr, pTreeController, hItem);
			CString sName(pModelCtrlr->GetItemAttrValue(hItem, sAttrName));
			theIDM.SetItemText(sName);
			theIDM.SetImageResID(nImageID);
			SetItemDisplayModel(&theIDM);
		}

		virtual ~ExplorerTreeItemController(void)
		{
		}
		
		CString GetAttrName() const
		{
			return sAttrName;
		}
		
	public:
		virtual void UpdateItemText(LPCTSTR lpszItemText)
		{
			SbjCore::Mvc::TreeView::ItemController::UpdateItemText(lpszItemText);
			SbjCore::Mvc::Model::Controller* pTheModelCtrlr = dynamic_cast<SbjCore::Mvc::Model::Controller*>(SbjCore::Mvc::Model::GetCurController());
			pTheModelCtrlr->SetItemAttrValue(GetModelItemHandle(), sAttrName, lpszItemText, true, true);
		}
		
	protected:
		ExplorerTreeItemController()
		{
		}		

	protected:
		void SetImageID(const int nID)
		{
			nImageID = nID;
		}
		
		void SetAttrName(LPCTSTR lpsz)
		{
			sAttrName = lpsz;
		}
	};
	IMPLEMENT_DYNCREATE(ExplorerTreeItemController, SbjCore::Mvc::TreeView::ItemController)

	///////////////////////////////////////////////////////////////////////////////////////////

	class DrawingItemController : public ExplorerTreeItemController
	{
		DECLARE_DYNCREATE(DrawingItemController)
	public:
		virtual void Create(const SbjCore::Mvc::Model::Controller* pModelCtrlr, const SbjCore::Mvc::TreeView::Controller* pTreeController, HANDLE hItem)
		{
			SetImageID(1);
			SetAttrName(_T("name"));
			ExplorerTreeItemController::Create(pModelCtrlr, pTreeController, hItem);
		};	
	
		virtual ~DrawingItemController(void)
		{
		}

	protected:
		DrawingItemController()
		{
		}		

		virtual bool CanDelete()
		{
			return false;
		}			


	protected:
		virtual SbjCore::Utils::Menu::ItemRange OnPrepareCtxMenu(CMenu& ctxMenu)
		{
			SbjCore::Utils::Menu::ItemRange menuItems;
			menuItems.nFirst = (int)ctxMenu.GetMenuItemCount()-1;
			menuItems.nLast = menuItems.nFirst; 
			if (SbjCore::Utils::Menu::InsertSeparator(ctxMenu, menuItems.nLast))
			{
				menuItems.nLast++;
			}
			(void)ctxMenu.InsertMenu(0, MF_BYPOSITION, ID_CMDS_NEWELLIPSE, _T("New &Ellipse"));
			(void)ctxMenu.InsertMenu(0, MF_BYPOSITION, ID_CMDS_NEWRECTANGLE, _T("New &Rectangle"));
			return SbjCore::Mvc::TreeView::ItemController::OnPrepareCtxMenu(ctxMenu);
		}
	};
	IMPLEMENT_DYNCREATE(DrawingItemController, ExplorerTreeItemController)

	///////////////////////////////////////////////////////////////////////////////////////////

	class RectangleItemController : public ExplorerTreeItemController
	{
		DECLARE_DYNCREATE(RectangleItemController)
	public:
		virtual void Create(const SbjCore::Mvc::Model::Controller* pModelCtrlr, const SbjCore::Mvc::TreeView::Controller* pTreeController, HANDLE hItem)
		{
			SetImageID(2);
			SetAttrName(_T("label"));
			ExplorerTreeItemController::Create(pModelCtrlr, pTreeController, hItem);
		};	

		virtual ~RectangleItemController(void)
		{
		}

	protected:
		RectangleItemController()
		{
		}		
	};
	IMPLEMENT_DYNCREATE(RectangleItemController, ExplorerTreeItemController)

	///////////////////////////////////////////////////////////////////////////////////////////

	class EllipseItemController : public ExplorerTreeItemController
	{
		DECLARE_DYNCREATE(EllipseItemController)
	public:
		virtual void Create(const SbjCore::Mvc::Model::Controller* pModelCtrlr, const SbjCore::Mvc::TreeView::Controller* pTreeController, HANDLE hItem)
		{
			SetImageID(4);
			SetAttrName(_T("label"));
			ExplorerTreeItemController::Create(pModelCtrlr, pTreeController, hItem);
		};	

		virtual ~EllipseItemController(void)
		{
		}

	protected:
		EllipseItemController()
		{
		}		

	};
	IMPLEMENT_DYNCREATE(EllipseItemController, ExplorerTreeItemController)


	// EventHandlers //////////////////////////////////////////////////////////////////////////////////////////

	class ItemTextChangedEventHandler : public SbjCore::EventMgr::EventHandler
	{
		SbjCore::Mvc::TreeView::Controller* pTheCtrlr;
	public:
		ItemTextChangedEventHandler() :
		  SbjCore::EventMgr::EventHandler(SbjCore::Mvc::Model::Events::EVID_ITEM_CHANGED),
			  pTheCtrlr(NULL)
		  {
		  }

		  void Init(SbjCore::Mvc::TreeView::Controller* p)
		  {
			  pTheCtrlr = p;
		  }

	private:		
		virtual void OnHandle(SbjCore::EventMgr::Event* pEvent)
		{
			ASSERT(pTheCtrlr != NULL);
			if (pTheCtrlr != NULL)
			{
				SbjCore::Mvc::Model::Events::ItemChange* pTheEvent = dynamic_cast<SbjCore::Mvc::Model::Events::ItemChange*>(pEvent);
				ASSERT(pTheEvent != NULL);
				if (pTheEvent != NULL)
				{
					ExplorerTreeItemController* pItemCtrlr = dynamic_cast<ExplorerTreeItemController*>(pTheCtrlr->GetItemController(pTheEvent->hItem));
					if (pItemCtrlr != NULL)
					{
						if (0 == pItemCtrlr->GetAttrName().Compare(pTheEvent->sAttrName))
						{
							SbjCore::Mvc::Model::Controller* pTheModelCtrlr = dynamic_cast<SbjCore::Mvc::Model::Controller*>(SbjCore::Mvc::Model::GetCurController());
							CString sName(pTheModelCtrlr->GetItemAttrValue(pTheEvent->hItem, pTheEvent->sAttrName));
							pItemCtrlr->SbjCore::Mvc::TreeView::ItemController::UpdateItemText(sName);
						}
					}
				}
			}
		}
	} theItemTextChangedEventHandler;

}


///////////////////////////////////////////////////////////////////////////////////////////

struct ExplorerPaneImpl : public SbjCore::Mvc::DockTreePaneController
{
	typedef SbjCore::Mvc::DockTreePaneController t_Base;
	
	SbjCore::Mvc::TreeView::Controller theTreeCtrlr;
	
	ExplorerPaneImpl() :
		t_Base(),
		theTreeCtrlr(IDB_TREE_IMAGES, 3, DEFAULT_TREE_STYLES | TVS_EDITLABELS)
	{
		SetTreeController(&theTreeCtrlr);
		theTreeCtrlr.SetUsingResIDs(false);							  
		
		theTreeCtrlr.MapItemRTCToModelTypeName(_T("Drawing"), RUNTIME_CLASS(localNS::DrawingItemController));
		theTreeCtrlr.MapItemRTCToModelTypeName(_T("Rectangle"), RUNTIME_CLASS(localNS::RectangleItemController));
		theTreeCtrlr.MapItemRTCToModelTypeName(_T("Ellipse"), RUNTIME_CLASS(localNS::EllipseItemController));
	
		localNS::theItemTextChangedEventHandler.Init(&theTreeCtrlr);
	}
	
	virtual ~ExplorerPaneImpl()
	{
	}
	
};

///////////////////////////////////////////////////////

ExplorerPane::ExplorerPane() :
	m_pImpl(new ExplorerPaneImpl)
{
	SetController(m_pImpl);
}

ExplorerPane::~ExplorerPane()
{
	try
	{
		delete m_pImpl;
	}
	catch (...)
	{
		ASSERT(FALSE);
	}
}

//*** Modification History ***
// $Log: /SbjDev/Shapes/ExplorerPane.cpp $
// 
// 8     11/12/08 2:22p Steve
// Finished Generalization of Model  v2.0.1
// 
// 7     10/20/08 11:06a Steve
// Removed source parameter from Event and replaced with EventT data
// 
// 6     10/14/08 1:12p Steve
// Implemented Deletes
// 
// 5     9/24/08 1:05p Steve
// Renamed Explorer
// 
// 4     9/24/08 1:00p 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
SBJ
United States United States
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.

Comments and Discussions