2004-05-26 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[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 #if NET_2_0
11 using System.Collections;
12 using System.Collections.Specialized;
13 using System.Text;
14 using System.Data;
15
16 namespace System.Web.UI.WebControls {
17         public class Parameter : ICloneable, IStateManager {
18
19                 public Parameter () : base ()
20                 {
21                 }
22
23                 protected Parameter (Parameter original)
24                 {
25                         this.DefaultValue = original.DefaultValue;
26                         this.Direction = original.Direction;
27                         this.TreatEmptyStringAsNull = original.TreatEmptyStringAsNull;
28                         this.Type = original.Type;
29                         this.Name = original.Name;
30                 }
31                 
32                 public Parameter (string name)
33                 {
34                         this.Name = name;
35                 }
36                 
37                 public Parameter(string name, TypeCode type) : this (name)
38                 {
39                         this.Type = type;
40                 }
41                 
42                 public Parameter (string name, TypeCode type, string defaultValue) : this (name, type)
43                 {
44                         this.DefaultValue = defaultValue;
45                 }
46                 
47                 protected virtual Parameter Clone ()
48                 {
49                         return new Parameter (this);
50                 }
51                 
52                 protected virtual object Evaluate (Control control)
53                 {
54                         return this.DefaultValue;
55                 }
56                 
57                 protected void OnParameterChanged ()
58                 {
59                         if (_owner != null)
60                                 _owner.CallOnParameterChanged ();
61                 }
62                 
63                 protected virtual void LoadViewState (object savedState)
64                 {
65                         if (savedState == null)
66                                 return;
67                         
68                         ViewState.LoadViewState (savedState);
69                 }
70                 
71                 protected virtual object SaveViewState ()
72                 {
73                         if (viewState == null)
74                                 return null;
75                         return viewState.SaveViewState ();
76                 }
77                 
78                 protected virtual void TrackViewState ()
79                 {
80                         isTrackingViewState = true;
81                         if (viewState != null)
82                                 viewState.TrackViewState ();
83                 }
84                 
85                 object ICloneable.Clone ()
86                 {
87                         return this.Clone ();
88                 }
89                 
90                 void IStateManager.LoadViewState (object savedState)
91                 {
92                         this.LoadViewState (savedState);
93                 }
94                 
95                 object IStateManager.SaveViewState ()
96                 {
97                         return this.SaveViewState ();
98                 }
99                 
100                 void IStateManager.TrackViewState ()
101                 {
102                         this.TrackViewState ();
103                 }
104                 
105                 bool IStateManager.IsTrackingViewState {
106                         get { return this.IsTrackingViewState; }
107                 }
108                 
109                 [MonoTODO]
110                 public override string ToString ()
111                 {
112                         return base.ToString ();
113                 }
114                 
115                 public string DefaultValue {
116                         get {
117                                 return ViewState ["DefaultValue"] as string;
118                         }
119                         set {
120                                 
121                                 if (DefaultValue != value) {
122                                         ViewState ["DefaultValue"] = value;
123                                         OnParameterChanged ();
124                                 }
125                         }
126                 }
127                 
128                 public ParameterDirection Direction {
129                         get {
130                                 object o = ViewState ["Direction"];
131                                 if (o != null)
132                                         return (ParameterDirection) o;
133                                 
134                                 return ParameterDirection.Input;
135                         }
136                         set {
137                                 
138                                 if (Direction != value) {
139                                         ViewState ["Direction"] = value;
140                                         OnParameterChanged ();
141                                 }
142                         }
143                 }
144                 
145
146                 public string Name {
147                         get {
148                                 string s = ViewState ["Name"] as string;
149                                 if (s != null)
150                                         return s;
151                                 
152                                 return "";
153                         }
154                         set {
155                                 
156                                 if (Name != value) {
157                                         ViewState ["Name"] = value;
158                                         OnParameterChanged ();
159                                 }
160                         }
161                 }
162
163                 public bool TreatEmptyStringAsNull {
164                         get {
165                                 object o = ViewState ["TreatEmptyStringAsNull"];
166                                 if (o != null)
167                                         return (bool) o;
168                                 
169                                 return false;
170                         }
171                         set {
172                                 
173                                 if (TreatEmptyStringAsNull != value) {
174                                         ViewState ["TreatEmptyStringAsNull"] = value;
175                                         OnParameterChanged ();
176                                 }
177                         }
178                 }
179                 
180                 public TypeCode Type {
181                         get {
182                                 object o = ViewState ["Type"];
183                                 if (o != null)
184                                         return (TypeCode) o;
185                                 
186                                 return TypeCode.Object;
187                         }
188                         set {
189                                 
190                                 if (Type != value) {
191                                         ViewState ["Type"] = value;
192                                         OnParameterChanged ();
193                                 }
194                         }
195                 }
196                 
197                 StateBag viewState;
198                 
199                 protected StateBag ViewState {
200                         get {
201                                 if (viewState == null) {
202                                         viewState = new StateBag ();
203                                         if (IsTrackingViewState)
204                                                 viewState.TrackViewState ();
205                                 }
206                                 return viewState;
207                         }
208                 }
209                 
210                 bool isTrackingViewState = false;
211                 protected bool IsTrackingViewState {
212                         get { return isTrackingViewState; }
213                 }
214                 
215                 private ParameterCollection _owner;
216
217                 internal void SetOwnerCollection (ParameterCollection own)
218                 {
219                         _owner = own;
220                 }
221
222                 internal object ParameterValue {
223                         get {
224                                 object param = ViewState["ParameterValue"];
225                                 //FIXME: need to do some null string checking magic with TreatEmptyStringAsNull here
226                                 if (param == null)
227                                 {
228                                         param = DefaultValue;
229                                         if (param == null)
230                                         {
231                                                 return null;
232                                         }
233                                 }
234                                 return Convert.ChangeType (param, Type);
235                         }
236                 }
237         }
238 }
239 #endif
240