From fc8a05358a92cc3c77c5f1e30d536807ef0614fd Mon Sep 17 00:00:00 2001 From: Victoria Plitt Date: Mon, 8 Apr 2019 13:49:55 +0300 Subject: were added scripting projects --- .../Utils/WeakEventManagerBase.cs | 86 ++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 Software/Visual_Studio/Scripting/Tango.Scripting.Editors/Utils/WeakEventManagerBase.cs (limited to 'Software/Visual_Studio/Scripting/Tango.Scripting.Editors/Utils/WeakEventManagerBase.cs') diff --git a/Software/Visual_Studio/Scripting/Tango.Scripting.Editors/Utils/WeakEventManagerBase.cs b/Software/Visual_Studio/Scripting/Tango.Scripting.Editors/Utils/WeakEventManagerBase.cs new file mode 100644 index 000000000..87e4de515 --- /dev/null +++ b/Software/Visual_Studio/Scripting/Tango.Scripting.Editors/Utils/WeakEventManagerBase.cs @@ -0,0 +1,86 @@ +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) + +using System; +using System.Diagnostics; +using System.Windows; + +namespace Tango.Scripting.Editors.Utils +{ + /// + /// WeakEventManager with AddListener/RemoveListener and CurrentManager implementation. + /// Helps implementing the WeakEventManager pattern with less code. + /// + public abstract class WeakEventManagerBase : WeakEventManager + where TManager : WeakEventManagerBase, new() + where TEventSource : class + { + /// + /// Creates a new WeakEventManagerBase instance. + /// + protected WeakEventManagerBase() + { + Debug.Assert(GetType() == typeof(TManager)); + } + + /// + /// Adds a weak event listener. + /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")] + public static void AddListener(TEventSource source, IWeakEventListener listener) + { + CurrentManager.ProtectedAddListener(source, listener); + } + + /// + /// Removes a weak event listener. + /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")] + public static void RemoveListener(TEventSource source, IWeakEventListener listener) + { + CurrentManager.ProtectedRemoveListener(source, listener); + } + + /// + protected sealed override void StartListening(object source) + { + if (source == null) + throw new ArgumentNullException("source"); + StartListening((TEventSource)source); + } + + /// + protected sealed override void StopListening(object source) + { + if (source == null) + throw new ArgumentNullException("source"); + StopListening((TEventSource)source); + } + + /// + /// Attaches the event handler. + /// + protected abstract void StartListening(TEventSource source); + + /// + /// Detaches the event handler. + /// + protected abstract void StopListening(TEventSource source); + + /// + /// Gets the current manager. + /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods")] + protected static TManager CurrentManager { + get { + Type managerType = typeof(TManager); + TManager manager = (TManager)GetCurrentManager(managerType); + if (manager == null) { + manager = new TManager(); + SetCurrentManager(managerType, manager); + } + return manager; + } + } + } +} -- cgit v1.3.1