Server/FormEditObject.cs
author Stephane Lenclud
Fri, 19 Aug 2016 17:12:54 +0200
changeset 243 cc2251d065db
parent 239 dd7770b97916
child 244 2e4d2558bb21
permissions -rw-r--r--
Optical drive eject action now functional.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Diagnostics;
     6 using System.Drawing;
     7 using System.Linq;
     8 using System.Text;
     9 using System.Threading.Tasks;
    10 using System.Windows.Forms;
    11 using SharpLib.Display;
    12 using SharpLib.Ear;
    13 using System.Reflection;
    14 using Microsoft.VisualBasic.CompilerServices;
    15 using SharpLib.Utils;
    16 using CodeProject.Dialog;
    17 using System.IO;
    18 
    19 namespace SharpDisplayManager
    20 {
    21     /// <summary>
    22     /// Object edit dialog form.
    23     /// </summary>
    24     public partial class FormEditObject<T> : Form where T: SharpLib.Ear.Object
    25     {
    26         public T Object = null;
    27 
    28         public FormEditObject()
    29         {
    30             InitializeComponent();
    31         }
    32 
    33         /// <summary>
    34         /// 
    35         /// </summary>
    36         /// <param name="sender"></param>
    37         /// <param name="e"></param>
    38         private void FormEditAction_Load(object sender, EventArgs e)
    39         {
    40             // Populate registered object types
    41             IEnumerable < Type > types = Reflection.GetConcreteClassesDerivedFrom<T>();
    42             foreach (Type type in types)
    43             {
    44                 ItemObjectType item = new ItemObjectType(type);
    45                 comboBoxActionType.Items.Add(item);
    46             }
    47 
    48             if (Object == null)
    49             {
    50                 // Creating new issue, select our first item
    51                 comboBoxActionType.SelectedIndex = 0;
    52             }
    53             else
    54             {
    55                 // Editing existing object
    56                 // Look up our item in our combobox
    57                 foreach (ItemObjectType item in comboBoxActionType.Items)
    58                 {
    59                     if (item.Type == Object.GetType())
    60                     {
    61                         comboBoxActionType.SelectedItem = item;
    62                     }
    63                 }
    64             }
    65         }
    66 
    67         private void buttonOk_Click(object sender, EventArgs e)
    68         {
    69             FetchPropertiesValue(Object);
    70             if (!Object.IsValid())
    71             {
    72                 // Tell for closing event to abort
    73                 DialogResult = DialogResult.None;
    74             }
    75         }
    76 
    77 
    78         private void FormEditObject_FormClosing(object sender, FormClosingEventArgs e)
    79         {
    80             e.Cancel = DialogResult == DialogResult.None;
    81         }
    82 
    83         private void comboBoxActionType_SelectedIndexChanged(object sender, EventArgs e)
    84         {
    85             //Instantiate an action corresponding to our type
    86             Type actionType = ((ItemObjectType) comboBoxActionType.SelectedItem).Type;
    87             //Create another type of action only if needed
    88             if (Object == null || Object.GetType() != actionType)
    89             {
    90                 Object = (T)Activator.CreateInstance(actionType);
    91             }
    92 
    93             //Disable ok button if our object is not valid
    94             buttonOk.Enabled = Object.IsValid();
    95 
    96             //Create input fields
    97             UpdateTableLayoutPanel(Object);
    98         }
    99 
   100 
   101         /// <summary>
   102         /// Get properties values from our generated input fields
   103         /// </summary>
   104         private void FetchPropertiesValue(T aObject)
   105         {
   106             int ctrlIndex = 0;
   107             //For each of our properties
   108             foreach (PropertyInfo pi in aObject.GetType().GetProperties())
   109             {
   110                 //Get our property attribute
   111                 AttributeObjectProperty[] attributes = ((AttributeObjectProperty[]) pi.GetCustomAttributes(typeof(AttributeObjectProperty), true));
   112                 if (attributes.Length != 1)
   113                 {
   114                     //No attribute, skip this property then.
   115                     continue;
   116                 }
   117                 AttributeObjectProperty attribute = attributes[0];
   118 
   119                 //Check that we support this type of property
   120                 if (!IsPropertyTypeSupported(pi))
   121                 {
   122                     continue;
   123                 }
   124 
   125                 //Now fetch our property value
   126                 GetPropertyValueFromControl(iTableLayoutPanel.Controls[ctrlIndex+1], pi, aObject); //+1 otherwise we get the label
   127 
   128                 ctrlIndex+=2; //Jump over the label too
   129             }
   130         }
   131 
   132         /// <summary>
   133         /// Extend this function to support reading new types of properties.
   134         /// </summary>
   135         /// <param name="aObject"></param>
   136         private void GetPropertyValueFromControl(Control aControl, PropertyInfo aInfo, T aObject)
   137         {
   138             if (aInfo.PropertyType == typeof(int))
   139             {
   140                 NumericUpDown ctrl=(NumericUpDown)aControl;
   141                 aInfo.SetValue(aObject,(int)ctrl.Value);
   142             }
   143             else if (aInfo.PropertyType.IsEnum)
   144             {
   145                 // Instantiate our enum
   146                 object enumValue= Activator.CreateInstance(aInfo.PropertyType);
   147                 // Parse our enum from combo box
   148                 enumValue = Enum.Parse(aInfo.PropertyType,((ComboBox)aControl).SelectedItem.ToString());
   149                 //enumValue = ((ComboBox)aControl).SelectedValue;
   150                 // Set enum value
   151                 aInfo.SetValue(aObject, enumValue);
   152             }
   153             else if (aInfo.PropertyType == typeof(bool))
   154             {
   155                 CheckBox ctrl = (CheckBox)aControl;
   156                 aInfo.SetValue(aObject, ctrl.Checked);
   157             }
   158             else if (aInfo.PropertyType == typeof(string))
   159             {
   160                 TextBox ctrl = (TextBox)aControl;
   161                 aInfo.SetValue(aObject, ctrl.Text);
   162             }
   163             else if (aInfo.PropertyType == typeof(PropertyFile))
   164             {
   165                 Button ctrl = (Button)aControl;
   166                 PropertyFile value = new PropertyFile {FullPath=ctrl.Text};
   167                 aInfo.SetValue(aObject, value);
   168             }
   169             else if (aInfo.PropertyType == typeof(PropertyComboBox))
   170             {
   171                 ComboBox ctrl = (ComboBox)aControl;
   172                 string currentItem = ctrl.SelectedItem.ToString();
   173                 PropertyComboBox pcb = (PropertyComboBox)aInfo.GetValue(aObject);
   174                 pcb.CurrentItem = currentItem;
   175             }
   176 
   177             //TODO: add support for other types here
   178         }
   179 
   180 
   181         /// <summary>
   182         /// Create a control for the given property.
   183         /// </summary>
   184         /// <param name="aInfo"></param>
   185         /// <param name="aAttribute"></param>
   186         /// <param name="aObject"></param>
   187         /// <returns></returns>
   188         private Control CreateControlForProperty(PropertyInfo aInfo, AttributeObjectProperty aAttribute, T aObject)
   189         {
   190             if (aInfo.PropertyType == typeof(int))
   191             {
   192                 //Integer properties are using numeric editor
   193                 NumericUpDown ctrl = new NumericUpDown();
   194                 ctrl.AutoSize = true;
   195                 ctrl.Minimum = Int32.Parse(aAttribute.Minimum);
   196                 ctrl.Maximum = Int32.Parse(aAttribute.Maximum);
   197                 ctrl.Increment = Int32.Parse(aAttribute.Increment);
   198                 ctrl.Value = (int)aInfo.GetValue(aObject);
   199                 return ctrl;
   200             }
   201             else if (aInfo.PropertyType.IsEnum)
   202             {
   203                 //Enum properties are using combo box
   204                 ComboBox ctrl = new ComboBox();
   205                 ctrl.AutoSize = true;
   206                 ctrl.Sorted = true;
   207                 ctrl.DropDownStyle = ComboBoxStyle.DropDownList;
   208                 //Data source is fine but it gives us duplicate entries for duplicated enum values
   209                 //ctrl.DataSource = Enum.GetValues(aInfo.PropertyType);
   210 
   211                 //Therefore we need to explicitly create our items
   212                 Size cbSize = new Size(0, 0);
   213                 foreach (string name in aInfo.PropertyType.GetEnumNames())
   214                 {
   215                     ctrl.Items.Add(name.ToString());
   216                     Graphics g = this.CreateGraphics();
   217                     //Since combobox autosize would not work we need to get measure text ourselves
   218                     SizeF size = g.MeasureString(name.ToString(), ctrl.Font);
   219                     cbSize.Width = Math.Max(cbSize.Width, (int)size.Width);
   220                     cbSize.Height = Math.Max(cbSize.Height, (int)size.Height);
   221                 }
   222 
   223                 //Make sure our combobox is large enough
   224                 ctrl.MinimumSize = cbSize;
   225 
   226                 // Instantiate our enum
   227                 object enumValue = Activator.CreateInstance(aInfo.PropertyType);
   228                 enumValue = aInfo.GetValue(aObject);
   229                 //Set the current item
   230                 ctrl.SelectedItem = enumValue.ToString();
   231 
   232                 return ctrl;
   233             }
   234             else if (aInfo.PropertyType == typeof(bool))
   235             {
   236                 CheckBox ctrl = new CheckBox();
   237                 ctrl.AutoSize = true;
   238                 ctrl.Text = aAttribute.Description;
   239                 ctrl.Checked = (bool)aInfo.GetValue(aObject);
   240                 return ctrl;
   241             }
   242             else if (aInfo.PropertyType == typeof(string))
   243             {
   244                 TextBox ctrl = new TextBox();
   245                 ctrl.AutoSize = true;
   246                 ctrl.Text = (string)aInfo.GetValue(aObject);
   247                 return ctrl;
   248             }
   249             else if (aInfo.PropertyType == typeof(PropertyFile))
   250             {
   251                 // We have a file property
   252                 // Create a button that will trigger the open file dialog to select our file.
   253                 Button ctrl = new Button();
   254                 ctrl.AutoSize = true;
   255                 ctrl.Text = ((PropertyFile)aInfo.GetValue(aObject)).FullPath;
   256 
   257                 // Add lambda expression to Click event
   258                 ctrl.Click += (sender, e) =>
   259                 {
   260                     // Create open file dialog
   261                     OpenFileDialog ofd = new OpenFileDialog();
   262                     ofd.RestoreDirectory = true;
   263                     // Use file filter specified by our property
   264                     ofd.Filter = aAttribute.Filter;
   265                     // Show our dialog
   266                     if (DlgBox.ShowDialog(ofd) == DialogResult.OK)
   267                     {
   268                         // Fetch selected file name
   269                         ctrl.Text = ofd.FileName;
   270                         //Enable Ok button then
   271                         buttonOk.Enabled = Object.IsValid();
   272                     }
   273                 };
   274 
   275                 return ctrl;
   276             }
   277             else if (aInfo.PropertyType == typeof(PropertyComboBox))
   278             {
   279                 //ComboBox property
   280                 ComboBox ctrl = new ComboBox();
   281                 ctrl.AutoSize = true;
   282                 ctrl.Sorted = true;
   283                 ctrl.DropDownStyle = ComboBoxStyle.DropDownList;
   284                 //Data source is such a pain to set the current item
   285                 //ctrl.DataSource = ((PropertyComboBox)aInfo.GetValue(aObject)).Items;                
   286 
   287                 PropertyComboBox pcb = ((PropertyComboBox)aInfo.GetValue(aObject));
   288                 foreach (string item in pcb.Items)
   289                 {
   290                     ctrl.Items.Add(item);
   291                 }
   292 
   293                 ctrl.SelectedItem = ((PropertyComboBox)aInfo.GetValue(aObject)).CurrentItem;
   294                 //
   295                 return ctrl;
   296             }
   297             //TODO: add support for other control type here
   298             return null;
   299         }
   300 
   301         /// <summary>
   302         /// Don't forget to extend that one and adding types
   303         /// </summary>
   304         /// <returns></returns>
   305         private bool IsPropertyTypeSupported(PropertyInfo aInfo)
   306         {
   307             if (aInfo.PropertyType == typeof(int))
   308             {
   309                 return true;
   310             }
   311             else if (aInfo.PropertyType.IsEnum)
   312             {
   313                 return true;
   314             }
   315             else if (aInfo.PropertyType == typeof(bool))
   316             {
   317                 return true;
   318             }
   319             else if (aInfo.PropertyType == typeof(string))
   320             {
   321                 return true;
   322             }
   323             else if (aInfo.PropertyType == typeof(PropertyFile))
   324             {
   325                 return true;
   326             }
   327             else if (aInfo.PropertyType == typeof(PropertyComboBox))
   328             {
   329                 return true;
   330             }
   331 
   332             //TODO: add support for other type here
   333 
   334             return false;
   335         }
   336 
   337         /// <summary>
   338         /// Update our table layout.
   339         /// Will instantiated every field control as defined by our action.
   340         /// Fields must be specified by rows from the left.
   341         /// </summary>
   342         /// <param name="aLayout"></param>
   343         private void UpdateTableLayoutPanel(T aObject)
   344         {
   345             toolTip.RemoveAll();
   346             //Debug.Print("UpdateTableLayoutPanel")
   347             //First clean our current panel
   348             iTableLayoutPanel.Controls.Clear();
   349             iTableLayoutPanel.RowStyles.Clear();
   350             iTableLayoutPanel.ColumnStyles.Clear();
   351             iTableLayoutPanel.RowCount = 0;
   352 
   353             //We always want two columns: one for label and one for the field
   354             iTableLayoutPanel.ColumnCount = 2;
   355             iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
   356             iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
   357 
   358 
   359             if (aObject == null)
   360             {
   361                 //Just drop it
   362                 return;
   363             }
   364             
   365             //IEnumerable<PropertyInfo> properties = aObject.GetType().GetProperties().Where(
   366             //    prop => Attribute.IsDefined(prop, typeof(AttributeObjectProperty)));
   367 
   368 
   369             foreach (PropertyInfo pi in aObject.GetType().GetProperties())
   370             {
   371                 AttributeObjectProperty[] attributes = ((AttributeObjectProperty[])pi.GetCustomAttributes(typeof(AttributeObjectProperty), true));
   372                 if (attributes.Length != 1)
   373                 {
   374                     continue;
   375                 }
   376 
   377                 AttributeObjectProperty attribute = attributes[0];
   378 
   379                 //Before anything we need to check if that kind of property is supported by our UI
   380                 //Create the editor
   381                 Control ctrl = CreateControlForProperty(pi, attribute, aObject);
   382                 if (ctrl == null)
   383                 {
   384                     //Property type not supported
   385                     continue;
   386                 }
   387 
   388                 //Add a new row
   389                 iTableLayoutPanel.RowCount++;
   390                 iTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
   391                 //Create the label
   392                 Label label = new Label();
   393                 label.AutoSize = true;
   394                 label.Dock = DockStyle.Fill;
   395                 label.TextAlign = ContentAlignment.MiddleCenter;
   396                 label.Text = attribute.Name;
   397                 toolTip.SetToolTip(label, attribute.Description);
   398                 iTableLayoutPanel.Controls.Add(label, 0, iTableLayoutPanel.RowCount-1);
   399 
   400                 //Add our editor to our form
   401                 iTableLayoutPanel.Controls.Add(ctrl, 1, iTableLayoutPanel.RowCount - 1);
   402                 //Add tooltip to editor too
   403                 toolTip.SetToolTip(ctrl, attribute.Description);
   404 
   405             }        
   406 
   407         }
   408 
   409         private void buttonTest_Click(object sender, EventArgs e)
   410         {
   411             FetchPropertiesValue(Object);
   412 
   413             //If our object has a test method with no parameters just run it then
   414             MethodInfo info = Object.GetType().GetMethod("Test");
   415             if ( info != null && info.GetParameters().Length==0)
   416             {
   417                 info.Invoke(Object,null);
   418             }
   419 
   420         }
   421     }
   422 }