StephaneLenclud@234: // StephaneLenclud@234: using System; StephaneLenclud@234: using System.Collections.Generic; StephaneLenclud@234: using System.ComponentModel; StephaneLenclud@234: using System.Linq; StephaneLenclud@234: using System.Reflection; StephaneLenclud@234: using System.Runtime.Serialization; StephaneLenclud@234: using SharpLib.Utils; StephaneLenclud@234: StephaneLenclud@235: StephaneLenclud@234: namespace SharpLib.Ear StephaneLenclud@234: { StephaneLenclud@234: /// StephaneLenclud@234: /// Event Action Router (Ear) is a generic and extensible framework allowing users to execute actions in response to events. StephaneLenclud@234: /// Users can implement their own events and actions. StephaneLenclud@234: /// StephaneLenclud@234: [DataContract] Stephane@243: public class Manager: Object StephaneLenclud@234: { StephaneLenclud@234: /// StephaneLenclud@234: /// Our events instances. StephaneLenclud@234: /// StephaneLenclud@234: [DataMember] StephaneLenclud@234: public List Events; StephaneLenclud@234: StephaneLenclud@234: StephaneLenclud@234: /// StephaneLenclud@234: /// Executes after internalization took place. StephaneLenclud@234: /// Stephane@243: protected override void DoConstruct() StephaneLenclud@234: { Stephane@243: base.DoConstruct(); Stephane@243: StephaneLenclud@234: if (Events == null) StephaneLenclud@234: { StephaneLenclud@234: Events = new List(); StephaneLenclud@234: } Stephane@243: Stephane@243: // TODO: Object properties should be constructed too Stephane@243: foreach (Event e in Events) Stephane@243: { Stephane@243: e.Construct(); Stephane@243: } StephaneLenclud@234: StephaneLenclud@234: } StephaneLenclud@234: StephaneLenclud@234: /// StephaneLenclud@234: /// Trigger the given event. StephaneLenclud@234: /// StephaneLenclud@234: /// StephaneLenclud@234: public void TriggerEvent() where T: class StephaneLenclud@234: { StephaneLenclud@234: //Only trigger enabled events matching the desired type StephaneLenclud@234: foreach (Event e in Events.Where(e => e.GetType() == typeof(T) && e.Enabled)) StephaneLenclud@234: { StephaneLenclud@234: e.Trigger(); StephaneLenclud@234: } StephaneLenclud@234: } StephaneLenclud@234: StephaneLenclud@237: /// StephaneLenclud@237: /// Trigger the given event. StephaneLenclud@237: /// StephaneLenclud@237: /// StephaneLenclud@237: public void TriggerEvent(T aEvent) where T : class StephaneLenclud@237: { StephaneLenclud@237: //Only trigger events matching the desired type StephaneLenclud@237: foreach (Event e in Events.Where(e => e.Equals(aEvent) && e.Enabled)) StephaneLenclud@237: { StephaneLenclud@237: e.Trigger(); StephaneLenclud@237: } StephaneLenclud@237: } StephaneLenclud@237: StephaneLenclud@234: StephaneLenclud@234: /// StephaneLenclud@234: /// Remove the specified action from the event it belongs too. StephaneLenclud@234: /// StephaneLenclud@234: /// StephaneLenclud@234: public void RemoveAction(Action aAction) StephaneLenclud@234: { StephaneLenclud@234: foreach (Event e in Events) StephaneLenclud@234: { StephaneLenclud@234: if (e.Actions.Remove(aAction)) StephaneLenclud@234: { StephaneLenclud@234: //We removed our action, we are done here. StephaneLenclud@234: return; StephaneLenclud@234: } StephaneLenclud@234: } StephaneLenclud@234: } StephaneLenclud@234: } StephaneLenclud@234: }