* DataControlField.cs: correct handling empty header/footer text.
[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 || HeaderImageUrl.Length > 0) {
88                                         if (sortingEnabled && SortExpression.Length > 0)
89                                                 cell.Controls.Add (new DataControlButton (control, HeaderText, HeaderImageUrl, DataControlCommands.SortCommandName, SortExpression, true));
90                                         else
91                                                 cell.Controls.Add (new DataControlButton (control, HeaderText, HeaderImageUrl, string.Empty, string.Empty, true));
92                                 }
93                                 else
94                                         cell.Text = " ";
95                         }
96                         else if (cellType == DataControlCellType.Footer) {
97                                 string footerText = FooterText;
98                                 cell.Text = (footerText.Length > 0) ? footerText : " ";
99                         }
100                 }
101                 
102                 protected internal DataControlField CloneField ()
103                 {
104                         DataControlField field = CreateField ();
105                         CopyProperties (field);
106                         return field;
107                 }
108                 
109                 protected abstract DataControlField CreateField ();
110                 
111                 protected virtual void CopyProperties (DataControlField newField)
112                 {
113                         newField.AccessibleHeaderText = AccessibleHeaderText;
114                         newField.ControlStyle.CopyFrom (ControlStyle);
115                         newField.FooterStyle.CopyFrom (FooterStyle);
116                         newField.FooterText = FooterText;
117                         newField.HeaderImageUrl = HeaderImageUrl;
118                         newField.HeaderStyle.CopyFrom (HeaderStyle);
119                         newField.HeaderText = HeaderText;
120                         newField.InsertVisible = InsertVisible;
121                         newField.ItemStyle.CopyFrom (ItemStyle);
122                         newField.ShowHeader = ShowHeader;
123                         newField.SortExpression = SortExpression;
124                         newField.Visible = Visible;
125                 }
126                 
127                 protected virtual void OnFieldChanged ()
128                 {
129                         if (FieldChanged != null)
130                                 FieldChanged (this, EventArgs.Empty);
131                 }       
132         
133                 protected virtual void LoadViewState (object savedState)
134                 {
135                         if (savedState == null)
136                                 return;
137                                 
138                         object [] states = (object []) savedState;
139                         viewState.LoadViewState (states[0]);
140                         
141                         if (states[1] != null)
142                                 ((IStateManager)controlStyle).LoadViewState (states[1]);
143                         if (states[2] != null)
144                                 ((IStateManager)footerStyle).LoadViewState (states[2]);
145                         if (states[3] != null)
146                                 ((IStateManager)headerStyle).LoadViewState (states[3]);
147                         if (states[4] != null)
148                                 ((IStateManager)itemStyle).LoadViewState (states[4]);
149                 }
150
151                 protected virtual object SaveViewState()
152                 {
153                         object[] state = new object [5];
154                         state [0] = viewState.SaveViewState ();
155                         if (controlStyle != null)
156                                 state [1] = ((IStateManager) controlStyle).SaveViewState ();
157                         if (footerStyle != null)
158                                 state [2] = ((IStateManager) footerStyle).SaveViewState ();
159                         if (headerStyle != null)
160                                 state [3] = ((IStateManager) headerStyle).SaveViewState ();
161                         if (itemStyle != null)
162                                 state [4] = ((IStateManager) itemStyle).SaveViewState ();
163                         
164                         if (state [0] == null && state [1] == null && state [2] == null && 
165                                 state [3] == null && state [4] == null)
166                                 return null;
167                                 
168                         return state;
169                 }
170
171                 protected virtual void TrackViewState()
172                 {
173                         if (controlStyle != null) ((IStateManager) controlStyle).TrackViewState ();
174                         if (footerStyle != null) ((IStateManager) footerStyle).TrackViewState ();
175                         if (headerStyle != null) ((IStateManager) headerStyle).TrackViewState ();
176                         if (itemStyle != null) ((IStateManager) itemStyle).TrackViewState ();
177                         viewState.TrackViewState ();
178                         tracking = true;                        
179                 }
180                 
181                 public virtual void ValidateSupportsCallback ()
182                 {
183                         throw new NotSupportedException ("Callback not supported");
184                 }
185
186                 void IStateManager.LoadViewState(object savedState)
187                 {
188                         LoadViewState(savedState);
189                 }
190
191                 object IStateManager.SaveViewState()
192                 {
193                         return SaveViewState();
194                 }
195
196                 void IStateManager.TrackViewState()
197                 {
198                         TrackViewState();
199                 }
200                 
201                 internal Exception GetNotSupportedPropException (string propName)
202                 {
203                         return new System.NotSupportedException ("The property '" + propName + "' is not supported in " + GetType().Name); 
204                 }
205
206                 [MonoTODO ("Render this")]
207                 [DefaultValueAttribute ("")]
208                 [LocalizableAttribute (true)]
209                 [WebCategoryAttribute ("Accessibility")]
210                 public virtual string AccessibleHeaderText {
211                         get {
212                                 object val = viewState ["accessibleHeaderText"];
213                                 return val != null ? (string) val : "";
214                         }
215                         set { 
216                                 viewState ["accessibleHeaderText"] = value;
217                                 OnFieldChanged ();
218                         }
219                 }
220
221                 protected Control Control {
222                         get { return control; }
223                 }
224
225                 [WebCategoryAttribute ("Styles")]
226                 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
227                 [DefaultValueAttribute (null)]
228                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
229                 public Style ControlStyle {
230                         get {
231                                 if (controlStyle == null) {
232                                         controlStyle = new Style ();
233                                         if (IsTrackingViewState)
234                                                 controlStyle.TrackViewState();
235                                 }
236                                 return controlStyle;
237                         }
238                 }
239         
240                 protected bool DesignMode {
241                         get { return control != null && control.Site != null ? control.Site.DesignMode : false; }
242                 }
243
244                 [DefaultValueAttribute (null)]
245                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
246                 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
247                 [WebCategoryAttribute ("Styles")]
248                 public TableItemStyle FooterStyle {
249                         get {
250                                 if (footerStyle == null) {
251                                         footerStyle = new TableItemStyle ();
252                                         if (IsTrackingViewState)
253                                                 footerStyle.TrackViewState();
254                                 }
255                                 return footerStyle;
256                         }
257                 }
258
259                 [LocalizableAttribute (true)]
260                 [WebCategoryAttribute ("Appearance")]
261                 [DefaultValue ("")]
262                 public virtual string FooterText {
263                         get {
264                                 object val = viewState ["footerText"];
265                                 return val != null ? (string) val : "";
266                         }
267                         set { 
268                                 viewState ["footerText"] = value;
269                                 OnFieldChanged ();
270                         }
271                 }
272
273                 [UrlPropertyAttribute]
274                 [DefaultValueAttribute ("")]
275                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
276                 [WebCategoryAttribute ("Appearance")]
277                 public virtual string HeaderImageUrl {
278                         get {
279                                 object val = viewState ["headerImageUrl"];
280                                 return val != null ? (string) val : "";
281                         }
282                         set { 
283                                 viewState ["headerImageUrl"] = value;
284                                 OnFieldChanged ();
285                         }
286                 }
287
288                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
289                 [WebCategoryAttribute ("Styles")]
290                 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
291                 [DefaultValueAttribute (null)]
292                 public TableItemStyle HeaderStyle {
293                         get {
294                                 if (headerStyle == null) {
295                                         headerStyle = new TableItemStyle ();
296                                         if (IsTrackingViewState)
297                                                 headerStyle.TrackViewState();
298                                 }
299                                 return headerStyle;
300                         }
301                 }
302
303                 [DefaultValueAttribute ("")]
304                 [LocalizableAttribute (true)]
305                 [WebCategoryAttribute ("Appearance")]
306                 public virtual string HeaderText {
307                         get {
308                                 object val = viewState ["headerText"];
309                                 return val != null ? (string) val : "";
310                         }
311                         set { 
312                                 viewState ["headerText"] = value;
313                                 OnFieldChanged ();
314                         }
315                 }
316
317                 [WebCategoryAttribute ("Behavior")]
318                 [DefaultValueAttribute (true)]
319                 public virtual bool InsertVisible {
320                         get {
321                                 object val = viewState ["InsertVisible"];
322                                 return val != null ? (bool) val : true;
323                         }
324                         set { 
325                                 viewState ["InsertVisible"] = value;
326                                 OnFieldChanged ();
327                         }
328                 }
329
330                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
331                 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
332                 [WebCategoryAttribute ("Styles")]
333                 [DefaultValueAttribute (null)]
334                 public TableItemStyle ItemStyle {
335                         get {
336                                 if (itemStyle == null) {
337                                         itemStyle = new TableItemStyle ();
338                                         if (IsTrackingViewState)
339                                                 itemStyle.TrackViewState();
340                                 }
341                                 return itemStyle;
342                         }
343                 }
344
345                 [WebCategoryAttribute ("Behavior")]
346                 [DefaultValueAttribute (true)]
347                 public virtual bool ShowHeader {
348                         get {
349                                 object val = viewState ["showHeader"];
350                                 return val != null ? (bool) val : true;
351                         }
352                         set { 
353                                 viewState ["showHeader"] = value;
354                                 OnFieldChanged ();
355                         }
356                 }
357
358                 [DefaultValueAttribute ("")]
359 //              [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
360                 [WebCategoryAttribute ("Behavior")]
361                 public virtual string SortExpression {
362                         get {
363                                 object val = viewState ["sortExpression"];
364                                 return val != null ? (string) val : "";
365                         }
366                         set { 
367                                 viewState ["sortExpression"] = value;
368                                 OnFieldChanged ();
369                         }
370                 }
371
372                 [WebCategoryAttribute ("Behavior")]
373                 [DefaultValueAttribute (true)]
374                 public bool Visible {
375                         get {
376                                 object val = viewState ["visible"];
377                                 return val != null ? (bool) val : true;
378                         }
379                         set { 
380                                 viewState ["visible"] = value;
381                                 OnFieldChanged ();
382                         }
383                 }
384
385                 protected bool IsTrackingViewState
386                 {
387                         get { return tracking; }
388                 }
389
390                 bool IStateManager.IsTrackingViewState
391                 {
392                         get { return IsTrackingViewState; }
393                 }
394
395                 object IDataSourceViewSchemaAccessor.DataSourceViewSchema {
396                         get { return viewState ["dataSourceViewSchema"]; }
397                         set { 
398                                 viewState ["dataSourceViewSchema"] = value;
399                         }
400                 }               
401
402                 internal event EventHandler FieldChanged;
403         }
404 }
405 #endif