summaryrefslogtreecommitdiff
path: root/mcs/class/System/System.ComponentModel/BindingList.cs
blob: b64d655efe0fa0b941f2209a056d12609699ecae (plain)
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
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2007 Novell, Inc.
//

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;

namespace System.ComponentModel {
	[SerializableAttribute] 
	public class BindingList<T> : Collection<T>,
		IBindingList, IList, ICollection, 
		IEnumerable, ICancelAddNew, IRaiseItemChangedEvents
	{
		bool allow_edit = true;
		bool allow_remove = true;
		bool allow_new;
		bool allow_new_set;

		bool raise_list_changed_events = true;
		
		bool type_has_default_ctor;
		bool type_raises_item_changed_events;

		bool add_pending;
		int pending_add_index;

		void CheckType ()
		{
			ConstructorInfo ci = typeof (T).GetConstructor (Type.EmptyTypes);
			type_has_default_ctor = (ci != null);
			type_raises_item_changed_events = typeof (INotifyPropertyChanged).IsAssignableFrom (typeof (T));
		}

		public BindingList (IList<T> list) : base(list)
		{
			CheckType ();
		}

		public BindingList () : base ()
		{
			CheckType ();
		}

		public bool AllowEdit {
			get { return allow_edit; }
			set {
				if (allow_edit != value) {
					allow_edit = value;

					if (raise_list_changed_events)
						OnListChanged (new ListChangedEventArgs (ListChangedType.Reset, -1 /* XXX */));
				}
			}
		}

		public bool AllowNew {
			get {
				/* if the user explicitly it, return that value */
				if (allow_new_set)
					return allow_new;

				/* if the list type has a default constructor we allow new */
				if (type_has_default_ctor)
					return true;

				/* if the user adds a delegate, we return true even if
				   the type doesn't have a default ctor */
				if (AddingNew != null)
					return true;

				return false;
			}
			set {
				// this funky check (using AllowNew
				// instead of allow_new allows us to
				// keep the logic for the 3 cases in
				// one place (the getter) instead of
				// spreading them around the file (in
				// the ctor, in the AddingNew add
				// handler, etc.
				if (AllowNew != value) {
					allow_new_set = true;

					allow_new = value;

					if (raise_list_changed_events)
						OnListChanged (new ListChangedEventArgs (ListChangedType.Reset, -1 /* XXX */));
				}
			}
		}

		public bool AllowRemove {
			get { return allow_remove; }
			set {
				if (allow_remove != value) {
					allow_remove = value;

					if (raise_list_changed_events)
						OnListChanged (new ListChangedEventArgs (ListChangedType.Reset, -1 /* XXX */));
				}
			}
		}

		protected virtual bool IsSortedCore {
			get { return false; }
		}

		public bool RaiseListChangedEvents {
			get { return raise_list_changed_events; }
			set { raise_list_changed_events = value; }
		}

		protected virtual ListSortDirection SortDirectionCore {
			get { return ListSortDirection.Ascending; }
		}

		protected virtual PropertyDescriptor SortPropertyCore {
			get { return null; }
		}

		protected virtual bool SupportsChangeNotificationCore {
			get { return true; }
		}

		protected virtual bool SupportsSearchingCore {
			get { return false; }
		}

		protected virtual bool SupportsSortingCore {
			get { return false; }
		}

		public event AddingNewEventHandler AddingNew;
		public event ListChangedEventHandler ListChanged;

		public T AddNew ()
		{
			return (T)AddNewCore ();
		}

		protected virtual object AddNewCore ()
		{
			if (!AllowNew)
				throw new InvalidOperationException ();

			AddingNewEventArgs args = new AddingNewEventArgs ();

			OnAddingNew (args);

			T new_obj = (T)args.NewObject;
			if (new_obj == null) {
				if (!type_has_default_ctor)
					throw new InvalidOperationException ();

				new_obj = (T)Activator.CreateInstance (typeof (T));
			}

			Add (new_obj);
			pending_add_index = IndexOf (new_obj);
			add_pending = true;
			
			return new_obj;
		}

		protected virtual void ApplySortCore (PropertyDescriptor prop, ListSortDirection direction)
		{
			throw new NotSupportedException ();
		}

		public virtual void CancelNew (int itemIndex)
		{
			if (!add_pending)
				return;

			if (itemIndex != pending_add_index)
				return;

			add_pending = false;

			base.RemoveItem (itemIndex);

			if (raise_list_changed_events)
				OnListChanged (new ListChangedEventArgs (ListChangedType.ItemDeleted, itemIndex));
		}

		protected override void ClearItems ()
		{
			EndNew (pending_add_index);
			if (type_raises_item_changed_events) {
				foreach ( T item in base.Items ) {
					(item as INotifyPropertyChanged).PropertyChanged -= Item_PropertyChanged;
				}
			}
			base.ClearItems ();

			OnListChanged (new ListChangedEventArgs (ListChangedType.Reset, -1));
		}

		public virtual void EndNew (int itemIndex)
		{
			if (!add_pending)
				return;

			if (itemIndex != pending_add_index)
				return;

			add_pending = false;
		}

		protected virtual int FindCore (PropertyDescriptor prop, object key)
		{
			throw new NotSupportedException ();
		}

		protected override void InsertItem (int index, T item)
		{
			EndNew (pending_add_index);

			base.InsertItem (index, item);

			if (raise_list_changed_events)
				OnListChanged (new ListChangedEventArgs (ListChangedType.ItemAdded, index));

			if (type_raises_item_changed_events)
				(item as INotifyPropertyChanged).PropertyChanged += Item_PropertyChanged;
		}

		void Item_PropertyChanged (object item, PropertyChangedEventArgs args)
		{
			OnListChanged (new ListChangedEventArgs (ListChangedType.ItemChanged, base.IndexOf ((T) item)) );
		}

		protected virtual void OnAddingNew (AddingNewEventArgs e)
		{
			if (AddingNew != null)
				AddingNew (this, e);
		}

		protected virtual void OnListChanged (ListChangedEventArgs e)
		{
			if (ListChanged != null)
				ListChanged (this, e);
		}

		protected override void RemoveItem (int index)
		{
			if (!AllowRemove)
				throw new NotSupportedException ();

			EndNew (pending_add_index);
			if (type_raises_item_changed_events) {
				(base[index] as INotifyPropertyChanged).PropertyChanged -= Item_PropertyChanged;
			}
			base.RemoveItem (index);

			if (raise_list_changed_events)
				OnListChanged (new ListChangedEventArgs (ListChangedType.ItemDeleted, index));
		}

		protected virtual void RemoveSortCore ()
		{
			throw new NotSupportedException ();
		}

		public void ResetBindings ()
		{
			OnListChanged (new ListChangedEventArgs (ListChangedType.Reset, -1));
		}

		public void ResetItem (int position)
		{
			OnListChanged (new ListChangedEventArgs (ListChangedType.ItemChanged, position));
		}

		protected override void SetItem (int index, T item)
		{
			if (type_raises_item_changed_events) {
				(base[index] as INotifyPropertyChanged).PropertyChanged -= Item_PropertyChanged;
				(item as INotifyPropertyChanged).PropertyChanged += Item_PropertyChanged;
			}
			base.SetItem (index, item);

			OnListChanged (new ListChangedEventArgs (ListChangedType.ItemChanged, index));
		}

		void IBindingList.AddIndex (PropertyDescriptor index)
		{
			/* no implementation by default */
		}

		object IBindingList.AddNew ()
		{
			return AddNew ();
		}

		void IBindingList.ApplySort (PropertyDescriptor property, ListSortDirection direction)
		{
			ApplySortCore (property, direction);
		}

		int IBindingList.Find (PropertyDescriptor property, object key)
		{
			return FindCore (property, key);
		}

		void IBindingList.RemoveIndex (PropertyDescriptor property)
		{
			/* no implementation by default */
		}

		void IBindingList.RemoveSort ()
		{
			RemoveSortCore ();
		}

		bool IBindingList.IsSorted {
			get { return IsSortedCore; }
		}

		ListSortDirection IBindingList.SortDirection {
			get { return SortDirectionCore; }
		}

		PropertyDescriptor IBindingList.SortProperty {
			get { return SortPropertyCore; }
		}

		bool IBindingList.AllowEdit {
			get { return AllowEdit; }
		}

		bool IBindingList.AllowNew {
			get { return AllowNew; }
		}

		bool IBindingList.AllowRemove {
			get { return AllowRemove; }
		}

		bool IBindingList.SupportsChangeNotification {
			get { return SupportsChangeNotificationCore; }
		}

		bool IBindingList.SupportsSearching {
			get { return SupportsSearchingCore; }
		}

		bool IBindingList.SupportsSorting {
			get { return SupportsSortingCore; }
		}

		bool IRaiseItemChangedEvents.RaisesItemChangedEvents {
			get { return type_raises_item_changed_events; }
		}
	}

}