2010-01-20 Zoltan Varga <vargaz@gmail.com>
[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 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 #if NET_2_0
32 using System;
33 using System.ComponentModel;
34 using System.Collections;
35
36 namespace System.Web.UI {
37         
38         public sealed class ExpressionBindingCollection : ICollection, IEnumerable
39         {
40                 static readonly object changedEvent = new object ();
41                 
42                 Hashtable list;
43                 ArrayList removed;
44
45                 EventHandlerList events = new EventHandlerList ();
46                 
47                 public event EventHandler Changed {
48                         add { events.AddHandler (changedEvent, value); }
49                         remove { events.RemoveHandler (changedEvent, value); }
50                 }
51                 
52                 public ExpressionBindingCollection ()
53                 {
54                         list = new Hashtable ();
55                         removed = new ArrayList ();
56                 }
57
58                 public int Count {
59                         get { return list.Count; }
60                 }
61
62                 public bool IsReadOnly {
63                         get { return list.IsReadOnly; }
64                 }
65
66                 public bool IsSynchronized {
67                         get { return list.IsSynchronized; }
68                 }
69
70                 public ExpressionBinding this [string propertyName] {
71                         get { return list [propertyName] as ExpressionBinding; }
72                 }
73
74                 public ICollection RemovedBindings {
75                         get { return removed; }
76                 }
77
78                 public object SyncRoot {
79                         get { return list.SyncRoot; }
80                 }
81
82                 public void Add (ExpressionBinding binding)
83                 {
84                         list.Add (binding.PropertyName, binding);
85                         OnChanged (new EventArgs ());
86                 }
87
88                 public void Clear ()
89                 {
90                         list.Clear ();
91                         removed.Clear ();
92                         OnChanged (new EventArgs ());
93                 }
94
95                 public bool Contains (string propertyName)
96                 {
97                         return list.Contains (propertyName);
98                 }
99
100                 public void CopyTo (Array array, int index)
101                 {
102                         list.CopyTo (array, index);
103                 }
104
105                 public void CopyTo (ExpressionBinding [] bindings, int index)
106                 {
107                         if (index < 0)
108                                 throw new ArgumentNullException ("Index cannot be negative");
109                         if (index >= bindings.Length)
110                                 throw new ArgumentException ("Index cannot be greater than or equal to length of array passed");            
111                         if (list.Count > (bindings.Length - index + 1))
112                                 throw new ArgumentException ("Number of elements in source is greater than available space from index to end of destination");
113             
114                         foreach (string key in list.Keys)
115                                 bindings [index++] = (ExpressionBinding) list [key];
116                 }
117
118                 public IEnumerator GetEnumerator ()
119                 {
120                         return list.GetEnumerator ();
121                 }
122
123                 public void Remove (ExpressionBinding binding)
124                 {
125                         Remove(binding.PropertyName, true);
126                 }
127
128                 public void Remove (string propertyName)
129                 {
130                         Remove (propertyName, true);            
131                 }
132
133                 public void Remove (string propertyName, bool addToRemovedList)
134                 {
135                         if (addToRemovedList)
136                                 removed.Add (String.Empty); 
137                         else
138                                 removed.Add (propertyName);
139
140                         list.Remove (propertyName);
141                         OnChanged (new EventArgs ());
142                 }
143
144                 void OnChanged (EventArgs e)   
145                 {
146                         EventHandler eh = events [changedEvent] as EventHandler;
147                         if (eh != null)
148                                 eh (this, e);
149                 }        
150
151         }
152 }
153 #endif