New test.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / ImageField.cs
1 //
2 // System.Web.UI.WebControls.ImageField.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 using System.Reflection;
38
39 namespace System.Web.UI.WebControls {
40
41         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43         public class ImageField : DataControlField
44         {
45                 public static readonly string ThisExpression = "!";
46                 
47                 PropertyDescriptor imageProperty;
48                 PropertyDescriptor textProperty;
49                 
50                 public override bool Initialize (bool sortingEnabled, Control control)
51                 {
52                         return base.Initialize (sortingEnabled, control);
53                 }
54
55                 [DefaultValueAttribute ("")]
56                 [LocalizableAttribute (true)]
57                 [WebSysDescription ("")]
58                 [WebCategoryAttribute ("Appearance")]
59                 public virtual string AlternateText {
60                         get {
61                                 object ob = ViewState ["AlternateText"];
62                                 if (ob != null) return (string) ob;
63                                 return string.Empty;
64                         }
65                         set {
66                                 ViewState ["AlternateText"] = value;
67                                 OnFieldChanged ();
68                         }
69                 }
70
71                 [DefaultValueAttribute (true)]
72                 [WebSysDescription ("")]
73                 [WebCategoryAttribute ("Behavior")]
74                 public virtual bool ConvertEmptyStringToNull {
75                         get {
76                                 object ob = ViewState ["ConvertEmptyStringToNull"];
77                                 if (ob != null) return (bool) ob;
78                                 return true;
79                         }
80                         set {
81                                 ViewState ["ConvertEmptyStringToNull"] = value;
82                                 OnFieldChanged ();
83                         }
84                 }
85
86                 [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
87                 [DefaultValueAttribute ("")]
88                 [WebSysDescription ("")]
89                 [WebCategoryAttribute ("Data")]
90                 public virtual string DataAlternateTextField {
91                         get {
92                                 object ob = ViewState ["DataAlternateTextField"];
93                                 if (ob != null) return (string) ob;
94                                 return "";
95                         }
96                         set {
97                                 ViewState ["DataAlternateTextField"] = value;
98                                 OnFieldChanged ();
99                         }
100                 }
101
102                 [DefaultValueAttribute ("")]
103                 [WebSysDescription ("")]
104                 [WebCategoryAttribute ("Data")]
105                 public virtual string DataAlternateTextFormatString {
106                         get {
107                                 object ob = ViewState ["DataAlternateTextFormatString"];
108                                 if (ob != null) return (string) ob;
109                                 return "";
110                         }
111                         set {
112                                 ViewState ["DataAlternateTextFormatString"] = value;
113                                 OnFieldChanged ();
114                         }
115                 }
116
117                 [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
118                 [DefaultValueAttribute ("")]
119                 [WebSysDescription ("")]
120                 [WebCategoryAttribute ("Data")]
121                 public virtual string DataImageUrlField {
122                         get {
123                                 object ob = ViewState ["DataImageUrlField"];
124                                 if (ob != null) return (string) ob;
125                                 return "";
126                         }
127                         set {
128                                 ViewState ["DataImageUrlField"] = value;
129                                 OnFieldChanged ();
130                         }
131                 }
132
133                 [DefaultValueAttribute ("")]
134                 [WebSysDescription ("")]
135                 [WebCategoryAttribute ("Data")]
136                 public virtual string DataImageUrlFormatString {
137                         get {
138                                 object ob = ViewState ["DataImageUrlFormatString"];
139                                 if (ob != null) return (string) ob;
140                                 return "";
141                         }
142                         set {
143                                 ViewState ["DataImageUrlFormatString"] = value;
144                                 OnFieldChanged ();
145                         }
146                 }
147
148                 [DefaultValueAttribute ("")]
149                 [LocalizableAttribute (true)]
150                 [WebSysDescription ("")]
151                 [WebCategoryAttribute ("Behavior")]
152                 public virtual string NullDisplayText {
153                         get {
154                                 object ob = ViewState ["NullDisplayText"];
155                                 if (ob != null) return (string) ob;
156                                 return "";
157                         }
158                         set {
159                                 ViewState ["NullDisplayText"] = value;
160                                 OnFieldChanged ();
161                         }
162                 }
163
164                 [DefaultValueAttribute ("")]
165                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
166                 [UrlPropertyAttribute]
167                 [WebSysDescription ("")]
168                 [WebCategoryAttribute ("Behavior")]
169                 public virtual string NullImageUrl {
170                         get {
171                                 object ob = ViewState ["NullImageUrl"];
172                                 if (ob != null) return (string) ob;
173                                 return "";
174                         }
175                         set {
176                                 ViewState ["NullImageUrl"] = value;
177                                 OnFieldChanged ();
178                         }
179                 }
180
181                 [DefaultValueAttribute (false)]
182                 [WebSysDescription ("")]
183                 [WebCategoryAttribute ("Behavior")]
184                 public virtual bool ReadOnly {
185                         get {
186                                 object val = ViewState ["ReadOnly"];
187                                 return val != null ? (bool) val : false;
188                         }
189                         set { 
190                                 ViewState ["ReadOnly"] = value;
191                                 OnFieldChanged ();
192                         }
193                 }
194
195                 public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
196                         DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
197                 {
198                         if ((ReadOnly && !includeReadOnly) || cell.Controls.Count == 0) return;
199                         
200                         bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
201                         if (includeReadOnly || editable) {
202                                 Control control = cell.Controls [0];
203                                 //TODO: other controls?
204                                 if (control is Image)
205                                         dictionary [DataImageUrlField] = ((Image)control).ImageUrl;
206                                 else
207                                 if (control is TextBox)
208                                         dictionary [DataImageUrlField] = ((TextBox) control).Text;
209                         }
210                 }
211                 
212                 public override void InitializeCell (DataControlFieldCell cell,
213                         DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
214                 {
215                         base.InitializeCell (cell, cellType, rowState, rowIndex);
216                         if (cellType == DataControlCellType.DataCell) {
217                                 InitializeDataCell (cell, rowState);
218                                 if ((rowState & DataControlRowState.Insert) == 0)
219                                         cell.DataBinding += new EventHandler (OnDataBindField);
220                         }
221                 }
222                 
223                 protected virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
224                 {
225                         bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
226                         if (editable && !ReadOnly) {
227                                 TextBox box = new TextBox ();
228                                 cell.Controls.Add (box);
229                         }
230                         else if (DataImageUrlField.Length > 0) {
231                                 Image img = new Image ();
232                                 img.ControlStyle.CopyFrom (ControlStyle);
233                                 cell.Controls.Add (img);
234                         }
235                 }
236                 
237                 protected virtual string FormatImageUrlValue (object value)
238                 {
239                         if (value == null)
240                                 return null;
241                         else if (DataImageUrlFormatString.Length > 0)
242                                 return string.Format (DataImageUrlFormatString, value);
243                         else
244                                 return value.ToString ();
245                 }
246                 
247                 protected virtual string GetFormattedAlternateText (Control controlContainer)
248                 {
249                         if (DataAlternateTextField.Length > 0)
250                         {
251                                 if (textProperty == null)
252                                         textProperty = GetProperty (controlContainer, DataAlternateTextField);
253                                         
254                                 object value = GetValue (controlContainer, DataAlternateTextField, ref textProperty);
255                                 
256                                 if (value == null || (value.ToString().Length == 0 && ConvertEmptyStringToNull))
257                                         return NullDisplayText;
258                                 else if (DataAlternateTextFormatString.Length > 0)
259                                         return string.Format (DataAlternateTextFormatString, value);
260                                 else
261                                         return value.ToString ();
262                         }
263                         else
264                                 return AlternateText;
265                 
266                 }
267                 
268                 protected virtual object GetValue (Control controlContainer, string fieldName, ref PropertyDescriptor cachedDescriptor)
269                 {
270                         if (DesignMode)
271                                 return GetDesignTimeValue ();
272                         else {
273                                 object dataItem = DataBinder.GetDataItem (controlContainer);
274                                 if (dataItem == null)
275                                         throw new HttpException ("A data item was not found in the container. The container must either implement IDataItemContainer, or have a property named DataItem.");
276                                 if (fieldName == ThisExpression)
277                                         return dataItem.ToString ();
278                                 else {
279                                         if (cachedDescriptor != null) return cachedDescriptor.GetValue (dataItem);
280                                         PropertyDescriptor prop = GetProperty (controlContainer, fieldName);
281                                         return prop.GetValue (dataItem);
282                                 }
283                         }
284                 }
285                 
286                 PropertyDescriptor GetProperty (Control controlContainer, string fieldName)
287                 {
288                         IDataItemContainer dic = (IDataItemContainer) controlContainer;
289                         PropertyDescriptor prop = TypeDescriptor.GetProperties (dic.DataItem) [fieldName];
290                         if (prop == null)
291                                 new InvalidOperationException ("Property '" + fieldName + "' not found in object of type " + dic.DataItem.GetType());
292                         return prop;
293                 }
294                 
295                 protected virtual string GetDesignTimeValue ()
296                 {
297                         return "Databound";
298                 }
299                 
300                 protected virtual void OnDataBindField (object sender, EventArgs e)
301                 {
302                         DataControlFieldCell cell = (DataControlFieldCell) sender;\r
303 \r
304                         if (cell.Controls.Count == 0)\r
305                                 return;
306                         
307                         if (imageProperty == null)
308                                 imageProperty = GetProperty (cell.BindingContainer, DataImageUrlField);
309                         
310                         Control c = cell.Controls [0];
311                         if (c is TextBox) {
312                                 object val = GetValue (cell.BindingContainer, DataImageUrlField, ref imageProperty);
313                                 ((TextBox)c).Text = val != null ? val.ToString() : "";
314                         }
315                         else if (c is Image) {
316                                 Image img = (Image)c;
317                                 string value =  FormatImageUrlValue (GetValue (cell.BindingContainer, DataImageUrlField, ref imageProperty));
318                                 if (value == null || (ConvertEmptyStringToNull && value.ToString ().Length == 0)) {
319                                         if (NullImageUrl == null || NullImageUrl.Length == 0) {
320                                                 c.Visible = false;
321                                                 Label label = new Label ();
322                                                 label.Text = NullDisplayText;
323                                                 cell.Controls.Add (label);
324                                         }
325                                         else
326                                                 value = NullImageUrl;
327                                 }
328                                 img.ImageUrl = value;
329                                 img.AlternateText = GetFormattedAlternateText (cell.BindingContainer);
330                         }
331                 }
332                 
333                 public override void ValidateSupportsCallback ()
334                 {
335                 }
336                 
337                 protected override DataControlField CreateField ()
338                 {
339                         return new ImageField ();
340                 }
341                 
342                 protected override void CopyProperties (DataControlField newField)
343                 {
344                         base.CopyProperties (newField);
345                         ImageField field = (ImageField) newField;
346                         field.AlternateText = AlternateText;
347                         field.ConvertEmptyStringToNull = ConvertEmptyStringToNull;
348                         field.DataAlternateTextField = DataAlternateTextField;
349                         field.DataAlternateTextFormatString = DataAlternateTextFormatString;
350                         field.DataImageUrlField = DataImageUrlField;
351                         field.DataImageUrlFormatString = DataImageUrlFormatString;
352                         field.NullDisplayText = NullDisplayText;
353                         field.NullImageUrl = NullImageUrl;
354                         field.ReadOnly = ReadOnly;
355                 }
356         }
357 }
358 #endif