SharpLibEar/ManagerEventAction.cs
author Stephane Lenclud
Sun, 31 Jul 2016 12:03:52 +0200
changeset 230 8c5cf2228e9a
parent 212 1a0791daa243
child 231 4c706feaf706
permissions -rw-r--r--
Published v0.10.2.0.
     1 //
     2 
     3 
     4 using System;
     5 using System.Collections.Generic;
     6 using System.ComponentModel;
     7 using System.Linq;
     8 using System.Reflection;
     9 using System.Runtime.Serialization;
    10 using SharpLib.Utils;
    11 
    12 namespace SharpLib.Ear
    13 {
    14     [TypeConverter(typeof(TypeConverterJson<ManagerEventAction>))]
    15     [DataContract]
    16     public class ManagerEventAction
    17     {
    18         public static ManagerEventAction Current = null;
    19         public IDictionary<string, Type> ActionTypes;
    20         public IDictionary<string, Event> Events;
    21         [DataMember]
    22         public Dictionary<string, List<Action>> ActionsByEvents = new Dictionary<string, List<Action>>();
    23 
    24 
    25         public ManagerEventAction()
    26         {
    27             Init();
    28         }
    29 
    30         /// <summary>
    31         /// 
    32         /// </summary>
    33         public void Init()
    34         {
    35             //Create our list of supported actions
    36             ActionTypes = Utils.Reflection.GetConcreteClassesDerivedFromByName<Action>();
    37             //Create our list or support events
    38             Events = Utils.Reflection.GetConcreteClassesInstanceDerivedFromByName<Event>();
    39 
    40             if (ActionsByEvents == null)
    41             {                
    42                 ActionsByEvents = new Dictionary<string, List<Action>>();
    43             }
    44 
    45             //Hook in loaded actions with corresponding events
    46             foreach (string key in Events.Keys)
    47             {
    48                 Event e = Events[key];
    49                 if (ActionsByEvents.ContainsKey(key))
    50                 {
    51                     //We have actions for that event, hook them in then
    52                     e.Actions = ActionsByEvents[key];
    53                 }
    54                 else
    55                 {
    56                     //We do not have actions for that event yet, create empty action list
    57                     e.Actions = new List<Action>();
    58                     ActionsByEvents[key] = e.Actions;
    59                 }
    60             }
    61         }
    62 
    63         /// <summary>
    64         /// Get and event instance from its type.
    65         /// </summary>
    66         /// <typeparam name="T"></typeparam>
    67         /// <returns></returns>
    68         public Event GetEvent<T>() where T : class
    69         {
    70             return Events[typeof(T).Name];
    71         }
    72 
    73         /// <summary>
    74         /// 
    75         /// </summary>
    76         /// <param name="aAction"></param>
    77         public void RemoveAction(Action aAction)
    78         {
    79             foreach (string key in Events.Keys)
    80             {
    81                 Event e = Events[key];
    82                 if (e.Actions.Remove(aAction))
    83                 {
    84                     //We removed our action, we are done here.
    85                     return;
    86                 }
    87 
    88             }
    89         }
    90 
    91     }
    92 }