New test.
[mono.git] / mcs / class / Managed.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
27 #if NET_2_0
28
29 using System.ComponentModel;
30 using System.Collections;
31
32 namespace System.Windows.Forms {
33
34
35         public class DataGridViewCellCollection : BaseCollection, IList, ICollection, IEnumerable {
36
37                 private DataGridViewRow dataGridViewRow;
38
39                 public DataGridViewCellCollection (DataGridViewRow dataGridViewRow) : base() {
40                         this.dataGridViewRow = dataGridViewRow;
41                 }
42
43                 public bool IsFixedSize {
44                         get { return base.List.IsFixedSize; }
45                 }
46
47                 object IList.this [int index] {
48                         get { return this[index]; }
49                         set { this[index] = value as DataGridViewCell; }
50                 }
51
52                 public DataGridViewCell this [int index] {
53                         get { return (DataGridViewCell) base.List[index]; }
54                         set { Insert(index, value); }
55                 }
56
57                 public DataGridViewCell this [string columnName] {
58                         get {
59                                 foreach (DataGridViewCell cell in base.List) {
60                                         if (cell.OwningColumn.Name == columnName) {
61                                                 return cell;
62                                         }
63                                 }
64                                 return null;
65                         }
66                         set {
67                                 for (int i = 0; i < base.List.Count; i++) {
68                                         DataGridViewCell cell = (DataGridViewCell) base.List[i];
69                                         if (cell.OwningColumn.Name == columnName) {
70                                                 Insert(i, value);
71                                                 return;
72                                         }
73                                 }
74                                 Add(value);
75                         }
76                 }
77
78                 public event CollectionChangeEventHandler CollectionChanged;
79
80                 int IList.Add (object o) {
81                         return Add(o as DataGridViewCell);
82                 }
83
84                 public virtual int Add (DataGridViewCell dataGridViewCell) {
85                         dataGridViewCell.SetOwningRow(dataGridViewRow);
86                         dataGridViewCell.SetColumnIndex(base.List.Count);
87                         dataGridViewCell.SetDataGridView(dataGridViewRow.DataGridView);
88                         int result = base.List.Add(dataGridViewCell);
89                         OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, dataGridViewCell));
90                         return result;
91                 }
92
93                 public virtual void AddRange (params DataGridViewCell[] dataGridViewCells) {
94                         foreach (DataGridViewCell cell in dataGridViewCells) {
95                                 this.Add(cell);
96                         }
97                 }
98
99                 public virtual void Clear () {
100                         base.List.Clear();
101                 }
102
103                 bool IList.Contains (object o) {
104                         return Contains(o as DataGridViewCell);
105                 }
106
107                 public virtual bool Contains (DataGridViewCell dataGridViewCell) {
108                         return base.List.Contains(dataGridViewCell);
109                 }
110
111                 public void CopyTo (DataGridViewCell[] array, int index) {
112                         base.List.CopyTo(array, index);
113                 }
114
115                 int IList.IndexOf (object o) {
116                         return IndexOf(o as DataGridViewCell);
117                 }
118
119                 public int IndexOf (DataGridViewCell dataGridViewCell) {
120                         return base.List.IndexOf(dataGridViewCell);
121                 }
122
123                 void IList.Insert (int index, object o) {
124                         Insert(index, o as DataGridViewCell);
125                 }
126
127                 public virtual void Insert (int index, DataGridViewCell dataGridViewCell) {
128                         dataGridViewCell.SetOwningRow(dataGridViewRow);
129                         dataGridViewCell.SetColumnIndex(index);
130                         dataGridViewCell.SetDataGridView(dataGridViewRow.DataGridView);
131                         base.List.Insert(index, dataGridViewCell);
132                         OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, dataGridViewCell));
133                 }
134
135                 void IList.Remove (object o) {
136                         Remove(o as DataGridViewCell);
137                 }
138
139                 public virtual void Remove (DataGridViewCell dataGridViewCell) {
140                         base.List.Remove(dataGridViewCell);
141                         OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Remove, dataGridViewCell));
142                 }
143
144                 public virtual void RemoveAt (int index) {
145                         DataGridViewCell cell = this[index];
146                         base.List.RemoveAt(index);
147                         OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Remove, cell));
148                 }
149
150                 protected override ArrayList List {
151                         get { return base.List; }
152                 }
153
154                 protected void OnCollectionChanged (CollectionChangeEventArgs e) {
155                         if (CollectionChanged != null) {
156                                 CollectionChanged(this, e);
157                         }
158                 }
159
160         }
161
162 }
163
164 #endif