Merge pull request #4453 from lambdageek/bug-49721
[mono.git] / mcs / class / System.Web / System.Web.UI / ExpressionBindingCollection.cs
1 //
2 // System.Web.UI.ExpressionBindingCollection.cs
3 //
4 // Authors:
5 //      Sanjay Gupta gsanjay@novell.com)
6 //
7 // (C) 2004-2010 Novell, Inc. (http://www.novell.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.ComponentModel;
33 using System.Collections;
34
35 namespace System.Web.UI {
36         
37         public sealed class ExpressionBindingCollection : ICollection, IEnumerable
38         {
39                 static readonly object changedEvent = new object ();
40                 
41                 Hashtable list;
42                 ArrayList removed;
43
44                 EventHandlerList events = new EventHandlerList ();
45                 
46                 public event EventHandler Changed {
47                         add { events.AddHandler (changedEvent, value); }
48                         remove { events.RemoveHandler (changedEvent, value); }
49                 }
50                 
51                 public ExpressionBindingCollection ()
52                 {
53                         list = new Hashtable ();
54                         removed = new ArrayList ();
55                 }
56
57                 public int Count {
58                         get { return list.Count; }
59                 }
60
61                 public bool IsReadOnly {
62                         get { return list.IsReadOnly; }
63                 }
64
65                 public bool IsSynchronized {
66                         get { return list.IsSynchronized; }
67                 }
68
69                 public ExpressionBinding this [string propertyName] {
70                         get { return list [propertyName] as ExpressionBinding; }
71                 }
72
73                 public ICollection RemovedBindings {
74                         get { return removed; }
75                 }
76
77                 public object SyncRoot {
78                         get { return list.SyncRoot; }
79                 }
80
81                 public void Add (ExpressionBinding binding)
82                 {
83                         list.Add (binding.PropertyName, binding);
84                         OnChanged (new EventArgs ());
85                 }
86
87                 public void Clear ()
88                 {
89                         list.Clear ();
90                         removed.Clear ();
91                         OnChanged (new EventArgs ());
92                 }
93
94                 public bool Contains (string propName)
95                 {
96                         return list.Contains (propName);
97                 }
98
99                 public void CopyTo (Array array, int index)
100                 {
101                         list.CopyTo (array, index);
102                 }
103
104                 public void CopyTo (ExpressionBinding [] array, int index)
105                 {
106                         if (index < 0)
107                                 throw new ArgumentNullException ("Index cannot be negative");
108                         if (index >= array.Length)
109                                 throw new ArgumentException ("Index cannot be greater than or equal to length of array passed");            
110                         if (list.Count > (array.Length - index + 1))
111                                 throw new ArgumentException ("Number of elements in source is greater than available space from index to end of destination");
112             
113                         foreach (string key in list.Keys)
114                                 array [index++] = (ExpressionBinding) list [key];
115                 }
116
117                 public IEnumerator GetEnumerator ()
118                 {
119                         return list.GetEnumerator ();
120                 }
121
122                 public void Remove (ExpressionBinding binding)
123                 {
124                         Remove(binding.PropertyName, true);
125                 }
126
127                 public void Remove (string propertyName)
128                 {
129                         Remove (propertyName, true);            
130                 }
131
132                 public void Remove (string propertyName, bool addToRemovedList)
133                 {
134                         if (addToRemovedList)
135                                 removed.Add (String.Empty); 
136                         else
137                                 removed.Add (propertyName);
138
139                         list.Remove (propertyName);
140                         OnChanged (new EventArgs ());
141                 }
142
143                 void OnChanged (EventArgs e)   
144                 {
145                         EventHandler eh = events [changedEvent] as EventHandler;
146                         if (eh != null)
147                                 eh (this, e);
148                 }        
149
150         }
151 }