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