2007-03-29 Jonathan Pobst <monkey@jpobst.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         [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 { return (DataGridColumnStyle) items[index]; }
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 { return items[index]; }
99                         set { throw new NotSupportedException (); }
100                 }
101
102                 #endregion Public Instance Properties
103                 
104                 #region Private Instance Properties
105                 internal bool FireEvents {
106                         get { return fire_event;}
107                         set { fire_event = value;}
108                 }
109                 #endregion Private Instance Properties
110
111                 #region Public Instance Methods
112                 public virtual int Add (DataGridColumnStyle column)
113                 {
114                         // TODO: MS allows duplicate columns. How they diferenciate between them?
115                         if (FromColumnNameToIndex (column.MappingName) != -1) {
116                                 throw new ArgumentException ("The ColumnStyles collection already has a column with this mapping name");
117                         }
118                         
119                         column.TableStyle = owner;
120                         column.SetDataGridInternal (owner.DataGrid);
121                         int cnt = items.Add (column);
122                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, column));
123                         return cnt;                     
124                 }
125
126                 public void AddRange (DataGridColumnStyle[] columns)
127                 {
128                         foreach (DataGridColumnStyle mi in columns)
129                                 Add (mi);                       
130                 }
131
132                 public void Clear ()
133                 {
134                         items.Clear ();
135                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh , null));
136                 }
137
138                 public bool Contains (DataGridColumnStyle column)
139                 {
140                         return (FromColumnNameToIndex (column.MappingName) != -1);
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 (FromColumnNameToIndex (name) != -1);
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                         return Add ((DataGridColumnStyle)value);                        
166                 }
167
168                 void IList.Clear ()
169                 {
170                         Clear ();
171                 }
172
173                 bool IList.Contains (object value)
174                 {
175                         return Contains ((DataGridColumnStyle)value);
176                 }
177
178                 int IList.IndexOf (object value)
179                 {
180                         return IndexOf ((DataGridColumnStyle)value);
181                 }
182
183                 void IList.Insert (int index, object value)
184                 {
185                         throw new NotSupportedException ();
186                 }
187
188                 void IList.Remove (object value)
189                 {
190                         Remove ((DataGridColumnStyle)value);
191                 }
192
193                 void IList.RemoveAt (int index)
194                 {
195                         RemoveAt (index);
196                 }
197                 
198                 public int IndexOf (DataGridColumnStyle element)
199                 {
200                         return items.IndexOf (element);
201                 }
202                 
203                 protected void OnCollectionChanged (CollectionChangeEventArgs ccevent)
204                 {                                               
205                         if (fire_event == true && CollectionChanged != null) {
206                                 CollectionChanged (this, ccevent);
207                         }
208                 }
209                 
210                 public void Remove (DataGridColumnStyle column)
211                 {
212                         items.Remove (column);
213                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, column));
214                 }
215                 
216                 public void RemoveAt (int index)
217                 {
218                         object item = items[index];
219                         items.RemoveAt (index);
220                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, item));
221                 }               
222                 
223                 public void ResetPropertyDescriptors ()
224                 {
225                         for (int i = 0; i < items.Count; i++) {
226                                 DataGridColumnStyle column = (DataGridColumnStyle) items[i];
227                                 if (column.PropertyDescriptor != null) {
228                                         column.PropertyDescriptor = null;
229                                 }
230                         }
231                 }
232
233                 #endregion Public Instance Methods
234
235                 #region Events
236                 public event CollectionChangeEventHandler CollectionChanged;            
237                 #endregion Events               
238                 
239                 #region Private Instance Methods
240                 private int FromColumnNameToIndex (string columnName)
241                 {       
242                         for (int i = 0; i < items.Count; i++) {
243                                 DataGridColumnStyle column = (DataGridColumnStyle) items[i];
244                                 
245                                 if (column.MappingName == null || column.MappingName == string.Empty)
246                                         continue;
247
248                                 if (String.Compare (column.MappingName, columnName, true) == 0) {
249                                         return i;
250                                 }
251                         }
252                         
253                         return -1;
254                 }
255                                 
256                 #endregion Private Instance Methods
257         }
258 }