2009-12-03 Marek Safar <marek.safar@gmail.com>
[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 //
22 // Authors:
23 //      Chris Toshok (toshok@novell.com)
24 //      Brian O'Keefe (zer0keefie@gmail.com)
25 //
26 #if NET_4_0
27 using System.Collections.Generic;
28 using System.Collections.Specialized;
29 using System.ComponentModel;
30 using System.Runtime.CompilerServices;
31
32 namespace System.Collections.ObjectModel
33 {
34         [Serializable]
35         [TypeForwardedFrom (Consts.WindowsBase_3_0)]
36         public class ObservableCollection<T> : Collection<T>, INotifyCollectionChanged, INotifyPropertyChanged {
37                 
38                 private class Reentrant : IDisposable {
39                         private int count = 0;
40
41                         public Reentrant()
42                         {
43                         }
44
45                         public void Enter()
46                         {
47                                 count++;
48                         }
49
50                         public void Dispose()
51                         {
52                                 count--;
53                         }
54
55                         public bool Busy
56                         {
57                                 get { return count > 0; }
58                         }
59                 }
60
61                 private Reentrant reentrant = new Reentrant ();
62
63                 public ObservableCollection()
64                 {
65                 }
66
67                 public ObservableCollection(IEnumerable<T> collection)
68                 {
69                         throw new NotImplementedException ();
70                 }
71
72                 public ObservableCollection(List<T> list)
73                         : base (list)
74                 {
75                 }
76
77                 public virtual event NotifyCollectionChangedEventHandler CollectionChanged;
78                 protected virtual event PropertyChangedEventHandler PropertyChanged;
79
80                 event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged {
81                         add { this.PropertyChanged += value; }
82                         remove { this.PropertyChanged -= value; }
83                 }
84
85                 protected IDisposable BlockReentrancy ()
86                 {
87                         reentrant.Enter ();
88                         return reentrant;
89                 }
90
91                 protected void CheckReentrancy ()
92                 {
93                         NotifyCollectionChangedEventHandler eh = CollectionChanged;
94
95                         // Only have a problem if we have more than one event listener.
96                         if (reentrant.Busy && eh != null && eh.GetInvocationList ().Length > 1)
97                                 throw new InvalidOperationException ("Cannot modify the collection while reentrancy is blocked.");
98                 }
99
100                 protected override void ClearItems ()
101                 {
102                         CheckReentrancy ();
103
104                         base.ClearItems ();
105
106                         OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Reset));
107                         OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
108                         OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
109                 }
110
111                 protected override void InsertItem (int index, T item)
112                 {
113                         CheckReentrancy ();
114
115                         base.InsertItem (index, item);
116
117                         OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, item, index));
118                         OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
119                         OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
120                 }
121
122                 public void Move (int oldIndex, int newIndex)
123                 {
124                         MoveItem (oldIndex, newIndex);
125                 }
126
127                 protected virtual void MoveItem (int oldIndex, int newIndex)
128                 {
129                         CheckReentrancy ();
130
131                         T item = Items [oldIndex];
132                         base.RemoveItem (oldIndex);
133                         base.InsertItem (newIndex, item);
134
135                         OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Move, item, newIndex, oldIndex));
136                         OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
137                 }
138
139                 protected virtual void OnCollectionChanged (NotifyCollectionChangedEventArgs e)
140                 {
141                         NotifyCollectionChangedEventHandler eh = CollectionChanged;
142
143                         if (eh != null) {
144                                 // Make sure that the invocation is done before the collection changes,
145                                 // Otherwise there's a chance of data corruption.
146                                 using (BlockReentrancy ()) {
147                                         eh (this, e);
148                                 }
149                         }
150                 }
151
152                 protected virtual void OnPropertyChanged (PropertyChangedEventArgs e)
153                 {
154                         PropertyChangedEventHandler eh = PropertyChanged;
155
156                         if (eh != null)
157                                 eh (this, e);
158                 }
159
160                 protected override void RemoveItem (int index)
161                 {
162                         CheckReentrancy ();
163
164                         T item = Items [index];
165
166                         base.RemoveItem (index);
167
168                         OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Remove, item, index));
169                         OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
170                         OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
171                 }
172
173                 protected override void SetItem (int index, T item)
174                 {
175                         CheckReentrancy ();
176
177                         T oldItem = Items [index];
178
179                         base.SetItem (index, item);
180
181                         OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Replace, item, oldItem, index));
182                         OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
183                 }
184         }
185 }
186 #endif