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 ();
68 onCollectionChangedHandler = null;
71 public bool IsReadOnly {
75 public BindingManagerBase this [object dataSource] {
76 get { return this [dataSource, String.Empty]; }
79 public BindingManagerBase this [object dataSource, string dataMember] {
81 if (dataSource == null)
82 throw new ArgumentNullException ("dataSource");
83 if (dataMember == null)
84 dataMember = String.Empty;
87 ICurrencyManagerProvider cm_provider = dataSource as ICurrencyManagerProvider;
88 if (cm_provider != null) {
89 if (dataMember.Length == 0)
90 return cm_provider.CurrencyManager;
92 return cm_provider.GetRelatedCurrencyManager (dataMember);
96 HashKey key = new HashKey (dataSource, dataMember);
97 BindingManagerBase res = managers [key] as BindingManagerBase;
102 res = CreateBindingManager (dataSource, dataMember);
105 managers [key] = res;
110 private BindingManagerBase CreateBindingManager (object data_source, string data_member)
112 if (data_member == "") {
113 if (IsListType (data_source.GetType ()))
114 return new CurrencyManager (data_source);
116 return new PropertyManager (data_source);
119 BindingMemberInfo info = new BindingMemberInfo (data_member);
121 BindingManagerBase parent_manager = this[data_source, info.BindingPath];
123 PropertyDescriptor pd = parent_manager == null ? null : parent_manager.GetItemProperties ().Find (info.BindingField, true);
126 throw new ArgumentException (String.Format ("Cannot create a child list for field {0}.", info.BindingField));
128 if (IsListType (pd.PropertyType))
129 return new RelatedCurrencyManager (parent_manager, pd);
131 return new RelatedPropertyManager (parent_manager, info.BindingField);
135 bool IsListType (Type t)
137 return (typeof (IList).IsAssignableFrom (t)
138 || typeof (IListSource).IsAssignableFrom (t));
141 #region Public Instance Methods
142 public bool Contains(object dataSource)
144 return Contains (dataSource, String.Empty);
147 public bool Contains (object dataSource, string dataMember)
149 if (dataSource == null)
150 throw new ArgumentNullException ("dataSource");
151 if (dataMember == null)
152 dataMember = String.Empty;
154 HashKey key = new HashKey (dataSource, dataMember);
155 return managers [key] != null;
157 #endregion // Public Instance Methods
159 #region Protected Instance Methods
161 protected internal void Add (object dataSource, BindingManagerBase listManager)
163 AddCore (dataSource, listManager);
164 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, dataSource));
167 protected virtual void AddCore (object dataSource, BindingManagerBase listManager)
169 if (dataSource == null)
170 throw new ArgumentNullException ("dataSource");
171 if (listManager == null)
172 throw new ArgumentNullException ("listManager");
174 HashKey key = new HashKey (dataSource, String.Empty);
175 managers [key] = listManager;
178 protected internal void Clear ()
181 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
184 protected virtual void ClearCore ()
189 protected virtual void OnCollectionChanged (CollectionChangeEventArgs ccevent)
191 if (onCollectionChangedHandler != null) {
192 onCollectionChangedHandler (this, ccevent);
196 protected internal void Remove (object dataSource)
198 if (dataSource == null)
199 throw new ArgumentNullException ("dataSource");
201 RemoveCore (dataSource);
202 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, dataSource));
205 protected virtual void RemoveCore (object dataSource)
207 HashKey[] keys = new HashKey [managers.Keys.Count];
208 managers.Keys.CopyTo (keys, 0);
210 for (int i = 0; i < keys.Length; i ++) {
211 if (keys[i].source == dataSource)
212 managers.Remove (keys[i]);
216 [MonoTODO ("Stub, does nothing")]
217 public static void UpdateBinding (BindingContext newBindingContext, Binding binding)
221 #endregion // Protected Instance Methods
226 [EditorBrowsable (EditorBrowsableState.Never)]
228 public event CollectionChangeEventHandler CollectionChanged {
229 add { throw new NotImplementedException (); }
230 remove { /* nothing to do here.. */ }
234 #region ICollection Interfaces
235 void ICollection.CopyTo (Array ar, int index)
237 managers.CopyTo (ar, index);
240 int ICollection.Count {
241 get { return managers.Count; }
244 bool ICollection.IsSynchronized {
245 get { return false; }
248 object ICollection.SyncRoot {
252 #endregion // ICollection Interfaces
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 ();
259 #endregion // IEnumerable Interfaces