Rename Managed.Windows.Forms to System.Windows.Forms for consistency.
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / DataGridViewCellCollection.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
21 //
22 // Author:
23 //      Pedro Martínez Juliá <pedromj@gmail.com>
24 //
25
26 using System.Collections;
27 using System.ComponentModel;
28
29 namespace System.Windows.Forms
30 {
31         [ListBindable (false)]
32         public class DataGridViewCellCollection : BaseCollection, IList, ICollection, IEnumerable
33         {
34                 private DataGridViewRow dataGridViewRow;
35
36                 public DataGridViewCellCollection (DataGridViewRow dataGridViewRow) : base()
37                 {
38                         this.dataGridViewRow = dataGridViewRow;
39                 }
40
41                 bool IList.IsFixedSize {
42                         get { return base.List.IsFixedSize; }
43                 }
44
45                 object IList.this [int index] {
46                         get { return this [index]; }
47                         set { this [index] = value as DataGridViewCell; }
48                 }
49
50                 public DataGridViewCell this [int index] {
51                         get { return (DataGridViewCell) base.List [index]; }
52                         set {
53                                 if (value == null)
54                                         throw new ArgumentNullException ("value");
55
56                                 Insert(index, value);
57                         }
58                 }
59
60                 internal DataGridViewCell GetCellInternal (int colIndex)
61                 {
62                         return (DataGridViewCell) base.List [colIndex];
63                 }
64                 
65                 public DataGridViewCell this [string columnName] {
66                         get {
67                                 if (columnName == null)
68                                         throw new ArgumentNullException ("columnName");
69
70                                 foreach (DataGridViewCell cell in base.List) {
71                                         if (string.Compare (cell.OwningColumn.Name, columnName, true) == 0)
72                                                 return cell;
73                                 }
74
75                                 throw new ArgumentException (string.Format (
76                                         "Column name {0} cannot be found.",
77                                         columnName), "columnName");
78                         }
79                         set {
80                                 if (columnName == null)
81                                         throw new ArgumentNullException ("columnName");
82                                 if (value == null)
83                                         throw new ArgumentNullException ("value");
84
85                                 for (int i = 0; i < base.List.Count; i++) {
86                                         DataGridViewCell cell = (DataGridViewCell) base.List [i];
87                                         if (string.Compare (cell.OwningColumn.Name, columnName, true) == 0) {
88                                                 Insert (i, value);
89                                                 return;
90                                         }
91                                 }
92                                 Add (value);
93                         }
94                 }
95
96                 internal DataGridViewCell GetBoundCell (string dataPropertyName)
97                 {
98                         foreach (DataGridViewCell cell in base.List) {
99                                 if (string.Compare (cell.OwningColumn.DataPropertyName, dataPropertyName, true) == 0)
100                                         return cell;
101                         }
102                         
103                         return null;
104                 }
105                 
106                 public event CollectionChangeEventHandler CollectionChanged;
107
108                 int IList.Add (object value)
109                 {
110                         return Add (value as DataGridViewCell);
111                 }
112
113                 public virtual int Add (DataGridViewCell dataGridViewCell)
114                 {
115                         int result = base.List.Add (dataGridViewCell);
116                         dataGridViewCell.SetOwningRow (dataGridViewRow);
117                         dataGridViewCell.SetColumnIndex (result);
118                         dataGridViewCell.SetDataGridView (dataGridViewRow.DataGridView);
119                         OnCollectionChanged (new CollectionChangeEventArgs (
120                                 CollectionChangeAction.Add, dataGridViewCell));
121                         return result;
122                 }
123
124                 internal void Replace (int columnIndex, DataGridViewCell dataGridViewCell)
125                 {
126                         RemoveAt (columnIndex);
127                         Insert (columnIndex, dataGridViewCell);
128                 }
129
130                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
131                 public virtual void AddRange (params DataGridViewCell[] dataGridViewCells)
132                 {
133                         foreach (DataGridViewCell cell in dataGridViewCells)
134                                 Add (cell);
135                 }
136
137                 public virtual void Clear ()
138                 {
139                         base.List.Clear();
140                 }
141
142                 bool IList.Contains (object value)
143                 {
144                         return Contains (value as DataGridViewCell);
145                 }
146
147                 public virtual bool Contains (DataGridViewCell dataGridViewCell)
148                 {
149                         return base.List.Contains (dataGridViewCell);
150                 }
151
152                 public void CopyTo (DataGridViewCell[] array, int index)
153                 {
154                         base.List.CopyTo (array, index);
155                 }
156
157                 int IList.IndexOf (object value)
158                 {
159                         return IndexOf (value as DataGridViewCell);
160                 }
161
162                 public int IndexOf (DataGridViewCell dataGridViewCell)
163                 {
164                         return base.List.IndexOf (dataGridViewCell);
165                 }
166
167                 void IList.Insert (int index, object value)
168                 {
169                         Insert (index, value as DataGridViewCell);
170                 }
171
172                 public virtual void Insert (int index, DataGridViewCell dataGridViewCell)
173                 {
174                         base.List.Insert (index, dataGridViewCell);
175                         dataGridViewCell.SetOwningRow (dataGridViewRow);
176                         dataGridViewCell.SetColumnIndex (index);
177                         dataGridViewCell.SetDataGridView (dataGridViewRow.DataGridView);
178                         ReIndex ();
179                         OnCollectionChanged (new CollectionChangeEventArgs (
180                                 CollectionChangeAction.Add, dataGridViewCell));
181                 }
182
183                 void IList.Remove (object value)
184                 {
185                         Remove (value as DataGridViewCell);
186                 }
187
188                 public virtual void Remove (DataGridViewCell cell)
189                 {
190                         base.List.Remove (cell);
191                         ReIndex ();
192                         OnCollectionChanged (new CollectionChangeEventArgs (
193                                 CollectionChangeAction.Remove, cell));
194                 }
195
196                 public virtual void RemoveAt (int index)
197                 {
198                         DataGridViewCell cell = this [index];
199                         base.List.RemoveAt (index);
200                         ReIndex ();
201                         OnCollectionChanged (new CollectionChangeEventArgs (
202                                 CollectionChangeAction.Remove, cell));
203                 }
204
205                 private void ReIndex ()
206                 {
207                         for (int i = 0; i < base.List.Count; i++)
208                                 this[i].SetColumnIndex (i);
209                 }
210
211                 protected override ArrayList List {
212                         get { return base.List; }
213                 }
214
215                 protected void OnCollectionChanged (CollectionChangeEventArgs e)
216                 {
217                         if (CollectionChanged != null) {
218                                 CollectionChanged(this, e);
219                         }
220                 }
221         }
222 }