Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Async/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Utils/WeakEventManagerBase.cs @ 13401

Last change on this file since 13401 was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 3.6 KB
Line 
1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4// software and associated documentation files (the "Software"), to deal in the Software
5// without restriction, including without limitation the rights to use, copy, modify, merge,
6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7// to whom the Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.Diagnostics;
21using System.Windows;
22
23namespace ICSharpCode.AvalonEdit.Utils
24{
25  /// <summary>
26  /// WeakEventManager with AddListener/RemoveListener and CurrentManager implementation.
27  /// Helps implementing the WeakEventManager pattern with less code.
28  /// </summary>
29  public abstract class WeakEventManagerBase<TManager, TEventSource> : WeakEventManager
30    where TManager : WeakEventManagerBase<TManager, TEventSource>, new()
31    where TEventSource : class
32  {
33    /// <summary>
34    /// Creates a new WeakEventManagerBase instance.
35    /// </summary>
36    protected WeakEventManagerBase()
37    {
38      Debug.Assert(GetType() == typeof(TManager));
39    }
40   
41    /// <summary>
42    /// Adds a weak event listener.
43    /// </summary>
44    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
45    public static void AddListener(TEventSource source, IWeakEventListener listener)
46    {
47      CurrentManager.ProtectedAddListener(source, listener);
48    }
49   
50    /// <summary>
51    /// Removes a weak event listener.
52    /// </summary>
53    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
54    public static void RemoveListener(TEventSource source, IWeakEventListener listener)
55    {
56      CurrentManager.ProtectedRemoveListener(source, listener);
57    }
58   
59    /// <inheritdoc/>
60    protected sealed override void StartListening(object source)
61    {
62      if (source == null)
63        throw new ArgumentNullException("source");
64      StartListening((TEventSource)source);
65    }
66   
67    /// <inheritdoc/>
68    protected sealed override void StopListening(object source)
69    {
70      if (source == null)
71        throw new ArgumentNullException("source");
72      StopListening((TEventSource)source);
73    }
74   
75    /// <summary>
76    /// Attaches the event handler.
77    /// </summary>
78    protected abstract void StartListening(TEventSource source);
79   
80    /// <summary>
81    /// Detaches the event handler.
82    /// </summary>
83    protected abstract void StopListening(TEventSource source);
84   
85    /// <summary>
86    /// Gets the current manager.
87    /// </summary>
88    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods")]
89    protected static TManager CurrentManager {
90      get {
91        Type managerType = typeof(TManager);
92        TManager manager = (TManager)GetCurrentManager(managerType);
93        if (manager == null) {
94          manager = new TManager();
95          SetCurrentManager(managerType, manager);
96        }
97        return manager;
98      }
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.