2005-04-22 Jordi Mas i Hernandez <jordi@ximian.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / GridColumnStylesCollection.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 //      Jordi Mas i Hernandez <jordi@ximian.com>
24 //
25
26 using System;
27 using System.Collections;
28 using System.ComponentModel;
29
30 namespace System.Windows.Forms
31 {
32         public class GridColumnStylesCollection : BaseCollection, IList
33         {
34                 private ArrayList items;
35                 private DataGridTableStyle owner;
36                 private bool fire_event;
37
38                 internal GridColumnStylesCollection (DataGridTableStyle tablestyle)
39                 {
40                         items = new ArrayList ();
41                         owner = tablestyle;
42                         fire_event = true;
43                 }
44
45                 #region Public Instance Properties
46                 public DataGridColumnStyle this [string columnName] {
47                         get {
48                                 int idx = FromColumnNameToIndex (columnName);
49                                 return idx == -1 ? null : this [idx];
50                         }
51                 }
52
53                 public DataGridColumnStyle this [int index] {
54                         get {
55                                 return (DataGridColumnStyle) items[index];
56                         }
57                 }
58
59                 
60                 public DataGridColumnStyle this [PropertyDescriptor propDesc] {
61                         get {                           
62                                 for (int i = 0; i < items.Count; i++) {
63                                         DataGridColumnStyle column = (DataGridColumnStyle) items[i];
64                                         if (column.PropertyDescriptor.Equals (propDesc)) {
65                                                 return column;
66                                         }
67                                 }
68                                 
69                                 return null;
70                         }
71                 }
72
73                 protected override ArrayList List {
74                         get { return items; }
75                 }
76
77                 int ICollection.Count {
78                         get { return items.Count;}
79                 }
80
81                 bool ICollection.IsSynchronized {
82                         get { return false; }
83                 }
84
85                 object ICollection.SyncRoot {
86                         get { return this;}
87                 }
88
89                 bool IList.IsFixedSize {
90                         get { return false; }
91                 }
92
93                 bool IList.IsReadOnly {
94                         get { return false;}
95                 }
96
97                 object IList.this [int index] {
98                         get {
99                                 return items[index];
100                         }
101                         set {
102                                 throw new NotSupportedException ();
103                         }
104                 }
105
106                 #endregion Public Instance Properties
107                 
108                 #region Private Instance Properties
109                 internal bool FireEvents {
110                         get { return fire_event;}
111                         set { fire_event = value;}
112                 }
113                 #endregion Private Instance Properties
114
115                 #region Public Instance Methods
116                 public virtual int Add (DataGridColumnStyle column)
117                 {
118                         int cnt = AddInternal (column);
119                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, column));
120                         return cnt;
121                 }
122
123                 public void AddRange (DataGridColumnStyle[] columns)
124                 {
125                         foreach (DataGridColumnStyle mi in columns)
126                                 AddInternal (mi);
127
128                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
129                 }
130
131                 public void Clear ()
132                 {
133                         items.Clear ();
134                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh , null));
135                 }
136
137                 public bool Contains (DataGridColumnStyle column)
138                 {
139                         return items.Contains (column);
140                 }
141
142                 
143                 public bool Contains (PropertyDescriptor propDesc)
144                 {
145                         return (this [propDesc] != null);
146                 }
147
148                 public bool Contains (string name)
149                 {
150                         return (this[name] != null);
151                 }
152
153                 void ICollection.CopyTo (Array dest, int index)
154                 {
155                         items.CopyTo (dest, index);
156                 }
157
158                 IEnumerator IEnumerable.GetEnumerator ()
159                 {
160                         return items.GetEnumerator ();
161                 }
162
163                 int IList.Add (object value)
164                 {
165                         int cnt = AddInternal ((DataGridColumnStyle)value);
166
167                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, null));
168                         return cnt;
169                 }
170
171                 void IList.Clear ()
172                 {
173                         items.Clear ();
174                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh , null));
175                 }
176
177                 bool IList.Contains (object value)
178                 {
179                         return items.Contains (value);
180                 }
181
182                 int IList.IndexOf (object value)
183                 {
184                         return items.IndexOf (value);
185                 }
186
187                 void IList.Insert (int index, object value)
188                 {
189                         throw new NotSupportedException ();
190                 }
191
192                 void IList.Remove (object value)
193                 {
194                         items.Remove (value);
195                 }
196
197                 void IList.RemoveAt (int index)
198                 {
199                         object item = items[index];
200                         
201                         items.RemoveAt (index);
202                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, item));
203                 }
204                 
205                 public int IndexOf (DataGridColumnStyle element)
206                 {
207                         return items.IndexOf (element);
208                 }
209                 
210                 protected void OnCollectionChanged (CollectionChangeEventArgs ccevent)
211                 {                                               
212                         if (fire_event == true && CollectionChanged != null) {
213                                 CollectionChanged (this, ccevent);
214                         }
215                 }
216                 
217                 public void Remove (DataGridColumnStyle column)
218                 {
219                         items.Remove (column);
220                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, column));
221                 }
222                 
223                 public void RemoveAt (int index)
224                 {
225                         object item = items[index];
226                         items.RemoveAt (index);
227                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, item));
228                 }               
229                 
230                 public void ResetPropertyDescriptors ()
231                 {
232                         for (int i = 0; i < items.Count; i++) {
233                                 DataGridColumnStyle column = (DataGridColumnStyle) items[i];
234                                 if (column.PropertyDescriptor != null) {
235                                         column.PropertyDescriptor = null;
236                                 }
237                         }
238                 }
239
240                 #endregion Public Instance Methods
241
242                 #region Events
243                 public event CollectionChangeEventHandler CollectionChanged;            
244                 #endregion Events               
245                 
246                 #region Private Instance Methods
247                 private int AddInternal (DataGridColumnStyle column)
248                 {                               
249                         if (FromColumnNameToIndex (column.MappingName) != -1) {
250                                 throw new ArgumentException ("The ColumnStyles collection already has a column with this mapping name");
251                         }
252                         
253                         column.TableStyle = owner;
254                         int cnt = items.Add (column);
255                         return cnt;                     
256                 }
257                 
258                 private int FromColumnNameToIndex (string columnName)
259                 {               
260                         for (int i = 0; i < items.Count; i++) {
261                                 DataGridColumnStyle column = (DataGridColumnStyle) items[i];
262                                 if (column.MappingName == columnName) {
263                                         return i;
264                                 }
265                         }
266                         
267                         return -1;
268                 }
269                                 
270                 #endregion Private Instance Methods
271         }
272 }