Merge pull request #757 from mlintner/master
[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-2010 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 using System.Collections;
32 using System.Collections.Specialized;
33 using System.Web.UI;
34 using System.ComponentModel;
35 using System.Security.Permissions;
36
37 namespace System.Web.UI.WebControls
38 {
39         [EditorBrowsableAttribute (EditorBrowsableState.Never)]
40         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         public sealed class AutoGeneratedField : BoundField
43         {
44                 Type dataType = typeof(string);
45                 
46                 internal AutoGeneratedField ()
47                 {
48                 }
49                 
50                 public AutoGeneratedField (string dataField)
51                 {
52                         DataField = dataField;
53                 }
54                 
55                 internal AutoGeneratedField (AutoGeneratedFieldProperties fieldProperties)
56                 {
57                         DataField = fieldProperties.DataField;
58                         DataType = fieldProperties.Type;
59                         SortExpression = fieldProperties.Name;
60                         HeaderText = fieldProperties.Name;
61                         ReadOnly = fieldProperties.IsReadOnly;
62                 }
63                 
64                 public Type DataType {
65                         get { return dataType; }
66                         set { dataType = value; }
67                 }
68                 
69                 public override bool ConvertEmptyStringToNull {
70                         get { return true; }
71                         set { throw new NotSupportedException (); }
72                 }
73
74                 public override string DataFormatString {
75                         get { return string.Empty; }
76                         set { throw new NotSupportedException (); }
77                 }
78
79                 public override bool InsertVisible {
80                         get { return true; }
81                         set { throw new NotSupportedException (); }
82                 }
83                 
84                 [MonoTODO ("Support other data types")]
85                 public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
86                         DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
87                 {
88                         if (dataType == typeof(bool)) {
89                                 bool editable = IsEditable (rowState);
90                                 if (editable || includeReadOnly) {
91                                         CheckBox box = (CheckBox) cell.Controls [0];
92                                         dictionary [DataField] = box.Checked;
93                                 }
94                         } else
95                                 base.ExtractValuesFromCell (dictionary, cell, rowState, includeReadOnly);
96                 }
97                 
98                 
99                 [MonoTODO ("Support other data types")]
100                 protected override void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
101                 {
102                         if (dataType == typeof(bool)) {
103                                 bool editable = IsEditable (rowState);
104                                 CheckBox box = new CheckBox ();
105                                 box.Enabled = editable;
106                                 box.ToolTip = HeaderText;
107                                 cell.Controls.Add (box);
108                         } else
109                                 base.InitializeDataCell (cell, rowState);
110                 }
111                 
112                 [MonoTODO ("Support other data types")]
113                 protected override void OnDataBindField (object sender, EventArgs e)
114                 {
115                         DataControlFieldCell cell = (DataControlFieldCell) sender;
116                         if (dataType == typeof(bool)) {
117                                 CheckBox box = (CheckBox) cell.Controls [0];
118                                 object val = GetValue (cell.BindingContainer);
119                                 if (val != null && val != DBNull.Value)
120                                         box.Checked = (bool)val;
121                                 else
122                                         if (string.IsNullOrEmpty (DataField)) {
123                                                 box.Visible = false;
124                                                 return;
125                                         }
126                                 
127                                 if (!box.Visible)
128                                         box.Visible = true;
129                         }
130                         else
131                                 base.OnDataBindField (sender, e);
132                 }
133                 
134                 public override void ValidateSupportsCallback ()
135                 {
136                 }
137                 
138                 protected override DataControlField CreateField ()
139                 {
140                         return new AutoGeneratedField ();
141                 }
142
143                 protected override void CopyProperties (DataControlField newField)
144                 {
145                         base.CopyProperties (newField);
146                         AutoGeneratedField field = (AutoGeneratedField) newField;
147                         field.DataType = DataType;
148                 }
149
150                 protected override object GetDesignTimeValue()
151                 {
152                         return base.GetDesignTimeValue ();
153                 }
154         }
155 }
156