New tests.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / ImageField.cs
index f24df4803d9c4eceebb4ea0997256a54daa774f9..7513feafd010053c2f3561b6c8a27b672765ecab 100644 (file)
@@ -52,9 +52,10 @@ namespace System.Web.UI.WebControls {
                        return base.Initialize (sortingEnabled, control);
                }
 
-           [DefaultValueAttribute ("")]\r
-           [WebCategoryAttribute ("Appearance")]\r
-           [LocalizableAttribute (true)]\r
+               [DefaultValueAttribute ("")]
+               [LocalizableAttribute (true)]
+               [WebSysDescription ("")]
+               [WebCategoryAttribute ("Appearance")]
                public virtual string AlternateText {
                        get {
                                object ob = ViewState ["AlternateText"];
@@ -68,6 +69,7 @@ namespace System.Web.UI.WebControls {
                }
 
                [DefaultValueAttribute (true)]
+               [WebSysDescription ("")]
                [WebCategoryAttribute ("Behavior")]
                public virtual bool ConvertEmptyStringToNull {
                        get {
@@ -81,9 +83,10 @@ namespace System.Web.UI.WebControls {
                        }
                }
 
-               [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
-               [WebCategoryAttribute ("Data")]
+               [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
                [DefaultValueAttribute ("")]
+               [WebSysDescription ("")]
+               [WebCategoryAttribute ("Data")]
                public virtual string DataAlternateTextField {
                        get {
                                object ob = ViewState ["DataAlternateTextField"];
@@ -96,8 +99,9 @@ namespace System.Web.UI.WebControls {
                        }
                }
 
-               [WebCategoryAttribute ("Data")]
                [DefaultValueAttribute ("")]
+               [WebSysDescription ("")]
+               [WebCategoryAttribute ("Data")]
                public virtual string DataAlternateTextFormatString {
                        get {
                                object ob = ViewState ["DataAlternateTextFormatString"];
@@ -110,9 +114,10 @@ namespace System.Web.UI.WebControls {
                        }
                }
 
-               [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
-               [WebCategoryAttribute ("Data")]
+               [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
                [DefaultValueAttribute ("")]
+               [WebSysDescription ("")]
+               [WebCategoryAttribute ("Data")]
                public virtual string DataImageUrlField {
                        get {
                                object ob = ViewState ["DataImageUrlField"];
@@ -125,8 +130,9 @@ namespace System.Web.UI.WebControls {
                        }
                }
 
-               [WebCategoryAttribute ("Data")]
                [DefaultValueAttribute ("")]
+               [WebSysDescription ("")]
+               [WebCategoryAttribute ("Data")]
                public virtual string DataImageUrlFormatString {
                        get {
                                object ob = ViewState ["DataImageUrlFormatString"];
@@ -140,8 +146,9 @@ namespace System.Web.UI.WebControls {
                }
 
                [DefaultValueAttribute ("")]
+               [LocalizableAttribute (true)]
+               [WebSysDescription ("")]
                [WebCategoryAttribute ("Behavior")]
-           [LocalizableAttribute (true)]\r
                public virtual string NullDisplayText {
                        get {
                                object ob = ViewState ["NullDisplayText"];
@@ -154,10 +161,11 @@ namespace System.Web.UI.WebControls {
                        }
                }
 
-           [DefaultValueAttribute ("")]\r
-           [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]\r
-           [UrlPropertyAttribute]\r
-           [WebCategoryAttribute ("Behavior")]\r
+               [DefaultValueAttribute ("")]
+               [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
+               [UrlPropertyAttribute]
+               [WebSysDescription ("")]
+               [WebCategoryAttribute ("Behavior")]
                public virtual string NullImageUrl {
                        get {
                                object ob = ViewState ["NullImageUrl"];
@@ -170,9 +178,10 @@ namespace System.Web.UI.WebControls {
                        }
                }
 
-               [WebCategoryAttribute ("Behavior")]
                [DefaultValueAttribute (false)]
-               public bool ReadOnly {
+               [WebSysDescription ("")]
+               [WebCategoryAttribute ("Behavior")]
+               public virtual bool ReadOnly {
                        get {
                                object val = ViewState ["ReadOnly"];
                                return val != null ? (bool) val : false;
@@ -188,12 +197,15 @@ namespace System.Web.UI.WebControls {
                {
                        if ((ReadOnly && !includeReadOnly) || cell.Controls.Count == 0) return;
                        
-                       if ((rowState & DataControlRowState.Edit) != 0 && !ReadOnly) {
-                               TextBox box = cell.Controls [0] as TextBox;
-                               dictionary [DataImageUrlField] = box.Text;
-                       } else if (includeReadOnly) {
-                               Image img = cell.Controls [0] as Image;
-                               dictionary [DataImageUrlField] = img.ImageUrl;
+                       bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
+                       if (includeReadOnly || editable) {
+                               Control control = cell.Controls [0];
+                               //TODO: other controls?
+                               if (control is Image)
+                                       dictionary [DataImageUrlField] = ((Image)control).ImageUrl;
+                               else
+                               if (control is TextBox)
+                                       dictionary [DataImageUrlField] = ((TextBox) control).Text;
                        }
                }
                
@@ -201,26 +213,31 @@ namespace System.Web.UI.WebControls {
                        DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
                {
                        base.InitializeCell (cell, cellType, rowState, rowIndex);
-                       if (cellType == DataControlCellType.DataCell)
+                       if (cellType == DataControlCellType.DataCell) {
                                InitializeDataCell (cell, rowState);
-                       cell.DataBinding += new EventHandler (OnDataBindField);
+                               if ((rowState & DataControlRowState.Insert) == 0)
+                                       cell.DataBinding += new EventHandler (OnDataBindField);
+                       }
                }
                
-               public virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
+               protected virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
                {
-                       if ((rowState & DataControlRowState.Edit) != 0 && !ReadOnly) {
+                       bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
+                       if (editable && !ReadOnly) {
                                TextBox box = new TextBox ();
                                cell.Controls.Add (box);
-                       } else {
+                       }
+                       else if (DataImageUrlField.Length > 0) {
                                Image img = new Image ();
+                               img.ControlStyle.CopyFrom (ControlStyle);
                                cell.Controls.Add (img);
                        }
                }
                
                protected virtual string FormatImageUrlValue (object value)
                {
-                       if (value == null || (value.ToString().Length == 0 && ConvertEmptyStringToNull))
-                               return NullImageUrl;
+                       if (value == null)
+                               return null;
                        else if (DataImageUrlFormatString.Length > 0)
                                return string.Format (DataImageUrlFormatString, value);
                        else
@@ -234,7 +251,7 @@ namespace System.Web.UI.WebControls {
                                if (textProperty == null)
                                        textProperty = GetProperty (controlContainer, DataAlternateTextField);
                                        
-                               object value = GetValue (controlContainer, DataAlternateTextField, textProperty);
+                               object value = GetValue (controlContainer, DataAlternateTextField, ref textProperty);
                                
                                if (value == null || (value.ToString().Length == 0 && ConvertEmptyStringToNull))
                                        return NullDisplayText;
@@ -248,28 +265,35 @@ namespace System.Web.UI.WebControls {
                
                }
                
-               protected virtual object GetValue (Control controlContainer, string fieldName, PropertyDescriptor cachedDescriptor)
+               protected virtual object GetValue (Control controlContainer, string fieldName, ref PropertyDescriptor cachedDescriptor)
                {
                        if (DesignMode)
                                return GetDesignTimeValue ();
                        else {
+                               object dataItem = DataBinder.GetDataItem (controlContainer);
+                               if (dataItem == null)
+                                       throw new HttpException ("A data item was not found in the container. The container must either implement IDataItemContainer, or have a property named DataItem.");
                                if (fieldName == ThisExpression)
-                                       return controlContainer.ToString ();
+                                       return dataItem;
                                else {
-                                       IDataItemContainer dic = (IDataItemContainer) controlContainer;
-                                       if (cachedDescriptor != null) return cachedDescriptor.GetValue (dic.DataItem);
+                                       if (cachedDescriptor != null) return cachedDescriptor.GetValue (dataItem);
                                        PropertyDescriptor prop = GetProperty (controlContainer, fieldName);
-                                       return prop.GetValue (dic.DataItem);
+                                       return prop.GetValue (dataItem);
                                }
                        }
                }
                
                PropertyDescriptor GetProperty (Control controlContainer, string fieldName)
                {
+                       if (fieldName == ThisExpression)
+                               return null;
+                       
                        IDataItemContainer dic = (IDataItemContainer) controlContainer;
-                       PropertyDescriptor prop = TypeDescriptor.GetProperties (dic.DataItem) [fieldName];
+                       PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (dic.DataItem);
+                       PropertyDescriptor prop = properties != null ? properties [fieldName] : null;
                        if (prop == null)
-                               new InvalidOperationException ("Property '" + fieldName + "' not found in object of type " + dic.DataItem.GetType());
+                               throw new InvalidOperationException ("Property '" + fieldName + "' not found in object of type " + dic.DataItem.GetType());
+                       
                        return prop;
                }
                
@@ -280,26 +304,67 @@ namespace System.Web.UI.WebControls {
                
                protected virtual void OnDataBindField (object sender, EventArgs e)
                {
-                       DataControlFieldCell cell = (DataControlFieldCell) sender;
-                       
+                       Control control = (Control) sender;
+                       ControlCollection controls = control != null ? control.Controls : null;
+                       Control namingContainer = control.NamingContainer;
+                       Control c;
+                       if (sender is DataControlFieldCell) {
+                               if (controls.Count == 0)
+                                       return;
+                               c = controls [0];
+                       } else if (sender is Image || sender is TextBox)
+                               c = control;
+                       else
+                               return;
+
                        if (imageProperty == null)
-                               imageProperty = GetProperty (cell.BindingContainer, DataImageUrlField);
+                               imageProperty = GetProperty (namingContainer, DataImageUrlField);
                        
-                       Control c = cell.Controls [0];
                        if (c is TextBox) {
-                               object val = GetValue (cell.BindingContainer, DataImageUrlField, imageProperty);
-                               ((TextBox)c).Text = val != null ? val.ToString() : "";
+                               object val = GetValue (namingContainer, DataImageUrlField, ref imageProperty);
+                               ((TextBox)c).Text = val != null ? val.ToString() : String.Empty;
                        }
                        else if (c is Image) {
                                Image img = (Image)c;
-                               img.ImageUrl = FormatImageUrlValue (GetValue (cell.BindingContainer, DataImageUrlField, imageProperty));
-                               img.AlternateText = GetFormattedAlternateText (cell.BindingContainer);
+                               string value =  FormatImageUrlValue (GetValue (namingContainer, DataImageUrlField, ref imageProperty));
+                               if (value == null || (ConvertEmptyStringToNull && value.Length == 0)) {
+                                       if (NullImageUrl == null || NullImageUrl.Length == 0) {
+                                               c.Visible = false;
+                                               Label label = new Label ();
+                                               label.Text = NullDisplayText;
+                                               controls.Add (label);
+                                       }
+                                       else
+                                               value = NullImageUrl;
+                               }
+                               img.ImageUrl = value;
+                               img.AlternateText = GetFormattedAlternateText (namingContainer);
                        }
                }
                
                public override void ValidateSupportsCallback ()
                {
                }
+               
+               protected override DataControlField CreateField ()
+               {
+                       return new ImageField ();
+               }
+               
+               protected override void CopyProperties (DataControlField newField)
+               {
+                       base.CopyProperties (newField);
+                       ImageField field = (ImageField) newField;
+                       field.AlternateText = AlternateText;
+                       field.ConvertEmptyStringToNull = ConvertEmptyStringToNull;
+                       field.DataAlternateTextField = DataAlternateTextField;
+                       field.DataAlternateTextFormatString = DataAlternateTextFormatString;
+                       field.DataImageUrlField = DataImageUrlField;
+                       field.DataImageUrlFormatString = DataImageUrlFormatString;
+                       field.NullDisplayText = NullDisplayText;
+                       field.NullImageUrl = NullImageUrl;
+                       field.ReadOnly = ReadOnly;
+               }
        }
 }
 #endif