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