2005-09-08 Peter Dennis Bartok <pbartok@novell.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / GridTableStylesCollection.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         [ListBindable(false)]
33         public class GridTableStylesCollection : BaseCollection, IList
34         {
35                 private ArrayList items;
36                 private DataGrid owner;
37
38                 internal GridTableStylesCollection (DataGrid grid)
39                 {
40                         items = new ArrayList ();
41                         owner = grid;
42                 }
43
44                 #region Public Instance Properties
45                 public DataGridTableStyle this[string tableName] {
46                         get {
47                                 int idx = FromTableNameToIndex (tableName);
48                                 return idx == -1 ? null : this [idx];
49                         }
50                 }
51
52                 public DataGridTableStyle this[int index] {
53                         get {
54                                 return (DataGridTableStyle) items[index];
55                         }
56                 }
57
58                 protected override ArrayList List {
59                         get { return items; }
60                 }
61
62                 int ICollection.Count {
63                         get { return items.Count;}
64                 }
65
66                 bool ICollection.IsSynchronized {
67                         get { return false; }
68                 }
69
70                 object ICollection.SyncRoot {
71                         get { return this;}
72                 }
73
74                 bool IList.IsFixedSize {
75                         get { return false; }
76                 }
77
78                 bool IList.IsReadOnly {
79                         get { return false;}
80                 }
81
82                 object IList.this [int index] {
83                         get {
84                                 return items[index];
85                         }
86                         set {
87                                 throw new NotSupportedException ();
88                         }
89                 }
90
91                 #endregion Public Instance Properties
92
93                 #region Public Instance Methods
94                 public virtual int Add (DataGridTableStyle table)
95                 {                       
96                         int cnt = AddInternal (table);
97                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, table));
98                         return cnt;
99                 }
100
101                 public void AddRange (DataGridTableStyle[] tables)
102                 {
103                         foreach (DataGridTableStyle mi in tables)
104                                 AddInternal (mi);
105
106                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
107                 }
108
109                 public void Clear ()
110                 {
111                         items.Clear ();
112                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh , null));
113                 }
114
115                 public bool Contains (DataGridTableStyle table)
116                 {
117                         return (FromTableNameToIndex (table.MappingName) != -1);
118                 }
119
120                 public bool Contains (string name)
121                 {
122                         return (FromTableNameToIndex (name) != -1);
123                 }
124
125                 void ICollection.CopyTo (Array dest, int index)
126                 {
127                         items.CopyTo (dest, index);
128                 }
129
130                 IEnumerator IEnumerable.GetEnumerator ()
131                 {
132                         return items.GetEnumerator ();
133                 }
134
135                 int IList.Add (object value)
136                 {
137                         int cnt = AddInternal ((DataGridTableStyle)value);
138
139                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, null));
140                         return cnt;
141                 }
142
143                 void IList.Clear ()
144                 {
145                         items.Clear ();
146                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh , null));
147                 }
148
149                 bool IList.Contains (object value)
150                 {
151                         return items.Contains (value);
152                 }
153
154                 int IList.IndexOf (object value)
155                 {
156                         return items.IndexOf (value);
157                 }
158
159                 void IList.Insert (int index, object value)
160                 {
161                         throw new NotSupportedException ();
162                 }
163
164                 void IList.Remove (object value)
165                 {
166                         items.Remove (value);
167                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, value));
168                 }
169
170                 void IList.RemoveAt (int index)
171                 {
172                         object item = items[index];
173                         items.RemoveAt (index);
174                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, item));
175                 }
176
177                 protected void OnCollectionChanged (CollectionChangeEventArgs ccevent)
178                 {
179                         if (CollectionChanged != null) {
180                                 CollectionChanged (this, ccevent);
181                         }
182                 }
183
184                 public void Remove (DataGridTableStyle table)
185                 {
186                         items.Remove (table);
187                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, table));
188                 }
189
190                 public void RemoveAt (int index)
191                 {
192                         object item = items[index];
193
194                         items.RemoveAt (index);
195                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, item));
196                 }
197
198                 #endregion Public Instance Methods
199
200                 #region Events
201                 public event CollectionChangeEventHandler CollectionChanged;
202                 #endregion Events               
203                 
204                 
205                 #region Private Instance Methods
206                 private int AddInternal (DataGridTableStyle table)
207                 {               
208                         // TODO: MS allows duplicate columns. How they diferenciate between them?               
209                         if (FromTableNameToIndex (table.MappingName) != -1) {
210                                 throw new ArgumentException ("The TableStyles collection already has a TableStyle with this mapping name");
211                         }
212                         
213                         table.DataGrid = owner;
214                         int cnt = items.Add (table);
215                         return cnt;                     
216                 }
217                 
218                 private int FromTableNameToIndex (string tableName)
219                 {               
220                         for (int i = 0; i < items.Count; i++) {
221                                 DataGridTableStyle table = (DataGridTableStyle) items[i];
222                                                                 
223                                 if (table.MappingName == null || table.MappingName == string.Empty)
224                                         continue;
225
226                                 if (String.Compare (table.MappingName, tableName, true) == 0) {
227                                         return i;
228                                 }
229                         }
230                         
231                         return -1;
232                 }
233                                 
234                 #endregion Private Instance Methods
235         }
236 }
237