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