Author: jackson
Date: 2006-05-30 15:53:30 -0400 (Tue, 30 May 2006)
New Revision: 61281

Modified:
   trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/BindingContext.cs
   trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/CurrencyManager.cs
Log:

        * BindingContext.cs: Create the currency manager tables here.
        This allows us to more easily create null tables (when bad data
        members are used), and more easily create related currency
        managers.
        * CurrencyManager.cs: All the table creation stuff is done by
        * the
        binding context now.
        - Current should throw an exception if listposition is -1.
        - CancelCurrentEdit/EndCurrentEdit, do nothing if the list
          hasn't
        been bound yet.



Modified: 
trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/BindingContext.cs
===================================================================
--- 
trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/BindingContext.cs    
    2006-05-30 19:02:21 UTC (rev 61280)
+++ 
trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/BindingContext.cs    
    2006-05-30 19:53:30 UTC (rev 61281)
@@ -24,6 +24,7 @@
 //     Jackson Harper  [EMAIL PROTECTED]
 
 
+using System.Data;
 using System.Collections;
 using System.Globalization;
 using System.ComponentModel;
@@ -57,6 +58,9 @@
                                if (res != null)
                                        return res;
                                res = CreateBindingManager (source, member);
+                               Console.WriteLine ("CREATED BINDING MANAGER:   
{0}", res);
+                               if (res == null)
+                                       return null;
                                members [member] = res;
                                return res;
                        }
@@ -116,13 +120,71 @@
                        if (data_source is IList || 
                                data_source is IListSource ||
                                data_source is IBindingList) {
-                               CurrencyManager res = new CurrencyManager 
(data_source, data_member);
-                               return res;
+                               return CreateCurrencyManager (data_source, 
data_member);
                        }
 
                        return new PropertyManager (data_source, data_member);
                }
 
