**** Merged r40732-r40872 from MCS ****
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / BindingContext.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) 2004-2005 Novell, Inc.
21 //
22 // Authors:
23 //      Peter Bartok    pbartok@novell.com
24 //      Jackson Harper  jackson@ximian.com
25
26
27 using System.Collections;
28 using System.Globalization;
29 using System.ComponentModel;
30
31
32 namespace System.Windows.Forms {
33
34         [DefaultEvent("CollectionChanged")]
35         public class BindingContext : ICollection, IEnumerable {
36
37                 private Hashtable managers;
38
39                 private class ManagerEntry {
40
41                         private object source;
42                         private string member;
43
44                         private int member_hash;
45
46                         public ManagerEntry (object source, string member)
47                         {
48                                 this.source = source;
49                                 if (member == null)
50                                         member = String.Empty;
51                                 this.member = member.ToLower (CultureInfo.InvariantCulture);
52
53                                 
54                                 member_hash = this.member.GetHashCode ();
55                                 if (member_hash == 0)
56                                         member_hash = 1;
57                         }
58
59                         public override bool Equals (object b)
60                         {
61                                 ManagerEntry o = (ManagerEntry) b;
62
63                                 return (o.source == source && o.member_hash == member_hash);
64                         }
65
66                         public override int GetHashCode ()
67                         {
68                                 return member_hash * source.GetHashCode ();
69                         }
70
71                         public override string ToString ()
72                         {
73                                 return source.ToString () + " + " + member.ToString ();
74                         }
75                 }
76
77                 public BindingContext () 
78                 {
79                         managers = new Hashtable ();
80                 }
81
82                 public bool IsReadOnly {
83                         get {
84                                 return false;
85                         }
86                 }
87
88                 public BindingManagerBase this [object dataSource] {
89                         get {
90                                 return this [dataSource, String.Empty];
91                         }
92                 }
93
94                 public BindingManagerBase this [object data_source, string data_member] {
95                         get {
96                                 ManagerEntry e = CreateEntry (data_source, data_member);
97                                 BindingManagerBase res = managers [e] as BindingManagerBase;
98
99                                 if (res != null)
100                                         return res;
101                                 res = AddManager (data_source, data_member);
102                                 return res;
103                         }
104                 }
105
106                 private BindingManagerBase AddManager (object data_source, string data_member)
107                 {
108                         BindingManagerBase res = CreateBindingManager (data_source, data_member);
109                         managers.Add (CreateEntry (data_source, data_member), res);
110                         managers.Add (CreateEntry (data_source, String.Empty), res);
111
112                         return res;
113                 }
114
115                 private BindingManagerBase CreateBindingManager (object data_source, 
116                         string data_member)
117                 {
118                         if (data_source is IList || 
119                                 data_source is IListSource ||
120                                 data_source is IBindingList) {
121                                 CurrencyManager res = new CurrencyManager (data_source);
122                                 return res;
123                         }
124
125                         return new PropertyManager (data_source, data_member);
126                 }
127
128                 #region Public Instance Methods
129                 public bool Contains(object dataSource)
130                 {
131                         return Contains (dataSource, String.Empty);
132                 }
133
134                 public bool Contains (object dataSource, string dataMember)
135                 {
136                         ManagerEntry entry = CreateEntry (dataSource, dataMember);
137
138                         return managers.ContainsKey (entry);
139                 }
140                 #endregion      // Public Instance Methods
141
142                 #region Protected Instance Methods
143
144                 protected internal void Add (object dataSource, BindingManagerBase listManager)
145                 {
146                         AddCore (dataSource, listManager);
147                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, dataSource));
148                 }
149
150                 protected virtual void AddCore (object dataSource, BindingManagerBase listManager)
151                 {
152                         if (dataSource == null)
153                                 throw new ArgumentNullException ("dataSource");
154                         if (listManager == null)
155                                 throw new ArgumentNullException ("listManager");
156                         managers.Add (CreateEntry (dataSource, String.Empty), listManager);
157                 }
158
159                 protected internal void Clear ()
160                 {
161                         ClearCore();
162                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
163                 }
164
165                 protected virtual void ClearCore ()
166                 {
167                         managers.Clear ();
168                 }
169
170                 protected virtual void OnCollectionChanged(System.ComponentModel.CollectionChangeEventArgs ccevent)
171                 {
172                         if (CollectionChanged != null) {
173                                 CollectionChanged (this, ccevent);
174                         }
175                 }
176
177                 protected internal void Remove (object dataSource)
178                 {
179                         RemoveCore (dataSource);
180                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, dataSource));
181                 }
182
183                 protected virtual void RemoveCore (object dataSource)
184                 {
185                         managers.Remove (CreateEntry (dataSource, String.Empty));
186                 }
187                 #endregion      // Protected Instance Methods
188
189                 #region Events
190                 public event CollectionChangeEventHandler CollectionChanged;
191                 #endregion      // Events
192
193                 #region ICollection Interfaces
194                 void ICollection.CopyTo (Array array, int index)
195                 {
196                         managers.CopyTo (array, index);
197                 }
198
199                 int ICollection.Count {
200                         get {
201                                 return managers.Count;
202                         }
203                 }
204
205                 bool ICollection.IsSynchronized {
206                         get {
207                                 return false;
208                         }
209                 }
210
211                 object ICollection.SyncRoot {
212                         get {
213                                 return null;
214                         }
215                 }
216
217                 #endregion      // ICollection Interfaces
218
219                 #region IEnumerable Interfaces
220                 [MonoTODO]
221                 IEnumerator IEnumerable.GetEnumerator() {
222                         throw new NotImplementedException();
223                 }
224                 #endregion      // IEnumerable Interfaces
225
226                 private ManagerEntry CreateEntry (object dataSource, string dataMember)
227                 {
228                         return new ManagerEntry (dataSource, dataMember);
229                 }
230         }
231 }