Stephane@212: // Stephane@212: Stephane@212: Stephane@212: using System; Stephane@212: using System.Collections.Generic; Stephane@212: using System.ComponentModel; Stephane@212: using System.Linq; Stephane@212: using System.Reflection; Stephane@212: using System.Runtime.Serialization; Stephane@212: using SharpLib.Utils; Stephane@212: Stephane@212: namespace SharpLib.Ear Stephane@212: { Stephane@212: [TypeConverter(typeof(TypeConverterJson))] Stephane@212: [DataContract] Stephane@212: public class ManagerEventAction Stephane@212: { Stephane@212: public static ManagerEventAction Current = null; Stephane@212: public IDictionary ActionTypes; Stephane@212: public IDictionary Events; Stephane@212: [DataMember] Stephane@212: public Dictionary> ActionsByEvents = new Dictionary>(); Stephane@212: Stephane@212: Stephane@212: public ManagerEventAction() Stephane@212: { Stephane@212: Init(); Stephane@212: } Stephane@212: Stephane@212: /// Stephane@212: /// Stephane@212: /// Stephane@212: public void Init() Stephane@212: { Stephane@212: //Create our list of supported actions Stephane@212: ActionTypes = Utils.Reflection.GetConcreteClassesDerivedFromByName(); Stephane@212: //Create our list or support events Stephane@212: Events = Utils.Reflection.GetConcreteClassesInstanceDerivedFromByName(); Stephane@212: Stephane@212: if (ActionsByEvents == null) StephaneLenclud@214: { Stephane@212: ActionsByEvents = new Dictionary>(); Stephane@212: } Stephane@212: Stephane@212: //Hook in loaded actions with corresponding events Stephane@212: foreach (string key in Events.Keys) Stephane@212: { Stephane@212: Event e = Events[key]; Stephane@212: if (ActionsByEvents.ContainsKey(key)) Stephane@212: { Stephane@212: //We have actions for that event, hook them in then Stephane@212: e.Actions = ActionsByEvents[key]; Stephane@212: } Stephane@212: else Stephane@212: { Stephane@212: //We do not have actions for that event yet, create empty action list Stephane@212: e.Actions = new List(); Stephane@212: ActionsByEvents[key] = e.Actions; Stephane@212: } Stephane@212: } Stephane@212: } Stephane@212: StephaneLenclud@214: /// StephaneLenclud@214: /// Get and event instance from its type. StephaneLenclud@214: /// StephaneLenclud@214: /// StephaneLenclud@214: /// Stephane@212: public Event GetEvent() where T : class Stephane@212: { Stephane@212: return Events[typeof(T).Name]; Stephane@212: } Stephane@212: StephaneLenclud@214: /// StephaneLenclud@214: /// StephaneLenclud@214: /// StephaneLenclud@214: /// StephaneLenclud@214: public void RemoveAction(Action aAction) StephaneLenclud@214: { StephaneLenclud@214: foreach (string key in Events.Keys) StephaneLenclud@214: { StephaneLenclud@214: Event e = Events[key]; StephaneLenclud@214: if (e.Actions.Remove(aAction)) StephaneLenclud@214: { StephaneLenclud@214: //We removed our action, we are done here. StephaneLenclud@214: return; StephaneLenclud@214: } StephaneLenclud@214: StephaneLenclud@214: } StephaneLenclud@214: } StephaneLenclud@214: Stephane@212: } Stephane@212: }