-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCollections.cs
303 lines (256 loc) · 9.02 KB
/
Collections.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
296
297
298
299
300
301
302
303
/***********************************************************************
Copyright 2018 CodeX Enterprises LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Major Changes:
12/2017 0.2 Initial release (Joel Champagne)
***********************************************************************/
using CodexMicroORM.Core;
using CodexMicroORM.Core.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
namespace CodexMicroORM.BindingSupport
{
/// <summary>
/// A GenericBindableSet is an ObservableCollection of DynamicBindable. It resembles a DataTable, as such, since it offers no strong-typed CLR properties to access data.
/// It's flexibility is in that it can be bound to WPF lists easily, as DynamicBindable's implement ICustomTypeProvider.
/// </summary>
public class GenericBindableSet : BindingList<DynamicBindable>, IDisposable, ITypedList
{
private bool _isDirty = false;
public event EventHandler<DirtyStateChangeEventArgs> DirtyStateChange;
public event EventHandler<PropertyChangedEventArgs> RowPropertyChanged;
public Dictionary<string, Type> ExternalSchema
{
get;
set;
} = new Dictionary<string, Type>();
public ServiceScope OwningScope
{
get;
set;
} = CEF.CurrentServiceScope;
public Type BaseItemType
{
get;
set;
}
public bool ScanClean
{
get;
set;
} = false;
internal GenericBindableSet(IEnumerable<DynamicBindable> source) : base(source.ToList())
{
AddTracking(source);
this.AllowNew = true;
this.AllowEdit = true;
this.AllowRemove = true;
}
private void ItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
SetDirty();
RowPropertyChanged?.Invoke(sender, e);
}
public bool IsValid
{
get
{
foreach (var r in this)
{
var ide = r.Wrapped as IDataErrorInfo;
if (ide != null && !string.IsNullOrEmpty(ide.Error))
{
return false;
}
}
return true;
}
}
public bool IsDirty => _isDirty;
protected override object AddNewCore()
{
using (CEF.UseServiceScope(OwningScope))
{
// We rely on construction of a new DynamicBindable that has the same shape as the first item in the collection, if any exist
if (this.Any())
{
var f = this.First();
var wot = f.Wrapped?.GetWrappedObject()?.GetType();
if (wot != null)
{
var no = Activator.CreateInstance(wot);
var wno = CEF.IncludeObject(no, ObjectState.Added);
var nod = wno.AsDynamicBindable();
base.Add(nod);
return nod;
}
}
// If none exist, need to rely on the "default schema" provided
if (BaseItemType != null)
{
var no = Activator.CreateInstance(BaseItemType);
var wno = CEF.IncludeObject(no, ObjectState.Added);
var iw = wno.AsInfraWrapped();
var nod = wno.AsDynamicBindable();
if (ExternalSchema != null)
{
foreach (var e in ExternalSchema)
{
if (!iw.HasProperty(e.Key))
{
iw.SetValue(e.Key, null, e.Value);
}
}
}
base.Add(nod);
return nod;
}
// No default schema? It's an error situation.
throw new InvalidOperationException("Cannot add a new item to the GenericBindableSet collection since there's no object definition available.");
}
}
public void ResetClean()
{
if (_isDirty)
{
DirtyStateChange?.Invoke(this, new DirtyStateChangeEventArgs(false));
_isDirty = false;
}
}
internal void SetDirty()
{
if (!_isDirty)
{
DirtyStateChange?.Invoke(this, new DirtyStateChangeEventArgs(true));
_isDirty = true;
}
}
private void AddTracking(IEnumerable<DynamicBindable> toAdd)
{
foreach (var s in toAdd)
{
s.PropertyChanged += ItemPropertyChanged;
s.DirtyStateChange += S_DirtyStateChange;
}
}
private void S_DirtyStateChange(object sender, Core.Services.DirtyStateChangeEventArgs e)
{
if (ScanClean && e.NewState == Core.ObjectState.Unchanged)
{
if (!(from a in this where a.State != Core.ObjectState.Unchanged select a).Any())
{
ResetClean();
}
}
}
private void RemoveTracking(IEnumerable<DynamicBindable> toRemove)
{
foreach (var s in toRemove)
{
s.PropertyChanged -= ItemPropertyChanged;
s.DirtyStateChange -= S_DirtyStateChange;
}
}
protected override void InsertItem(int index, DynamicBindable item)
{
base.InsertItem(index, item);
AddTracking(new DynamicBindable[] { item });
SetDirty();
}
protected override void RemoveItem(int index)
{
var i = this[index];
RemoveTracking(new DynamicBindable[] { this[index] });
base.RemoveItem(index);
using (CEF.UseServiceScope(OwningScope))
{
var uw = i.Wrapped;
if (uw != null)
{
CEF.DeleteObject(uw);
}
}
SetDirty();
}
protected override void SetItem(int index, DynamicBindable item)
{
bool change = (item != this[index]);
RemoveTracking(new DynamicBindable[] { this[index] });
base.SetItem(index, item);
AddTracking(new DynamicBindable[] { item });
if (change)
{
SetDirty();
}
}
protected override void ClearItems()
{
RemoveTracking(this);
base.ClearItems();
SetDirty();
}
public class DirtyStateChangeEventArgs : EventArgs
{
private bool _newState;
internal DirtyStateChangeEventArgs(bool newState)
{
_newState = newState;
}
public bool IsDirty => _newState;
}
#region IDisposable Support
private bool _disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
RemoveTracking(this);
}
_disposedValue = true;
}
}
public void Dispose()
{
Dispose(true);
}
#endregion
public string GetListName(PropertyDescriptor[] listAccessors)
{
return typeof(GenericBindableSet).Name;
}
public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
{
PropertyDescriptorCollection pdc;
if (this.Count > 0)
{
// If we have data, use the underlying data
var i = this[0];
pdc = ((ICustomTypeDescriptor)i).GetProperties();
}
else
{
pdc = new PropertyDescriptorCollection(null);
}
// No data, rely on external schema if available
foreach (var s in ExternalSchema)
{
pdc.Add(DynamicBindable.GetNewPropertyDescriptor(s.Key, s.Value));
}
return pdc;
}
}
}