92 lines
3 KiB
C#
92 lines
3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using JD.TeaBoxAddinSystem.Addins.Description;
|
|
|
|
namespace JD.TeaBoxAddinSystem.Addins.Internal
|
|
{
|
|
using ExtensionChangedEventHandler = EventHandler<ExtensionNodeChangedEventArgs>;
|
|
|
|
internal class ChangedHandlerManager
|
|
{
|
|
#region Variables
|
|
|
|
private Dictionary<string, ExtensionChangedEventHandler> m_handlers = new Dictionary<string, ExtensionChangedEventHandler>();
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
/// <summary>
|
|
/// Adds an handler for the given path.
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <param name="handler"></param>
|
|
public void Add(string path, ExtensionChangedEventHandler handler)
|
|
{
|
|
System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(path));
|
|
System.Diagnostics.Debug.Assert(handler != null);
|
|
// Path should already be normalized.
|
|
System.Diagnostics.Debug.Assert(path == ExtensionPoint.NormalizePath(path));
|
|
|
|
ExtensionChangedEventHandler existingHandler;
|
|
m_handlers.TryGetValue(path, out existingHandler);
|
|
existingHandler += handler;
|
|
m_handlers[path] = existingHandler;
|
|
|
|
// Trigger it right now if the node already exists
|
|
TreeNode existingNode = AddinManager.ExtensionTree[path];
|
|
if (existingNode != null)
|
|
{
|
|
handler(null, new ExtensionNodeChangedEventArgs(existingNode));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes an handler for the given path.
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <param name="handler"></param>
|
|
public void Remove(string path, ExtensionChangedEventHandler handler)
|
|
{
|
|
System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(path));
|
|
System.Diagnostics.Debug.Assert(handler != null);
|
|
// Path should already be normalized.
|
|
System.Diagnostics.Debug.Assert(path == ExtensionPoint.NormalizePath(path));
|
|
|
|
ExtensionChangedEventHandler existingHandler;
|
|
if (m_handlers.TryGetValue(path, out existingHandler))
|
|
{
|
|
existingHandler -= handler;
|
|
|
|
if (existingHandler == null)
|
|
{
|
|
m_handlers.Remove(path);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clears all the handlers.
|
|
/// </summary>
|
|
public void Clear()
|
|
{
|
|
m_handlers.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Triggers a changed event.
|
|
/// </summary>
|
|
/// <param name="e"></param>
|
|
public void OnChanged(ExtensionNodeChangedEventArgs e)
|
|
{
|
|
ExtensionChangedEventHandler existingHandler;
|
|
if (m_handlers.TryGetValue(e.Path, out existingHandler))
|
|
{
|
|
System.Diagnostics.Debug.Assert(existingHandler != null);
|
|
existingHandler(null, e);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|