# HG changeset patch # User StephaneLenclud # Date 1471343120 -7200 # Node ID 0c75dec19d39c35fc6b8579ad85da2d47390f25b # Parent 2b9541e54f7d97664a02601ec4a86517d0306249 Ear cleanup. diff -r 2b9541e54f7d -r 0c75dec19d39 Server/ConsumerElectronicControl.cs --- a/Server/ConsumerElectronicControl.cs Mon Aug 15 12:11:26 2016 +0200 +++ b/Server/ConsumerElectronicControl.cs Tue Aug 16 12:25:20 2016 +0200 @@ -82,14 +82,14 @@ { MonitorPowerOn = true; //Trigger corresponding event thus executing associated actions - ManagerEventAction.Current.TriggerEvent(); + Manager.Current.TriggerEvent(); } private void OnMonitorPowerOff() { MonitorPowerOn = false; //Trigger corresponding event thus executing associated actions - ManagerEventAction.Current.TriggerEvent(); + Manager.Current.TriggerEvent(); } /// diff -r 2b9541e54f7d -r 0c75dec19d39 Server/FormMain.cs --- a/Server/FormMain.cs Mon Aug 15 12:11:26 2016 +0200 +++ b/Server/FormMain.cs Tue Aug 16 12:25:20 2016 +0200 @@ -46,7 +46,7 @@ using SharpDisplay; using MiniDisplayInterop; using SharpLib.Display; -using SharpLib.Ear; +using Ear = SharpLib.Ear; namespace SharpDisplayManager { @@ -80,7 +80,7 @@ [System.ComponentModel.DesignerCategory("Form")] public partial class FormMain : FormMainHid, IMMNotificationClient { - //public ManagerEventAction iManager = new ManagerEventAction(); + //public Manager iManager = new Manager(); DateTime LastTickTime; Display iDisplay; System.Drawing.Bitmap iBmp; @@ -141,18 +141,18 @@ public FormMain() { - ManagerEventAction.Current = Properties.Settings.Default.Events; - if (ManagerEventAction.Current == null) + Ear.Manager.Current = Properties.Settings.Default.Events; + if (Ear.Manager.Current == null) { //No actions in our settings yet - ManagerEventAction.Current = new ManagerEventAction(); - Properties.Settings.Default.Events = ManagerEventAction.Current; + Ear.Manager.Current = new Ear.Manager(); + Properties.Settings.Default.Events = Ear.Manager.Current; } else { //We loaded actions from our settings //We need to hook them with corresponding events - ManagerEventAction.Current.Init(); + Ear.Manager.Current.Init(); } iSkipFrameRendering = false; iClosing = false; @@ -322,14 +322,14 @@ buttonActionAdd.Enabled = false; buttonActionDelete.Enabled = false; - Event currentEvent = CurrentEvent(); - SharpLib.Ear.Action currentAction = CurrentAction(); + Ear.Event currentEvent = CurrentEvent(); + Ear.Action currentAction = CurrentAction(); TreeNode treeNodeToSelect = null; //Reset our tree iTreeViewEvents.Nodes.Clear(); //Populate registered events - foreach (SharpLib.Ear.Event e in ManagerEventAction.Current.Events) + foreach (Ear.Event e in Ear.Manager.Current.Events) { //Create our event node TreeNode eventNode = iTreeViewEvents.Nodes.Add(e.Name); @@ -347,7 +347,7 @@ actionsNodes.ForeColor = eventNode.ForeColor; // Add our actions for that event - foreach (SharpLib.Ear.Action a in e.Actions) + foreach (Ear.Action a in e.Actions) { TreeNode actionNode = actionsNodes.Nodes.Add(a.Brief()); actionNode.Tag = a; @@ -2765,7 +2765,7 @@ /// /// /// - private void SelectEvent(Event aEvent) + private void SelectEvent(Ear.Event aEvent) { if (aEvent == null) { @@ -2788,16 +2788,16 @@ /// Get the current event based on event tree view selection. /// /// - private Event CurrentEvent() + private Ear.Event CurrentEvent() { //Walk up the tree from the selected node to find our event TreeNode node = iTreeViewEvents.SelectedNode; - Event selectedEvent = null; + Ear.Event selectedEvent = null; while (node != null) { - if (node.Tag is Event) + if (node.Tag is Ear.Event) { - selectedEvent = (Event) node.Tag; + selectedEvent = (Ear.Event) node.Tag; break; } node = node.Parent; @@ -2810,12 +2810,12 @@ /// Get the current action based on event tree view selection /// /// - private SharpLib.Ear.Action CurrentAction() + private Ear.Action CurrentAction() { TreeNode node = iTreeViewEvents.SelectedNode; - if (node != null && node.Tag is SharpLib.Ear.Action) + if (node != null && node.Tag is Ear.Action) { - return (SharpLib.Ear.Action) node.Tag; + return (Ear.Action) node.Tag; } return null; @@ -2828,20 +2828,20 @@ /// private void buttonActionAdd_Click(object sender, EventArgs e) { - Event selectedEvent = CurrentEvent(); + Ear.Event selectedEvent = CurrentEvent(); if (selectedEvent == null) { //We did not find a corresponding event return; } - FormEditObject ea = new FormEditObject(); + FormEditObject ea = new FormEditObject(); ea.Text = "Add action"; DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(ea); if (res == DialogResult.OK) { selectedEvent.Actions.Add(ea.Object); - Properties.Settings.Default.Events = ManagerEventAction.Current; + Properties.Settings.Default.Events = Ear.Manager.Current; Properties.Settings.Default.Save(); PopulateEventsTreeView(); } @@ -2854,15 +2854,15 @@ /// private void buttonActionEdit_Click(object sender, EventArgs e) { - Event selectedEvent = CurrentEvent(); - SharpLib.Ear.Action selectedAction = CurrentAction(); + Ear.Event selectedEvent = CurrentEvent(); + Ear.Action selectedAction = CurrentAction(); if (selectedEvent == null || selectedAction == null) { //We did not find a corresponding event return; } - FormEditObject ea = new FormEditObject(); + FormEditObject ea = new FormEditObject(); ea.Text = "Edit action"; ea.Object = selectedAction; int actionIndex = iTreeViewEvents.SelectedNode.Index; @@ -2872,7 +2872,7 @@ //Update our action selectedEvent.Actions[actionIndex]=ea.Object; //Save and rebuild our event tree view - Properties.Settings.Default.Events = ManagerEventAction.Current; + Properties.Settings.Default.Events = Ear.Manager.Current; Properties.Settings.Default.Save(); PopulateEventsTreeView(); } @@ -2886,15 +2886,15 @@ private void buttonActionDelete_Click(object sender, EventArgs e) { - SharpLib.Ear.Action action = CurrentAction(); + Ear.Action action = CurrentAction(); if (action == null) { //Must select action node return; } - ManagerEventAction.Current.RemoveAction(action); - Properties.Settings.Default.Events = ManagerEventAction.Current; + Ear.Manager.Current.RemoveAction(action); + Properties.Settings.Default.Events = Ear.Manager.Current; Properties.Settings.Default.Save(); PopulateEventsTreeView(); } @@ -2906,7 +2906,7 @@ /// private void buttonActionTest_Click(object sender, EventArgs e) { - SharpLib.Ear.Action a = CurrentAction(); + Ear.Action a = CurrentAction(); if (a != null) { a.Test(); @@ -2921,7 +2921,7 @@ /// private void buttonActionMoveUp_Click(object sender, EventArgs e) { - SharpLib.Ear.Action a = CurrentAction(); + Ear.Action a = CurrentAction(); if (a == null || //Action already at the top of the list iTreeViewEvents.SelectedNode.Index == 0) @@ -2930,15 +2930,15 @@ } //Swap actions in event's action list - Event currentEvent = CurrentEvent(); + Ear.Event currentEvent = CurrentEvent(); int currentIndex = iTreeViewEvents.SelectedNode.Index; - SharpLib.Ear.Action movingUp = currentEvent.Actions[currentIndex]; - SharpLib.Ear.Action movingDown = currentEvent.Actions[currentIndex-1]; + Ear.Action movingUp = currentEvent.Actions[currentIndex]; + Ear.Action movingDown = currentEvent.Actions[currentIndex-1]; currentEvent.Actions[currentIndex] = movingDown; currentEvent.Actions[currentIndex-1] = movingUp; //Save and populate our tree again - Properties.Settings.Default.Events = ManagerEventAction.Current; + Properties.Settings.Default.Events = Ear.Manager.Current; Properties.Settings.Default.Save(); PopulateEventsTreeView(); @@ -2951,7 +2951,7 @@ /// private void buttonActionMoveDown_Click(object sender, EventArgs e) { - SharpLib.Ear.Action a = CurrentAction(); + Ear.Action a = CurrentAction(); if (a == null || //Action already at the bottom of the list iTreeViewEvents.SelectedNode.Index == iTreeViewEvents.SelectedNode.Parent.Nodes.Count - 1) @@ -2960,15 +2960,15 @@ } //Swap actions in event's action list - Event currentEvent = CurrentEvent(); + Ear.Event currentEvent = CurrentEvent(); int currentIndex = iTreeViewEvents.SelectedNode.Index; - SharpLib.Ear.Action movingDown = currentEvent.Actions[currentIndex]; - SharpLib.Ear.Action movingUp = currentEvent.Actions[currentIndex + 1]; + Ear.Action movingDown = currentEvent.Actions[currentIndex]; + Ear.Action movingUp = currentEvent.Actions[currentIndex + 1]; currentEvent.Actions[currentIndex] = movingUp; currentEvent.Actions[currentIndex + 1] = movingDown; //Save and populate our tree again - Properties.Settings.Default.Events = ManagerEventAction.Current; + Properties.Settings.Default.Events = Ear.Manager.Current; Properties.Settings.Default.Save(); PopulateEventsTreeView(); } @@ -2981,7 +2981,7 @@ /// private void buttonEventTest_Click(object sender, EventArgs e) { - Event earEvent = CurrentEvent(); + Ear.Event earEvent = CurrentEvent(); if (earEvent != null) { earEvent.Test(); @@ -3013,7 +3013,7 @@ buttonEventEdit.Enabled = CurrentEvent() != null; - SharpLib.Ear.Action currentAction = CurrentAction(); + Ear.Action currentAction = CurrentAction(); //If an action is selected enable the following buttons buttonActionTest.Enabled = buttonActionDelete.Enabled = @@ -3033,13 +3033,13 @@ private void buttonEventAdd_Click(object sender, EventArgs e) { - FormEditObject ea = new FormEditObject(); + FormEditObject ea = new FormEditObject(); ea.Text = "Add event"; DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(ea); if (res == DialogResult.OK) { - ManagerEventAction.Current.Events.Add(ea.Object); - Properties.Settings.Default.Events = ManagerEventAction.Current; + Ear.Manager.Current.Events.Add(ea.Object); + Properties.Settings.Default.Events = Ear.Manager.Current; Properties.Settings.Default.Save(); PopulateEventsTreeView(); SelectEvent(ea.Object); @@ -3048,29 +3048,29 @@ private void buttonEventDelete_Click(object sender, EventArgs e) { - SharpLib.Ear.Event currentEvent = CurrentEvent(); + Ear.Event currentEvent = CurrentEvent(); if (currentEvent == null) { //Must select action node return; } - ManagerEventAction.Current.Events.Remove(currentEvent); - Properties.Settings.Default.Events = ManagerEventAction.Current; + Ear.Manager.Current.Events.Remove(currentEvent); + Properties.Settings.Default.Events = Ear.Manager.Current; Properties.Settings.Default.Save(); PopulateEventsTreeView(); } private void buttonEventEdit_Click(object sender, EventArgs e) { - Event selectedEvent = CurrentEvent(); + Ear.Event selectedEvent = CurrentEvent(); if (selectedEvent == null) { //We did not find a corresponding event return; } - FormEditObject ea = new FormEditObject(); + FormEditObject ea = new FormEditObject(); ea.Text = "Edit event"; ea.Object = selectedEvent; int actionIndex = iTreeViewEvents.SelectedNode.Index; @@ -3078,7 +3078,7 @@ if (res == DialogResult.OK) { //Save and rebuild our event tree view - Properties.Settings.Default.Events = ManagerEventAction.Current; + Properties.Settings.Default.Events = Ear.Manager.Current; Properties.Settings.Default.Save(); PopulateEventsTreeView(); } diff -r 2b9541e54f7d -r 0c75dec19d39 Server/Properties/Settings.Designer.cs --- a/Server/Properties/Settings.Designer.cs Mon Aug 15 12:11:26 2016 +0200 +++ b/Server/Properties/Settings.Designer.cs Tue Aug 16 12:25:20 2016 +0200 @@ -156,9 +156,9 @@ [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - public global::SharpLib.Ear.ManagerEventAction Events { + public global::SharpLib.Ear.Manager Events { get { - return ((global::SharpLib.Ear.ManagerEventAction)(this["Events"])); + return ((global::SharpLib.Ear.Manager)(this["Events"])); } set { this["Events"] = value; diff -r 2b9541e54f7d -r 0c75dec19d39 Server/Properties/Settings.settings --- a/Server/Properties/Settings.settings Mon Aug 15 12:11:26 2016 +0200 +++ b/Server/Properties/Settings.settings Tue Aug 16 12:25:20 2016 +0200 @@ -35,7 +35,7 @@ True - + diff -r 2b9541e54f7d -r 0c75dec19d39 SharpLibEar/Manager.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SharpLibEar/Manager.cs Tue Aug 16 12:25:20 2016 +0200 @@ -0,0 +1,81 @@ +// +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Reflection; +using System.Runtime.Serialization; +using SharpLib.Utils; + +namespace SharpLib.Ear +{ + /// + /// Event Action Router (Ear) is a generic and extensible framework allowing users to execute actions in response to events. + /// Users can implement their own events and actions. + /// + [TypeConverter(typeof(TypeConverterJson))] + [DataContract] + public class Manager + { + /// + /// Access the currently installed EAR manager. + /// + public static Manager Current = null; + + /// + /// Our events instances. + /// + [DataMember] + public List Events; + + /// + /// Constructor + /// + public Manager() + { + Init(); + } + + /// + /// Executes after internalization took place. + /// + public void Init() + { + if (Events == null) + { + Events = new List(); + } + + } + + /// + /// Trigger the given event. + /// + /// + public void TriggerEvent() where T: class + { + //Only trigger enabled events matching the desired type + foreach (Event e in Events.Where(e => e.GetType() == typeof(T) && e.Enabled)) + { + e.Trigger(); + } + } + + + /// + /// Remove the specified action from the event it belongs too. + /// + /// + public void RemoveAction(Action aAction) + { + foreach (Event e in Events) + { + if (e.Actions.Remove(aAction)) + { + //We removed our action, we are done here. + return; + } + } + } + } +} \ No newline at end of file diff -r 2b9541e54f7d -r 0c75dec19d39 SharpLibEar/ManagerEventAction.cs --- a/SharpLibEar/ManagerEventAction.cs Mon Aug 15 12:11:26 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,78 +0,0 @@ -// - - -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Reflection; -using System.Runtime.Serialization; -using SharpLib.Utils; - -namespace SharpLib.Ear -{ - [TypeConverter(typeof(TypeConverterJson))] - [DataContract] - public class ManagerEventAction - { - public static ManagerEventAction Current = null; - //public IDictionary ActionTypes; - //public IDictionary EventTypes; - - /// - /// Our events instances. - /// - [DataMember] - public List Events; - - - - - public ManagerEventAction() - { - Init(); - } - - /// - /// Executes after internalization took place. - /// - public void Init() - { - if (Events == null) - { - Events = new List(); - } - - } - - /// - /// - /// - /// - public void TriggerEvent() where T: class - { - //Only trigger enabled events matching the desired type - foreach (Event e in Events.Where(e => e.GetType() == typeof(T) && e.Enabled)) - { - e.Trigger(); - } - } - - - /// - /// - /// - /// - public void RemoveAction(Action aAction) - { - foreach (Event e in Events) - { - if (e.Actions.Remove(aAction)) - { - //We removed our action, we are done here. - return; - } - } - } - } -} \ No newline at end of file diff -r 2b9541e54f7d -r 0c75dec19d39 SharpLibEar/SharpLibEar.csproj --- a/SharpLibEar/SharpLibEar.csproj Mon Aug 15 12:11:26 2016 +0200 +++ b/SharpLibEar/SharpLibEar.csproj Tue Aug 16 12:25:20 2016 +0200 @@ -68,7 +68,7 @@ - +