2007-01-01 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / GridTableStylesCollection.cs
index 22af8c6a08cb434023e93591b39f596ca801aaf4..ae4a0c3b89a5bb24343682e56a1ddba0dec382bd 100644 (file)
@@ -20,7 +20,7 @@
 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
 //
 // Author:
-//     Jordi Mas i Hernadez <jordi@ximian.com>
+//     Jordi Mas i Hernandez <jordi@ximian.com>
 //
 
 using System;
@@ -29,6 +29,7 @@ using System.ComponentModel;
 
 namespace System.Windows.Forms
 {
+       [ListBindable(false)]
        public class GridTableStylesCollection : BaseCollection, IList
        {
                private ArrayList items;
@@ -37,20 +38,14 @@ namespace System.Windows.Forms
                internal GridTableStylesCollection (DataGrid grid)
                {
                        items = new ArrayList ();
-                       owner = owner;
+                       owner = grid;
                }
 
                #region Public Instance Properties
-               public DataGridTableStyle this[string columnName] {
+               public DataGridTableStyle this[string tableName] {
                        get {
-                               for (int i = 0; i < items.Count; i++) {
-                                       DataGridTableStyle column = (DataGridTableStyle) items[i];
-                                       if (column.MappingName == columnName) {
-                                               return column;
-                                       }
-                               }
-
-                               return null;
+                               int idx = FromTableNameToIndex (tableName);
+                               return idx == -1 ? null : this [idx];
                        }
                }
 
@@ -96,18 +91,17 @@ namespace System.Windows.Forms
                #endregion Public Instance Properties
 
                #region Public Instance Methods
-               public virtual int Add (DataGridTableStyle column)
-               {
-                       int cnt = items.Add (column);
-
-                       OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, column));
+               public virtual int Add (DataGridTableStyle table)
+               {                       
+                       int cnt = AddInternal (table);
+                       OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, table));
                        return cnt;
                }
 
-               public void AddRange (DataGridTableStyle[] columns)
+               public virtual void AddRange (DataGridTableStyle[] tables)
                {
-                       foreach (DataGridTableStyle mi in columns)
-                               items.Add (mi);
+                       foreach (DataGridTableStyle mi in tables)
+                               AddInternal (mi);
 
                        OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
                }
@@ -118,14 +112,14 @@ namespace System.Windows.Forms
                        OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh , null));
                }
 
-               public bool Contains (DataGridTableStyle column)
+               public bool Contains (DataGridTableStyle table)
                {
-                       return items.Contains (column);
+                       return (FromTableNameToIndex (table.MappingName) != -1);
                }
 
                public bool Contains (string name)
                {
-                       return (this[name] != null);
+                       return (FromTableNameToIndex (name) != -1);
                }
 
                void ICollection.CopyTo (Array dest, int index)
@@ -140,21 +134,17 @@ namespace System.Windows.Forms
 
                int IList.Add (object value)
                {
-                       int cnt = items.Add (value);
-
-                       OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, null));
-                       return cnt;
+                       return Add ((DataGridTableStyle)value);
                }
 
                void IList.Clear ()
                {
-                       items.Clear ();
-                       OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh , null));
+                       Clear ();
                }
 
                bool IList.Contains (object value)
                {
-                       return items.Contains (value);
+                       return Contains ((DataGridTableStyle) value);
                }
 
                int IList.IndexOf (object value)
@@ -169,15 +159,12 @@ namespace System.Windows.Forms
 
                void IList.Remove (object value)
                {
-                       items.Remove (value);
-                       OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, value));
+                       Remove ((DataGridTableStyle) value);
                }
 
                void IList.RemoveAt (int index)
                {
-                       object item = items[index];
-                       items.RemoveAt (index);
-                       OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, item));
+                       RemoveAt (index);
                }
 
                protected void OnCollectionChanged (CollectionChangeEventArgs ccevent)
@@ -193,19 +180,58 @@ namespace System.Windows.Forms
                        OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, table));
                }
 
+               void MappingNameChanged (object sender, EventArgs args)
+               {
+                       OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
+               }
+
                public void RemoveAt (int index)
                {
-                       object item = items[index];
+                       DataGridTableStyle style = (DataGridTableStyle)items[index];
 
                        items.RemoveAt (index);
-                       OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, item));
+                       style.MappingNameChanged -= new EventHandler (MappingNameChanged);
+                       OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, style));
                }
 
                #endregion Public Instance Methods
 
                #region Events
                public event CollectionChangeEventHandler CollectionChanged;
-               #endregion Events
+               #endregion Events               
+               
+               
+               #region Private Instance Methods
+               private int AddInternal (DataGridTableStyle table)
+               {               
+                       // TODO: MS allows duplicate columns. How they diferenciate between them?               
+                       if (FromTableNameToIndex (table.MappingName) != -1) {
+                               throw new ArgumentException ("The TableStyles collection already has a TableStyle with this mapping name");
+                       }
+
+                       table.MappingNameChanged += new EventHandler (MappingNameChanged);
+                       table.DataGrid = owner;
+                       int cnt = items.Add (table);
+                       return cnt;
+               }
+               
+               private int FromTableNameToIndex (string tableName)
+               {               
+                       for (int i = 0; i < items.Count; i++) {
+                               DataGridTableStyle table = (DataGridTableStyle) items[i];
+                                                               
+                               if (table.MappingName == null || table.MappingName == string.Empty)
+                                       continue;
+
+                               if (String.Compare (table.MappingName, tableName, true) == 0) {
+                                       return i;
+                               }
+                       }
+                       
+                       return -1;
+               }
+                               
+               #endregion Private Instance Methods
        }
 }