* TreeView.cs: Don't draw the selected node when we lose
[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                         return cnt;
122                 }
123
124                 public void AddRange (DataGridColumnStyle[] columns)
125                 {
126                         foreach (DataGridColumnStyle mi in columns)
127                                 AddInternal (mi);                       
128                 }
129
130                 public void Clear ()
131                 {
132                         items.Clear ();
133                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh , null));
134                 }
135
136                 public bool Contains (DataGridColumnStyle column)
137                 {
138                         return (FromColumnNameToIndex (column.MappingName) != -1);
139                 }
140                 
141                 public bool Contains (PropertyDescriptor propDesc)
142                 {
143                         return (this [propDesc] != null);
144                 }
145
146                 public bool Contains (string name)
147                 {
148                         return (FromColumnNameToIndex (name) != -1);
149                 }
150
151                 void ICollection.CopyTo (Array dest, int index)
152                 {
153                         items.CopyTo (dest, index);
154                 }
155
156                 IEnumerator IEnumerable.GetEnumerator ()
157                 {
158                         return items.GetEnumerator ();
159                 }
160
161                 int IList.Add (object value)
162                 {
163                         return AddInternal ((DataGridColumnStyle)value);                        
164                 }
165
166                 void IList.Clear ()
167                 {
168                         items.Clear ();
169                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh , null));
170                 }
171
172                 bool IList.Contains (object value)
173                 {
174                         return items.Contains (value);
175                 }
176
177                 int IList.IndexOf (object value)
178                 {
179                         return items.IndexOf (value);
180                 }
181
182                 void IList.Insert (int index, object value)
183                 {
184                         throw new NotSupportedException ();
185                 }
186
187                 void IList.Remove (object value)
188                 {
189                         items.Remove (value);
190                 }
191
192                 void IList.RemoveAt (int index)
193                 {
194                         object item = items[index];
195                         
196                         items.RemoveAt (index);
197                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, item));
198                 }
199                 
200                 public int IndexOf (DataGridColumnStyle element)
201                 {
202                         return items.IndexOf (element);
203                 }
204                 
205                 protected void OnCollectionChanged (CollectionChangeEventArgs ccevent)
206                 {                                               
207                         if (fire_event == true && CollectionChanged != null) {
208                                 CollectionChanged (this, ccevent);
209                         }
210                 }
211                 
212                 public void Remove (DataGridColumnStyle column)
213                 {
214                         items.Remove (column);
215                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, column));
216                 }
217                 
218                 public void RemoveAt (int index)
219                 {
220                         object item = items[index];
221                         items.RemoveAt (index);
222                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, item));
223                 }               
224                 
225                 public void ResetPropertyDescriptors ()
226                 {
227                         for (int i = 0; i < items.Count; i++) {
228                                 DataGridColumnStyle column = (DataGridColumnStyle) items[i];
229                                 if (column.PropertyDescriptor != null) {
230                                         column.PropertyDescriptor = null;
231                                 }
232                         }
233                 }
234
235                 #endregion Public Instance Methods
236
237                 #region Events
238                 public event CollectionChangeEventHandler CollectionChanged;            
239                 #endregion Events               
240                 
241                 #region Private Instance Methods
242                 private int AddInternal (DataGridColumnStyle column)
243                 {                               
244                         // TODO: MS allows duplicate columns. How they diferenciate between them?
245                         if (FromColumnNameToIndex (column.MappingName) != -1) {
246                                 throw new ArgumentException ("The ColumnStyles collection already has a column with this mapping name");
247                         }
248                         
249                         column.TableStyle = owner;
250                         int cnt = items.Add (column);
251                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, column));
252                         return cnt;                     
253                 }
254                 
255                 private int FromColumnNameToIndex (string columnName)
256                 {       
257                         for (int i = 0; i < items.Count; i++) {
258                                 DataGridColumnStyle column = (DataGridColumnStyle) items[i];
259                                 
260                                 if (column.MappingName == null || column.MappingName == string.Empty)
261                                         continue;
262
263                                 if (String.Compare (column.MappingName, columnName, true) == 0) {
264                                         return i;
265                                 }
266                         }
267                         
268                         return -1;
269                 }
270                                 
271                 #endregion Private Instance Methods
272         }
273 }