2008-03-13 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / AutoGeneratedField.cs
1 //
2 // System.Web.UI.WebControls.AutoGeneratedField.cs
3 //
4 // Authors:
5 //      Lluis Sanchez Gual (lluis@novell.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 #if NET_2_0
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Web.UI;
35 using System.ComponentModel;
36 using System.Security.Permissions;
37
38 namespace System.Web.UI.WebControls {
39
40         [EditorBrowsableAttribute (EditorBrowsableState.Never)]
41         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43         public sealed class AutoGeneratedField : BoundField
44         {
45                 Type dataType = typeof(string);
46                 
47                 internal AutoGeneratedField ()
48                 {
49                 }
50                 
51                 public AutoGeneratedField (string dataField)
52                 {
53                         DataField = dataField;
54                 }
55                 
56                 internal AutoGeneratedField (AutoGeneratedFieldProperties fieldProperties)
57                 {
58                         DataField = fieldProperties.DataField;
59                         DataType = fieldProperties.Type;
60                         SortExpression = fieldProperties.Name;
61                         HeaderText = fieldProperties.Name;
62                         ReadOnly = fieldProperties.IsReadOnly;
63                 }
64                 
65                 public Type DataType {
66                         get { return dataType; }
67                         set { dataType = value; }
68                 }
69                 
70                 public override bool ConvertEmptyStringToNull {
71                         get { return true; }
72                         set { throw new NotSupportedException (); }
73                 }
74
75                 public override string DataFormatString {
76                         get { return string.Empty; }
77                         set { throw new NotSupportedException (); }
78                 }
79
80                 public override bool InsertVisible {
81                         get { return true; }
82                         set { throw new NotSupportedException (); }
83                 }
84                 
85                 [MonoTODO ("Support other data types")]
86                 public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
87                         DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
88                 {
89                         if (dataType == typeof(bool)) {
90                                 bool editable = IsEditable (rowState);
91                                 if (editable || includeReadOnly) {
92                                         CheckBox box = (CheckBox) cell.Controls [0];
93                                         dictionary [DataField] = box.Checked;
94                                 }
95                         } else
96                                 base.ExtractValuesFromCell (dictionary, cell, rowState, includeReadOnly);
97                 }
98                 
99                 
100                 [MonoTODO ("Support other data types")]
101                 protected override void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
102                 {
103                         if (dataType == typeof(bool)) {
104                                 bool editable = IsEditable (rowState);
105                                 CheckBox box = new CheckBox ();
106                                 box.Enabled = editable;
107                                 box.ToolTip = HeaderText;
108                                 cell.Controls.Add (box);
109                         } else
110                                 base.InitializeDataCell (cell, rowState);
111                 }
112                 
113                 [MonoTODO ("Support other data types")]
114                 protected override void OnDataBindField (object sender, EventArgs e)
115                 {
116                         DataControlFieldCell cell = (DataControlFieldCell) sender;
117                         if (dataType == typeof(bool)) {
118                                 CheckBox box = (CheckBox) cell.Controls [0];
119                                 object val = GetValue (cell.BindingContainer);
120                                 if (val != null && val != DBNull.Value)
121                                         box.Checked = (bool)val;
122                                 else
123                                         if (string.IsNullOrEmpty (DataField)) {
124                                                 box.Visible = false;
125                                                 return;
126                                         }
127                                 
128                                 if (!box.Visible)
129                                         box.Visible = true;
130                         }
131                         else
132                                 base.OnDataBindField (sender, e);
133                 }
134                 
135                 public override void ValidateSupportsCallback ()
136                 {
137                 }
138                 
139                 protected override DataControlField CreateField ()
140                 {
141                         return new AutoGeneratedField ();
142                 }
143
144                 protected override void CopyProperties (DataControlField newField)
145                 {
146                         base.CopyProperties (newField);
147                         AutoGeneratedField field = (AutoGeneratedField) newField;
148                         field.DataType = DataType;
149                 }
150
151                 protected override object GetDesignTimeValue()
152                 {
153                         return base.GetDesignTimeValue ();
154                 }
155         }
156 }
157 #endif