using Systemm = System;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace Trigger.Events
{
///
/// This takes care of all available s and provides access to instances of them when a needs it
///
public class Manager
{
#region Properties
/// A list of the s
private Dictionary EventPluginInstances = new Dictionary();
private List EventPluginInstancesAll = new List();
private List EventPluginTypes = new List();
///
/// The type of the event
///
public enum EventTypes
{
/// A simple event with no further information
Simple,
/// An event which gives only one value (like a new item's name)
SingleValue,
/// An event which gives the previous and post status of a value
ChangingValue,
}
private Log Log;
#endregion
#region Constructors
///
///
public Manager(Main Main)
{
this.Log = Main.Log;
List types = new List(Systemm.Reflection.Assembly.GetExecutingAssembly().GetTypes());
this.EventPluginTypes = types.FindAll(new Systemm.Predicate(item => { return item.BaseType == typeof(EventPlugin) && item.Namespace == "Trigger.Events"; }));
}
#endregion
#region Methods
///
/// Checks whether the with the specified exists
///
///
///
public bool PluginExists(Type type)
{
return this.EventPluginTypes.Contains(type);
}
///
/// Checks whether the exists
///
///
///
public bool PluginExists() where T : EventPlugin
{
return this.EventPluginTypes.Contains(typeof(T));
}
///
/// Searches for an existing instance of the . If non is found, a new one is created.
///
/// The that shall be loaded
///
/// The specified does not exist!
public T GetPlugin() where T : EventPlugin
{
return this.GetPlugin(null, true);
}
///
/// Creates a new instance of the specified passing the specified
///
/// Additional arguments that shall be passed to the constructor
/// The that shall be loaded
///
/// The specified does not exist!
public T GetPlugin(object[] args) where T:EventPlugin
{
return GetPlugin(args, false);
}
///
/// Returns an instance of the passing the specified
///
/// Additional arguments that shall be passed to the constructor
/// Whether an existing instance of this class shall be reused.
/// The that shall be loaded
///
/// The specified does not exist!
public T GetPlugin(object[] args, bool reuseExisting) where T : EventPlugin
{
Type primKey = typeof(T);
if (this.PluginExists(primKey) == false)
throw new ArgumentException("The EventPlugin \"" + primKey.Name + "\" does not exist!", "name");
if (reuseExisting && this.EventPluginInstances.ContainsKey(primKey))
return (T)this.EventPluginInstances[primKey];
#if DEBUG
Systemm.Diagnostics.Stopwatch swInitEvent = new Systemm.Diagnostics.Stopwatch();
swInitEvent.Start();
#endif
T plugin = (T)Systemm.Activator.CreateInstance(primKey, args);
if (!this.EventPluginInstances.ContainsKey(primKey)) // do we really want to store the plugin?
this.EventPluginInstances.Add(primKey, plugin);
this.EventPluginInstancesAll.Add(plugin);
#if DEBUG
swInitEvent.Stop();
this.Log.LogLine("Loaded Event plugin \"" + primKey.Name + "\" in " + swInitEvent.ElapsedMilliseconds + "ms", Log.Type.Other);
#else
this.Log.LogLine("Loaded Event plugin \"" + primKey.Name + "\"", Log.Type.Other);
#endif
return plugin;
}
///
/// Returns the current status of this
/// This includes the lists with available and with loaded s
///
///
public TreeNode GetStatus(TreeView tv)
{
TreeNode tnMain = new TreeNode("Event Manager");
TreeNode tnTypes = new TreeNode("Available types (" + this.EventPluginTypes.Count + ")");
tnMain.Nodes.Add(tnTypes);
this.EventPluginTypes.ForEach(new Action((item) =>
tnTypes.Nodes.Add(new TreeNode(item.Name))
));
TreeNode tnClasses = new TreeNode("Loaded classes (" + this.EventPluginInstancesAll.Count + ")");
tnMain.Nodes.Add(tnClasses);
foreach (EventPlugin ep in this.EventPluginInstancesAll)
tnClasses.Nodes.Add(ep.GetStatus());
return tnMain;
}
#endregion
}
}