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