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