1 //------------------------------------------------------------------------------
2 // <copyright file="TableCellCollection.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6 
7 namespace System.Web.UI.WebControls {
8 
9     using System;
10     using System.Collections;
11     using System.ComponentModel;
12     using System.Drawing.Design;
13     using System.Web;
14     using System.Web.UI;
15 
16     /// <devdoc>
17     /// <para>Encapsulates the collection of <see cref='System.Web.UI.WebControls.TableHeaderCell'/> and <see cref='System.Web.UI.WebControls.TableCell'/> objects within a
18     /// <see cref='System.Web.UI.WebControls.Table'/>
19     /// control.</para>
20     /// </devdoc>
21     [
22     Editor("System.Web.UI.Design.WebControls.TableCellsCollectionEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor))
23     ]
24     public sealed class TableCellCollection : IList {
25 
26         /// <devdoc>
27         ///    <para>
28         ///       A protected field of type <see cref='System.Web.UI.WebControls.TableRow'/>. Represents the
29         ///    <see cref='System.Web.UI.WebControls.TableCell'/>
30         ///    collection internally.
31         /// </para>
32         /// </devdoc>
33         private TableRow owner;
34 
35 
36         /// <devdoc>
37         /// </devdoc>
TableCellCollection(TableRow owner)38         internal TableCellCollection(TableRow owner) {
39             this.owner = owner;
40         }
41 
42 
43         /// <devdoc>
44         /// <para>Gets the <see cref='System.Web.UI.WebControls.TableCell'/>
45         /// count in the collection.</para>
46         /// </devdoc>
47         public int Count {
48             get {
49                 if (owner.HasControls()) {
50                     return owner.Controls.Count;
51                 }
52                 return 0;
53             }
54         }
55 
56 
57         /// <devdoc>
58         ///    <para>
59         ///       Gets a <see cref='System.Web.UI.WebControls.TableCell'/>
60         ///       referenced by the specified
61         ///       ordinal index value.
62         ///    </para>
63         /// </devdoc>
64         public TableCell this[int index] {
65             get {
66                 return(TableCell)owner.Controls[index];
67             }
68         }
69 
70 
71         /// <devdoc>
72         ///    <para>
73         ///       Adds the specified <see cref='System.Web.UI.WebControls.TableCell'/> to the end of the collection.
74         ///    </para>
75         /// </devdoc>
Add(TableCell cell)76         public int Add(TableCell cell) {
77             AddAt(-1, cell);
78             return owner.Controls.Count - 1;
79         }
80 
81 
82         /// <devdoc>
83         ///    <para>
84         ///       Adds the specified <see cref='System.Web.UI.WebControls.TableCell'/> to the collection at the specified
85         ///       index location.
86         ///    </para>
87         /// </devdoc>
AddAt(int index, TableCell cell)88         public void AddAt(int index, TableCell cell) {
89             owner.Controls.AddAt(index, cell);
90         }
91 
92 
93         /// <devdoc>
94         /// </devdoc>
AddRange(TableCell[] cells)95         public void AddRange(TableCell[] cells) {
96             if (cells == null) {
97                 throw new ArgumentNullException("cells");
98             }
99             foreach(TableCell cell in cells) {
100                 Add(cell);
101             }
102         }
103 
104 
105         /// <devdoc>
106         /// <para>Removes all <see cref='System.Web.UI.WebControls.TableCell'/> controls
107         ///    from the collection.</para>
108         /// </devdoc>
Clear()109         public void Clear() {
110             if (owner.HasControls()) {
111                 owner.Controls.Clear();
112             }
113         }
114 
115 
116         /// <devdoc>
117         ///    <para>Returns an ordinal index value that represents the position of the
118         ///       specified <see cref='System.Web.UI.WebControls.TableCell'/> within the collection.</para>
119         /// </devdoc>
GetCellIndex(TableCell cell)120         public int GetCellIndex(TableCell cell) {
121             if (owner.HasControls()) {
122                 return owner.Controls.IndexOf(cell);
123             }
124             return -1;
125         }
126 
127 
128         /// <devdoc>
129         ///    <para>
130         ///       Returns an enumerator of all <see cref='System.Web.UI.WebControls.TableCell'/> controls within the
131         ///       collection.
132         ///    </para>
133         /// </devdoc>
GetEnumerator()134         public IEnumerator GetEnumerator() {
135             return owner.Controls.GetEnumerator();
136         }
137 
138 
139         /// <devdoc>
140         /// <para>Copies contents from the collection to the specified <see cref='System.Array' qualify='true'/> with the
141         ///    specified starting index.</para>
142         /// </devdoc>
CopyTo(Array array, int index)143         public void CopyTo(Array array, int index) {
144             for (IEnumerator e = this.GetEnumerator(); e.MoveNext();)
145                 array.SetValue(e.Current, index++);
146         }
147 
148 
149         /// <devdoc>
150         ///    <para>
151         ///       Gets the object that can be used to synchronize access to the
152         ///       collection. In this case, it is the collection itself.
153         ///    </para>
154         /// </devdoc>
155         public Object SyncRoot {
156             get { return this;}
157         }
158 
159 
160         /// <devdoc>
161         ///    <para>
162         ///       Gets a value indicating whether the collection is read-only.
163         ///    </para>
164         /// </devdoc>
165         public bool IsReadOnly {
166             get { return false;}
167         }
168 
169 
170         /// <devdoc>
171         ///    <para>
172         ///       Gets a value indicating whether access to the collection is synchronized
173         ///       (thread-safe).
174         ///    </para>
175         /// </devdoc>
176         public bool IsSynchronized {
177             get { return false;}
178         }
179 
180 
181         /// <devdoc>
182         ///    <para>
183         ///       Removes the specified <see cref='System.Web.UI.WebControls.TableCell'/> from the
184         ///       collection.
185         ///    </para>
186         /// </devdoc>
Remove(TableCell cell)187         public void Remove(TableCell cell) {
188             owner.Controls.Remove(cell);
189         }
190 
191 
192         /// <devdoc>
193         ///    <para>
194         ///       Removes the <see cref='System.Web.UI.WebControls.TableCell'/> from the collection at the
195         ///       specified index location.
196         ///    </para>
197         /// </devdoc>
RemoveAt(int index)198         public void RemoveAt(int index) {
199             owner.Controls.RemoveAt(index);
200         }
201 
202         // IList implementation, required by collection editor
203 
204         /// <internalonly/>
205         object IList.this[int index] {
206             get {
207                 return owner.Controls[index];
208             }
209             set {
210                 RemoveAt(index);
211                 AddAt(index, (TableCell)value);
212             }
213         }
214 
215 
216         /// <internalonly/>
217         bool IList.IsFixedSize {
218             get {
219                 return false;
220             }
221         }
222 
223 
224         /// <internalonly/>
IList.Add(object o)225         int IList.Add(object o) {
226             return Add((TableCell) o);
227         }
228 
229 
230         /// <internalonly/>
IList.Contains(object o)231         bool IList.Contains(object o) {
232             return owner.Controls.Contains((TableCell)o);
233         }
234 
235 
236         /// <internalonly/>
IList.IndexOf(object o)237         int IList.IndexOf(object o) {
238             return owner.Controls.IndexOf((TableCell)o);
239         }
240 
241 
242         /// <internalonly/>
IList.Insert(int index, object o)243         void IList.Insert(int index, object o) {
244             owner.Controls.AddAt(index, (TableCell)o);
245         }
246 
247 
248         /// <internalonly/>
IList.Remove(object o)249         void IList.Remove(object o) {
250             owner.Controls.Remove((TableCell)o);
251         }
252 
253     }
254 }
255 
256