Merge pull request #615 from nealef/master
[mono.git] / mcs / class / WindowsBase / System.Collections.ObjectModel / ObservableCollection.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
21 // Copyright 2011 Xamarin Inc.
22 //
23 // Authors:
24 //      Chris Toshok (toshok@novell.com)
25 //      Brian O'Keefe (zer0keefie@gmail.com)
26 //      Marek Safar (marek.safar@gmail.com)
27 //
28
29 #if NET_4_0
30
31 using System.Collections.ObjectModel;
32 using System.Runtime.CompilerServices;
33 [assembly:TypeForwardedTo (typeof (ObservableCollection<>))]
34
35 #else
36
37 using System.Collections.Generic;
38 using System.Collections.Specialized;
39 using System.ComponentModel;
40
41 namespace System.Collections.ObjectModel
42 {
43         [Serializable]
44         public class ObservableCollection<T> : Collection<T>, INotifyCollectionChanged, INotifyPropertyChanged {
45                 
46                 private class Reentrant : IDisposable {
47                         private int _busyCount = 0;
48
49                         public Reentrant()
50                         {
51                         }
52
53                         public void Enter()
54                         {
55                                 _busyCount++;
56                         }
57
58                         public void Dispose()
59                         {
60                                 _busyCount--;
61                         }
62
63                         public bool Busy
64                         {
65                                 get { return _busyCount > 0; }
66                         }
67                 }
68
69                 private Reentrant reentrant = new Reentrant ();
70
71                 public ObservableCollection()
72                 {
73                 }
74
75                 public ObservableCollection(IEnumerable<T> collection)
76                 {
77                         if (collection == null)
78                                 throw new ArgumentNullException ("collection");
79
80                         foreach (var item in collection)
81                                 Add (item);
82                 }
83
84                 public ObservableCollection(List<T> list)
85                         : base (list != null ? new List<T> (list) : null)
86                 {
87                 }
88
89                 public virtual event NotifyCollectionChangedEventHandler CollectionChanged;
90                 protected virtual event PropertyChangedEventHandler PropertyChanged;
91
92                 event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged {
93                         add { this.PropertyChanged += value; }
94                         remove { this.PropertyChanged -= value; }
95                 }
96
97                 protected IDisposable BlockReentrancy ()
98                 {
99                         reentrant.Enter ();
100                         return reentrant;
101                 }
102
103                 protected void CheckReentrancy ()
104                 {
105                         NotifyCollectionChangedEventHandler eh = CollectionChanged;
106
107                         // Only have a problem if we have more than one event listener.
108                         if (reentrant.Busy && eh != null && eh.GetInvocationList ().Length > 1)
109                                 throw new InvalidOperationException ("Cannot modify the collection while reentrancy is blocked.");
110                 }
111
112                 protected override void ClearItems ()
113                 {
114                         CheckReentrancy ();
115
116                         base.ClearItems ();
117
118                         OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Reset));
119                         OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
120                         OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
121                 }
122
123                 protected override void InsertItem (int index, T item)
124                 {
125                         CheckReentrancy ();
126
127                         base.InsertItem (index, item);
128
129                         OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, item, index));
130                         OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
131                         OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
132                 }
133
134                 public void Move (int oldIndex, int newIndex)
135                 {
136                         MoveItem (oldIndex, newIndex);
137                 }
138
139                 protected virtual void MoveItem (int oldIndex, int newIndex)
140                 {
141                         CheckReentrancy ();
142
143                         T item = Items [oldIndex];
144                         base.RemoveItem (oldIndex);
145                         base.InsertItem (newIndex, item);
146
147                         OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Move, item, newIndex, oldIndex));
148                         OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
149                 }
150
151                 protected virtual void OnCollectionChanged (NotifyCollectionChangedEventArgs e)
152                 {
153                         NotifyCollectionChangedEventHandler eh = CollectionChanged;
154
155                         if (eh != null) {
156                                 // Make sure that the invocation is done before the collection changes,
157                                 // Otherwise there's a chance of data corruption.
158                                 using (BlockReentrancy ()) {
159                                         eh (this, e);
160                                 }
161                         }
162                 }
163
164                 protected virtual void OnPropertyChanged (PropertyChangedEventArgs e)
165                 {
166                         PropertyChangedEventHandler eh = PropertyChanged;
167
168                         if (eh != null)
169                                 eh (this, e);
170                 }
171
172                 protected override void RemoveItem (int index)
173                 {
174                         CheckReentrancy ();
175
176                         T item = Items [index];
177
178                         base.RemoveItem (index);
179
180                         OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Remove, item, index));
181                         OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
182                         OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
183                 }
184
185                 protected override void SetItem (int index, T item)
186                 {
187                         CheckReentrancy ();
188
189                         T oldItem = Items [index];
190
191                         base.SetItem (index, item);
192
193                         OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Replace, item, oldItem, index));
194                         OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
195                 }
196         }
197 }
198 #endif