2006-05-15 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / CurrencyManager.cs
index cc97a15c610ec73caa38a9908851dedba77bfc83..2479e7a739b37ebdef6c8f50277e50bc8a5e06fc 100644 (file)
@@ -24,6 +24,7 @@
 //
 
 using System;
+using System.Data;
 using System.Reflection;
 using System.Collections;
 using System.ComponentModel;
@@ -38,12 +39,12 @@ namespace System.Windows.Forms {
                private IList list;
                private bool binding_suspended;
 
-               internal CurrencyManager (object data_source)
-               {                       
-                       if (data_source is IListSource) {
-                               list = ((IListSource) data_source).GetList ();
-                       } else if (data_source is IList) {
+               internal CurrencyManager (object data_source, string data_member)
+               {
+                       if (data_source is IList) {
                                list = (IList) data_source;
+                       } else if (data_source is IListSource) {
+                               list = ((IListSource) data_source).GetList ();
                        } else {
                                throw new Exception ("Attempted to create currency manager " +
                                        "from invalid type: " + data_source.GetType ());
@@ -58,7 +59,32 @@ namespace System.Windows.Forms {
                                        finalType = null;
                                }
                        }
-               }               
+
+                       DataTable table = data_source as DataTable;
+                       if (table == null && data_source is DataView)
+                               table = ((DataView) data_source).Table;
+
+                       
+                       if (table == null) {
+                               DataSet dataset = data_source as DataSet;
+                               int sp = data_member.IndexOf ('.');
+                               if (sp != -1) {
+                                       data_member = data_member.Substring (0, sp);
+                               }
+                               if (dataset != null) {
+                                       table = dataset.Tables [data_member];
+                               }
+                       }
+
+                       if (table != null) {
+                               list = new DataView (table);
+                               ((DataView) list).ListChanged += new ListChangedEventHandler (ListChangedHandler);
+                               table.Columns.CollectionChanged  += new CollectionChangeEventHandler (MetaDataChangedHandler);
+                               table.ChildRelations.CollectionChanged  += new CollectionChangeEventHandler (MetaDataChangedHandler);
+                               table.ParentRelations.CollectionChanged  += new CollectionChangeEventHandler (MetaDataChangedHandler);
+                               table.Constraints.CollectionChanged += new CollectionChangeEventHandler (MetaDataChangedHandler);
+                       }
+               }
 
                public IList List {
                        get { return list; }
@@ -66,6 +92,8 @@ namespace System.Windows.Forms {
 
                public override object Current {
                        get {
+                               if (list.Count == 0)
+                                       return null;
                                return list [listposition];
                        }
                }
@@ -189,6 +217,18 @@ namespace System.Windows.Forms {
                        editable.EndEdit ();
                }
 
+               public void Refresh ()
+               {
+                       PullData ();
+               }
+
+               protected void CheckEmpty ()
+               {
+                       if (list == null || list.Count < 1)
+                               throw new Exception ("List is empty.");
+                               
+               }
+
                protected internal override void OnCurrentChanged (EventArgs e)
                {
                        PullData ();
@@ -224,7 +264,6 @@ namespace System.Windows.Forms {
                        return String.Empty;
                }
 
-               [MonoTODO ("Not totally sure how this works, its doesn't seemt to do a pull/push like i originally assumed")]
                protected override void UpdateIsBinding ()
                {
                        UpdateItem ();
@@ -251,7 +290,20 @@ namespace System.Windows.Forms {
                        return TypeDescriptor.GetProperties (t, att);
                }
 
+               private void MetaDataChangedHandler (object sender, CollectionChangeEventArgs e)
+               {
+                       if (MetaDataChanged != null)
+                               MetaDataChanged (this, EventArgs.Empty);
+               }
+
+               private void ListChangedHandler (object sender, ListChangedEventArgs e)
+               {
+                       OnItemChanged (new ItemChangedEventArgs (-1));  
+                       UpdateIsBinding ();
+               }
+
                public event ItemChangedEventHandler ItemChanged;
+               public event EventHandler MetaDataChanged;
        }
 }