From: Eric C. <ecr...@us...> - 2006-10-01 13:27:43
|
User: ecrutchfield Date: 06/10/01 06:27:42 Modified: andromda-aspdotnet/src/main/resources/resources FormPopulator.cs Log: override PopulateForm method to support more scenarios Revision Changes Path 1.3 +63 -53 cartridges/andromda-aspdotnet/src/main/resources/resources/FormPopulator.cs Index: FormPopulator.cs =================================================================== RCS file: /cvsroot/andromdaplugins/cartridges/andromda-aspdotnet/src/main/resources/resources/FormPopulator.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -u -w -r1.2 -r1.3 --- FormPopulator.cs 25 Sep 2006 19:02:48 -0000 1.2 +++ FormPopulator.cs 1 Oct 2006 13:27:42 -0000 1.3 @@ -15,94 +15,104 @@ } /// <summary> - /// Populate a particular field of a form object using the value of the supplied <see cref="System.Web.UI.Control"/>. + /// Populate a particular field of a form object using the supplied value. /// </summary> - /// <param name="fromControl">The control containing the value.</param> + /// <param name="value">The value to populate the form with.</param> /// <param name="toForm">The form object where the value will be written.</param> /// <param name="propertyName">The property of the form where the value will be written.</param> - public static void PopulateForm(System.Web.UI.Control fromControl, object toForm, string propertyName) + public static void PopulateForm(object value, object toForm, string propertyName) { - if (fromControl == null) - throw new ArgumentNullException("fromControl"); - - if (toForm == null) - throw new ArgumentNullException("toForm"); - - if (propertyName == null) - throw new ArgumentNullException("propertyName"); - - object val = null; - switch(fromControl.GetType().FullName) - { - case "System.Web.UI.WebControls.TextBox": - val = ((System.Web.UI.WebControls.TextBox)fromControl).Text; - break; - case "System.Web.UI.WebControls.TextArea": - val = ((System.Web.UI.WebControls.TextBox)fromControl).Text; - break; - case "System.Web.UI.WebControls.CheckBox": - val = ((System.Web.UI.WebControls.CheckBox)fromControl).Checked; - break; - case "System.Web.UI.WebControls.DropDownList": - val = ((System.Web.UI.WebControls.DropDownList)fromControl).SelectedValue; - break; - case "System.Web.UI.WebControls.ListBox": - System.Web.UI.WebControls.ListBox ctl = (System.Web.UI.WebControls.ListBox)fromControl; - if (ctl.SelectionMode == System.Web.UI.WebControls.ListSelectionMode.Single) - val = ((System.Web.UI.WebControls.ListBox)fromControl).SelectedValue; - else - val = org.andromda.cartridges.aspdotnet.StringUtils.GetSelectedItems(((System.Web.UI.WebControls.ListBox)fromControl).Items); - break; - default: - throw new ArgumentException(String.Format("The {0} control is not yet supported.", fromControl.GetType().FullName)); - } - - if (val == null) - throw new ArgumentNullException(String.Format("The value of the {0} control can't be null.", fromControl.GetType().FullName)); + if (value == null) + throw new ArgumentNullException("The value can't be null."); PropertyInfo propInfo = toForm.GetType().GetProperty(propertyName); object assignedValue = null; switch (propInfo.PropertyType.Name) { case "String": - assignedValue = val; + assignedValue = value; break; case "Int16": - assignedValue = Int16.Parse( (string) val, NumberStyles.Integer ); + assignedValue = Int16.Parse((string)value, NumberStyles.Integer); break; case "Int32": - assignedValue = Int32.Parse( (string) val, NumberStyles.Integer ); + assignedValue = Int32.Parse((string)value, NumberStyles.Integer); break; case "Int64": - assignedValue = Int32.Parse ( (string) val, NumberStyles.Integer); + assignedValue = Int32.Parse((string)value, NumberStyles.Integer); break; case "Byte": - assignedValue = Convert.ToByte(val); + assignedValue = Convert.ToByte(value); break; case "Decimal": - assignedValue = Decimal.Parse( (string) val,NumberStyles.Any); + assignedValue = Decimal.Parse((string)value, NumberStyles.Any); break; case "Double": - assignedValue = Double.Parse( (string) val,NumberStyles.Any); + assignedValue = Double.Parse((string)value, NumberStyles.Any); break; case "Boolean": - assignedValue = val; + assignedValue = value; break; case "DateTime": - assignedValue = Convert.ToDateTime(val); + assignedValue = Convert.ToDateTime(value); break; case "IList": - if (val is System.Collections.IList) - assignedValue = val; + if (value is System.Collections.IList) + assignedValue = value; else - throw new ArgumentException(String.Format("The field {0} can't be converted to a type compatible with the form property {1}", fromControl.ID, propertyName)); + throw new ArgumentException(String.Format("The value can't be converted to a type compatible with the form property {0}", propertyName)); break; default: throw new ArgumentException("Field type not handled."); - break; } propInfo.SetValue(toForm, assignedValue, null); } + + /// <summary> + /// Populate a particular field of a form object using the value of the supplied <see cref="System.Web.UI.Control"/>. + /// </summary> + /// <param name="fromControl">The control containing the value.</param> + /// <param name="toForm">The form object where the value will be written.</param> + /// <param name="propertyName">The property of the form where the value will be written.</param> + public static void PopulateForm(System.Web.UI.Control fromControl, object toForm, string propertyName) + { + if (fromControl == null) + throw new ArgumentNullException("fromControl"); + + if (toForm == null) + throw new ArgumentNullException("toForm"); + + if (propertyName == null) + throw new ArgumentNullException("propertyName"); + + object value = null; + switch(fromControl.GetType().FullName) + { + case "System.Web.UI.WebControls.TextBox": + value = ((System.Web.UI.WebControls.TextBox)fromControl).Text; + break; + case "System.Web.UI.WebControls.TextArea": + value = ((System.Web.UI.WebControls.TextBox)fromControl).Text; + break; + case "System.Web.UI.WebControls.CheckBox": + value = ((System.Web.UI.WebControls.CheckBox)fromControl).Checked; + break; + case "System.Web.UI.WebControls.DropDownList": + value = ((System.Web.UI.WebControls.DropDownList)fromControl).SelectedValue; + break; + case "System.Web.UI.WebControls.ListBox": + System.Web.UI.WebControls.ListBox ctl = (System.Web.UI.WebControls.ListBox)fromControl; + if (ctl.SelectionMode == System.Web.UI.WebControls.ListSelectionMode.Single) + value = ((System.Web.UI.WebControls.ListBox)fromControl).SelectedValue; + else + value = org.andromda.cartridges.aspdotnet.StringUtils.GetSelectedItems(((System.Web.UI.WebControls.ListBox)fromControl).Items); + break; + default: + throw new ArgumentException(String.Format("The {0} control is not yet supported.", fromControl.GetType().FullName)); + } + + PopulateForm(value, toForm, propertyName); + } } } |