2007-09-28 Carlos Alberto Cortez <calberto.cortez@gmail.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                         ConnectColumnEvents (column);
122                         int cnt = items.Add (column);
123                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, column));
124                         return cnt;                     
125                 }
126
127                 public void AddRange (DataGridColumnStyle[] columns)
128                 {
129                         foreach (DataGridColumnStyle mi in columns)
130                                 Add (mi);
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 (FromColumnNameToIndex (column.MappingName) != -1);
142                 }
143                 
144                 public bool Contains (PropertyDescriptor propDesc)
145                 {
146                         return (this [propDesc] != null);
147                 }
148
149                 public bool Contains (string name)
150                 {
151                         return (FromColumnNameToIndex (name) != -1);
152                 }
153
154                 void ICollection.CopyTo (Array dest, int index)
155                 {
156                         items.CopyTo (dest, index);
157                 }
158
159                 IEnumerator IEnumerable.GetEnumerator ()
160                 {
161                         return items.GetEnumerator ();
162                 }
163
164                 int IList.Add (object value)
165                 {
166                         return Add ((DataGridColumnStyle)value);                        
167                 }
168
169                 void IList.Clear ()
170                 {
171                         Clear ();
172                 }
173
174                 bool IList.Contains (object value)
175                 {
176                         return Contains ((DataGridColumnStyle)value);
177                 }
178
179                 int IList.IndexOf (object value)
180                 {
181                         return IndexOf ((DataGridColumnStyle)value);
182                 }
183
184                 void IList.Insert (int index, object value)
185                 {
186                         throw new NotSupportedException ();
187                 }
188
189                 void IList.Remove (object value)
190                 {
191                         Remove ((DataGridColumnStyle)value);
192                 }
193
194                 void IList.RemoveAt (int index)
195                 {
196                         RemoveAt (index);
197                 }
198                 
199                 public int IndexOf (DataGridColumnStyle element)
200                 {
201                         return items.IndexOf (element);
202                 }
203                 
204                 protected void OnCollectionChanged (CollectionChangeEventArgs ccevent)
205                 {                                               
206                         if (fire_event == true && CollectionChanged != null) {
207                                 CollectionChanged (this, ccevent);
208                         }
209                 }
210                 
211                 public void Remove (DataGridColumnStyle column)
212                 {
213                         items.Remove (column);
214                         DisconnectColumnEvents (column);
215                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, column));
216                 }
217                 
218                 public void RemoveAt (int index)
219                 {
220                         DataGridColumnStyle item = (DataGridColumnStyle)items[index];
221                         items.RemoveAt (index);
222                         DisconnectColumnEvents (item);
223                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, item));
224                 }               
225                 
226                 public void ResetPropertyDescriptors ()
227                 {
228                         for (int i = 0; i < items.Count; i++) {
229                                 DataGridColumnStyle column = (DataGridColumnStyle) items[i];
230                                 if (column.PropertyDescriptor != null) {
231                                         column.PropertyDescriptor = null;
232                                 }
233                         }
234                 }
235
236                 #endregion Public Instance Methods
237
238                 #region Events
239                 public event CollectionChangeEventHandler CollectionChanged;            
240                 #endregion Events               
241                 
242                 #region Private Instance Methods
243                 void ConnectColumnEvents (DataGridColumnStyle col)
244                 {
245                         col.AlignmentChanged += new EventHandler (ColumnAlignmentChangedEvent);
246                         col.FontChanged += new EventHandler (ColumnFontChangedEvent);
247                         col.HeaderTextChanged += new EventHandler (ColumnHeaderTextChanged);
248                         col.MappingNameChanged += new EventHandler (ColumnMappingNameChangedEvent);
249                         col.NullTextChanged += new EventHandler (ColumnNullTextChangedEvent);
250                         col.PropertyDescriptorChanged += new EventHandler (ColumnPropertyDescriptorChanged);
251                         col.ReadOnlyChanged += new EventHandler (ColumnReadOnlyChangedEvent);
252                         col.WidthChanged += new EventHandler (ColumnWidthChangedEvent);
253                 }
254
255                 void DisconnectColumnEvents (DataGridColumnStyle col)
256                 {
257                         col.AlignmentChanged -= new EventHandler (ColumnAlignmentChangedEvent);
258                         col.FontChanged -= new EventHandler (ColumnFontChangedEvent);
259                         col.HeaderTextChanged -= new EventHandler (ColumnHeaderTextChanged);
260                         col.MappingNameChanged -= new EventHandler (ColumnMappingNameChangedEvent);
261                         col.NullTextChanged -= new EventHandler (ColumnNullTextChangedEvent);
262                         col.PropertyDescriptorChanged -= new EventHandler (ColumnPropertyDescriptorChanged);
263                         col.ReadOnlyChanged -= new EventHandler (ColumnReadOnlyChangedEvent);
264                         col.WidthChanged -= new EventHandler (ColumnWidthChangedEvent);
265                 }
266
267                 void ColumnAlignmentChangedEvent (object sender, EventArgs e)
268                 {
269                         // XXX should this do a CollectionChangedEvent (Refresh, sender)?
270                 }
271
272                 void ColumnFontChangedEvent (object sender, EventArgs e)
273                 {
274                         // XXX should this do a CollectionChangedEvent (Refresh, sender)?
275                 }
276
277                 void ColumnHeaderTextChanged (object sender, EventArgs e)
278                 {
279                         // XXX should this do a CollectionChangedEvent (Refresh, sender)?
280                 }
281
282                 void ColumnMappingNameChangedEvent (object sender, EventArgs e)
283                 {
284                         // XXX should this do a CollectionChangedEvent (Refresh, sender)?
285                 }
286
287                 void ColumnNullTextChangedEvent (object sender, EventArgs e)
288                 {
289                         // XXX should this do a CollectionChangedEvent (Refresh, sender)?
290                 }
291
292                 void ColumnPropertyDescriptorChanged (object sender, EventArgs e)
293                 {
294                         // XXX should this do a CollectionChangedEvent (Refresh, sender)?
295                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, sender));
296                 }
297
298                 void ColumnReadOnlyChangedEvent (object sender, EventArgs e)
299                 {
300                         // XXX should this do a CollectionChangedEvent (Refresh, sender)?
301                 }
302
303                 void ColumnWidthChangedEvent (object sender, EventArgs e)
304                 {
305                         // XXX should this do a CollectionChangedEvent (Refresh, sender)?
306                 }
307
308
309                 private int FromColumnNameToIndex (string columnName)
310                 {       
311                         for (int i = 0; i < items.Count; i++) {
312                                 DataGridColumnStyle column = (DataGridColumnStyle) items[i];
313                                 
314                                 if (column.MappingName == null || column.MappingName == string.Empty)
315                                         continue;
316
317                                 if (String.Compare (column.MappingName, columnName, true) == 0) {
318                                         return i;
319                                 }
320                         }
321                         
322                         return -1;
323                 }
324                                 
325                 #endregion Private Instance Methods
326         }
327 }