Don't tag some CAS types as obsolete.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / HyperLinkField.cs
1 //
2 // System.Web.UI.WebControls.HyperLinkField.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 using System.Reflection;
37
38 namespace System.Web.UI.WebControls
39 {
40         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         public class HyperLinkField : DataControlField
43         {
44                 PropertyDescriptor textProperty;
45                 PropertyDescriptor[] urlProperties;
46                 static string[] emptyFields;
47                 
48                 public override bool Initialize (bool sortingEnabled, Control control)
49                 {
50                         return base.Initialize (sortingEnabled, control);
51                 }
52                 
53                 [EditorAttribute ("System.Web.UI.Design.WebControls.DataFieldEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
54                 [TypeConverterAttribute (typeof(StringArrayConverter))]
55                 [WebCategoryAttribute ("Data")]
56                 [DefaultValueAttribute (null)]
57                 public virtual string[] DataNavigateUrlFields {
58                         get {
59                                 object ob = ViewState ["DataNavigateUrlFields"];
60                                 if (ob != null)
61                                         return (string[]) ob;
62                                 if (emptyFields == null)
63                                         emptyFields = new string[0];
64                                 return emptyFields;
65                         }
66                         set {
67                                 ViewState ["DataNavigateUrlFields"] = value;
68                                 OnFieldChanged ();
69                         }
70                 }
71
72                 [DefaultValueAttribute ("")]
73                 [WebCategoryAttribute ("Data")]
74                 public virtual string DataNavigateUrlFormatString {
75                         get {
76                                 object ob = ViewState ["DataNavigateUrlFormatString"];
77                                 if (ob != null)
78                                         return (string) ob;
79                                 return String.Empty;
80                         }
81                         set {
82                                 ViewState ["DataNavigateUrlFormatString"] = value;
83                                 OnFieldChanged ();
84                         }
85                 }
86
87                 [WebCategoryAttribute ("Data")]
88                 [DefaultValueAttribute ("")]
89                 [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
90                 public virtual string DataTextField {
91                         get {
92                                 object ob = ViewState ["DataTextField"];
93                                 if (ob != null)
94                                         return (string) ob;
95                                 return String.Empty;
96                         }
97                         set {
98                                 ViewState ["DataTextField"] = value;
99                                 OnFieldChanged ();
100                         }
101                 }
102
103                 [DefaultValueAttribute ("")]
104                 [WebCategoryAttribute ("Data")]
105                 public virtual string DataTextFormatString {
106                         get {
107                                 object ob = ViewState ["DataTextFormatString"];
108                                 if (ob != null)
109                                         return (string) ob;
110                                 return String.Empty;
111                         }
112                         set {
113                                 ViewState ["DataTextFormatString"] = value;
114                                 OnFieldChanged ();
115                         }
116                 }
117
118                 [DefaultValueAttribute ("")]
119                 [EditorAttribute ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
120                 [UrlPropertyAttribute]
121                 [WebCategoryAttribute ("Behavior")]
122                 public virtual string NavigateUrl {
123                         get {
124                                 object ob = ViewState ["NavigateUrl"];
125                                 if (ob != null)
126                                         return (string) ob;
127                                 return String.Empty;
128                         }
129                         set {
130                                 ViewState ["NavigateUrl"] = value;
131                                 OnFieldChanged ();
132                         }
133                 }
134
135                 [DefaultValueAttribute ("")]
136                 [WebCategoryAttribute ("Behavior")]
137                 [TypeConverterAttribute (typeof(TargetConverter))]
138                 public virtual string Target {
139                         get {
140                                 object ob = ViewState ["Target"];
141                                 if (ob != null)
142                                         return (string) ob;
143                                 return String.Empty;
144                         }
145                         set {
146                                 ViewState ["Target"] = value;
147                                 OnFieldChanged ();
148                         }
149                 }
150
151                 [LocalizableAttribute (true)]
152                 [DefaultValueAttribute ("")]
153                 [WebCategoryAttribute ("Appearance")]
154                 public virtual string Text {
155                         get {
156                                 object ob = ViewState ["Text"];
157                                 if (ob != null)
158                                         return (string) ob;
159                                 return String.Empty;
160                         }
161                         set {
162                                 ViewState ["Text"] = value;
163                                 OnFieldChanged ();
164                         }
165                 }
166                 
167                 public override void InitializeCell (DataControlFieldCell cell,DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
168                 {
169                         base.InitializeCell (cell, cellType, rowState, rowIndex);
170                         if (cellType == DataControlCellType.DataCell) {
171                                 HyperLink link = new HyperLink ();
172                                 bool bind = false;
173
174                                 if (Target.Length > 0)
175                                         link.Target = Target;
176
177                                 if (DataTextField.Length > 0)
178                                         bind = true;
179                                 else
180                                         link.Text = Text;
181
182                                 string [] fields = DataNavigateUrlFields;
183                                 if (fields.Length > 0)
184                                         bind = true;
185                                 else
186                                         link.NavigateUrl = NavigateUrl;
187
188                                 if (bind && cellType == DataControlCellType.DataCell && (rowState & DataControlRowState.Insert) == 0)
189                                         cell.DataBinding += new EventHandler (OnDataBindField);
190
191                                 link.ControlStyle.CopyFrom (ControlStyle);
192
193                                 cell.Controls.Add (link);
194                         }
195                 }
196                 
197                 protected virtual string FormatDataNavigateUrlValue (object[] dataUrlValues)
198                 {
199                         if (dataUrlValues == null || dataUrlValues.Length == 0)
200                                 return String.Empty;
201                         else if (DataNavigateUrlFormatString.Length > 0)
202                                 return string.Format (DataNavigateUrlFormatString, dataUrlValues);
203                         else
204                                 return dataUrlValues[0].ToString ();
205                 }
206                 
207                 protected virtual string FormatDataTextValue (object dataTextValue)
208                 {
209                         if (DataTextFormatString.Length > 0)
210                                 return string.Format (DataTextFormatString, dataTextValue);
211                         else if (dataTextValue == null)
212                                 return String.Empty;
213                         else
214                                 return dataTextValue.ToString ();
215                 }
216                 
217                 void OnDataBindField (object sender, EventArgs e)
218                 {
219                         DataControlFieldCell cell = (DataControlFieldCell) sender;
220                         HyperLink link = (HyperLink) cell.Controls [0];
221                         object controlContainer = cell.BindingContainer;
222                         object item = DataBinder.GetDataItem (controlContainer);
223                         
224                         if (DataTextField.Length > 0) {
225                                 if (textProperty == null) SetupProperties (controlContainer);
226                                 link.Text = FormatDataTextValue (textProperty.GetValue (item));
227                         }
228                         
229                         string[] urlFields = DataNavigateUrlFields;
230                         if (urlFields.Length > 0) {
231                                 if (urlProperties == null) SetupProperties (controlContainer);
232                                 object[] dataUrlValues = new object [urlFields.Length];
233                                 for (int n=0; n<dataUrlValues.Length; n++)
234                                         dataUrlValues [n] = urlProperties [n].GetValue (item);
235                                 link.NavigateUrl = FormatDataNavigateUrlValue (dataUrlValues);
236                         }
237                 }
238                 
239                 void SetupProperties (object controlContainer)
240                 {
241                         object item = DataBinder.GetDataItem (controlContainer);
242                         PropertyDescriptorCollection props = TypeDescriptor.GetProperties (item); 
243                         
244                         if (DataTextField.Length > 0) {
245                                 textProperty = props.Find (DataTextField, true);
246                                 if (textProperty == null)
247                                         throw new InvalidOperationException ("Property '" + DataTextField + "' not found in object of type " + item.GetType());
248                         }
249                         
250                         string[] urlFields = DataNavigateUrlFields;
251                         if (urlFields.Length > 0) {
252                                 urlProperties = new PropertyDescriptor [urlFields.Length];
253                                 for (int n=0; n<urlFields.Length; n++) {
254                                         PropertyDescriptor prop = props.Find (urlFields [n], true);
255                                         if (prop == null)
256                                                 throw new InvalidOperationException ("Property '" + urlFields [n] + "' not found in object of type " + item.GetType());
257                                         urlProperties [n] = prop;
258                                 }
259                         }
260                 }
261                 
262                 protected override DataControlField CreateField ()
263                 {
264                         return new HyperLinkField ();
265                 }
266                 
267                 protected override void CopyProperties (DataControlField newField)
268                 {
269                         base.CopyProperties (newField);
270                         HyperLinkField field = (HyperLinkField) newField;
271                         field.DataNavigateUrlFields = DataNavigateUrlFields;
272                         field.DataNavigateUrlFormatString = DataNavigateUrlFormatString;
273                         field.DataTextField = DataTextField;
274                         field.DataTextFormatString = DataTextFormatString;
275                         field.NavigateUrl = NavigateUrl;
276                         field.Target = Target;
277                         field.Text = Text;
278                 }
279                 
280                 public override void ValidateSupportsCallback ()
281                 {
282                 }
283         }
284 }
285