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]
StephaneLenclud@235: [KnownType("DerivedTypes")]
StephaneLenclud@234: public class Manager
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: /// Constructor
StephaneLenclud@234: ///
StephaneLenclud@234: public Manager()
StephaneLenclud@234: {
StephaneLenclud@234: Init();
StephaneLenclud@234: }
StephaneLenclud@234:
StephaneLenclud@234: ///
StephaneLenclud@234: /// Executes after internalization took place.
StephaneLenclud@234: ///
StephaneLenclud@234: public void Init()
StephaneLenclud@234: {
StephaneLenclud@234: if (Events == null)
StephaneLenclud@234: {
StephaneLenclud@234: Events = new List();
StephaneLenclud@234: }
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@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@235:
StephaneLenclud@235: ///
StephaneLenclud@235: /// Allow extending our data contract.
StephaneLenclud@235: /// See KnownType above.
StephaneLenclud@235: ///
StephaneLenclud@235: ///
StephaneLenclud@235: private static IEnumerable DerivedTypes()
StephaneLenclud@235: {
StephaneLenclud@235: return SharpLib.Utils.Reflection.GetDerivedTypes();
StephaneLenclud@235: }
StephaneLenclud@234: }
StephaneLenclud@234: }