New tests, updates
[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 #if NET_2_0
87                                 ICurrencyManagerProvider cm_provider = dataSource as ICurrencyManagerProvider;
88                                 if (cm_provider != null) {
89                                         if (dataMember.Length == 0)
90                                                 return cm_provider.CurrencyManager;
91
92                                         return cm_provider.GetRelatedCurrencyManager (dataMember);
93                                 }
94 #endif
95
96                                 HashKey key = new HashKey (dataSource, dataMember);
97                                 BindingManagerBase res = managers [key] as BindingManagerBase;
98
99                                 if (res != null)
100                                         return res;
101
102                                 res = CreateBindingManager (dataSource, dataMember);
103                                 if (res == null)
104                                         return null;
105                                 managers [key] = res;
106                                 return res;
107                         }
108                 }
109
110                 private BindingManagerBase CreateBindingManager (object data_source, string data_member)
111                 {
112                         if (data_member == "") {
113                                 if (IsListType (data_source.GetType ()))
114                                         return new CurrencyManager (data_source);
115                                 else
116                                         return new PropertyManager (data_source);
117                         }
118                         else {
119                                 BindingMemberInfo info = new BindingMemberInfo (data_member);
120
121                                 BindingManagerBase parent_manager = this[data_source, info.BindingPath];
122
123                                 PropertyDescriptor pd = parent_manager == null ? null : parent_manager.GetItemProperties ().Find (info.BindingField, true);
124
125                                 if (pd == null)
126                                         throw new ArgumentException (String.Format ("Cannot create a child list for field {0}.", info.BindingField));
127
128                                 if (IsListType (pd.PropertyType))
129                                         return new RelatedCurrencyManager (parent_manager, pd);
130                                 else
131                                         return new RelatedPropertyManager (parent_manager, info.BindingField);
132                         }
133                 }
134
135                 bool IsListType (Type t)
136                 {
137                         return (typeof (IList).IsAssignableFrom (t)
138                                 || typeof (IListSource).IsAssignableFrom (t));
139                 }
140
141                 #region Public Instance Methods
142                 public bool Contains(object dataSource)
143                 {
144                         return Contains (dataSource, String.Empty);
145                 }
146
147                 public bool Contains (object dataSource, string dataMember)
148                 {
149                         if (dataSource == null)
150                                 throw new ArgumentNullException ("dataSource");
151                         if (dataMember == null)
152                                 dataMember = String.Empty;
153
154                         HashKey key = new HashKey (dataSource, dataMember);
155                         return managers [key] != null;
156                 }
157                 #endregion      // Public Instance Methods
158
159                 #region Protected Instance Methods
160
161                 protected internal void Add (object dataSource, BindingManagerBase listManager)
162                 {
163                         AddCore (dataSource, listManager);
164                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, dataSource));
165                 }
166
167                 protected virtual void AddCore (object dataSource, BindingManagerBase listManager)
168                 {
169                         if (dataSource == null)
170                                 throw new ArgumentNullException ("dataSource");
171                         if (listManager == null)
172                                 throw new ArgumentNullException ("listManager");
173
174                         HashKey key = new HashKey (dataSource, String.Empty);
175                         managers [key] = listManager;
176                 }
177
178                 protected internal void Clear ()
179                 {
180                         ClearCore();
181                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
182                 }
183
184                 protected virtual void ClearCore ()
185                 {
186                         managers.Clear ();
187                 }
188
189                 protected virtual void OnCollectionChanged (CollectionChangeEventArgs ccevent)
190                 {
191                         if (onCollectionChangedHandler != null) {
192                                 onCollectionChangedHandler (this, ccevent);
193                         }
194                 }
195
196                 protected internal void Remove (object dataSource)
197                 {
198                         if (dataSource == null)
199                                 throw new ArgumentNullException ("dataSource");
200
201                         RemoveCore (dataSource);
202                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, dataSource));
203                 }
204
205                 protected virtual void RemoveCore (object dataSource)
206                 {
207                         HashKey[] keys = new HashKey [managers.Keys.Count];
208                         managers.Keys.CopyTo (keys, 0);
209
210                         for (int i = 0; i < keys.Length; i ++) {
211                                 if (keys[i].source == dataSource)
212                                         managers.Remove (keys[i]);
213                         }
214                 }
215 #if NET_2_0
216                 [MonoTODO ("Stub, does nothing")]
217                 public static void UpdateBinding (BindingContext newBindingContext, Binding binding)
218                 {
219                 }
220 #endif
221                 #endregion      // Protected Instance Methods
222
223                 #region Events
224 #if NET_2_0
225                 [Browsable (false)]
226                 [EditorBrowsable (EditorBrowsableState.Never)]
227 #endif
228                 public event CollectionChangeEventHandler CollectionChanged {
229                         add { throw new NotImplementedException (); }
230                         remove { /* nothing to do here.. */ }
231                 }
232                 #endregion      // Events
233
234                 #region ICollection Interfaces
235                 void ICollection.CopyTo (Array ar, int index)
236                 {
237                         managers.CopyTo (ar, index);
238                 }
239
240                 int ICollection.Count {
241                         get { return managers.Count; }
242                 }
243
244                 bool ICollection.IsSynchronized {
245                         get { return false; }
246                 }
247
248                 object ICollection.SyncRoot {
249                         get { return null; }
250                 }
251
252                 #endregion      // ICollection Interfaces
253
254                 #region IEnumerable Interfaces
255                 [MonoInternalNote ("our enumerator is slightly different.  in MS's implementation the Values are WeakReferences to the managers.")]
256                 IEnumerator IEnumerable.GetEnumerator() {
257                         return managers.GetEnumerator ();
258                 }
259                 #endregion      // IEnumerable Interfaces
260         }
261 }