using System; using System.Collections.Generic; using JD.TeaBoxAddinSystem.Addins.Description; namespace JD.TeaBoxAddinSystem.Addins.Internal { using ExtensionChangedEventHandler = EventHandler; internal class ChangedHandlerManager { #region Variables private Dictionary m_handlers = new Dictionary(); #endregion #region Methods /// /// Adds an handler for the given path. /// /// /// 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)); } } /// /// Removes an handler for the given path. /// /// /// 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); } } } /// /// Clears all the handlers. /// public void Clear() { m_handlers.Clear(); } /// /// Triggers a changed event. /// /// 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 } }