-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathObjectView.cs
More file actions
462 lines (409 loc) · 13.5 KB
/
ObjectView.cs
File metadata and controls
462 lines (409 loc) · 13.5 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
namespace Equin.ApplicationFramework
{
/// <summary>
/// Serves a wrapper for items being viewed in a <see cref="BindingListView<T>"/>.
/// This class implements <see cref="INotifyEditableObject"/> so will raise the necessary events during
/// the item edit life-cycle.
/// </summary>
/// <remarks>
/// If <typeparamref name="T"/> implements <see cref="System.ComponentModel.IEditableObject"/> this class will call BeginEdit/CancelEdit/EndEdit on the <typeparamref name="T"/> object as well.
/// If <typeparamref name="T"/> implements <see cref="System.ComponentModel.IDataErrorInfo"/> this class will use that implementation as its own.
/// </remarks>
/// <typeparam name="T">The type of object being viewed.</typeparam>
[Serializable]
public class ObjectView<T> : INotifyingEditableObject, IDataErrorInfo, INotifyPropertyChanged, ICustomTypeDescriptor
{
/// <summary>
/// Creates a new <see cref="ObjectView<T>"/> wrapper for a <typeparamref name="T"/> object.
/// </summary>
/// <param name="object">The <typeparamref name="T"/> object being wrapped.</param>
public ObjectView(T @object, AggregateBindingListView<T> parent)
{
_parent = parent;
Object = @object;
if (Object is INotifyPropertyChanged)
{
((INotifyPropertyChanged)Object).PropertyChanged += new PropertyChangedEventHandler(ObjectPropertyChanged);
}
if (typeof(ICustomTypeDescriptor).IsAssignableFrom(typeof(T)))
{
_isCustomTypeDescriptor = true;
_customTypeDescriptor = Object as ICustomTypeDescriptor;
Debug.Assert(_customTypeDescriptor != null);
}
_providedViews = new Dictionary<string, object>();
CreateProvidedViews();
}
/// <summary>
/// The view containing this ObjectView.
/// </summary>
private AggregateBindingListView<T> _parent;
/// <summary>
/// Flag that signals if we are currently editing the object.
/// </summary>
private bool _editing;
/// <summary>
/// The actual object being edited.
/// </summary>
private T _object;
/// <summary>
/// Flag set to true if type of T implements ICustomTypeDescriptor
/// </summary>
private bool _isCustomTypeDescriptor;
/// <summary>
/// Holds the Object pre-casted ICustomTypeDescriptor (if supported).
/// </summary>
private ICustomTypeDescriptor _customTypeDescriptor;
/// <summary>
/// A collection of BindingListView objects, indexed by name, for views auto-provided for any generic IList members.
/// </summary>
private Dictionary<string, object> _providedViews;
/// <summary>
/// Gets the object being edited.
/// </summary>
public T Object
{
get { return _object; }
private set
{
if (value == null)
{
throw new ArgumentNullException("Object", Properties.Resources.ObjectCannotBeNull);
}
_object = value;
}
}
public object GetProvidedView(string name)
{
return _providedViews[name];
}
/// <summary>
/// Casts an ObjectView<T> to a T by getting the wrapped T object.
/// </summary>
/// <param name="eo">The ObjectView<T> to cast to a T</param>
/// <returns>The object that is wrapped.</returns>
public static explicit operator T(ObjectView<T> eo)
{
return eo.Object;
}
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
if (obj is T)
{
return Object.Equals(obj);
}
else if (obj is ObjectView<T>)
{
return Object.Equals((obj as ObjectView<T>).Object);
}
else
{
return base.Equals(obj);
}
}
public override int GetHashCode()
{
return Object.GetHashCode();
}
public override string ToString()
{
return Object.ToString();
}
private void ObjectPropertyChanged(object sender, PropertyChangedEventArgs e)
{
// Raise our own event
OnPropertyChanged(sender, new PropertyChangedEventArgs(e.PropertyName));
}
private bool ShouldProvideViewOf(PropertyDescriptor listProp)
{
return _parent.ShouldProvideView(listProp);
}
private string GetProvidedViewName(PropertyDescriptor listProp)
{
return _parent.GetProvidedViewName(listProp);
}
private void CreateProvidedViews()
{
foreach (PropertyDescriptor prop in (this as ICustomTypeDescriptor).GetProperties())
{
if (ShouldProvideViewOf(prop))
{
object view = _parent.CreateProvidedView(this, prop);
string viewName = GetProvidedViewName(prop);
_providedViews.Add(viewName, view);
}
}
}
#region INotifyEditableObject Members
/// <summary>
/// Indicates an edit has just begun.
/// </summary>
[field: NonSerialized]
public event EventHandler EditBegun;
/// <summary>
/// Indicates the edit was cancelled.
/// </summary>
[field: NonSerialized]
public event EventHandler EditCancelled;
/// <summary>
/// Indicated the edit was ended.
/// </summary>
[field: NonSerialized]
public event EventHandler EditEnded;
protected virtual void OnEditBegun()
{
if (EditBegun != null)
{
EditBegun(this, EventArgs.Empty);
}
}
protected virtual void OnEditCancelled()
{
if (EditCancelled != null)
{
EditCancelled(this, EventArgs.Empty);
}
}
protected virtual void OnEditEnded()
{
if (EditEnded != null)
{
EditEnded(this, EventArgs.Empty);
}
}
#endregion
#region IEditableObject Members
public void BeginEdit()
{
// As per documentation, this method may get called multiple times for a single edit.
// So we set a flag to only honor the first call.
if (!_editing)
{
_editing = true;
// If possible call the object's BeginEdit() method
// to let it do what ever it needs e.g. save state
if (Object is IEditableObject)
{
((IEditableObject)Object).BeginEdit();
}
// Raise the EditBegun event.
OnEditBegun();
}
}
public void CancelEdit()
{
// We can only cancel if currently editing
if (_editing)
{
// If possible call the object's CancelEdit() method
// to let it do what ever it needs e.g. rollback state
if (Object is IEditableObject)
{
((IEditableObject)Object).CancelEdit();
}
// Raise the EditCancelled event.
OnEditCancelled();
// No longer editing now.
_editing = false;
}
}
public void EndEdit()
{
// We can only end if currently editing
if (_editing)
{
// If possible call the object's EndEdit() method
// to let it do what ever it needs e.g. commit state
if (Object is IEditableObject)
{
((IEditableObject)Object).EndEdit();
}
// Raise the EditEnded event.
OnEditEnded();
// No longer editing now.
_editing = false;
}
}
#endregion
#region IDataErrorInfo Members
// If the wrapped Object support IDataErrorInfo we forward calls to it.
// Otherwise, we just return empty strings that signal "no error".
string IDataErrorInfo.Error
{
get
{
if (Object is IDataErrorInfo)
{
return ((IDataErrorInfo)Object).Error;
}
return string.Empty;
}
}
string IDataErrorInfo.this[string columnName]
{
get
{
if (Object is IDataErrorInfo)
{
return ((IDataErrorInfo)Object)[columnName];
}
return string.Empty;
}
}
#endregion
#region INotifyPropertyChanged Members
[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(object sender, PropertyChangedEventArgs args)
{
if (PropertyChanged != null)
{
PropertyChanged(sender, args);
}
}
#endregion
#region ICustomTypeDescriptor Members
AttributeCollection ICustomTypeDescriptor.GetAttributes()
{
if (_isCustomTypeDescriptor)
{
return _customTypeDescriptor.GetAttributes();
}
else
{
return TypeDescriptor.GetAttributes(Object);
}
}
string ICustomTypeDescriptor.GetClassName()
{
if (_isCustomTypeDescriptor)
{
return _customTypeDescriptor.GetClassName();
}
else
{
return typeof(T).FullName;
}
}
string ICustomTypeDescriptor.GetComponentName()
{
if (_isCustomTypeDescriptor)
{
return _customTypeDescriptor.GetComponentName();
}
else
{
return TypeDescriptor.GetFullComponentName(Object);
}
}
TypeConverter ICustomTypeDescriptor.GetConverter()
{
if (_isCustomTypeDescriptor)
{
return _customTypeDescriptor.GetConverter();
}
else
{
return TypeDescriptor.GetConverter(Object);
}
}
EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
{
if (_isCustomTypeDescriptor)
{
return _customTypeDescriptor.GetDefaultEvent();
}
else
{
return TypeDescriptor.GetDefaultEvent(Object);
}
}
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
{
if (_isCustomTypeDescriptor)
{
return _customTypeDescriptor.GetDefaultProperty();
}
else
{
return TypeDescriptor.GetDefaultProperty(Object);
}
}
object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
{
if (_isCustomTypeDescriptor)
{
return _customTypeDescriptor.GetEditor(editorBaseType);
}
else
{
return null;
}
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
{
if (_isCustomTypeDescriptor)
{
return _customTypeDescriptor.GetEvents();
}
else
{
return TypeDescriptor.GetEvents(Object, attributes);
}
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
{
if (_isCustomTypeDescriptor)
{
return _customTypeDescriptor.GetEvents();
}
else
{
return TypeDescriptor.GetEvents(Object);
}
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
{
if (_isCustomTypeDescriptor)
{
return _customTypeDescriptor.GetProperties();
}
else
{
return TypeDescriptor.GetProperties(Object, attributes);
}
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
{
if (_isCustomTypeDescriptor)
{
return _customTypeDescriptor.GetProperties();
}
else
{
return TypeDescriptor.GetProperties(Object);
}
}
object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
{
if (_isCustomTypeDescriptor)
{
return _customTypeDescriptor.GetPropertyOwner(pd);
}
else
{
return Object;
}
}
#endregion
}
}