* Style.cs: fixed restoring FontInfo from ViewState
[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 //      Sanjay Gupta (gsanjay@novell.com)
7 //
8 // (C) 2003 Ben Maurer
9 // (C) 2004 Novell, Inc. (http://www.novell.com)
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 #if NET_2_0
34 using System.Collections;
35 using System.Collections.Specialized;
36 using System.Text;
37 using System.Data;
38 using System.ComponentModel;
39
40 namespace System.Web.UI.WebControls {
41         [DefaultPropertyAttribute ("DefaultValue")]
42         public class Parameter : ICloneable, IStateManager {
43
44                 public Parameter () : base ()
45                 {
46                 }
47
48                 protected Parameter (Parameter original)
49                 {
50                         this.DefaultValue = original.DefaultValue;
51                         this.Direction = original.Direction;
52                         this.ConvertEmptyStringToNull = original.ConvertEmptyStringToNull;
53                         this.Type = original.Type;
54                         this.Name = original.Name;
55                 }
56                 
57                 public Parameter (string name)
58                 {
59                         this.Name = name;
60                 }
61                 
62                 public Parameter(string name, TypeCode type) : this (name)
63                 {
64                         this.Type = type;
65                 }
66                 
67                 public Parameter (string name, TypeCode type, string defaultValue) : this (name, type)
68                 {
69                         this.DefaultValue = defaultValue;
70                 }
71                 
72                 protected virtual Parameter Clone ()
73                 {
74                         return new Parameter (this);
75                 }
76                 
77                 protected void OnParameterChanged ()
78                 {
79                         if (_owner != null)
80                                 _owner.CallOnParameterChanged ();
81                 }
82                 
83                 protected virtual void LoadViewState (object savedState)
84                 {
85                         ViewState.LoadViewState (savedState);
86                 }
87                 
88                 protected virtual object SaveViewState ()
89                 {
90                         return ViewState.SaveViewState ();
91                 }
92                 
93                 protected virtual void TrackViewState ()
94                 {
95                         isTrackingViewState = true;
96                         if (viewState != null)
97                                 viewState.TrackViewState ();
98                 }
99                 
100                 object ICloneable.Clone ()
101                 {
102                         return this.Clone ();
103                 }
104                 
105                 void IStateManager.LoadViewState (object savedState)
106                 {
107                         this.LoadViewState (savedState);
108                 }
109                 
110                 object IStateManager.SaveViewState ()
111                 {
112                         return this.SaveViewState ();
113                 }
114                 
115                 void IStateManager.TrackViewState ()
116                 {
117                         this.TrackViewState ();
118                 }
119                 
120                 bool IStateManager.IsTrackingViewState {
121                         get { return this.IsTrackingViewState; }
122                 }
123                 
124                 public override string ToString ()
125                 {
126                         object o = GetValue (HttpContext.Current, null);
127                         if (o != null) return o.ToString();
128                         return "";
129                 }
130                 
131                 [WebCategoryAttribute ("Parameter")]
132                 [DefaultValueAttribute (null)]
133                 [WebSysDescriptionAttribute ("Default value to be used in case value is null.")]
134                 public string DefaultValue {
135                         get { return ViewState.GetString ("DefaultValue", null); }
136                         set {
137                                 
138                                 if (DefaultValue != value) {
139                                         ViewState ["DefaultValue"] = value;
140                                         OnParameterChanged ();
141                                 }
142                         }
143                 }
144
145                 [WebCategoryAttribute ("Parameter")]
146                 [DefaultValueAttribute ("Input")]
147                 [WebSysDescriptionAttribute ("Parameter's direction.")]
148                 public ParameterDirection Direction
149                 {
150                         get { return (ParameterDirection) ViewState.GetInt ("Direction", (int)ParameterDirection.Input); }
151                         set {                           
152                                 if (Direction != value) {
153                                         ViewState ["Direction"] = value;
154                                         OnParameterChanged ();
155                                 }
156                         }
157                 }
158
159
160                 [WebCategoryAttribute ("Parameter")]
161                 [DefaultValueAttribute ("")]
162                 [WebSysDescriptionAttribute ("Parameter's name.")]
163                 public string Name
164                 {
165                         get {
166                                 string s = ViewState ["Name"] as string;
167                                 if (s != null)
168                                         return s;
169                                 
170                                 return "";
171                         }
172                         set {
173                                 
174                                 if (Name != value) {
175                                         ViewState ["Name"] = value;
176                                         OnParameterChanged ();
177                                 }
178                         }
179                 }
180
181                 [WebCategoryAttribute ("Parameter")]
182                 [DefaultValueAttribute (true)]
183                 [WebSysDescriptionAttribute ("Checks whether an empty string is treated as a null value.")]
184                 public bool ConvertEmptyStringToNull
185                 {
186                         get { return ViewState.GetBool ("ConvertEmptyStringToNull", true); }
187                         set {
188                                 if (ConvertEmptyStringToNull != value) {
189                                         ViewState["ConvertEmptyStringToNull"] = value;
190                                         OnParameterChanged ();
191                                 }
192                         }
193                 }
194
195                 [DefaultValue (0)]
196                 public int Size {
197                         get { return ViewState.GetInt ("Size", 0); }
198                         set {
199                                 if (Size != value) {
200                                         ViewState["Size"] = value;
201                                         OnParameterChanged ();
202                                 }
203                         }
204                 }
205
206                 [DefaultValueAttribute (TypeCode.Empty)]
207                 [WebCategoryAttribute ("Parameter"), 
208                 WebSysDescriptionAttribute("Represents type of the parameter.")]
209                 public TypeCode Type
210                 {
211                         get { return (TypeCode) ViewState.GetInt ("Type", (int)TypeCode.Empty); }
212                         set {
213                                 
214                                 if (Type != value) {
215                                         ViewState ["Type"] = value;
216                                         OnParameterChanged ();
217                                 }
218                         }
219                 }
220                 
221                 StateBag viewState;
222                 [BrowsableAttribute (false), 
223                 DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
224                 protected StateBag ViewState {
225                         get {
226                                 if (viewState == null) {
227                                         viewState = new StateBag ();
228                                         if (IsTrackingViewState)
229                                                 viewState.TrackViewState ();
230                                 }
231                                 return viewState;
232                         }
233                 }
234                 
235                 bool isTrackingViewState = false;
236                 protected bool IsTrackingViewState {
237                         get { return isTrackingViewState; }
238                 }
239
240                 protected virtual object Evaluate (HttpContext context, Control control)
241                 {
242                         return this.DefaultValue;
243                 }
244                 
245                 internal object GetValue (HttpContext context, Control control)
246                 {
247                         object oldValue = ViewState ["ParameterValue"];
248                         
249                         object newValue = ConvertValue (Evaluate (context, control));
250                         if (newValue == null)
251                                 newValue = ConvertValue (DefaultValue);
252
253                         if (!object.Equals (oldValue, newValue)) {
254                                 ViewState ["ParameterValue"] = newValue;
255                                 OnParameterChanged ();
256                         }
257                         return newValue;
258                 }
259                 
260                 object ConvertValue (object val)
261                 {
262                         if (val == null) return null;
263                         if (ConvertEmptyStringToNull && val.Equals (string.Empty))
264                                 return null;
265                         return Convert.ChangeType (val, Type);
266                 }
267                 
268                 protected internal virtual void SetDirty()
269                 {
270                         ViewState.SetDirty (true);
271                 }
272
273
274                 private ParameterCollection _owner;
275
276                 internal void SetOwnerCollection (ParameterCollection own)
277                 {
278                         _owner = own;
279                 }
280         }
281 }
282 #endif
283