-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunctionalEventManager.cs
295 lines (245 loc) · 13.4 KB
/
FunctionalEventManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FunctionalTreeLibrary
{
public class FunctionalEventManager
{
#region Variables
internal static FunctionalEventManager Instance { get; private set; }
private Dictionary<Type, List<FunctionalEvent>> FunctionalEventsInternal { get; set; }
private Dictionary<FunctionalEvent, Dictionary<IFunctionalTreeElement, List<FunctionalEventHandlerContainer>>> HandlerListInternal { get; set; }
#endregion
static FunctionalEventManager()
{
Instance = new FunctionalEventManager();
}
private FunctionalEventManager()
{
FunctionalEventsInternal = new Dictionary<Type, List<FunctionalEvent>>();
HandlerListInternal = new Dictionary<FunctionalEvent, Dictionary<IFunctionalTreeElement, List<FunctionalEventHandlerContainer>>>();
}
/// <summary>
/// Registers a new functional event.
/// </summary>
/// <param name="name">The name of the functional event. The name must be unique within the owner type and cannot be null or an empty string.</param>
/// <param name="functionalStretegy">The functional strategy of the event as a value of the enumeration.</param>
/// <param name="handlerType">The type of the event handler. This must be a delegate type and cannot be null.</param>
/// <param name="ownerType">The owner class type of the routed event. This cannot be null.</param>
private FunctionalEvent RegisterEventInternal(string name, FunctionalStrategy functionalStrategy, Type handlerType, Type ownerType)
{
if (String.IsNullOrEmpty(name))
throw new ArgumentNullException("name", "name is null.");
if (handlerType == null)
throw new ArgumentNullException("handlerType", "handlerType is null.");
if (ownerType == null)
throw new ArgumentNullException("ownerType", "ownerType is null.");
if (FunctionalEventsInternal.ContainsKey(ownerType))
{
if (FunctionalEventsInternal[ownerType].Any(cur => cur.Name == name))
throw new ArgumentException(String.Format("RoutedEvent Name '{0}' for OwnerType '{1}' already used.", name, ownerType));
}
FunctionalEvent functionalEvent = new FunctionalEvent(name, functionalStrategy, handlerType, ownerType);
if (!FunctionalEventsInternal.ContainsKey(ownerType))
FunctionalEventsInternal[ownerType] = new List<FunctionalEvent>();
FunctionalEventsInternal[ownerType].Add(functionalEvent);
return functionalEvent;
}
internal void AddHandler(FunctionalEvent functionalEvent, IFunctionalTreeElement element, Delegate handler, bool handledEventsToo = false)
{
if (functionalEvent == null)
throw new ArgumentNullException("functionalEvent", "functionalEvent is null.");
if (element == null)
throw new ArgumentNullException("element", "element is null.");
if (handler == null)
throw new ArgumentNullException("handler", "handler is null.");
if (handler.GetType() != functionalEvent.HandlerType && handler.GetType() != typeof(FunctionalEventHandler))
throw new ArgumentException("handler", "Handler type mismatched.");
if (!HandlerListInternal.ContainsKey(functionalEvent))
{
HandlerListInternal[functionalEvent] = new Dictionary<IFunctionalTreeElement, List<FunctionalEventHandlerContainer>>();
}
if (!HandlerListInternal[functionalEvent].ContainsKey(element))
{
HandlerListInternal[functionalEvent][element] = new List<FunctionalEventHandlerContainer>();
}
GetHandlerList(functionalEvent, element).Add(new FunctionalEventHandlerContainer(handler, handledEventsToo));
}
internal void RemoveHandler(FunctionalEvent functionalEvent, IFunctionalTreeElement element, Delegate handler)
{
if (functionalEvent == null)
throw new ArgumentNullException("functionalEvent", "functionalEvent is null.");
if (element == null)
throw new ArgumentNullException("element", "element is null.");
if (handler == null)
throw new ArgumentNullException("handler", "handler is null.");
List<FunctionalEventHandlerContainer> handlerList = GetHandlerList(functionalEvent, element);
if (handlerList != null)
handlerList.RemoveAll(cur => cur.Handler == handler);
}
internal List<FunctionalEventHandlerContainer> GetHandlerList(FunctionalEvent functionalEvent, IFunctionalTreeElement element)
{
if (HandlerListInternal.ContainsKey(functionalEvent) && HandlerListInternal[functionalEvent].ContainsKey(element))
return HandlerListInternal[functionalEvent][element];
return null;
}
internal void RaiseEvent(FunctionalEventArgs eventArgs, Predicate<IFunctionalTreeElement> predicate = null)
{
if (eventArgs == null)
throw new ArgumentNullException("eventArgs", "eventArgs is null.");
if (eventArgs.FunctionalEvent == null)
throw new ArgumentNullException("eventArgs", "eventArgs is null.");
if (!FunctionalEventsInternal.ContainsKey(eventArgs.FunctionalEvent.OwnerType))
return;
FunctionalEvent functionalEvent = eventArgs.FunctionalEvent;
FunctionalElementRoute route = null;
switch (functionalEvent.FunctionalStretegy)
{
case FunctionalStrategy.Bubble:
route = BuildBubbleEventRoute(eventArgs.Source, eventArgs);
break;
case FunctionalStrategy.Tunnel:
route = BuildTunnelEventRoute(eventArgs.Source, eventArgs);
break;
case FunctionalStrategy.Spread:
route = BuildSpreadEventRoute(eventArgs.Source, eventArgs);
break;
case FunctionalStrategy.Children:
route = BuildChildrenEventRoute(eventArgs.Source, eventArgs);
break;
case FunctionalStrategy.Descendents:
route = BuildDescendentsEventRoute(eventArgs.Source, eventArgs);
break;
case FunctionalStrategy.Siblings:
route = BuildSiblingsEventRoute(eventArgs.Source, eventArgs);
break;
case FunctionalStrategy.Parent:
route = BuildParentEventRoute(eventArgs.Source, eventArgs);
break;
}
FunctionalEventTracingArgs tracingEventArgs = new FunctionalEventTracingArgs(route);
foreach (IFunctionalTreeElement element in route)
{
tracingEventArgs.UpdateCurrentIndex(route.IndexOf(element));
RaiseEventOnElement(element, eventArgs, tracingEventArgs, predicate);
if (tracingEventArgs.Abort)
break;
}
}
/// <summary>
/// The event will be raised only on objects of type T.
/// </summary>
internal void RaiseEventPair(FunctionalEvent tunnelEvent, FunctionalEvent bubbleEvent, FunctionalEventArgs eventArgs, Predicate<IFunctionalTreeElement> predicate = null)
{
if (tunnelEvent == null)
throw new ArgumentNullException("tunnelEvent", "tunnelEvent is null.");
if (bubbleEvent == null)
throw new ArgumentNullException("bubbleEvent", "bubbleEvent is null.");
if (eventArgs == null)
throw new ArgumentNullException("eventArgs", "eventArgs is null.");
if (tunnelEvent.FunctionalStretegy != FunctionalStrategy.Tunnel)
throw new ArgumentNullException("tunnelEvent", "tunnelEvent is not tunnel.");
if (bubbleEvent.FunctionalStretegy != FunctionalStrategy.Bubble)
throw new ArgumentNullException("bubbleEvent", "bubbleEvent is bubble.");
if (!FunctionalEventsInternal.ContainsKey(eventArgs.FunctionalEvent.OwnerType))
return;
eventArgs.FunctionalEvent = tunnelEvent;
RaiseEvent(eventArgs, predicate);
eventArgs.FunctionalEvent = bubbleEvent;
RaiseEvent(eventArgs, predicate);
}
private FunctionalElementRoute BuildBubbleEventRoute(IFunctionalTreeElement element, FunctionalEventArgs eventArgs)
{
FunctionalElementRoute route = new FunctionalElementRoute();
IFunctionalTreeElement curElement = element;
while (curElement != null)
{
route.Add(curElement);
curElement = FunctionalTreeHelper.GetFunctionalParent(curElement);
}
return route;
}
private FunctionalElementRoute BuildParentEventRoute(IFunctionalTreeElement element, FunctionalEventArgs eventArgs)
{
FunctionalElementRoute route = new FunctionalElementRoute();
route.Add(FunctionalTreeHelper.GetFunctionalParent(element));
return route;
}
private FunctionalElementRoute BuildTunnelEventRoute(IFunctionalTreeElement element, FunctionalEventArgs eventArgs)
{
FunctionalElementRoute route = BuildBubbleEventRoute(element, eventArgs);
route.Reverse();
return route;
}
private FunctionalElementRoute BuildSpreadEventRoute(IFunctionalTreeElement element, FunctionalEventArgs eventArgs)
{
FunctionalElementRoute route = new FunctionalElementRoute();
IFunctionalTreeElement treeRoot = FunctionalTreeHelper.GetFunctionalTree(element).Root;
route.Add(treeRoot);
route.AddRange(GetDescendents(treeRoot));
return route;
}
private FunctionalElementRoute BuildChildrenEventRoute(IFunctionalTreeElement element, FunctionalEventArgs eventArgs)
{
FunctionalElementRoute route = new FunctionalElementRoute();
route.AddRange(FunctionalTreeHelper.GetFunctionalChildren(element));
return route;
}
private FunctionalElementRoute BuildDescendentsEventRoute(IFunctionalTreeElement element, FunctionalEventArgs eventArgs)
{
FunctionalElementRoute route = new FunctionalElementRoute();
route.AddRange(GetDescendents(element));
return route;
}
private FunctionalElementRoute BuildSiblingsEventRoute(IFunctionalTreeElement element, FunctionalEventArgs eventArgs)
{
FunctionalElementRoute route = new FunctionalElementRoute();
IFunctionalTreeElement parent = FunctionalTreeHelper.GetFunctionalParent(element);
if (parent == null)
return route;
route.AddRange(FunctionalTreeHelper.GetFunctionalChildren(parent).Where(cur => cur != element));
return route;
}
private IEnumerable<IFunctionalTreeElement> GetDescendents(IFunctionalTreeElement element)
{
List<IFunctionalTreeElement> descendents = new List<IFunctionalTreeElement>();
foreach (IFunctionalTreeElement item in FunctionalTreeHelper.GetFunctionalChildren(element))
{
descendents.Add(item);
descendents.AddRange(GetDescendents(item));
}
return descendents;
}
private void RaiseEventOnElement(IFunctionalTreeElement element,
FunctionalEventArgs eventArgs,
FunctionalEventTracingArgs tracingEventArgs,
Predicate<IFunctionalTreeElement> predicate = null)
{
if (element == null || eventArgs == null || (predicate != null && !predicate(element)))
return;
List<FunctionalEventHandlerContainer> eventHandlerList = GetHandlerList(eventArgs.FunctionalEvent, element);
if (eventHandlerList != null && eventHandlerList.Count() > 0)
{
foreach (FunctionalEventHandlerContainer handlerContainer in eventHandlerList)
{
if (!eventArgs.Handled || handlerContainer.HandledEventsToo)
{
eventArgs.FunctionalEvent.EventTracer.RaiseEventRaising(eventArgs.FunctionalEvent, tracingEventArgs);
if (tracingEventArgs.Abort)
return;
foreach (Delegate handler in handlerContainer.Handler.GetInvocationList())
handler.Method.Invoke(element, new object[] { element, eventArgs });
eventArgs.FunctionalEvent.EventTracer.RaiseEventRaised(eventArgs.FunctionalEvent, tracingEventArgs);
if (tracingEventArgs.Abort)
return;
}
}
}
}
public static FunctionalEvent RegisterEvent(string name, FunctionalStrategy functionalStrategy, Type handlerType, Type ownerType)
{
return Instance.RegisterEventInternal(name, functionalStrategy, handlerType, ownerType);
}
}
}