|
using System;
using System.Xml.Serialization;
using System.IO;
using GUIStateMVP.Model;
namespace GUIStateMVP
{
/// <summary>
/// Loads logic definition file (xml), and responds to presenter for form logic
/// </summary>
public class LogicController : ILogicController
{
private FormLogic fl = null;
private IView _view;
public LogicController(IView view)
{
Init();
_view = view;
}
/// <summary>
/// Loads xml file into object model
/// </summary>
private void Init()
{
FileStream fs = new FileStream("FormStateEvent.xml", FileMode.Open);
try
{
XmlSerializer se = new XmlSerializer(typeof(FormLogic));
object x = se.Deserialize(fs);
fl = x as FormLogic;
}
catch(Exception ex)
{
Console.Out.WriteLine(ex.Message);
}
}
//Set View's inttial state
public void InitView()
{
//Hard code view initial state here, it can be moved to xml as well
IState initState = FindState("BooleanTrue", false);
_view.UpdateFieldState("Simpsons", initState);
initState = FindState("MembersOptionSimpsons", false);
_view.UpdateFieldState("Members", initState);
}
#region ILogicController Members
/// <summary>
/// Controller will parse through the logic table, trigger all the subscriber of current publish if
/// state is match.
/// Todo: subscriber can also become publisher to start a chain of events. So this method
/// could be called recursively. But the original publisher cannot become a subscriber, which is to
/// prevent circulor event.
/// </summary>
/// <param name="subject"></param>
public void RaiseEvent(string subject)
{
//Create an empty state type base on xml file definition
foreach(Event e in fl.Events.Event)
{
if(e.pubs[0].Sub == subject)
{
string stateName = e.pubs[0].State;
IState curState = FindState(stateName, true);
//Get publisher's current state
_view.GetFieldState(subject, curState);
//If publisher's state statisfy the condition
if(FindState(stateName, false).Equals(curState))
{
//Set state for each subscriber
foreach(Subscriber subscriber in e.subs)
{
IState state = FindState(subscriber.State, false);
if(state != null)
{
//Update state in subscriber
_view.UpdateFieldState(subscriber.Obj, state);
}
}
}
}
}
}
#endregion
/// <summary>
/// Return IState object based on searching
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
protected IState FindState(string name, bool empty)
{
foreach(State state in fl.States.State)
{
if(state.Name == name)
{
if(empty)
{
return state.GetEmptyState();
}
else
{
return state.InnerState;
}
}
}
return null;
}
}
}
|
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.
I have over 13 Years IT industry experience as Principle/Senior Programmer. I am experienced in .NET/J2EE system design and detailed implementation. I master UML modelling and OO design methodology with a strong C#/C++/Java coding background. I have been in working/managing a distributed project using Agile/Waterfall approach. My business knowledge includes Telecommunication, Financial Investment/Trading, and Banking.