Merge pull request #708 from spicypixel/hotfix/mono-object-to-string
[mono.git] / mcs / class / System / 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 using System.Collections.Generic;
31 using System.Collections.Specialized;
32 using System.ComponentModel;
33 using System.Runtime.CompilerServices;
34
35 namespace System.Collections.ObjectModel
36 {
37         [Serializable]
38 #if !MOBILE
39         [TypeForwardedFrom (Consts.WindowsBase_3_0)]
40 #endif
41         public class ObservableCollection<T> : Collection<T>, INotifyCollectionChanged, INotifyPropertyChanged {
42                 [Serializable]
43 #if !MOBILE
44                 [TypeForwardedFrom (Consts.WindowsBase_3_0)]
45 #endif
46                 sealed class SimpleMonitor : IDisposable {
47                         private int _busyCount;
48
49                         public SimpleMonitor()
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 SimpleMonitor _monitor = new SimpleMonitor ();
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                 [field:NonSerialized]
90                 public virtual event NotifyCollectionChangedEventHandler CollectionChanged;
91                 [field:NonSerialized]
92                 protected virtual event PropertyChangedEventHandler PropertyChanged;
93
94                 event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged {
95                         add { this.PropertyChanged += value; }
96                         remove { this.PropertyChanged -= value; }
97                 }
98
99                 protected IDisposable BlockReentrancy ()
100                 {
101                         _monitor.Enter ();
102                         return _monitor;
103                 }
104
105                 protected void CheckReentrancy ()
106                 {
107                         NotifyCollectionChangedEventHandler eh = CollectionChanged;
108
109                         // Only have a problem if we have more than one event listener.
110                         if (_monitor.Busy && eh != null && eh.GetInvocationList ().Length > 1)
111                                 throw new InvalidOperationException ("Cannot modify the collection while reentrancy is blocked.");
112                 }
113
114                 protected override void ClearItems ()
115                 {
116                         CheckReentrancy ();
117
118                         base.ClearItems ();
119
120                         OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Reset));
121                         OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
122                         OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
123                 }
124
125                 protected override void InsertItem (int index, T item)
126                 {
127                         CheckReentrancy ();
128
129                         base.InsertItem (index, item);
130
131                         OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, item, index));
132                         OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
133                         OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
134                 }
135
136                 public void Move (int oldIndex, int newIndex)
137                 {
138                         MoveItem (oldIndex, newIndex);
139                 }
140
141                 protected virtual void MoveItem (int oldIndex, int newIndex)
142                 {
143                         CheckReentrancy ();
144
145                         T item = Items [oldIndex];
146                         base.RemoveItem (oldIndex);
147                         base.InsertItem (newIndex, item);
148
149                         OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Move, item, newIndex, oldIndex));
150                         OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
151                 }
152
153                 protected virtual void OnCollectionChanged (NotifyCollectionChangedEventArgs e)
154                 {
155                         NotifyCollectionChangedEventHandler eh = CollectionChanged;
156
157                         if (eh != null) {
158                                 // Make sure that the invocation is done before the collection changes,
159                                 // Otherwise there's a chance of data corruption.
160                                 using (BlockReentrancy ()) {
161                                         eh (this, e);
162                                 }
163                         }
164                 }
165
166                 protected virtual void OnPropertyChanged (PropertyChangedEventArgs e)
167                 {
168                         PropertyChangedEventHandler eh = PropertyChanged;
169
170                         if (eh != null)
171                                 eh (this, e);
172                 }
173
174                 protected override void RemoveItem (int index)
175                 {
176                         CheckReentrancy ();
177
178                         T item = Items [index];
179
180                         base.RemoveItem (index);
181
182                         OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Remove, item, index));
183                         OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
184                         OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
185                 }
186
187                 protected override void SetItem (int index, T item)
188                 {
189                         CheckReentrancy ();
190
191                         T oldItem = Items [index];
192
193                         base.SetItem (index, item);
194
195                         OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Replace, item, oldItem, index));
196                         OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
197                 }
198         }
199 }
200 #endif