2006-05-15 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / BindingContext.cs
index 69d3a50960afdc13d608d1881ed5148c8b8c6c3d..234de4104ce077b4112d14ab041e2942bc97b9a6 100644 (file)
 //
 // Authors:
 //     Peter Bartok    pbartok@novell.com
-//
+//     Jackson Harper  jackson@ximian.com
 
-// NOT COMPLETE
 
 using System.Collections;
+using System.Globalization;
 using System.ComponentModel;
 
+
 namespace System.Windows.Forms {
+
        [DefaultEvent("CollectionChanged")]
        public class BindingContext : ICollection, IEnumerable {
-               #region Public Constructors
-               public BindingContext() {
+
+               private Hashtable managers;
+               private object null_data_source = new object ();
+
+               private class DataSourceEntry {
+
+                       private object source;
+                       private Hashtable members;
+                       // private BindingManagerBase default_manager;
+                       
+                       public DataSourceEntry (object source)
+                       {
+                               this.source = source;
+                               members = new Hashtable ();
+                       }
+
+                       public BindingManagerBase AddMember (string member)
+                       {
+                               if (member == null)
+                                       member = String.Empty;
+                               BindingManagerBase res = members [member] as BindingManagerBase;
+                               if (res != null)
+                                       return res;
+                               res = CreateBindingManager (source, member);
+                               members [member] = res;
+                               return res;
+                       }
+
+                       public void AddMember (string member, BindingManagerBase manager)
+                       {
+                               members [member] = manager;
+                       }
+
+                       public bool Contains (string member)
+                       {
+                               return members.Contains (member);
+                       }
+               }
+
+               public BindingContext () 
+               {
+                       managers = new Hashtable ();
                }
-               #endregion      // Public Constructors
 
-               #region Public Instance Properties
-               [MonoTODO]
                public bool IsReadOnly {
                        get {
-                               throw new NotImplementedException();
+                               return false;
                        }
                }
 
-               public BindingManagerBase this[object dataSource] {
+               public BindingManagerBase this [object dataSource] {
                        get {
-                               return this[dataSource, String.Empty];
+                               return this [dataSource, String.Empty];
                        }
                }
 
-               [MonoTODO]
-               public BindingManagerBase this[object dataSource, string dataMember] {
+               public BindingManagerBase this [object data_source, string data_member] {
                        get {
-                               throw new NotImplementedException();
+                               DataSourceEntry ds = GetEntry (data_source, data_member, true);
+                               return ds.AddMember (data_member);
+                       }
+               }
+
+               private DataSourceEntry GetEntry (object data_source, string data_member, bool create)
+               {
+                       if (data_source == null)
+                               data_source = null_data_source;
+                               
+                       DataSourceEntry ds = managers [data_source] as DataSourceEntry;
+                       if (ds == null && create) {
+                               ds = new DataSourceEntry (data_source);
+                               managers [data_source] = ds;
                        }
+
+                       return ds;
                }
 
-               
-               #endregion      // Public Instance Properties
+               private static BindingManagerBase CreateBindingManager (object data_source, 
+                       string data_member)
+               {
+                       if (data_source is IList || 
+                               data_source is IListSource ||
+                               data_source is IBindingList) {
+                               CurrencyManager res = new CurrencyManager (data_source, data_member);
+                               return res;
+                       }
+
+                       return new PropertyManager (data_source, data_member);
+               }
 
                #region Public Instance Methods
-               public bool Contains(object dataSource) {
-                       return Contains(dataSource, String.Empty);
+               public bool Contains(object dataSource)
+               {
+                       return Contains (dataSource, String.Empty);
                }
 
-               [MonoTODO]
-               public bool Contains(object dataSource, string dataMember) {
-                       throw new NotImplementedException();
+               public bool Contains (object dataSource, string dataMember)
+               {
+                       DataSourceEntry ds = GetEntry (dataSource, dataMember, false);
+                       if (ds == null)
+                               return false;
+                       return ds.Contains (dataMember);
+
                }
                #endregion      // Public Instance Methods
 
                #region Protected Instance Methods
-               protected internal void Add(object dataSource, BindingManagerBase listManager) {
-                       AddCore(dataSource, listManager);
+
+               protected internal void Add (object dataSource, BindingManagerBase listManager)
+               {
+                       AddCore (dataSource, listManager);
+                       OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, dataSource));
                }
 
-               [MonoTODO]
-               protected virtual void AddCore(object dataSource, BindingManagerBase listManager) {
-                       throw new NotImplementedException();
+               protected virtual void AddCore (object dataSource, BindingManagerBase listManager)
+               {
+                       if (dataSource == null)
+                               throw new ArgumentNullException ("dataSource");
+                       if (listManager == null)
+                               throw new ArgumentNullException ("listManager");
+                       DataSourceEntry ds = GetEntry (dataSource, String.Empty, true);
+                       ds.AddMember (String.Empty, listManager);
                }
 
-               protected internal void Clear() {
+               protected internal void Clear ()
+               {
                        ClearCore();
+                       OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
                }
 
-               [MonoTODO]
-               protected virtual void ClearCore() {
-                       throw new NotImplementedException();
+               protected virtual void ClearCore ()
+               {
+                       managers.Clear ();
                }
 
-               protected virtual void OnCollectionChanged(System.ComponentModel.CollectionChangeEventArgs ccevent) {
-                       if (CollectionChanged!=null) CollectionChanged(this, ccevent);
+               protected virtual void OnCollectionChanged(System.ComponentModel.CollectionChangeEventArgs ccevent)
+               {
+                       if (CollectionChanged != null) {
+                               CollectionChanged (this, ccevent);
+                       }
                }
 
-               protected internal void Remove(object dataSource) {
-                       RemoveCore(dataSource);
+               protected internal void Remove (object dataSource)
+               {
+                       RemoveCore (dataSource);
+                       OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, dataSource));
                }
 
-               [MonoTODO]
-               protected virtual void RemoveCore(object dataSource) {
-                       throw new NotImplementedException();
+               protected virtual void RemoveCore (object dataSource)
+               {
+                       managers.Remove (dataSource);
                }
                #endregion      // Protected Instance Methods
 
@@ -109,15 +192,14 @@ namespace System.Windows.Forms {
                #endregion      // Events
 
                #region ICollection Interfaces
-               [MonoTODO]
-               void ICollection.CopyTo(Array array, int index) {
-                       throw new NotImplementedException();
+               void ICollection.CopyTo (Array array, int index)
+               {
+                       managers.CopyTo (array, index);
                }
 
-               [MonoTODO]
                int ICollection.Count {
                        get {
-                               throw new NotImplementedException();
+                               return managers.Count;
                        }
                }
 
@@ -129,7 +211,7 @@ namespace System.Windows.Forms {
 
                object ICollection.SyncRoot {
                        get {
-                               return this;
+                               return null;
                        }
                }