1 //------------------------------------------------------------------------------
2 // <copyright file="ExpressionBindingCollection.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6 
7 namespace System.Web.UI {
8 
9     using System;
10     using System.Collections;
11     using System.Collections.Specialized;
12     using System.ComponentModel;
13     using System.ComponentModel.Design;
14     using System.Data;
15     using System.Web.Util;
16     using System.Security.Permissions;
17 
18 
19     /// <devdoc>
20     /// </devdoc>
21     public sealed class ExpressionBindingCollection : ICollection {
22         private EventHandler changedEvent;
23 
24         private Hashtable bindings;
25         private Hashtable removedBindings;
26 
27 
28         /// <devdoc>
29         /// </devdoc>
ExpressionBindingCollection()30         public ExpressionBindingCollection() {
31             this.bindings = new Hashtable(StringComparer.OrdinalIgnoreCase);
32         }
33 
34 
35         /// <devdoc>
36         /// </devdoc>
37         public int Count {
38             get {
39                 return bindings.Count;
40             }
41         }
42 
43 
44         /// <devdoc>
45         /// </devdoc>
46         public bool IsReadOnly {
47             get {
48                 return false;
49             }
50         }
51 
52 
53         /// <devdoc>
54         /// </devdoc>
55         public bool IsSynchronized {
56             get {
57                 return false;
58             }
59         }
60 
61 
62         /// <devdoc>
63         /// </devdoc>
64         public ICollection RemovedBindings {
65             get {
66                 int bindingCount = 0;
67                 ICollection keys = null;
68 
69                 if (removedBindings != null) {
70                     keys = removedBindings.Keys;
71                     bindingCount = keys.Count;
72 
73                     string[] removedNames = new string[bindingCount];
74                     int i = 0;
75 
76                     foreach (string s in keys) {
77                         removedNames[i++] = s;
78                     }
79 
80                     removedBindings.Clear();
81                     return removedNames;
82                 }
83                 else {
84                     return new string[0];
85                 }
86             }
87         }
88 
89 
90         /// <devdoc>
91         /// </devdoc>
92         private Hashtable RemovedBindingsTable {
93             get {
94                 if (removedBindings == null) {
95                     removedBindings = new Hashtable(StringComparer.OrdinalIgnoreCase);
96                 }
97                 return removedBindings;
98             }
99         }
100 
101 
102         /// <devdoc>
103         /// </devdoc>
104         public object SyncRoot {
105             get {
106                 return this;
107             }
108         }
109 
110 
111         /// <devdoc>
112         /// </devdoc>
113         public ExpressionBinding this[string propertyName] {
114             get {
115                 object o = bindings[propertyName];
116                 if (o != null)
117                     return(ExpressionBinding)o;
118                 return null;
119             }
120         }
121 
122 
123         public event EventHandler Changed {
124             add {
125                 changedEvent = (EventHandler)Delegate.Combine(changedEvent, value);
126             }
127             remove {
128                 changedEvent = (EventHandler)Delegate.Remove(changedEvent, value);
129             }
130         }
131 
132 
133 
134         /// <devdoc>
135         /// </devdoc>
Add(ExpressionBinding binding)136         public void Add(ExpressionBinding binding) {
137             bindings[binding.PropertyName] = binding;
138             RemovedBindingsTable.Remove(binding.PropertyName);
139 
140             OnChanged();
141         }
142 
143 
144         /// <devdoc>
145         /// </devdoc>
Contains(string propName)146         public bool Contains(string propName) {
147             return bindings.Contains(propName);
148         }
149 
150 
151         /// <devdoc>
152         /// </devdoc>
Clear()153         public void Clear() {
154             ICollection keys = bindings.Keys;
155             if ((keys.Count != 0) && (removedBindings == null)) {
156                 // ensure the removedBindings hashtable is created
157                 Hashtable h = RemovedBindingsTable;
158             }
159             foreach (string s in keys) {
160                 removedBindings[s] = String.Empty;
161             }
162 
163             bindings.Clear();
164 
165             OnChanged();
166         }
167 
168 
169         /// <devdoc>
170         /// </devdoc>
CopyTo(Array array, int index)171         public void CopyTo(Array array, int index) {
172             for (IEnumerator e = this.GetEnumerator(); e.MoveNext();)
173                 array.SetValue(e.Current, index++);
174         }
175 
176 
177         /// <devdoc>
178         /// </devdoc>
CopyTo(ExpressionBinding[] array, int index)179         public void CopyTo(ExpressionBinding[] array, int index) {
180             for (IEnumerator e = this.GetEnumerator(); e.MoveNext();)
181                 array.SetValue(e.Current, index++);
182         }
183 
184 
185         /// <devdoc>
186         /// </devdoc>
GetEnumerator()187         public IEnumerator GetEnumerator() {
188             return bindings.Values.GetEnumerator();
189         }
190 
OnChanged()191         private void OnChanged() {
192             if (changedEvent != null) {
193                 changedEvent(this, EventArgs.Empty);
194             }
195         }
196 
197 
198         /// <devdoc>
199         /// </devdoc>
Remove(string propertyName)200         public void Remove(string propertyName) {
201             Remove(propertyName, true);
202         }
203 
204 
205         /// <devdoc>
206         /// </devdoc>
Remove(ExpressionBinding binding)207         public void Remove(ExpressionBinding binding) {
208             Remove(binding.PropertyName, true);
209         }
210 
211 
212         /// <devdoc>
213         /// </devdoc>
Remove(string propertyName, bool addToRemovedList)214         public void Remove(string propertyName, bool addToRemovedList) {
215             if (Contains(propertyName)) {
216                 if (addToRemovedList && bindings.Contains(propertyName)) {
217                     RemovedBindingsTable[propertyName] = String.Empty;
218                 }
219                 bindings.Remove(propertyName);
220 
221                 OnChanged();
222             }
223         }
224     }
225 }
226 
227