Almost eliminate wrong use of Constants.WsaNamespace.
[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 using System.ComponentModel;
35 using System.Data;
36
37 namespace System.Web.UI.WebControls
38 {
39
40         [EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
41         public class ParameterCollection : StateManagedCollection
42         {
43
44                 static Type[] _knownTypes = new Type[] {
45                                                     typeof (ControlParameter),
46                                                     typeof (CookieParameter),
47                                                     typeof (FormParameter),
48                                                     typeof (Parameter),
49                                                     typeof (QueryStringParameter),
50                                                     typeof (SessionParameter) };
51                                                     
52                 EventHandler _parametersChanged;
53
54                 public int Add (Parameter param)
55                 {
56                         return ((IList)this).Add (param);
57                 }
58
59                 public int Add (string name, string value)
60                 {
61                         return ((IList)this).Add (new Parameter (name, TypeCode.Object, value));
62                 }
63
64                 public int Add (string name, TypeCode type, string value)
65                 {
66                         return ((IList)this).Add (new Parameter (name, type, value));
67                 }
68
69                 public int Add (string name, DbType dbType, string value)
70                 {
71                         return ((IList)this).Add (new Parameter (name, dbType, value));
72                 }
73                 
74                 protected override object CreateKnownType (int idx)
75                 {
76                         switch (idx) {
77                         case 0:
78                                 return new ControlParameter ();
79                         case 1:
80                                 return new CookieParameter ();                  
81                         case 2:
82                                 return new FormParameter ();                    
83                         case 3:
84                                 return new Parameter ();                
85                         case 4:
86                                 return new QueryStringParameter ();             
87                         case 5:
88                                 return new SessionParameter ();                 
89                         }
90
91                         throw new ArgumentOutOfRangeException ("index");
92                 }
93
94                 protected override Type[] GetKnownTypes ()
95                 {
96                         return _knownTypes;
97                 }
98
99                 public IOrderedDictionary GetValues (HttpContext context, Control control)
100                 {
101                         OrderedDictionary values = new OrderedDictionary ();
102                         foreach (Parameter param in this)
103                         {
104                                 string name = param.Name;
105                                 for (int i = 1; values.Contains (name); i++)
106                                         name = param.Name + i.ToString ();
107                                 values.Add (name, param.GetValue (context, control));
108                         }
109                         return values;
110                 }
111                 
112                 public void UpdateValues (HttpContext context, Control control)
113                 {
114                         foreach (Parameter param in this)
115                                 param.UpdateValue (context, control);
116                 }
117                 
118                 public void Insert (int idx, Parameter param)
119                 {
120                         ((IList)this).Insert (idx, param);
121                 }
122
123                 protected override void OnClearComplete ()
124                 {
125                         base.OnClearComplete ();
126                         OnParametersChanged (EventArgs.Empty);
127                 }
128
129                 protected override void OnInsert (int idx, object value)
130                 {
131                         base.OnInsert (idx, value);
132                         ((Parameter)value).SetOwnerCollection (this);
133                 }
134
135                 protected override void OnInsertComplete (int idx, object value)
136                 {
137                         base.OnInsertComplete (idx, value);
138                         OnParametersChanged (EventArgs.Empty);
139                 }
140
141                 protected virtual void OnParametersChanged (EventArgs e)
142                 {
143                         if (_parametersChanged != null)
144                                 _parametersChanged(this, e);
145                 }
146
147                 protected override void OnValidate (object o)
148                 {
149                         base.OnValidate (o);
150                         
151                         if ((o is Parameter) == false)
152                                 throw new ArgumentException ("o is not a Parameter");
153                 }
154
155                 public void Remove (Parameter param)
156                 {
157                         ((IList)this).Remove (param);
158                 }
159
160                 public void RemoveAt (int idx)
161                 {
162                         ((IList)this).RemoveAt (idx);
163                 }
164
165                 protected override void SetDirtyObject (object o)
166                 {
167                         ((Parameter)o).SetDirty ();
168                 }
169
170                 internal void CallOnParameterChanged ()
171                 {
172                         OnParametersChanged (EventArgs.Empty);
173                 }
174
175                 int IndexOfString (string name)
176                 {
177                         for (int i = 0; i < Count; i++)
178                         {
179                                 if (string.Compare (((Parameter) ((IList) this) [i]).Name, name, StringComparison.OrdinalIgnoreCase) == 0)
180                                         return i;
181                         }
182                         return -1;
183                 }
184
185                 public Parameter this[int idx] {
186                         get {
187                                 return (Parameter) ((IList)this)[idx];
188                         }
189                         set {
190                                 ((IList)this)[idx] = value;
191                         }
192                 }
193
194                 public Parameter this[string name] {
195                         get {
196                                 int idx = IndexOfString (name);
197                                 if (idx == -1)
198                                         return null;
199                                 return ((Parameter) ((IList)this)[idx]);
200                         }
201                         set {
202                                 int idx = IndexOfString (name);
203                                 if (idx == -1) {
204                                         Add (value);
205                                         return;
206                                 }
207                                 ((IList)this)[idx] = value;
208                         }
209                 }
210
211                 public event EventHandler ParametersChanged {
212                         add { _parametersChanged += value; }
213                         remove { _parametersChanged -= value; }
214                 }
215
216                 public bool Contains (Parameter param)
217                 {
218                         return ((IList)this).Contains (param);
219                 }
220
221                 public void CopyTo (Parameter[] paramArray, int index)
222                 {
223                         ((IList)this).CopyTo (paramArray, index);
224                 }
225
226                 public int IndexOf (Parameter param)
227                 {
228                         return ((IList)this).IndexOf (param);
229                 }
230
231                 protected override void OnRemoveComplete (int index, object value)
232                 {
233                         base.OnRemoveComplete (index, value);                   
234                         OnParametersChanged (EventArgs.Empty);
235                 }
236                 
237         }
238 }
239
240 #endif