This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / Parameter.cs
1 //
2 // System.Web.UI.WebControls.Parameter
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //
7 // (C) 2003 Ben Maurer
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.Collections;
33 using System.Collections.Specialized;
34 using System.Text;
35 using System.Data;
36
37 namespace System.Web.UI.WebControls {
38         public class Parameter : ICloneable, IStateManager {
39
40                 public Parameter () : base ()
41                 {
42                 }
43
44                 protected Parameter (Parameter original)
45                 {
46                         this.DefaultValue = original.DefaultValue;
47                         this.Direction = original.Direction;
48                         this.TreatEmptyStringAsNull = original.TreatEmptyStringAsNull;
49                         this.Type = original.Type;
50                         this.Name = original.Name;
51                 }
52                 
53                 public Parameter (string name)
54                 {
55                         this.Name = name;
56                 }
57                 
58                 public Parameter(string name, TypeCode type) : this (name)
59                 {
60                         this.Type = type;
61                 }
62                 
63                 public Parameter (string name, TypeCode type, string defaultValue) : this (name, type)
64                 {
65                         this.DefaultValue = defaultValue;
66                 }
67                 
68                 protected virtual Parameter Clone ()
69                 {
70                         return new Parameter (this);
71                 }
72                 
73                 protected virtual object Evaluate (Control control)
74                 {
75                         return this.DefaultValue;
76                 }
77                 
78                 protected void OnParameterChanged ()
79                 {
80                         if (_owner != null)
81                                 _owner.CallOnParameterChanged ();
82                 }
83                 
84                 protected virtual void LoadViewState (object savedState)
85                 {
86                         if (savedState == null)
87                                 return;
88                         
89                         ViewState.LoadViewState (savedState);
90                 }
91                 
92                 protected virtual object SaveViewState ()
93                 {
94                         if (viewState == null)
95                                 return null;
96                         return viewState.SaveViewState ();
97                 }
98                 
99                 protected virtual void TrackViewState ()
100                 {
101                         isTrackingViewState = true;
102                         if (viewState != null)
103                                 viewState.TrackViewState ();
104                 }
105                 
106                 object ICloneable.Clone ()
107                 {
108                         return this.Clone ();
109                 }
110                 
111                 void IStateManager.LoadViewState (object savedState)
112                 {
113                         this.LoadViewState (savedState);
114                 }
115                 
116                 object IStateManager.SaveViewState ()
117                 {
118                         return this.SaveViewState ();
119                 }
120                 
121                 void IStateManager.TrackViewState ()
122                 {
123                         this.TrackViewState ();
124                 }
125                 
126                 bool IStateManager.IsTrackingViewState {
127                         get { return this.IsTrackingViewState; }
128                 }
129                 
130                 [MonoTODO]
131                 public override string ToString ()
132                 {
133                         return base.ToString ();
134                 }
135                 
136                 public string DefaultValue {
137                         get {
138                                 return ViewState ["DefaultValue"] as string;
139                         }
140                         set {
141                                 
142                                 if (DefaultValue != value) {
143                                         ViewState ["DefaultValue"] = value;
144                                         OnParameterChanged ();
145                                 }
146                         }
147                 }
148                 
149                 public ParameterDirection Direction {
150                         get {
151                                 object o = ViewState ["Direction"];
152                                 if (o != null)
153                                         return (ParameterDirection) o;
154                                 
155                                 return ParameterDirection.Input;
156                         }
157                         set {
158                                 
159                                 if (Direction != value) {
160                                         ViewState ["Direction"] = value;
161                                         OnParameterChanged ();
162                                 }
163                         }
164                 }
165                 
166
167                 public string Name {
168                         get {
169                                 string s = ViewState ["Name"] as string;
170                                 if (s != null)
171                                         return s;
172                                 
173                                 return "";
174                         }
175                         set {
176                                 
177                                 if (Name != value) {
178                                         ViewState ["Name"] = value;
179                                         OnParameterChanged ();
180                                 }
181                         }
182                 }
183
184                 public bool TreatEmptyStringAsNull {
185                         get {
186                                 object o = ViewState ["TreatEmptyStringAsNull"];
187                                 if (o != null)
188                                         return (bool) o;
189                                 
190                                 return false;
191                         }
192                         set {
193                                 
194                                 if (TreatEmptyStringAsNull != value) {
195                                         ViewState ["TreatEmptyStringAsNull"] = value;
196                                         OnParameterChanged ();
197                                 }
198                         }
199                 }
200                 
201                 public TypeCode Type {
202                         get {
203                                 object o = ViewState ["Type"];
204                                 if (o != null)
205                                         return (TypeCode) o;
206                                 
207                                 return TypeCode.Object;
208                         }
209                         set {
210                                 
211                                 if (Type != value) {
212                                         ViewState ["Type"] = value;
213                                         OnParameterChanged ();
214                                 }
215                         }
216                 }
217                 
218                 StateBag viewState;
219                 
220                 protected StateBag ViewState {
221                         get {
222                                 if (viewState == null) {
223                                         viewState = new StateBag ();
224                                         if (IsTrackingViewState)
225                                                 viewState.TrackViewState ();
226                                 }
227                                 return viewState;
228                         }
229                 }
230                 
231                 bool isTrackingViewState = false;
232                 protected bool IsTrackingViewState {
233                         get { return isTrackingViewState; }
234                 }
235                 
236                 private ParameterCollection _owner;
237
238                 internal void SetOwnerCollection (ParameterCollection own)
239                 {
240                         _owner = own;
241                 }
242
243                 internal object ParameterValue {
244                         get {
245                                 object param = ViewState["ParameterValue"];
246                                 //FIXME: need to do some null string checking magic with TreatEmptyStringAsNull here
247                                 if (param == null)
248                                 {
249                                         param = DefaultValue;
250                                         if (param == null)
251                                         {
252                                                 return null;
253                                         }
254                                 }
255                                 return Convert.ChangeType (param, Type);
256                         }
257                 }
258         }
259 }
260 #endif
261