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