SharpLibEar/Manager.cs
author StephaneLenclud
Thu, 18 Aug 2016 17:43:03 +0200
changeset 240 5c4f1e2bf29a
parent 235 ba14a29944c4
child 243 cc2251d065db
permissions -rw-r--r--
Adding empty class for eject optical drive action.
     1 //
     2 using System;
     3 using System.Collections.Generic;
     4 using System.ComponentModel;
     5 using System.Linq;
     6 using System.Reflection;
     7 using System.Runtime.Serialization;
     8 using SharpLib.Utils;
     9 
    10 
    11 namespace SharpLib.Ear
    12 {
    13     /// <summary>
    14     /// Event Action Router (Ear) is a generic and extensible framework allowing users to execute actions in response to events. 
    15     /// Users can implement their own events and actions.
    16     /// </summary>
    17     [DataContract]
    18     [KnownType("DerivedTypes")]
    19     public class Manager
    20     {
    21         /// <summary>
    22         /// Our events instances.
    23         /// </summary>
    24         [DataMember]
    25         public List<Event> Events;
    26 
    27         /// <summary>
    28         /// Constructor
    29         /// </summary>
    30         public Manager()
    31         {
    32             Init();
    33         }
    34 
    35         /// <summary>
    36         /// Executes after internalization took place.
    37         /// </summary>
    38         public void Init()
    39         {
    40             if (Events == null)
    41             {
    42                 Events = new List<Event>();
    43             }
    44             
    45         }
    46 
    47         /// <summary>
    48         /// Trigger the given event.
    49         /// </summary>
    50         /// <param name="aEventType"></param>
    51         public void TriggerEvent<T>() where T: class
    52         {
    53             //Only trigger enabled events matching the desired type
    54             foreach (Event e in Events.Where(e => e.GetType() == typeof(T) && e.Enabled))
    55             {
    56                 e.Trigger();
    57             }
    58         }
    59 
    60         /// <summary>
    61         /// Trigger the given event.
    62         /// </summary>
    63         /// <param name="aEventType"></param>
    64         public void TriggerEvent<T>(T aEvent) where T : class
    65         {
    66             //Only trigger events matching the desired type
    67             foreach (Event e in Events.Where(e => e.Equals(aEvent) && e.Enabled))
    68             {
    69                 e.Trigger();
    70             }
    71         }
    72 
    73 
    74         /// <summary>
    75         /// Remove the specified action from the event it belongs too.
    76         /// </summary>
    77         /// <param name="aAction"></param>
    78         public void RemoveAction(Action aAction)
    79         {
    80             foreach (Event e in Events)
    81             {
    82                 if (e.Actions.Remove(aAction))
    83                 {
    84                     //We removed our action, we are done here.
    85                     return;
    86                 }
    87             }
    88         }
    89 
    90         /// <summary>
    91         /// Allow extending our data contract.
    92         /// See KnownType above.
    93         /// </summary>
    94         /// <returns></returns>
    95         private static IEnumerable<Type> DerivedTypes()
    96         {
    97             return SharpLib.Utils.Reflection.GetDerivedTypes<Manager>();
    98         }
    99     }
   100 }