+               private static CurrencyManager CreateCurrencyManager (object 
data_source, string data_member)
+               {
+                       IList list = null;
+
+                       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 ());
+                       }
+
+                       DataTable table = data_source as DataTable;
+                       if (table == null && data_source is DataView)
+                               table = ((DataView) data_source).Table;
+
+                       DataSet dataset = data_source as DataSet;
+                       if (table == null && dataset != null) {
+                               string table_name = data_member;
+                               int sp = data_member != null ? 
data_member.IndexOf ('.') : -1;
+                               if (sp != -1) {
+                                       table_name = data_member.Substring (0, 
sp);
+                                       data_member = data_member.Substring (sp 
+ 1);
+                               }
+                               if (dataset != null && table_name != 
String.Empty) {
+                                       Console.WriteLine ("TABLE NAME:  {0}   
data member:   {1}", table_name, data_member);
+                                       table = dataset.Tables [table_name];
+                                       if (table == null)
+                                               throw new ArgumentException 
(String.Format ("Specified data member table {0} does not exist in the data 
source DataSet", data_member));
+                                       if (data_member != table_name) {
+                                               /*
+                                               Console.WriteLine ("CHECKING 
FOR COLUMN:   {0}", data_member);
+                                               DataColumn col = table.Columns 
[data_member];
+                                               DataRelation rel = (col == null 
? dataset.Relations [data_member] : null);
+                                               Console.WriteLine ("COLUMN:   
{0}    RELATION:  {1}", col, rel);
+                                               if (rel == null && col == null)
+                                                       throw new 
ArgumentException (String.Format ("Specified data member {0} does not exist in 
the data table {1}", data_member, table_name));
+
+                                               // FIXME: hmm, in such case 
what should we do?
+                                               */
+                                               table = null;
+                                               list = null;
+                                       }
+                               }
+                       }
+
+                       Console.WriteLine ("DATA TABLE:   {0}", table);
+                       
+                       if (table != null) {
+                               Console.WriteLine ("CREATING VIEW ON:  {0}", 
table.TableName);
+                               list = new DataView (table);
+                       }
+
+                       if (list == null)
+                               return null;
+                       return new CurrencyManager (list);
+               }
+
                #region Public Instance Methods
                public bool Contains(object dataSource)
                {

Modified: 
trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/CurrencyManager.cs
===================================================================
--- 
trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/CurrencyManager.cs   
    2006-05-30 19:02:21 UTC (rev 61280)
+++ 
trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/CurrencyManager.cs   
    2006-05-30 19:53:30 UTC (rev 61281)
@@ -39,64 +39,27 @@
                private IList list;
                private bool binding_suspended;
 
-               internal CurrencyManager (object data_source, string 
data_member)
+               internal CurrencyManager (IList data_source)
                {
-                       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 ());
-                       }
+                       list = data_source;
 
                        if (data_source as ArrayList != null) {
-                               finalType = ((ArrayList)data_source).GetType ();
+                               finalType = ((ArrayList) data_source).GetType 
();
+                       } else if (data_source as Array != null) {
+                               finalType = ((Array) data_source).GetType ();
                        } else {
-                               if (data_source as Array != null) {
-                                       finalType = ((Array) 
data_source).GetType ();
-                               } else {
-                                       finalType = null;
-                               }
+                               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;
-                               string table_name = data_member;
-                               int sp = data_member != null ? 
data_member.IndexOf ('.') : -1;
-                               if (sp != -1) {
-                                       table_name = data_member.Substring (0, 
sp);
-                                       data_member = data_member.Substring (sp 
+ 1);
-                               }
-                               if (dataset != null && table_name != 
String.Empty) {
-                                       table = dataset.Tables [table_name];
-                                       if (table == null)
-                                               throw new ArgumentException 
(String.Format ("Specified data member table {0} does not exist in the data 
source DataSet", data_member));
-                                       if (data_member != table_name) {
-                                               DataColumn col = table.Columns 
[data_member];
-                                               DataRelation rel = (col == null 
? dataset.Relations [data_member] : null);
-                                               if (rel == null && col == null)
-                                                       throw new 
ArgumentException (String.Format ("Specified data member {0} does not exist in 
the data table {1}", data_member, table_name));
-
-                                               // FIXME: hmm, in such case 
what should we do?
-                                       }
-                               }
+                       DataView view = list as DataView;
+                       if (view != null) {
+                               view.ListChanged += new ListChangedEventHandler 
(ListChangedHandler);
+                               view.Table.Columns.CollectionChanged  += new 
CollectionChangeEventHandler (MetaDataChangedHandler);
+                               view.Table.ChildRelations.CollectionChanged  += 
new CollectionChangeEventHandler (MetaDataChangedHandler);
+                               view.Table.ParentRelations.CollectionChanged  
+= new CollectionChangeEventHandler (MetaDataChangedHandler);
+                               view.Table.Constraints.CollectionChanged += new 
CollectionChangeEventHandler (MetaDataChangedHandler);
                        }
 
-                       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);
-                       }
-
                        if (list.Count > 0)
                                listposition = 0;
                        else
@@ -109,8 +72,8 @@
 
                public override object Current {
                        get {
-                               if (list.Count == 0 || listposition == -1)
-                                       return null;
+                               if (listposition == -1 || listposition >= 
list.Count)
+                                       throw new IndexOutOfRangeException 
("list position");
                                return list [listposition];
                        }
                }
@@ -217,6 +180,9 @@
 
                public override void CancelCurrentEdit ()
                {
+                       if (listposition == -1)
+                               return;
+
                        IEditableObject editable = Current as IEditableObject;
 
                        if (editable == null)
@@ -227,6 +193,9 @@
                
                public override void EndCurrentEdit ()
                {
+                       if (listposition == -1)
+                               return;
+
                        IEditableObject editable = Current as IEditableObject;
 
                        if (editable == null)
@@ -315,6 +284,10 @@
 
                private void ListChangedHandler (object sender, 
ListChangedEventArgs e)
                {
+                       // Start a latebound list
+                       if (listposition == -1)
+                               listposition = 0;
+
                        OnItemChanged (new ItemChangedEventArgs (-1));
                        UpdateIsBinding ();
                }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to