use ResolveClientUrl instead of ResolveUrl to be complient with MS.NET
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / DataControlField.cs
1 //
2 // System.Web.UI.WebControls.DataControlField.cs
3 //
4 // Authors:
5 //      Sanjay Gupta (gsanjay@novell.com)
6 //      Lluis Sanchez Gual (lluis@novell.com)
7 //
8 // (C) 2004 Novell, Inc. (http://www.novell.com)
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 #if NET_2_0
33 using System.Collections;
34 using System.Collections.Specialized;
35 using System.Web.UI;
36 using System.ComponentModel;
37 using System.Security.Permissions;
38
39 namespace System.Web.UI.WebControls {
40
41         [DefaultPropertyAttribute ("HeaderText")]
42         [TypeConverterAttribute (typeof(ExpandableObjectConverter))]
43         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45         public abstract class DataControlField : IStateManager, IDataSourceViewSchemaAccessor
46         {
47                 bool tracking = false;
48                 StateBag viewState;
49                 Control control;
50                 Style controlStyle;
51                 TableItemStyle footerStyle;
52                 TableItemStyle headerStyle;
53                 TableItemStyle itemStyle;
54                 bool sortingEnabled;
55                 
56                 protected DataControlField()
57                 { 
58                         viewState = new StateBag ();
59                 }
60                 
61                 internal void SetDirty ()
62                 {
63                         viewState.SetDirty (true);
64                 }
65                 
66                 protected StateBag ViewState {
67                         get { return viewState; }
68                 }
69
70                 public virtual void ExtractValuesFromCell (IOrderedDictionary dictionary,
71                         DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
72                 {
73                 }
74
75                 public virtual bool Initialize (bool sortingEnabled, Control control)
76                 {
77                         this.sortingEnabled = sortingEnabled;
78                         this.control = control;
79                         return false;
80                 }
81
82                 public virtual void InitializeCell (DataControlFieldCell cell,
83                         DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
84                 {
85                         if (cellType == DataControlCellType.Header)
86                         {
87                                 if (HeaderText.Length > 0 && sortingEnabled && SortExpression.Length > 0)
88                                         cell.Controls.Add (new DataControlButton (control, HeaderText, HeaderImageUrl, DataControlCommands.SortCommandName, SortExpression, true));
89                                 else if (HeaderImageUrl.Length > 0) {
90                                         Image image = new Image ();
91                                         image.ImageUrl = HeaderImageUrl;
92                                         cell.Controls.Add (image);
93                                 }
94                                 else
95                                         cell.Text = HeaderText.Length > 0 ? HeaderText : " ";
96                         }
97                         else if (cellType == DataControlCellType.Footer) {
98                                 string footerText = FooterText;
99                                 cell.Text = (footerText.Length > 0) ? footerText : " ";
100                         }
101                 }
102                 
103                 protected internal DataControlField CloneField ()
104                 {
105                         DataControlField field = CreateField ();
106                         CopyProperties (field);
107                         return field;
108                 }
109                 
110                 protected abstract DataControlField CreateField ();
111                 
112                 protected virtual void CopyProperties (DataControlField newField)
113                 {
114                         newField.AccessibleHeaderText = AccessibleHeaderText;
115                         newField.ControlStyle.CopyFrom (ControlStyle);
116                         newField.FooterStyle.CopyFrom (FooterStyle);
117                         newField.FooterText = FooterText;
118                         newField.HeaderImageUrl = HeaderImageUrl;
119                         newField.HeaderStyle.CopyFrom (HeaderStyle);
120                         newField.HeaderText = HeaderText;
121                         newField.InsertVisible = InsertVisible;
122                         newField.ItemStyle.CopyFrom (ItemStyle);
123                         newField.ShowHeader = ShowHeader;
124                         newField.SortExpression = SortExpression;
125                         newField.Visible = Visible;
126                 }
127                 
128                 protected virtual void OnFieldChanged ()
129                 {
130                         if (FieldChanged != null)
131                                 FieldChanged (this, EventArgs.Empty);
132                 }       
133         
134                 protected virtual void LoadViewState (object savedState)
135                 {
136                         if (savedState == null)
137                                 return;
138                                 
139                         object [] states = (object []) savedState;
140                         viewState.LoadViewState (states[0]);
141                         
142                         if (states[1] != null)
143                                 ((IStateManager)ControlStyle).LoadViewState (states[1]);
144                         if (states[2] != null)
145                                 ((IStateManager)FooterStyle).LoadViewState (states[2]);
146                         if (states[3] != null)
147                                 ((IStateManager)HeaderStyle).LoadViewState (states[3]);
148                         if (states[4] != null)
149                                 ((IStateManager)ItemStyle).LoadViewState (states[4]);
150                 }
151
152                 protected virtual object SaveViewState()
153                 {
154                         object[] state = new object [5];
155                         state [0] = viewState.SaveViewState ();
156                         if (controlStyle != null)
157                                 state [1] = ((IStateManager) controlStyle).SaveViewState ();
158                         if (footerStyle != null)
159                                 state [2] = ((IStateManager) footerStyle).SaveViewState ();
160                         if (headerStyle != null)
161                                 state [3] = ((IStateManager) headerStyle).SaveViewState ();
162                         if (itemStyle != null)
163                                 state [4] = ((IStateManager) itemStyle).SaveViewState ();
164                         
165                         if (state [0] == null && state [1] == null && state [2] == null && 
166                                 state [3] == null && state [4] == null)
167                                 return null;
168                                 
169                         return state;
170                 }
171
172                 protected virtual void TrackViewState()
173                 {
174                         if (controlStyle != null) ((IStateManager) controlStyle).TrackViewState ();
175                         if (footerStyle != null) ((IStateManager) footerStyle).TrackViewState ();
176                         if (headerStyle != null) ((IStateManager) headerStyle).TrackViewState ();
177                         if (itemStyle != null) ((IStateManager) itemStyle).TrackViewState ();
178                         viewState.TrackViewState ();
179                         tracking = true;                        
180                 }
181                 
182                 public virtual void ValidateSupportsCallback ()
183                 {
184                         throw new NotSupportedException ("Callback not supported");
185                 }
186
187                 void IStateManager.LoadViewState(object savedState)
188                 {
189                         LoadViewState(savedState);
190                 }
191
192                 object IStateManager.SaveViewState()
193                 {
194                         return SaveViewState();
195                 }
196
197                 void IStateManager.TrackViewState()
198                 {
199                         TrackViewState();
200                 }
201                 
202                 internal Exception GetNotSupportedPropException (string propName)
203                 {
204                         return new System.NotSupportedException ("The property '" + propName + "' is not supported in " + GetType().Name); 
205                 }
206
207                 [MonoTODO ("Render this")]
208                 [DefaultValueAttribute ("")]
209                 [LocalizableAttribute (true)]
210                 [WebCategoryAttribute ("Accessibility")]
211                 public virtual string AccessibleHeaderText {
212                         get {
213                                 object val = viewState ["accessibleHeaderText"];
214                                 return val != null ? (string) val : "";
215                         }
216                         set { 
217                                 viewState ["accessibleHeaderText"] = value;
218                                 OnFieldChanged ();
219                         }
220                 }
221
222                 protected Control Control {
223                         get { return control; }
224                 }
225
226                 [WebCategoryAttribute ("Styles")]
227                 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
228                 [DefaultValueAttribute (null)]
229                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
230                 public Style ControlStyle {
231                         get {
232                                 if (controlStyle == null) {
233                                         controlStyle = new Style ();
234                                         if (IsTrackingViewState)
235                                                 controlStyle.TrackViewState();
236                                 }
237                                 return controlStyle;
238                         }
239                 }
240         
241                 protected bool DesignMode {
242                         get { return control != null && control.Site != null ? control.Site.DesignMode : false; }
243                 }
244
245                 [DefaultValueAttribute (null)]
246                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
247                 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
248                 [WebCategoryAttribute ("Styles")]
249                 public TableItemStyle FooterStyle {
250                         get {
251                                 if (footerStyle == null) {
252                                         footerStyle = new TableItemStyle ();
253                                         if (IsTrackingViewState)
254                                                 footerStyle.TrackViewState();
255                                 }
256                                 return footerStyle;
257                         }
258                 }
259
260                 [LocalizableAttribute (true)]
261                 [WebCategoryAttribute ("Appearance")]
262                 [DefaultValue ("")]
263                 public virtual string FooterText {
264                         get {
265                                 object val = viewState ["footerText"];
266                                 return val != null ? (string) val : "";
267                         }
268                         set { 
269                                 viewState ["footerText"] = value;
270                                 OnFieldChanged ();
271                         }
272                 }
273
274                 [UrlPropertyAttribute]
275                 [DefaultValueAttribute ("")]
276                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
277                 [WebCategoryAttribute ("Appearance")]
278                 public virtual string HeaderImageUrl {
279                         get {
280                                 object val = viewState ["headerImageUrl"];
281                                 return val != null ? (string) val : "";
282                         }
283                         set { 
284                                 viewState ["headerImageUrl"] = value;
285                                 OnFieldChanged ();
286                         }
287                 }
288
289                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
290                 [WebCategoryAttribute ("Styles")]
291                 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
292                 [DefaultValueAttribute (null)]
293                 public TableItemStyle HeaderStyle {
294                         get {
295                                 if (headerStyle == null) {
296                                         headerStyle = new TableItemStyle ();
297                                         if (IsTrackingViewState)
298                                                 headerStyle.TrackViewState();
299                                 }
300                                 return headerStyle;
301                         }
302                 }
303
304                 [DefaultValueAttribute ("")]
305                 [LocalizableAttribute (true)]
306                 [WebCategoryAttribute ("Appearance")]
307                 public virtual string HeaderText {
308                         get {
309                                 object val = viewState ["headerText"];
310                                 return val != null ? (string) val : "";
311                         }
312                         set { 
313                                 viewState ["headerText"] = value;
314                                 OnFieldChanged ();
315                         }
316                 }
317
318                 [WebCategoryAttribute ("Behavior")]
319                 [DefaultValueAttribute (true)]
320                 public virtual bool InsertVisible {
321                         get {
322                                 object val = viewState ["InsertVisible"];
323                                 return val != null ? (bool) val : true;
324                         }
325                         set { 
326                                 viewState ["InsertVisible"] = value;
327                                 OnFieldChanged ();
328                         }
329                 }
330
331                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
332                 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
333                 [WebCategoryAttribute ("Styles")]
334                 [DefaultValueAttribute (null)]
335                 public TableItemStyle ItemStyle {
336                         get {
337                                 if (itemStyle == null) {
338                                         itemStyle = new TableItemStyle ();
339                                         if (IsTrackingViewState)
340                                                 itemStyle.TrackViewState();
341                                 }
342                                 return itemStyle;
343                         }
344                 }
345
346                 [WebCategoryAttribute ("Behavior")]
347                 [DefaultValueAttribute (true)]
348                 public virtual bool ShowHeader {
349                         get {
350                                 object val = viewState ["showHeader"];
351                                 return val != null ? (bool) val : true;
352                         }
353                         set { 
354                                 viewState ["showHeader"] = value;
355                                 OnFieldChanged ();
356                         }
357                 }
358
359                 [DefaultValueAttribute ("")]
360 //              [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
361                 [WebCategoryAttribute ("Behavior")]
362                 public virtual string SortExpression {
363                         get {
364                                 object val = viewState ["sortExpression"];
365                                 return val != null ? (string) val : "";
366                         }
367                         set { 
368                                 viewState ["sortExpression"] = value;
369                                 OnFieldChanged ();
370                         }
371                 }
372
373                 [WebCategoryAttribute ("Behavior")]
374                 [DefaultValueAttribute (true)]
375                 public bool Visible {
376                         get {
377                                 object val = viewState ["visible"];
378                                 return val != null ? (bool) val : true;
379                         }
380                         set { 
381                                 viewState ["visible"] = value;
382                                 OnFieldChanged ();
383                         }
384                 }
385
386                 protected bool IsTrackingViewState
387                 {
388                         get { return tracking; }
389                 }
390
391                 bool IStateManager.IsTrackingViewState
392                 {
393                         get { return IsTrackingViewState; }
394                 }
395
396                 object IDataSourceViewSchemaAccessor.DataSourceViewSchema {
397                         get { return viewState ["dataSourceViewSchema"]; }
398                         set { 
399                                 viewState ["dataSourceViewSchema"] = value;
400                         }
401                 }               
402
403                 internal event EventHandler FieldChanged;
404
405                 public override string ToString ()
406                 {
407                         if (string.IsNullOrEmpty (HeaderText))
408                                 return base.ToString ();
409                         return HeaderText;
410                 }
411         }
412 }
413 #endif