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