2007-01-01 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ListViewGroupCollection.cs
1 //
2 //  ListViewGroupCollection.cs
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23 // Copyright (c) 2006 Daniel Nauck
24 //
25 // Author:
26 //      Daniel Nauck    (dna(at)mono-project(dot)de)
27
28 #if NET_2_0
29
30 using System;
31 using System.Text;
32 using System.Collections;
33 using System.ComponentModel;
34
35 namespace System.Windows.Forms
36 {
37         [ListBindable(false)]
38         public class ListViewGroupCollection : IList, ICollection, IEnumerable
39         {
40                 private ArrayList list = null;
41                 private ListView list_view_owner = null;
42
43                 ListViewGroupCollection()
44                 {
45                         list = new ArrayList();
46                 }
47
48                 internal ListViewGroupCollection(ListView listViewOwner) : this()
49                 {
50                         list_view_owner = listViewOwner;
51                 }
52
53                 internal ListView ListViewOwner {
54                         get { return list_view_owner; }
55                         set { list_view_owner = value; }
56                 }
57
58                 #region IEnumerable Members
59
60                 public IEnumerator GetEnumerator()
61                 {
62                         return list.GetEnumerator();
63                 }
64
65                 #endregion
66
67                 #region ICollection Members
68
69                 public void CopyTo(Array array, int index)
70                 {
71                         list.CopyTo(array, index);
72                 }
73
74                 public int Count {
75                         get { return list.Count; }
76                 }
77
78                 bool ICollection.IsSynchronized {
79                         get { return true; }
80                 }
81
82                 object ICollection.SyncRoot {
83                         get { return this; }
84                 }
85
86                 #endregion
87
88                 #region IList Members
89
90                 int IList.Add(object value)
91                 {
92                         if (!(value is ListViewGroup))
93                                 throw new ArgumentException("value");
94
95                         return Add((ListViewGroup)value);               
96                 }
97
98                 public int Add(ListViewGroup value)
99                 {
100                         if (Contains(value))
101                                 return -1;
102
103                         CheckListViewItemsInGroup(value);
104                         value.ListViewOwner = this.list_view_owner;
105                         int index = list.Add(value);
106
107                         if (this.list_view_owner != null)
108                                 list_view_owner.Redraw(true);
109
110                         return index;
111                 }
112
113                 public ListViewGroup Add(string key, string headerText)
114                 {
115                         ListViewGroup newGroup = new ListViewGroup(key, headerText);
116                         Add(newGroup);
117
118                         return newGroup;
119                 }
120
121                 public void Clear()
122                 {
123                         list.Clear();
124
125                         if(list_view_owner != null)
126                                 list_view_owner.Redraw(true);
127                 }
128
129                 bool IList.Contains(object value)
130                 {
131                         if (value is ListViewGroup)
132                                 return Contains((ListViewGroup)value);
133                         else
134                                 return false;
135                 }
136
137                 public bool Contains(ListViewGroup value)
138                 {
139                         return list.Contains(value);
140                 }
141
142                 int IList.IndexOf(object value)
143                 {
144                         if (value is ListViewGroup)
145                                 return IndexOf((ListViewGroup)value);
146                         else
147                                 return -1;
148                 }
149
150                 public int IndexOf(ListViewGroup value)
151                 {
152                         return list.IndexOf(value);
153                 }
154
155                 void IList.Insert(int index, object value)
156                 {
157                         if (value is ListViewGroup)
158                                 Insert(index, (ListViewGroup)value);
159                 }
160
161                 public void Insert(int index, ListViewGroup group)
162                 {
163                         if (Contains(group))
164                                 return;
165
166                         CheckListViewItemsInGroup(group);
167                         list.Insert(index, group);
168
169                         if(list_view_owner != null)
170                                 list_view_owner.Redraw(true);
171                 }
172
173                 bool IList.IsFixedSize {
174                         get { return false; }
175                 }
176
177                 bool IList.IsReadOnly {
178                         get { return false; }
179                 }
180
181                 void IList.Remove(object value)
182                 {
183                         Remove((ListViewGroup)value);
184                 }
185
186                 public void Remove(ListViewGroup value)
187                 {
188                         if (!list.Contains(value))
189                                 return;
190
191                         list.Remove(value);
192
193                         if (list_view_owner != null)
194                                 list_view_owner.Redraw(true);
195                 }
196
197                 public void RemoveAt(int index)
198                 {
199                         if (list.Count <= index || index < 0)
200                                 return;
201
202                         Remove(this[index]);
203                 }
204
205                 object IList.this[int index] {
206                         get { return this[index]; }
207                         set {
208                                 if (value is ListViewGroup)
209                                         this[index] = (ListViewGroup)value;
210                         }
211                 }
212
213                 public ListViewGroup this[int index] {
214                         get {
215                                 if (list.Count <= index || index < 0)
216                                         throw new ArgumentOutOfRangeException("index");
217
218                                 return (ListViewGroup)list[index];
219                         }
220                         set {
221                                 if (list.Count <= index || index < 0)
222                                         throw new ArgumentOutOfRangeException("index");
223
224                                 if (!Contains(value)) {
225                                         CheckListViewItemsInGroup(value);
226                                         list[index] = value;
227                     
228                                         if (list_view_owner != null)
229                                                 list_view_owner.Redraw(true);
230                                 }
231                         }
232                 }
233
234                 public ListViewGroup this[string key] {
235                         get {
236                                 foreach (ListViewGroup item in list)
237                                 {
238                                         if (item.Name.Equals(key))
239                                                 return item;
240                                 }
241
242                                 return null;
243                         }
244                         set {
245                                 for(int i = 0; i < list.Count; i++)
246                                 {
247                                         if ((this[i].Name != null) && (this[i].Name.Equals(key))) {
248                                                 this[i] = value;
249                                                 break;
250                                         }
251                                 }
252                         }
253                 }
254
255                 #endregion
256
257                 public void AddRange(ListViewGroup[] groups)
258                 {
259                         foreach (ListViewGroup item in groups)
260                         {
261                                 Add(item);
262                         }
263                 }
264
265                 public void AddRange(ListViewGroupCollection groups)
266                 {
267                         foreach (ListViewGroup item in groups)
268                         {
269                                 Add(item);
270                         }
271                 }
272
273                 private void CheckListViewItemsInGroup(ListViewGroup value)
274                 {
275                         //check for correct ListView
276                         foreach (ListViewItem item in value.Items)
277                         {
278                                 if (item.ListView != null && item.ListView != this.list_view_owner)
279                                         throw new ArgumentException("ListViewItem belongs to a ListView control other than the one that owns this ListViewGroupCollection.",
280                                                 "ListViewGroup");
281                         }
282                 }
283         }
284 }
285 #endif