2004-05-26 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / DataGridColumn.cs
1 //
2 // System.Web.UI.WebControls.DataGridColumn.cs
3 //
4 // Authors:
5 //   Gaurav Vaish (gvaish@iitk.ac.in)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
10 //
11
12 using System;
13 using System.Web;
14 using System.Web.UI;
15 using System.ComponentModel;
16
17 namespace System.Web.UI.WebControls
18 {
19         [TypeConverter(typeof(ExpandableObjectConverter))]
20         public abstract class DataGridColumn : IStateManager
21         {
22                 private StateBag viewState;
23                 private bool     marked;
24                 private TableItemStyle footerStyle;
25                 private TableItemStyle headerStyle;
26                 private TableItemStyle itemStyle;
27
28                 private DataGrid owner;
29                 private bool     designMode;
30
31                 public DataGridColumn()
32                 {
33                         viewState = new StateBag();
34                 }
35
36                 internal TableItemStyle FooterStyleInternal
37                 {
38                         get
39                         {
40                                 return footerStyle;
41                         }
42                 }
43
44                 internal TableItemStyle HeaderStyleInternal
45                 {
46                         get
47                         {
48                                 return headerStyle;
49                         }
50                 }
51
52                 internal TableItemStyle ItemStyleInternal
53                 {
54                         get
55                         {
56                                 return itemStyle;
57                         }
58                 }
59
60                 [DefaultValue (null), WebCategory ("Misc")]
61                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
62                 [PersistenceMode (PersistenceMode.InnerProperty)]
63                 [WebSysDescription ("The style applied to the footer of this column.")]
64                 public virtual TableItemStyle FooterStyle
65                 {
66                         get
67                         {
68                                 if(footerStyle == null)
69                                 {
70                                         footerStyle = new TableItemStyle();
71                                         if(IsTrackingViewState)
72                                         {
73                                                 footerStyle.TrackViewState();
74                                         }
75                                 }
76                                 return footerStyle;
77                         }
78                 }
79
80                 [DefaultValue (null), WebCategory ("Misc")]
81                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
82                 [PersistenceMode (PersistenceMode.InnerProperty)]
83                 [WebSysDescription ("The style applied to the header of this column.")]
84                 public virtual TableItemStyle HeaderStyle
85                 {
86                         get
87                         {
88                                 if(headerStyle == null)
89                                 {
90                                         headerStyle= new TableItemStyle();
91                                         if(IsTrackingViewState)
92                                         {
93                                                 headerStyle.TrackViewState();
94                                         }
95                                 }
96                                 return headerStyle;
97                         }
98                 }
99
100                 [DefaultValue (null), WebCategory ("Misc")]
101                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
102                 [PersistenceMode (PersistenceMode.InnerProperty)]
103                 [WebSysDescription ("The style applied to the rows of this column.")]
104                 public virtual TableItemStyle ItemStyle
105                 {
106                         get
107                         {
108                                 if(itemStyle == null)
109                                 {
110                                         itemStyle = new TableItemStyle();
111                                         if(IsTrackingViewState)
112                                         {
113                                                 itemStyle.TrackViewState();
114                                         }
115                                 }
116                                 return itemStyle;
117                         }
118                 }
119
120                 [DefaultValue (""), WebCategory ("Misc")]
121                 [WebSysDescription ("The text within the footer of this column.")]
122                 public virtual string FooterText
123                 {
124                         get
125                         {
126                                 object o = ViewState["FooterText"];
127                                 if(o != null)
128                                 {
129                                         return (string)o;
130                                 }
131                                 return String.Empty;
132                         }
133                         set
134                         {
135                                 ViewState["FooterText"] = value;
136                                 OnColumnChanged();
137                         }
138                 }
139
140                 [DefaultValue (""), WebCategory ("Misc")]
141                 [WebSysDescription ("The URL to an image that is displayed in the header of this column.")]
142                 public virtual string HeaderImageUrl
143                 {
144                         get
145                         {
146                                 object o = ViewState["HeaderImageUrl"];
147                                 if(o != null)
148                                 {
149                                         return (string)o;
150                                 }
151                                 return String.Empty;
152                         }
153                         set
154                         {
155                                 ViewState["HeaderImageUrl"] = value;
156                                 OnColumnChanged();
157                         }
158                 }
159
160                 [DefaultValue (""), WebCategory ("Misc")]
161                 [WebSysDescription ("The text within the header of this column.")]
162                 public virtual string HeaderText
163                 {
164                         get
165                         {
166                                 object o = ViewState["HeaderText"];
167                                 if(o != null)
168                                 {
169                                         return (string)o;
170                                 }
171                                 return String.Empty;
172                         }
173                         set
174                         {
175                                 ViewState["HeaderText"] = value;
176                                 OnColumnChanged();
177                         }
178                 }
179
180                 [DefaultValue (""), WebCategory ("Misc")]
181                 [WebSysDescription ("An expression that determines how the colum should be sorted.")]
182                 public virtual string SortExpression
183                 {
184                         get
185                         {
186                                 object o = ViewState["SortExpression"];
187                                 if(o != null)
188                                 {
189                                         return (string)o;
190                                 }
191                                 return String.Empty;
192                         }
193                         set
194                         {
195                                 ViewState["SortExpression"] = value;
196                                 OnColumnChanged();
197                         }
198                 }
199
200                 [DefaultValue (true), WebCategory ("Misc")]
201                 [WebSysDescription ("The visibility of this column.")]
202                 public bool Visible
203                 {
204                         get
205                         {
206                                 object o = ViewState["Visible"];
207                                 if(o != null)
208                                 {
209                                         return (bool)o;
210                                 }
211                                 return true;
212                         }
213                         set
214                         {
215                                 ViewState["Visible"] = value;
216                                 OnColumnChanged();
217                         }
218                 }
219
220                 public virtual void Initialize()
221                 {
222                         if(owner != null && owner.Site != null)
223                         {
224                                 designMode = owner.Site.DesignMode;
225                         }
226                 }
227
228                 public virtual void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
229                 {
230                         switch(itemType)
231                         {
232                                 case ListItemType.Header : InitializeCellHeader(cell, columnIndex);
233                                                            break;
234                                 case ListItemType.Footer : InitializeCellFooter(cell, columnIndex);
235                                                            break;
236                                 default                  : if (ItemStyleInternal != null)
237                                                                 cell.ApplyStyle (ItemStyleInternal);
238                                                            return;
239                         }
240                 }
241
242                 private void InitializeCellHeader(TableCell cell, int columnIndex)
243                 {
244                         WebControl ctrl = null;
245                         bool       sort = true;
246                         string     sortExpr = "";
247                         ImageButton headButton;
248                         Image       headImage;
249                         LinkButtonInternal link;
250
251                         if(owner != null)
252                         {
253                                 sort = owner.AllowSorting;
254                         }
255                         if(sort)
256                         {
257                                 sortExpr = SortExpression;
258                                 if(sortExpr.Length == 0)
259                                 {
260                                         sort = false;
261                                 }
262                         }
263                         if(HeaderImageUrl.Length > 0)
264                         {
265                                 if(sort)
266                                 {
267                                         headButton = new ImageButton();
268                                         headButton.ImageUrl = HeaderImageUrl;
269                                         headButton.CommandName = "Sort";
270                                         headButton.CommandArgument = sortExpr;
271                                         headButton.CausesValidation = false;
272                                         ctrl = headButton;
273                                 } else
274                                 {
275                                         headImage = new Image();
276                                         headImage.ImageUrl = HeaderImageUrl;
277                                         ctrl = headImage;
278                                 }
279                         } else
280                         {
281                                 if(sort)
282                                 {
283                                         link = new LinkButtonInternal();
284                                         link.Text = HeaderText;
285                                         link.CommandName = "Sort";
286                                         link.CommandArgument = sortExpr;
287                                         link.CausesValidation = false;
288                                         ctrl = link;
289                                 } else
290                                 {
291                                         if(HeaderText.Length > 0)
292                                         {
293                                                 cell.Text = HeaderText;
294                                         } else
295                                         {
296                                                 cell.Text = "&nbsp;";
297                                         }
298                                 }
299                         }
300                         if(ctrl != null)
301                         {
302                                 cell.Controls.Add(ctrl);
303                         }
304                         if (HeaderStyleInternal != null)
305                                 cell.ApplyStyle (HeaderStyleInternal);
306                 }
307
308                 private void InitializeCellFooter(TableCell cell, int columnIndex)
309                 {
310                         cell.Text = (FooterText.Length > 0 ? FooterText : "&nbsp;");
311                         if (FooterStyleInternal != null)
312                                 cell.ApplyStyle (FooterStyleInternal);
313                 }
314
315                 public override string ToString()
316                 {
317                         return String.Empty;
318                 }
319
320                 protected bool DesignMode
321                 {
322                         get
323                         {
324                                 return designMode;
325                         }
326                 }
327
328                 protected DataGrid Owner
329                 {
330                         get
331                         {
332                                 return owner;
333                         }
334                 }
335
336                 protected StateBag ViewState
337                 {
338                         get
339                         {
340                                 return viewState;
341                         }
342                 }
343
344                 /// <summary>
345                 /// Undocumented
346                 /// </summary>
347                 protected virtual void OnColumnChanged()
348                 {
349                         if(owner != null)
350                         {
351                                 owner.OnColumnsChanged();
352                         }
353                 }
354
355                 internal void SetOwner (DataGrid datagrid)
356                 {
357                         owner = datagrid;
358                 }
359                 
360                 protected virtual object SaveViewState()
361                 {
362                         object[] states = new object[4];
363                         states[0] = ViewState.SaveViewState();
364                         states[1] = (footerStyle == null ? null : footerStyle.SaveViewState());
365                         states[2] = (headerStyle == null ? null : headerStyle.SaveViewState());
366                         states[3] = (itemStyle == null ? null : itemStyle.SaveViewState());
367                         
368                         for (int i = 0; i < states.Length; i++) {
369                                 if (states [i] != null)
370                                         return states;
371                         }
372                         return null;
373                 }
374
375                 protected virtual void LoadViewState(object savedState)
376                 {
377                         if (savedState == null)
378                                 return;
379
380                         object[] states = (object[]) savedState;
381                         ViewState.LoadViewState (states [0]);
382                         FooterStyle.LoadViewState (states [1]);
383                         HeaderStyle.LoadViewState (states [2]);
384                         ItemStyle.LoadViewState (states [3]);
385                 }
386
387                 protected virtual void TrackViewState()
388                 {
389                         marked = true;
390                         ViewState.TrackViewState();
391                         if(footerStyle != null)
392                         {
393                                 footerStyle.TrackViewState();
394                         }
395                         if(headerStyle != null)
396                         {
397                                 headerStyle.TrackViewState();
398                         }
399                         if(itemStyle != null)
400                         {
401                                 itemStyle.TrackViewState();
402                         }
403                 }
404
405                 protected bool IsTrackingViewState
406                 {
407                         get
408                         {
409                                 return marked;
410                         }
411                 }
412
413                 void IStateManager.LoadViewState(object savedState)
414                 {
415                         LoadViewState(savedState);
416                 }
417
418                 object IStateManager.SaveViewState()
419                 {
420                         return SaveViewState();
421                 }
422
423                 void IStateManager.TrackViewState()
424                 {
425                         TrackViewState();
426                 }
427
428                 bool IStateManager.IsTrackingViewState
429                 {
430                         get
431                         {
432                                 return IsTrackingViewState;
433                         }
434                 }
435         }
436 }