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
28 using System.Collections;
29 using System.Globalization;
30 using System.ComponentModel;
33 namespace System.Windows.Forms {
35 [DefaultEvent("CollectionChanged")]
36 public class BindingContext : ICollection, IEnumerable {
38 private Hashtable managers;
39 private EventHandler onCollectionChangedHandler;
41 private class HashKey {
45 public HashKey (object source, string member)
51 public override int GetHashCode ()
53 return source.GetHashCode() + member.GetHashCode ();
56 public override bool Equals (object o)
58 HashKey hk = o as HashKey;
61 return hk.source == source && hk.member == member;
65 public BindingContext ()
67 managers = new Hashtable ();
70 public bool IsReadOnly {
74 public BindingManagerBase this [object dataSource] {
75 get { return this [dataSource, String.Empty]; }
78 public BindingManagerBase this [object data_source, string data_member] {
80 if (data_source == null)
81 throw new ArgumentNullException ("data_source");
82 if (data_member == null)
83 data_member = String.Empty;
85 HashKey key = new HashKey (data_source, data_member);
86 BindingManagerBase res = managers [key] as BindingManagerBase;
91 res = CreateBindingManager (data_source, data_member);
99 private BindingManagerBase CreateBindingManager (object data_source, string data_member)
101 if (data_member == "") {
102 if (IsListType (data_source.GetType ()))
103 return new CurrencyManager (data_source);
105 return new PropertyManager (data_source);
108 BindingMemberInfo info = new BindingMemberInfo (data_member);
110 BindingManagerBase parent_manager = this[data_source, info.BindingPath];
112 PropertyDescriptor pd = parent_manager == null ? null : parent_manager.GetItemProperties ().Find (info.BindingField, true);
115 throw new ArgumentException (String.Format ("Cannot create a child list for field {0}.", info.BindingField));
117 if (IsListType (pd.PropertyType))
118 return new RelatedCurrencyManager (parent_manager, pd);
120 return new RelatedPropertyManager (parent_manager, info.BindingField);
124 bool IsListType (Type t)
126 return (typeof (IList).IsAssignableFrom (t)
127 || typeof (IListSource).IsAssignableFrom (t));
130 #region Public Instance Methods
131 public bool Contains(object dataSource)
133 return Contains (dataSource, String.Empty);
136 public bool Contains (object dataSource, string dataMember)
138 if (dataSource == null)
139 throw new ArgumentNullException ("dataSource");
140 if (dataMember == null)
141 dataMember = String.Empty;
143 HashKey key = new HashKey (dataSource, dataMember);
144 return managers [key] != null;
146 #endregion // Public Instance Methods
148 #region Protected Instance Methods
150 protected internal void Add (object dataSource, BindingManagerBase listManager)
152 AddCore (dataSource, listManager);
153 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, dataSource));
156 protected virtual void AddCore (object dataSource, BindingManagerBase listManager)
158 if (dataSource == null)
159 throw new ArgumentNullException ("dataSource");
160 if (listManager == null)
161 throw new ArgumentNullException ("listManager");
163 HashKey key = new HashKey (dataSource, String.Empty);
164 managers [key] = listManager;
167 protected internal void Clear ()
170 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
173 protected virtual void ClearCore ()
178 protected virtual void OnCollectionChanged (CollectionChangeEventArgs ccevent)
180 if (onCollectionChangedHandler != null) {
181 onCollectionChangedHandler (this, ccevent);
185 protected internal void Remove (object dataSource)
187 if (dataSource == null)
188 throw new ArgumentNullException ("dataSource");
190 RemoveCore (dataSource);
191 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, dataSource));
194 protected virtual void RemoveCore (object dataSource)
196 HashKey[] keys = new HashKey [managers.Keys.Count];
197 managers.Keys.CopyTo (keys, 0);
199 for (int i = 0; i < keys.Length; i ++) {
200 if (keys[i].source == dataSource)
201 managers.Remove (keys[i]);
204 #endregion // Protected Instance Methods
207 public event CollectionChangeEventHandler CollectionChanged {
208 add { throw new NotImplementedException (); }
209 remove { /* nothing to do here.. */ }
213 #region ICollection Interfaces
214 void ICollection.CopyTo (Array array, int index)
216 managers.CopyTo (array, index);
219 int ICollection.Count {
220 get { return managers.Count; }
223 bool ICollection.IsSynchronized {
224 get { return false; }
227 object ICollection.SyncRoot {
231 #endregion // ICollection Interfaces
233 #region IEnumerable Interfaces
234 [MonoTODO ("our enumerator is slightly different. in MS's implementation the Values are WeakReferences to the managers.")]
235 IEnumerator IEnumerable.GetEnumerator() {
236 return managers.GetEnumerator ();
238 #endregion // IEnumerable Interfaces