Fix bug #395
[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.Data;
28 using System.Collections;
29 using System.Globalization;
30 using System.ComponentModel;
31
32
33 namespace System.Windows.Forms {
34
35         [DefaultEvent("CollectionChanged")]
36         public class BindingContext : ICollection, IEnumerable {
37
38                 private Hashtable managers;
39                 private EventHandler onCollectionChangedHandler;
40
41                 private class HashKey {
42                         public object source;
43                         public string member;
44
45                         public HashKey (object source, string member)
46                         {
47                                 this.source = source;
48                                 this.member = member;
49                         }
50
51                         public override int GetHashCode ()
52                         {
53                                 return source.GetHashCode() ^ member.GetHashCode ();
54                         }
55
56                         public override bool Equals (object o)
57                         {
58                                 HashKey hk = o as HashKey;
59                                 if (hk == null)
60                                         return false;
61                                 return hk.source == source && hk.member == member;
62                         }
63                 }
64
65                 public BindingContext () 
66                 {
67                         managers = new Hashtable ();
68                         onCollectionChangedHandler = null;
69                 }
70
71                 public bool IsReadOnly {
72                         get { return false; }
73                 }
74
75                 public BindingManagerBase this [object dataSource] {
76                         get { return this [dataSource, String.Empty]; }
77                 }
78
79                 public BindingManagerBase this [object dataSource, string dataMember] {
80                         get {
81                                 if (dataSource == null)
82                                         throw new ArgumentNullException ("dataSource");
83                                 if (dataMember == null)
84                                         dataMember = String.Empty;
85
86                                 ICurrencyManagerProvider cm_provider = dataSource as ICurrencyManagerProvider;
87                                 if (cm_provider != null) {
88                                         if (dataMember.Length == 0)
89                                                 return cm_provider.CurrencyManager;
90
91                                         return cm_provider.GetRelatedCurrencyManager (dataMember);
92                                 }
93
94                                 HashKey key = new HashKey (dataSource, dataMember);
95                                 BindingManagerBase res = managers [key] as BindingManagerBase;
96
97                                 if (res != null)
98                                         return res;
99
100                                 res = CreateBindingManager (dataSource, dataMember);
101                                 if (res == null)
102                                         return null;
103                                 managers [key] = res;
104                                 return res;
105                         }
106                 }
107
108                 private BindingManagerBase CreateBindingManager (object data_source, string data_member)
109                 {
110                         if (data_member == "") {
111                                 if (IsListType (data_source.GetType ()))
112                                         return new CurrencyManager (data_source);
113                                 else
114                                         return new PropertyManager (data_source);
115                         }
116                         else {
117                                 BindingMemberInfo info = new BindingMemberInfo (data_member);
118
119                                 BindingManagerBase parent_manager = this[data_source, info.BindingPath];
120
121                                 PropertyDescriptor pd = parent_manager == null ? null : parent_manager.GetItemProperties ().Find (info.BindingField, true);
122
123                                 if (pd == null)
124                                         throw new ArgumentException (String.Format ("Cannot create a child list for field {0}.", info.BindingField));
125
126                                 if (IsListType (pd.PropertyType))
127                                         return new RelatedCurrencyManager (parent_manager, pd);
128                                 else
129                                         return new RelatedPropertyManager (parent_manager, info.BindingField);
130                         }
131                 }
132
133                 bool IsListType (Type t)
134                 {
135                         return (typeof (IList).IsAssignableFrom (t)
136                                 || typeof (IListSource).IsAssignableFrom (t));
137                 }
138
139                 #region Public Instance Methods
140                 public bool Contains(object dataSource)
141                 {
142                         return Contains (dataSource, String.Empty);
143                 }
144
145                 public bool Contains (object dataSource, string dataMember)
146                 {
147                         if (dataSource == null)
148                                 throw new ArgumentNullException ("dataSource");
149                         if (dataMember == null)
150                                 dataMember = String.Empty;
151
152                         HashKey key = new HashKey (dataSource, dataMember);
153                         return managers [key] != null;
154                 }
155                 #endregion      // Public Instance Methods
156
157                 #region Protected Instance Methods
158
159                 protected internal void Add (object dataSource, BindingManagerBase listManager)
160                 {
161                         AddCore (dataSource, listManager);
162                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, dataSource));
163                 }
164
165                 protected virtual void AddCore (object dataSource, BindingManagerBase listManager)
166                 {
167                         if (dataSource == null)
168                                 throw new ArgumentNullException ("dataSource");
169                         if (listManager == null)
170                                 throw new ArgumentNullException ("listManager");
171
172                         HashKey key = new HashKey (dataSource, String.Empty);
173                         managers [key] = listManager;
174                 }
175
176                 protected internal void Clear ()
177                 {
178                         ClearCore();
179                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
180                 }
181
182                 protected virtual void ClearCore ()
183                 {
184                         managers.Clear ();
185                 }
186
187                 protected virtual void OnCollectionChanged (CollectionChangeEventArgs ccevent)
188                 {
189                         if (onCollectionChangedHandler != null) {
190                                 onCollectionChangedHandler (this, ccevent);
191                         }
192                 }
193
194                 protected internal void Remove (object dataSource)
195                 {
196                         if (dataSource == null)
197                                 throw new ArgumentNullException ("dataSource");
198
199                         RemoveCore (dataSource);
200                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, dataSource));
201                 }
202
203                 protected virtual void RemoveCore (object dataSource)
204                 {
205                         HashKey[] keys = new HashKey [managers.Keys.Count];
206                         managers.Keys.CopyTo (keys, 0);
207
208                         for (int i = 0; i < keys.Length; i ++) {
209                                 if (keys[i].source == dataSource)
210                                         managers.Remove (keys[i]);
211                         }
212                 }
213
214                 [MonoTODO ("Stub, does nothing")]
215                 public static void UpdateBinding (BindingContext newBindingContext, Binding binding)
216                 {
217                 }
218
219                 #endregion      // Protected Instance Methods
220
221                 #region Events
222                 [Browsable (false)]
223                 [EditorBrowsable (EditorBrowsableState.Never)]
224                 public event CollectionChangeEventHandler CollectionChanged {
225                         add { throw new NotImplementedException (); }
226                         remove { /* nothing to do here.. */ }
227                 }
228                 #endregion      // Events
229
230                 #region ICollection Interfaces
231                 void ICollection.CopyTo (Array ar, int index)
232                 {
233                         managers.CopyTo (ar, index);
234                 }
235
236                 int ICollection.Count {
237                         get { return managers.Count; }
238                 }
239
240                 bool ICollection.IsSynchronized {
241                         get { return false; }
242                 }
243
244                 object ICollection.SyncRoot {
245                         get { return null; }
246                 }
247
248                 #endregion      // ICollection Interfaces
249
250                 #region IEnumerable Interfaces
251                 [MonoInternalNote ("our enumerator is slightly different.  in MS's implementation the Values are WeakReferences to the managers.")]
252                 IEnumerator IEnumerable.GetEnumerator() {
253                         return managers.GetEnumerator ();
254                 }
255                 #endregion      // IEnumerable Interfaces
256         }
257 }