Make a copy of the old ZipLib
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / CurrencyManager.cs
index 76cff46876ca7a3ef2c997bae57226284dd544dd..23a214175fd57e569c280a09ed0d1f9dc2ef3995 100644 (file)
 //
 
 using System;
+using System.Data;
+using System.Reflection;
 using System.Collections;
 using System.ComponentModel;
 
 namespace System.Windows.Forms {
-
+       [DefaultMember("Item")]
        public class CurrencyManager : BindingManagerBase {
 
                protected Type finalType;
                protected int listposition;
 
                private IList list;
-               private IBindingList binding_list;
-
                private bool binding_suspended;
 
                internal CurrencyManager (object data_source)
-               {
-                       binding_list = data_source as IBindingList;
+               {                       
                        if (data_source is IListSource) {
                                list = ((IListSource) data_source).GetList ();
                        } else if (data_source is IList) {
@@ -60,7 +59,18 @@ 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) {
+                               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; }
@@ -92,23 +102,47 @@ namespace System.Windows.Forms {
                                OnPositionChanged (EventArgs.Empty);
                        }
                }
+               
+               internal string ListName {
+                       get {
+                               ITypedList typed = list as ITypedList;
+                               
+                               if (typed == null) {
+                                       return finalType.Name;
+                               } else {
+                                       return typed.GetListName (null);
+                               }
+                       }               
+               }
 
                public override PropertyDescriptorCollection GetItemProperties ()
                {
                        ITypedList typed = list as ITypedList;
-                       
+
+                       if (list is Array) {
+                               Type element = list.GetType ().GetElementType ();
+                               return TypeDescriptor.GetProperties (element);
+                       }
+
                        if (typed != null) {
                                return typed.GetItemProperties (null);
                        }
-                               
-                       if (list.Count > 0){ 
-                               System.Attribute[] att = new System.Attribute [1];
-                               att[0] = new BrowsableAttribute (true);                         
-                               return TypeDescriptor.GetProperties (list[0], att);
+
+                       PropertyInfo [] props = finalType.GetProperties ();
+                       for (int i = 0; i < props.Length; i++) {
+                               if (props [i].Name == "Item") {
+                                       Type t = props [i].PropertyType;
+                                       if (t == typeof (object))
+                                               continue;
+                                       return GetBrowsableProperties (t);
+                               }
+                       }
+
+                       if (list.Count > 0) {
+                               return GetBrowsableProperties (list [0].GetType ());
                        }
                        
-                       return new PropertyDescriptorCollection (new PropertyDescriptor [1]);
-                       
+                       return new PropertyDescriptorCollection (null);
                }
 
                public override void RemoveAt (int index)
@@ -129,12 +163,23 @@ namespace System.Windows.Forms {
                 internal override bool IsSuspended {
                         get { return binding_suspended; }
                 }
+                
+                internal bool CanAddRows {
+                       get {
+                               if (list as IBindingList == null) {
+                                       return false;
+                               }
+                               
+                               return true;
+                       }
+               }
 
                public override void AddNew ()
                {
-                       if (binding_list == null)
+                       if (list as IBindingList == null)
                                throw new NotSupportedException ();
-                       binding_list.AddNew ();
+                               
+                       (list as IBindingList).AddNew ();
                }
 
                public override void CancelCurrentEdit ()
@@ -156,6 +201,19 @@ namespace System.Windows.Forms {
                        editable.EndEdit ();
                }
 
+               public void Refresh ()
+               {
+                       PullData ();
+               }
+
+               [MonoTODO ("This is just a guess, as I can't figure out how to test this method")]
+               protected void CheckEmpty ()
+               {
+                       if (list == null || list.Count < 1)
+                               throw new Exception ("List is empty.");
+                               
+               }
+
                protected internal override void OnCurrentChanged (EventArgs e)
                {
                        PullData ();
@@ -209,9 +267,23 @@ namespace System.Windows.Forms {
                internal object GetItem (int index)
                {
                        return list [index];
+               }               
+               
+               private PropertyDescriptorCollection GetBrowsableProperties (Type t)
+               {
+                       Attribute [] att = new System.Attribute [1];
+                       att [0] = new BrowsableAttribute (true);
+                       return TypeDescriptor.GetProperties (t, att);
+               }
+
+               private void MetaDataChangedHandler (object sender, CollectionChangeEventArgs e)
+               {
+                       if (MetaDataChanged != null)
+                               MetaDataChanged (this, EventArgs.Empty);
                }
 
                public event ItemChangedEventHandler ItemChanged;
+               public event EventHandler MetaDataChanged;
        }
 }