New test.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridViewColumnCollection.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         public class DataGridViewColumnCollection : BaseCollection, IList, ICollection, IEnumerable {
35
36                 private DataGridView dataGridView;
37
38                 public DataGridViewColumnCollection (DataGridView dataGridView) {
39                         this.dataGridView = dataGridView;
40                 }
41
42                 public bool IsFixedSize {
43                         get { return base.List.IsFixedSize; }
44                 }
45
46                 object IList.this [int index] {
47                         get { return this[index]; }
48                         set { throw new NotSupportedException(); }
49                 }
50
51                 public DataGridViewColumn this [int index] {
52                         get { return (DataGridViewColumn) base.List[index]; }
53                 }
54
55                 public DataGridViewColumn this [string columnName] {
56                         get {
57                                 foreach (DataGridViewColumn col in base.List) {
58                                         if (col.Name == columnName) {
59                                                 return col;
60                                         }
61                                 }
62                                 return null;
63                         }
64                 }
65
66                 public event CollectionChangeEventHandler CollectionChanged;
67
68                 int IList.Add (object o) {
69                         return Add(o as DataGridViewColumn);
70                 }
71
72                 public virtual int Add (DataGridViewColumn dataGridViewColumn) {
73                         dataGridViewColumn.SetIndex(base.List.Count);
74                         dataGridViewColumn.SetDataGridView(dataGridView);
75                         int result = base.List.Add(dataGridViewColumn);
76                         OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, dataGridViewColumn));
77                         return result;
78                 }
79
80                 public virtual int Add (string columnName, string headerText) {
81                         DataGridViewColumn col = new DataGridViewColumn();
82                         col.Name = columnName;
83                         col.HeaderText = headerText;
84                         return Add(col);
85                 }
86
87                 public virtual void AddRange (params DataGridViewColumn[] dataGridViewColumns) {
88                         foreach (DataGridViewColumn col in dataGridViewColumns) {
89                                 Add(col);
90                         }
91                 }
92
93                 public virtual void Clear () {
94                         base.List.Clear();
95                 }
96
97                 bool IList.Contains (object o) {
98                         return Contains(o as DataGridViewColumn);
99                 }
100
101                 public virtual bool Contains (DataGridViewColumn dataGridViewColumn) {
102                         return base.List.Contains(dataGridViewColumn);
103                 }
104
105                 public virtual bool Contains (string columnName) {
106                         foreach (DataGridViewColumn col in base.List) {
107                                 if (col.Name == columnName) {
108                                         return true;
109                                 }
110                         }
111                         return false;
112                 }
113
114                 public void CopyTo (DataGridViewColumn[] array, int index) {
115                         base.List.CopyTo(array, index);
116                 }
117
118                 public int GetColumnCount (DataGridViewElementStates includeFilter) {
119                         return 0;
120                 }
121
122                 public int GetColumnsWidth (DataGridViewElementStates includeFilter) {
123                         return 0;
124                 }
125
126                 public DataGridViewColumn GetFirstColumn (DataGridViewElementStates includeFilter) {
127                         return null;
128                 }
129
130                 public DataGridViewColumn GetFirstColumn (DataGridViewElementStates includeFilter, DataGridViewElementStates excludeFilter) {
131                         return null;
132                 }
133
134                 public DataGridViewColumn GetLastColumn (DataGridViewElementStates includeFilter, DataGridViewElementStates excludeFilter) {
135                         return null;
136                 }
137
138                 public DataGridViewColumn GetNextColumn (DataGridViewColumn dataGridViewColumnStart, DataGridViewElementStates includeFilter, DataGridViewElementStates excludeFilter) {
139                         return null;
140                 }
141
142                 public DataGridViewColumn GetPreviousColumn (DataGridViewColumn dataGridViewColumnStart, DataGridViewElementStates includeFilter, DataGridViewElementStates excludeFilter) {
143                         return null;
144                 }
145
146                 int IList.IndexOf (object o) {
147                         return IndexOf(o as DataGridViewColumn);
148                 }
149
150                 public int IndexOf (DataGridViewColumn dataGridViewColumn) {
151                         return base.List.IndexOf(dataGridViewColumn);
152                 }
153
154                 void IList.Insert (int columnIndex, object o) {
155                         Insert(columnIndex, o as DataGridViewColumn);
156                 }
157
158                 public virtual void Insert (int columnIndex, DataGridViewColumn dataGridViewColumn) {
159                         dataGridViewColumn.SetIndex(columnIndex);
160                         base.List.Insert(columnIndex, dataGridViewColumn);
161                         OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, dataGridViewColumn));
162                 }
163
164                 void IList.Remove (object o) {
165                         Remove(o as DataGridViewColumn);
166                 }
167
168                 public virtual void Remove (DataGridViewColumn dataGridViewColumn) {
169                         base.List.Remove(dataGridViewColumn);
170                         OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Remove, dataGridViewColumn));
171                 }
172
173                 public virtual void Remove (string columnName) {
174                         foreach (DataGridViewColumn col in base.List) {
175                                 if (col.Name == columnName) {
176                                         Remove(col);
177                                         return;
178                                 }
179                         }
180                 }
181
182                 public virtual void RemoveAt (int index) {
183                         DataGridViewColumn col = this[index];
184                         base.List.RemoveAt(index);
185                         OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, col));
186                 }
187
188                 protected DataGridView DataGridView {
189                         get { return dataGridView; }
190                 }
191
192                 protected virtual void OnCollectionChanged (CollectionChangeEventArgs e) {
193                         if (CollectionChanged != null) {
194                                 CollectionChanged(this, e);
195                         }
196                 }
197
198                 protected override ArrayList List {
199                         get { return base.List; }
200                 }
201
202                 /************************/
203
204                 internal ArrayList ColumnDisplayIndexSortedArrayList {
205                         get {
206                                 ArrayList result = (ArrayList) base.List.Clone();
207                                 result.Sort(new ColumnDisplayIndexComparator());
208                                 return result;
209                         }
210                 }
211
212                 private class ColumnDisplayIndexComparator : IComparer {
213
214                         public int Compare (object o1, object o2) {
215                                 DataGridViewColumn col1 = (DataGridViewColumn) o1;
216                                 DataGridViewColumn col2 = (DataGridViewColumn) o2;
217                                 if (col1.DisplayIndex < col2.DisplayIndex) {
218                                         return -1;
219                                 }
220                                 else if (col1.DisplayIndex > col2.DisplayIndex) {
221                                         return 1;
222                                 }
223                                 else {
224                                         return 0;
225                                 }
226                         }
227
228                 }
229
230                 /************************/
231
232
233         }
234
235 }
236
237 #endif