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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2004-2005 Novell, Inc.
23 // Peter Bartok pbartok@novell.com
24 // Jackson Harper jackson@ximian.com
27 using System.Collections;
28 using System.Globalization;
29 using System.ComponentModel;
32 namespace System.Windows.Forms {
34 [DefaultEvent("CollectionChanged")]
35 public class BindingContext : ICollection, IEnumerable {
37 private Hashtable managers;
39 private class ManagerEntry {
41 private object source;
42 private string member;
44 private int member_hash;
46 public ManagerEntry (object source, string member)
50 member = String.Empty;
51 this.member = member.ToLower (CultureInfo.InvariantCulture);
54 member_hash = this.member.GetHashCode ();
59 public override bool Equals (object b)
61 ManagerEntry o = (ManagerEntry) b;
63 return (o.source == source && o.member_hash == member_hash);
66 public override int GetHashCode ()
68 return member_hash * source.GetHashCode ();
71 public override string ToString ()
73 return source.ToString () + " + " + member.ToString ();
77 public BindingContext ()
79 managers = new Hashtable ();
82 public bool IsReadOnly {
88 public BindingManagerBase this [object dataSource] {
90 return this [dataSource, String.Empty];
94 public BindingManagerBase this [object data_source, string data_member] {
96 ManagerEntry e = CreateEntry (data_source, data_member);
97 BindingManagerBase res = managers [e] as BindingManagerBase;
101 res = AddManager (data_source, data_member);
106 private BindingManagerBase AddManager (object data_source, string data_member)
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);
115 private BindingManagerBase CreateBindingManager (object data_source,
118 if (data_source is IList ||
119 data_source is IListSource ||
120 data_source is IBindingList) {
121 CurrencyManager res = new CurrencyManager (data_source);
125 return new PropertyManager (data_source, data_member);
128 #region Public Instance Methods
129 public bool Contains(object dataSource)
131 return Contains (dataSource, String.Empty);
134 public bool Contains (object dataSource, string dataMember)
136 ManagerEntry entry = CreateEntry (dataSource, dataMember);
138 return managers.ContainsKey (entry);
140 #endregion // Public Instance Methods
142 #region Protected Instance Methods
144 protected internal void Add (object dataSource, BindingManagerBase listManager)
146 AddCore (dataSource, listManager);
147 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, dataSource));
150 protected virtual void AddCore (object dataSource, BindingManagerBase listManager)
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);
159 protected internal void Clear ()
162 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
165 protected virtual void ClearCore ()
170 protected virtual void OnCollectionChanged(System.ComponentModel.CollectionChangeEventArgs ccevent)
172 if (CollectionChanged != null) {
173 CollectionChanged (this, ccevent);
177 protected internal void Remove (object dataSource)
179 RemoveCore (dataSource);
180 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, dataSource));
183 protected virtual void RemoveCore (object dataSource)
185 managers.Remove (CreateEntry (dataSource, String.Empty));
187 #endregion // Protected Instance Methods
190 public event CollectionChangeEventHandler CollectionChanged;
193 #region ICollection Interfaces
194 void ICollection.CopyTo (Array array, int index)
196 managers.CopyTo (array, index);
199 int ICollection.Count {
201 return managers.Count;
205 bool ICollection.IsSynchronized {
211 object ICollection.SyncRoot {
217 #endregion // ICollection Interfaces
219 #region IEnumerable Interfaces
221 IEnumerator IEnumerable.GetEnumerator() {
222 throw new NotImplementedException();
224 #endregion // IEnumerable Interfaces
226 private ManagerEntry CreateEntry (object dataSource, string dataMember)
228 return new ManagerEntry (dataSource, dataMember);