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;
38 private object null_data_source = new object ();
40 private class DataSourceEntry {
42 private object source;
43 private Hashtable members;
44 // private BindingManagerBase default_manager;
46 public DataSourceEntry (object source)
49 members = new Hashtable ();
52 public BindingManagerBase AddMember (string member)
55 member = String.Empty;
56 BindingManagerBase res = members [member] as BindingManagerBase;
59 res = CreateBindingManager (source, member);
60 members [member] = res;
64 public void AddMember (string member, BindingManagerBase manager)
66 members [member] = manager;
69 public bool Contains (string member)
71 return members.Contains (member);
75 public BindingContext ()
77 managers = new Hashtable ();
80 public bool IsReadOnly {
86 public BindingManagerBase this [object dataSource] {
88 return this [dataSource, String.Empty];
92 public BindingManagerBase this [object data_source, string data_member] {
94 DataSourceEntry ds = GetEntry (data_source, data_member, true);
95 return ds.AddMember (data_member);
99 private DataSourceEntry GetEntry (object data_source, string data_member, bool create)
101 if (data_source == null)
102 data_source = null_data_source;
104 DataSourceEntry ds = managers [data_source] as DataSourceEntry;
105 if (ds == null && create) {
106 ds = new DataSourceEntry (data_source);
107 managers [data_source] = ds;
113 private static 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, data_member);
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 DataSourceEntry ds = GetEntry (dataSource, dataMember, false);
137 return ds.Contains (dataMember);
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 DataSourceEntry ds = GetEntry (dataSource, String.Empty, true);
157 ds.AddMember (String.Empty, listManager);
160 protected internal void Clear ()
163 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
166 protected virtual void ClearCore ()
171 protected virtual void OnCollectionChanged(System.ComponentModel.CollectionChangeEventArgs ccevent)
173 if (CollectionChanged != null) {
174 CollectionChanged (this, ccevent);
178 protected internal void Remove (object dataSource)
180 RemoveCore (dataSource);
181 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, dataSource));
184 protected virtual void RemoveCore (object dataSource)
186 managers.Remove (dataSource);
188 #endregion // Protected Instance Methods
191 public event CollectionChangeEventHandler CollectionChanged;
194 #region ICollection Interfaces
195 void ICollection.CopyTo (Array array, int index)
197 managers.CopyTo (array, index);
200 int ICollection.Count {
202 return managers.Count;
206 bool ICollection.IsSynchronized {
212 object ICollection.SyncRoot {
218 #endregion // ICollection Interfaces
220 #region IEnumerable Interfaces
222 IEnumerator IEnumerable.GetEnumerator() {
223 throw new NotImplementedException();
225 #endregion // IEnumerable Interfaces