2002-07-17 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI / ControlCollection.cs
1 //
2 // System.Web.UI.ControlCollection.cs
3 //
4 // Duncan Mak  (duncan@ximian.com)
5 //
6 // (C) Ximian, Inc.
7 //
8
9 using System;
10 using System.Collections;
11
12 namespace System.Web.UI {
13
14         public class ControlCollection : ICollection, IEnumerable
15         {
16                 ArrayList list = new ArrayList ();
17                 Control owner;
18                 
19                 public ControlCollection (Control owner)
20                 {
21                         if (owner == null)
22                                 throw new ArgumentException ();
23
24                         this.owner = owner;
25                 }
26
27                 public int Count {
28                         get { return list.Count; }
29                 }
30
31                 public bool IsReadOnly {
32                         get { return list.IsReadOnly; }
33                 }
34
35                 public bool  IsSynchronized {
36                         get { return list.IsSynchronized; }
37                 }
38
39                 public virtual Control this [int index] {
40                         get { return list [index] as Control; }
41                 }
42
43                 protected Control Owner {
44                         get { return owner; }
45                 }
46
47                 public object SyncRoot {
48                         get { return list.SyncRoot; }
49                 }
50
51                 public virtual void Add (Control child)
52                 {
53                         if (child == null)
54                                 throw new ArgumentNullException ();
55                         if (IsReadOnly)
56                                 throw new HttpException ();
57
58                         list.Add (child);
59                         owner.AddedControl (child, list.Count - 1);
60                 }
61
62                 public virtual void AddAt (int index, Control child)
63                 {
64                         if (child == null) // maybe we should check for ! (child is Control)?
65                                 throw new ArgumentNullException ();
66                         
67                         if ((index < -1) || (index > Count))
68                                 throw new ArgumentOutOfRangeException ();
69
70                         if (IsReadOnly)
71                                 throw new HttpException ();
72
73                         if (index == -1){
74                                 Add (child);
75                         } else {
76                                 list [index] = child;
77                                 owner.AddedControl (child, index);
78                         }
79                 }
80
81                 public virtual void Clear ()
82                 {
83                         list.Clear ();
84                 }
85
86                 public virtual bool Contains (Control c)
87                 {
88                         return list.Contains (c);
89                 }
90
91                 public void CopyTo (Array array, int index)
92                 {
93                         list.CopyTo (array, index);
94                 }
95
96                 public IEnumerator GetEnumerator ()
97                 {
98                         return list.GetEnumerator ();
99                 }
100
101                 public virtual int IndexOf (Control c)
102                 {
103                         return list.IndexOf (c);
104                 }
105
106                 public virtual void Remove (Control value)
107                 {
108                         list.Remove (value);
109                         owner.RemovedControl (value);
110                 }
111
112                 public virtual void RemoveAt (int index)
113                 {
114                         if (IsReadOnly)
115                                 throw new HttpException ();
116
117                         Control value = (Control) list [index];
118                         list.RemoveAt (index);
119                         owner.RemovedControl (value);
120                 }
121         }
122 }