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 WeakReference member_ref;
44 private int member_hash;
46 public ManagerEntry (object source, string member)
50 member = String.Empty;
52 member_hash = member.ToLower (CultureInfo.InvariantCulture).GetHashCode ();
55 member_ref = new WeakReference (member, false);
58 public override bool Equals (object b)
60 ManagerEntry o = (ManagerEntry) b;
62 return (o.source == source && o.member_ref.Target == member_ref.Target);
65 public override int GetHashCode ()
67 return member_hash * source.GetHashCode ();
70 public override string ToString ()
72 return source.ToString () + " + " + (member_ref.Target == null ? " -- null --" : member_ref.Target.ToString ());
76 public BindingContext ()
78 managers = new Hashtable ();
81 public bool IsReadOnly {
87 public BindingManagerBase this [object dataSource] {
89 return this [dataSource, String.Empty];
93 public BindingManagerBase this [object data_source, string data_member] {
95 ManagerEntry e = CreateEntry (data_source, data_member);
96 WeakReference wref = managers [e] as WeakReference;
98 if (wref != null && wref.Target != null)
99 return wref.Target as BindingManagerBase;
100 BindingManagerBase res = AddManager (data_source, data_member);
105 private BindingManagerBase AddManager (object data_source, string data_member)
107 BindingManagerBase res = CreateBindingManager (data_source, data_member);
108 managers [CreateEntry (data_source, data_member)] = new WeakReference (res);
113 private BindingManagerBase CreateBindingManager (object data_source,
116 if (data_source is IList ||
117 data_source is IListSource ||
118 data_source is IBindingList) {
119 CurrencyManager res = new CurrencyManager (data_source);
123 return new PropertyManager (data_source, data_member);
126 #region Public Instance Methods
127 public bool Contains(object dataSource)
129 return Contains (dataSource, String.Empty);
132 public bool Contains (object dataSource, string dataMember)
134 ManagerEntry entry = CreateEntry (dataSource, dataMember);
136 return managers.ContainsKey (entry);
138 #endregion // Public Instance Methods
140 #region Protected Instance Methods
142 protected internal void Add (object dataSource, BindingManagerBase listManager)
144 AddCore (dataSource, listManager);
145 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, dataSource));
148 protected virtual void AddCore (object dataSource, BindingManagerBase listManager)
150 if (dataSource == null)
151 throw new ArgumentNullException ("dataSource");
152 if (listManager == null)
153 throw new ArgumentNullException ("listManager");
154 managers.Add (CreateEntry (dataSource, String.Empty), new WeakReference (listManager));
157 protected internal void Clear ()
160 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
163 protected virtual void ClearCore ()
168 protected virtual void OnCollectionChanged(System.ComponentModel.CollectionChangeEventArgs ccevent)
170 if (CollectionChanged != null) {
171 CollectionChanged (this, ccevent);
175 protected internal void Remove (object dataSource)
177 RemoveCore (dataSource);
178 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, dataSource));
181 protected virtual void RemoveCore (object dataSource)
183 managers.Remove (CreateEntry (dataSource, String.Empty));
185 #endregion // Protected Instance Methods
188 public event CollectionChangeEventHandler CollectionChanged;
191 #region ICollection Interfaces
192 void ICollection.CopyTo (Array array, int index)
194 managers.CopyTo (array, index);
197 int ICollection.Count {
199 return managers.Count;
203 bool ICollection.IsSynchronized {
209 object ICollection.SyncRoot {
215 #endregion // ICollection Interfaces
217 #region IEnumerable Interfaces
219 IEnumerator IEnumerable.GetEnumerator() {
220 throw new NotImplementedException();
222 #endregion // IEnumerable Interfaces
224 private ManagerEntry CreateEntry (object dataSource, string dataMember)
226 return new ManagerEntry (dataSource, dataMember);