ChangeLog: Updated ChangeLog.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / ParameterCollection.cs
1 //
2 // System.Web.UI.WebControls/ParameterCollection.cs
3 //
4 // Author: Todd Berman <tberman@gentoo.org>
5 //
6 // (C) 2003 Todd Berman
7
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_2_0
30
31 using System.Web.UI;
32 using System.Collections;
33 using System.Collections.Specialized;
34
35 namespace System.Web.UI.WebControls
36 {
37
38         public class ParameterCollection : StateManagedCollection
39         {
40
41                 private static Type[] _knownTypes = new Type[] {
42                                                     typeof (ControlParameter),
43                                                     typeof (CookieParameter),
44                                                     typeof (FormParameter),
45                                                     typeof (Parameter),
46                                                     typeof (QueryStringParameter),
47                                                     typeof (SessionParameter) };
48                                                     
49                 private EventHandler _parametersChanged;
50                 private KeyedList _values;
51
52
53                 public int Add (Parameter param)
54                 {
55                         return ((IList)this).Add (param);
56                 }
57
58                 public int Add (string name, string value)
59                 {
60                         return ((IList)this).Add (new Parameter (name, TypeCode.Object, value));
61                 }
62
63                 public int Add (string name, TypeCode type, string value)
64                 {
65                         return ((IList)this).Add (new Parameter (name, type, value));
66                 }
67
68                 protected override object CreateKnownType (int idx)
69                 {
70                         switch (idx) {
71                         case 0:
72                                 return new ControlParameter ();
73                                 break;
74                         case 1:
75                                 return new CookieParameter ();
76                                 break;
77                         case 2:
78                                 return new FormParameter ();
79                                 break;
80                         case 3:
81                                 return new Parameter ();
82                                 break;
83                         case 4:
84                                 return new QueryStringParameter ();
85                                 break;
86                         case 5:
87                                 return new SessionParameter ();
88                                 break;
89                         }
90
91                         throw new ArgumentOutOfRangeException ("index");
92                 }
93
94                 protected override Type[] GetKnownTypes ()
95                 {
96                         return _knownTypes;
97                 }
98
99                 public IOrderedDictionary GetValues (Control control)
100                 {
101                         if (_values == null)
102                         {
103                                 _values = new KeyedList ();
104                                 foreach (Parameter param in this)
105                                 {
106                                         string name = param.Name;
107                                         for (int i = 1; _values.Contains (name); i++)
108                                         {
109                                                 name = param.Name + i.ToString ();
110                                         }
111                                         _values.Add (name, param.ParameterValue);
112                                 }
113                         }
114                         return _values;
115                 }
116                 
117                 public void Insert (int idx, Parameter param)
118                 {
119                         ((IList)this).Insert (idx, param);
120                 }
121
122                 protected override void OnClearComplete ()
123                 {
124                         base.OnClearComplete ();
125                         OnParametersChanged (EventArgs.Empty);
126                 }
127
128                 protected override void OnInsert (int idx, object value)
129                 {
130                         base.OnInsert (idx, value);
131                         ((Parameter)value).SetOwnerCollection (this);
132                 }
133
134                 protected override void OnInsertComplete (int idx, object value)
135                 {
136                         base.OnInsertComplete (idx, value);
137                         OnParametersChanged (EventArgs.Empty);
138                 }
139
140                 protected virtual void OnParametersChanged (EventArgs e)
141                 {
142                         if (_parametersChanged != null)
143                                 _parametersChanged(this, e);
144                         
145                         _values = null;
146                 }
147
148                 protected override void OnValidate (object o)
149                 {
150                         base.OnValidate (o);
151                         
152                         if ((o is Parameter) == false)
153                                 throw new ArgumentException ("o is not a Parameter");
154                 }
155
156                 public void Remove (Parameter param)
157                 {
158                         ((IList)this).Remove (param);
159                 }
160
161                 public void RemoveAt (int idx)
162                 {
163                         ((IList)this).RemoveAt (idx);
164                 }
165
166                 [MonoTODO("eh?")]
167                 protected override void SetDirtyObject (object o)
168                 {
169                         throw new NotImplementedException ();
170                 }
171
172                 internal void CallOnParameterChanged ()
173                 {
174                         OnParametersChanged (EventArgs.Empty);
175                 }
176
177                 private int IndexOfString (string name)
178                 {
179                         for (int i = 0; i < Count; i++)
180                         {
181                                 if (((Parameter)((IList)this)[i]).Name == name)
182                                         return i;
183                         }
184                         return -1;
185                 }
186
187                 public Parameter this[int idx] {
188                         get {
189                                 return (Parameter) ((IList)this)[idx];
190                         }
191                         set {
192                                 ((IList)this)[idx] = value;
193                         }
194                 }
195
196                 public Parameter this[string name] {
197                         get {
198                                 int idx = IndexOfString (name);
199                                 if (idx == -1)
200                                         return null;
201                                 return ((Parameter) ((IList)this)[idx]);
202                         }
203                         set {
204                                 int idx = IndexOfString (name);
205                                 if (idx == -1) {
206                                         Add (value);
207                                         return;
208                                 }
209                                 ((IList)this)[idx] = value;
210                         }
211                 }
212
213                 public event EventHandler ParametersChanged {
214                         add { _parametersChanged += value; }
215                         remove { _parametersChanged -= value; }
216                 }
217         }
218 }
219
220 #endif