2008-03-13 Marek Habersack <mhabersack@novell.com>
[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 ((Control) DataControlButton.CreateButton (String.IsNullOrEmpty (HeaderImageUrl) ? ButtonType.Link : ButtonType.Image, 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 : "&nbsp;";
96                         }
97                         else if (cellType == DataControlCellType.Footer) {
98                                 string footerText = FooterText;
99                                 cell.Text = (footerText.Length > 0) ? footerText : "&nbsp;";
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                 internal bool ControlStyleCreated { get { return controlStyle != null; } }
208                 
209                 internal bool HeaderStyleCreated { get { return headerStyle != null; } }
210                 
211                 internal bool FooterStyleCreated { get { return footerStyle != null; } }
212                 
213                 internal bool ItemStyleCreated { get { return itemStyle != null; } }
214
215                 [MonoTODO ("Render this")]
216                 [DefaultValueAttribute ("")]
217                 [LocalizableAttribute (true)]
218                 [WebCategoryAttribute ("Accessibility")]
219                 public virtual string AccessibleHeaderText {
220                         get {
221                                 object val = viewState ["accessibleHeaderText"];
222                                 return val != null ? (string) val : "";
223                         }
224                         set { 
225                                 viewState ["accessibleHeaderText"] = value;
226                                 OnFieldChanged ();
227                         }
228                 }
229
230                 protected Control Control {
231                         get { return control; }
232                 }
233
234                 [WebCategoryAttribute ("Styles")]
235                 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
236                 [DefaultValueAttribute (null)]
237                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
238                 public Style ControlStyle {
239                         get {
240                                 if (controlStyle == null) {
241                                         controlStyle = new Style ();
242                                         if (IsTrackingViewState)
243                                                 controlStyle.TrackViewState();
244                                 }
245                                 return controlStyle;
246                         }
247                 }
248         
249                 protected bool DesignMode {
250                         get { return control != null && control.Site != null ? control.Site.DesignMode : false; }
251                 }
252
253                 [DefaultValueAttribute (null)]
254                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
255                 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
256                 [WebCategoryAttribute ("Styles")]
257                 public TableItemStyle FooterStyle {
258                         get {
259                                 if (footerStyle == null) {
260                                         footerStyle = new TableItemStyle ();
261                                         if (IsTrackingViewState)
262                                                 footerStyle.TrackViewState();
263                                 }
264                                 return footerStyle;
265                         }
266                 }
267
268                 [LocalizableAttribute (true)]
269                 [WebCategoryAttribute ("Appearance")]
270                 [DefaultValue ("")]
271                 public virtual string FooterText {
272                         get {
273                                 object val = viewState ["footerText"];
274                                 return val != null ? (string) val : "";
275                         }
276                         set { 
277                                 viewState ["footerText"] = value;
278                                 OnFieldChanged ();
279                         }
280                 }
281
282                 [UrlPropertyAttribute]
283                 [DefaultValueAttribute ("")]
284                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
285                 [WebCategoryAttribute ("Appearance")]
286                 public virtual string HeaderImageUrl {
287                         get {
288                                 object val = viewState ["headerImageUrl"];
289                                 return val != null ? (string) val : "";
290                         }
291                         set { 
292                                 viewState ["headerImageUrl"] = value;
293                                 OnFieldChanged ();
294                         }
295                 }
296
297                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
298                 [WebCategoryAttribute ("Styles")]
299                 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
300                 [DefaultValueAttribute (null)]
301                 public TableItemStyle HeaderStyle {
302                         get {
303                                 if (headerStyle == null) {
304                                         headerStyle = new TableItemStyle ();
305                                         if (IsTrackingViewState)
306                                                 headerStyle.TrackViewState();
307                                 }
308                                 return headerStyle;
309                         }
310                 }
311
312                 [DefaultValueAttribute ("")]
313                 [LocalizableAttribute (true)]
314                 [WebCategoryAttribute ("Appearance")]
315                 public virtual string HeaderText {
316                         get {
317                                 object val = viewState ["headerText"];
318                                 return val != null ? (string) val : "";
319                         }
320                         set { 
321                                 viewState ["headerText"] = value;
322                                 OnFieldChanged ();
323                         }
324                 }
325
326                 [WebCategoryAttribute ("Behavior")]
327                 [DefaultValueAttribute (true)]
328                 public virtual bool InsertVisible {
329                         get {
330                                 object val = viewState ["InsertVisible"];
331                                 return val != null ? (bool) val : true;
332                         }
333                         set { 
334                                 viewState ["InsertVisible"] = value;
335                                 OnFieldChanged ();
336                         }
337                 }
338
339                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
340                 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
341                 [WebCategoryAttribute ("Styles")]
342                 [DefaultValueAttribute (null)]
343                 public TableItemStyle ItemStyle {
344                         get {
345                                 if (itemStyle == null) {
346                                         itemStyle = new TableItemStyle ();
347                                         if (IsTrackingViewState)
348                                                 itemStyle.TrackViewState();
349                                 }
350                                 return itemStyle;
351                         }
352                 }
353
354                 [WebCategoryAttribute ("Behavior")]
355                 [DefaultValueAttribute (true)]
356                 public virtual bool ShowHeader {
357                         get {
358                                 object val = viewState ["showHeader"];
359                                 return val != null ? (bool) val : true;
360                         }
361                         set { 
362                                 viewState ["showHeader"] = value;
363                                 OnFieldChanged ();
364                         }
365                 }
366
367                 [DefaultValueAttribute ("")]
368 //              [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
369                 [WebCategoryAttribute ("Behavior")]
370                 public virtual string SortExpression {
371                         get {
372                                 object val = viewState ["sortExpression"];
373                                 return val != null ? (string) val : "";
374                         }
375                         set { 
376                                 viewState ["sortExpression"] = value;
377                                 OnFieldChanged ();
378                         }
379                 }
380
381                 [WebCategoryAttribute ("Behavior")]
382                 [DefaultValueAttribute (true)]
383                 public bool Visible {
384                         get {
385                                 object val = viewState ["visible"];
386                                 return val != null ? (bool) val : true;
387                         }
388                         set { 
389                                 if (value == Visible)
390                                         return;
391                                 viewState ["visible"] = value;
392                                 OnFieldChanged ();
393                         }
394                 }
395
396                 protected bool IsTrackingViewState
397                 {
398                         get { return tracking; }
399                 }
400
401                 bool IStateManager.IsTrackingViewState
402                 {
403                         get { return IsTrackingViewState; }
404                 }
405
406                 object IDataSourceViewSchemaAccessor.DataSourceViewSchema {
407                         get { return viewState ["dataSourceViewSchema"]; }
408                         set { 
409                                 viewState ["dataSourceViewSchema"] = value;
410                         }
411                 }               
412
413                 internal event EventHandler FieldChanged;
414
415                 public override string ToString ()
416                 {
417                         if (string.IsNullOrEmpty (HeaderText))
418                                 return base.ToString ();
419                         return HeaderText;
420                 }
421         }
422 }
423 #endif