-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathTwoListSynchronizer.cs
276 lines (239 loc) · 10.4 KB
/
TwoListSynchronizer.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
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Windows;
namespace FunctionalFun.UI.Behaviours
{
/// <summary>
/// Keeps two lists synchronized.
/// </summary>
public class TwoListSynchronizer : IWeakEventListener
{
private static readonly IListItemConverter DefaultConverter = new DoNothingListItemConverter();
private readonly IList _masterList;
private readonly IListItemConverter _masterTargetConverter;
private readonly IList _targetList;
/// <summary>
/// Initializes a new instance of the <see cref="TwoListSynchronizer"/> class.
/// </summary>
/// <param name="masterList">The master list.</param>
/// <param name="targetList">The target list.</param>
/// <param name="masterTargetConverter">The master-target converter.</param>
public TwoListSynchronizer(IList masterList, IList targetList, IListItemConverter masterTargetConverter)
{
_masterList = masterList;
_targetList = targetList;
_masterTargetConverter = masterTargetConverter;
}
/// <summary>
/// Initializes a new instance of the <see cref="TwoListSynchronizer"/> class.
/// </summary>
/// <param name="masterList">The master list.</param>
/// <param name="targetList">The target list.</param>
public TwoListSynchronizer(IList masterList, IList targetList)
: this(masterList, targetList, DefaultConverter)
{
}
private delegate void ChangeListAction(IList list, NotifyCollectionChangedEventArgs e, Converter<object, object> converter);
/// <summary>
/// Starts synchronizing the lists.
/// </summary>
public void StartSynchronizing()
{
ListenForChangeEvents(_masterList);
ListenForChangeEvents(_targetList);
// Update the Target list from the Master list
SetListValuesFromSource(_masterList, _targetList, ConvertFromMasterToTarget);
// In some cases the target list might have its own view on which items should included:
// so update the master list from the target list
// (This is the case with a ListBox SelectedItems collection: only items from the ItemsSource can be included in SelectedItems)
if (!TargetAndMasterCollectionsAreEqual())
{
SetListValuesFromSource(_targetList, _masterList, ConvertFromTargetToMaster);
}
}
/// <summary>
/// Stop synchronizing the lists.
/// </summary>
public void StopSynchronizing()
{
StopListeningForChangeEvents(_masterList);
StopListeningForChangeEvents(_targetList);
}
/// <summary>
/// Receives events from the centralized event manager.
/// </summary>
/// <param name="managerType">The type of the <see cref="T:System.Windows.WeakEventManager"/> calling this method.</param>
/// <param name="sender">Object that originated the event.</param>
/// <param name="e">Event data.</param>
/// <returns>
/// true if the listener handled the event. It is considered an error by the <see cref="T:System.Windows.WeakEventManager"/> handling in WPF to register a listener for an event that the listener does not handle. Regardless, the method should return false if it receives an event that it does not recognize or handle.
/// </returns>
public bool ReceiveWeakEvent(Type managerType, object sender, EventArgs e)
{
HandleCollectionChanged(sender as IList, e as NotifyCollectionChangedEventArgs);
return true;
}
/// <summary>
/// Listens for change events on a list.
/// </summary>
/// <param name="list">The list to listen to.</param>
protected void ListenForChangeEvents(IList list)
{
if (list is INotifyCollectionChanged)
{
CollectionChangedEventManager.AddListener(list as INotifyCollectionChanged, this);
}
}
/// <summary>
/// Stops listening for change events.
/// </summary>
/// <param name="list">The list to stop listening to.</param>
protected void StopListeningForChangeEvents(IList list)
{
if (list is INotifyCollectionChanged)
{
CollectionChangedEventManager.RemoveListener(list as INotifyCollectionChanged, this);
}
}
private void AddItems(IList list, NotifyCollectionChangedEventArgs e, Converter<object, object> converter)
{
int itemCount = e.NewItems.Count;
for (int i = 0; i < itemCount; i++)
{
int insertionPoint = e.NewStartingIndex + i;
if (insertionPoint > list.Count)
{
list.Add(converter(e.NewItems[i]));
}
else
{
list.Insert(insertionPoint, converter(e.NewItems[i]));
}
}
}
private object ConvertFromMasterToTarget(object masterListItem)
{
return _masterTargetConverter == null ? masterListItem : _masterTargetConverter.Convert(masterListItem);
}
private object ConvertFromTargetToMaster(object targetListItem)
{
return _masterTargetConverter == null ? targetListItem : _masterTargetConverter.ConvertBack(targetListItem);
}
private void HandleCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
IList sourceList = sender as IList;
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
PerformActionOnAllLists(AddItems, sourceList, e);
break;
case NotifyCollectionChangedAction.Move:
PerformActionOnAllLists(MoveItems, sourceList, e);
break;
case NotifyCollectionChangedAction.Remove:
PerformActionOnAllLists(RemoveItems, sourceList, e);
break;
case NotifyCollectionChangedAction.Replace:
PerformActionOnAllLists(ReplaceItems, sourceList, e);
break;
case NotifyCollectionChangedAction.Reset:
UpdateListsFromSource(sender as IList);
break;
default:
break;
}
}
private void MoveItems(IList list, NotifyCollectionChangedEventArgs e, Converter<object, object> converter)
{
RemoveItems(list, e, converter);
AddItems(list, e, converter);
}
private void PerformActionOnAllLists(ChangeListAction action, IList sourceList, NotifyCollectionChangedEventArgs collectionChangedArgs)
{
if (sourceList == _masterList)
{
PerformActionOnList(_targetList, action, collectionChangedArgs, ConvertFromMasterToTarget);
}
else
{
PerformActionOnList(_masterList, action, collectionChangedArgs, ConvertFromTargetToMaster);
}
}
private void PerformActionOnList(IList list, ChangeListAction action, NotifyCollectionChangedEventArgs collectionChangedArgs, Converter<object, object> converter)
{
StopListeningForChangeEvents(list);
action(list, collectionChangedArgs, converter);
ListenForChangeEvents(list);
}
private void RemoveItems(IList list, NotifyCollectionChangedEventArgs e, Converter<object, object> converter)
{
int itemCount = e.OldItems.Count;
// for the number of items being removed, remove the item from the Old Starting Index
// (this will cause following items to be shifted down to fill the hole).
for (int i = 0; i < itemCount; i++)
{
list.RemoveAt(e.OldStartingIndex);
}
}
private void ReplaceItems(IList list, NotifyCollectionChangedEventArgs e, Converter<object, object> converter)
{
RemoveItems(list, e, converter);
AddItems(list, e, converter);
}
private void SetListValuesFromSource(IList sourceList, IList targetList, Converter<object, object> converter)
{
StopListeningForChangeEvents(targetList);
targetList.Clear();
foreach (object o in sourceList)
{
targetList.Add(converter(o));
}
ListenForChangeEvents(targetList);
}
private bool TargetAndMasterCollectionsAreEqual()
{
return _masterList.Cast<object>().SequenceEqual(_targetList.Cast<object>().Select(item => ConvertFromTargetToMaster(item)));
}
/// <summary>
/// Makes sure that all synchronized lists have the same values as the source list.
/// </summary>
/// <param name="sourceList">The source list.</param>
private void UpdateListsFromSource(IList sourceList)
{
if (sourceList == _masterList)
{
SetListValuesFromSource(_masterList, _targetList, ConvertFromMasterToTarget);
}
else
{
SetListValuesFromSource(_targetList, _masterList, ConvertFromTargetToMaster);
}
}
/// <summary>
/// An implementation that does nothing in the conversions.
/// </summary>
internal class DoNothingListItemConverter : IListItemConverter
{
/// <summary>
/// Converts the specified master list item.
/// </summary>
/// <param name="masterListItem">The master list item.</param>
/// <returns>The result of the conversion.</returns>
public object Convert(object masterListItem)
{
return masterListItem;
}
/// <summary>
/// Converts the specified target list item.
/// </summary>
/// <param name="targetListItem">The target list item.</param>
/// <returns>The result of the conversion.</returns>
public object ConvertBack(object targetListItem)
{
return targetListItem;
}
}
}
}