Server/FormEditObject.cs
author StephaneLenclud
Sun, 21 Aug 2016 16:30:58 +0200
changeset 248 0220cb84df58
parent 246 30a221eecc06
child 250 b2121d03f6f0
permissions -rw-r--r--
Fixing EAR event edition.
EAR HID event now displays 'Press a key' if not valid.
     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                 iComboBoxObjectType.Items.Add(item);
    46             }
    47 
    48             if (Object == null)
    49             {
    50                 // Creating new issue, select our first item
    51                 iComboBoxObjectType.SelectedIndex = 0;
    52             }
    53             else
    54             {
    55                 // Editing existing object
    56                 // Look up our item in our object type combobox
    57                 foreach (ItemObjectType item in iComboBoxObjectType.Items)
    58                 {
    59                     if (item.Type == Object.GetType())
    60                     {
    61                         iComboBoxObjectType.SelectedItem = item;
    62                     }
    63                 }
    64 
    65             }
    66         }
    67 
    68         /// <summary>
    69         /// 
    70         /// </summary>
    71         /// <param name="sender"></param>
    72         /// <param name="e"></param>
    73         private void buttonOk_Click(object sender, EventArgs e)
    74         {
    75             FetchPropertiesValue(Object);
    76             if (!Object.IsValid())
    77             {
    78                 // Tell closing event to cancel
    79                 DialogResult = DialogResult.None;
    80             }
    81         }
    82 
    83 
    84         /// <summary>
    85         /// 
    86         /// </summary>
    87         /// <param name="sender"></param>
    88         /// <param name="e"></param>
    89         private void FormEditObject_FormClosing(object sender, FormClosingEventArgs e)
    90         {
    91             //Check if we need to cancel the closing of our form.
    92             e.Cancel = DialogResult == DialogResult.None;
    93 
    94             if (!e.Cancel)
    95             {
    96                 //Exit edit mode
    97                 Object.CurrentState = SharpLib.Ear.Object.State.Rest;
    98                 Object.PropertyChanged -= PropertyChangedEventHandlerThreadSafe;
    99             }
   100         }
   101 
   102         /// <summary>
   103         /// 
   104         /// </summary>
   105         /// <param name="sender"></param>
   106         /// <param name="e"></param>
   107         private void comboBoxActionType_SelectedIndexChanged(object sender, EventArgs e)
   108         {
   109             //Instantiate an action corresponding to our type
   110             Type actionType = ((ItemObjectType) iComboBoxObjectType.SelectedItem).Type;
   111             //Create another type of action only if needed
   112             if (Object == null || Object.GetType() != actionType)
   113             {
   114                 Object = (T)Activator.CreateInstance(actionType);
   115             }
   116 
   117             //Create input fields
   118             UpdateControls();
   119         }
   120 
   121 
   122         /// <summary>
   123         /// Get properties values from our generated input fields
   124         /// </summary>
   125         private void FetchPropertiesValue(T aObject)
   126         {
   127             int ctrlIndex = 0;
   128             //For each of our properties
   129             foreach (PropertyInfo pi in aObject.GetType().GetProperties())
   130             {
   131                 //Get our property attribute
   132                 AttributeObjectProperty[] attributes = ((AttributeObjectProperty[]) pi.GetCustomAttributes(typeof(AttributeObjectProperty), true));
   133                 if (attributes.Length != 1)
   134                 {
   135                     //No attribute, skip this property then.
   136                     continue;
   137                 }
   138                 AttributeObjectProperty attribute = attributes[0];
   139 
   140                 //Check that we support this type of property
   141                 if (!IsPropertyTypeSupported(pi))
   142                 {
   143                     continue;
   144                 }
   145 
   146                 //Now fetch our property value
   147                 GetPropertyValueFromControl(iTableLayoutPanel.Controls[ctrlIndex+1], pi, aObject); //+1 otherwise we get the label
   148 
   149                 ctrlIndex+=2; //Jump over the label too
   150             }
   151         }
   152 
   153         /// <summary>
   154         /// Extend this function to support reading new types of properties.
   155         /// </summary>
   156         /// <param name="aObject"></param>
   157         private void GetPropertyValueFromControl(Control aControl, PropertyInfo aInfo, T aObject)
   158         {
   159             if (aInfo.PropertyType == typeof(int))
   160             {
   161                 NumericUpDown ctrl=(NumericUpDown)aControl;
   162                 aInfo.SetValue(aObject,(int)ctrl.Value);
   163             }
   164             else if (aInfo.PropertyType.IsEnum)
   165             {
   166                 // Instantiate our enum
   167                 object enumValue= Activator.CreateInstance(aInfo.PropertyType);
   168                 // Parse our enum from combo box
   169                 enumValue = Enum.Parse(aInfo.PropertyType,((ComboBox)aControl).SelectedItem.ToString());
   170                 //enumValue = ((ComboBox)aControl).SelectedValue;
   171                 // Set enum value
   172                 aInfo.SetValue(aObject, enumValue);
   173             }
   174             else if (aInfo.PropertyType == typeof(bool))
   175             {
   176                 CheckBox ctrl = (CheckBox)aControl;
   177                 aInfo.SetValue(aObject, ctrl.Checked);
   178             }
   179             else if (aInfo.PropertyType == typeof(string))
   180             {
   181                 TextBox ctrl = (TextBox)aControl;
   182                 aInfo.SetValue(aObject, ctrl.Text);
   183             }
   184             else if (aInfo.PropertyType == typeof(PropertyFile))
   185             {
   186                 Button ctrl = (Button)aControl;
   187                 PropertyFile value = new PropertyFile {FullPath=ctrl.Text};
   188                 aInfo.SetValue(aObject, value);
   189             }
   190             else if (aInfo.PropertyType == typeof(PropertyComboBox))
   191             {
   192                 ComboBox ctrl = (ComboBox)aControl;
   193                 string currentItem = ctrl.SelectedItem.ToString();
   194                 PropertyComboBox value = (PropertyComboBox)aInfo.GetValue(aObject);
   195                 value.CurrentItem = currentItem;
   196                 //Not strictly needed but makes sure the set method is called
   197                 aInfo.SetValue(aObject, value);                
   198             }
   199 
   200             //TODO: add support for other types here
   201         }
   202 
   203 
   204         /// <summary>
   205         /// Create a control for the given property.
   206         /// </summary>
   207         /// <param name="aInfo"></param>
   208         /// <param name="aAttribute"></param>
   209         /// <param name="aObject"></param>
   210         /// <returns></returns>
   211         private Control CreateControlForProperty(PropertyInfo aInfo, AttributeObjectProperty aAttribute, T aObject)
   212         {
   213             if (aInfo.PropertyType == typeof(int))
   214             {
   215                 //Integer properties are using numeric editor
   216                 NumericUpDown ctrl = new NumericUpDown();
   217                 ctrl.AutoSize = true;
   218                 ctrl.Minimum = Int32.Parse(aAttribute.Minimum);
   219                 ctrl.Maximum = Int32.Parse(aAttribute.Maximum);
   220                 ctrl.Increment = Int32.Parse(aAttribute.Increment);
   221                 ctrl.Value = (int)aInfo.GetValue(aObject);
   222                 // Hook-in change notification after setting the value 
   223                 ctrl.ValueChanged += ControlValueChanged;
   224                 return ctrl;
   225             }
   226             else if (aInfo.PropertyType.IsEnum)
   227             {
   228                 //Enum properties are using combo box
   229                 ComboBox ctrl = new ComboBox();
   230                 ctrl.AutoSize = true;
   231                 ctrl.Sorted = true;
   232                 ctrl.DropDownStyle = ComboBoxStyle.DropDownList;
   233                 //Data source is fine but it gives us duplicate entries for duplicated enum values
   234                 //ctrl.DataSource = Enum.GetValues(aInfo.PropertyType);
   235 
   236                 //Therefore we need to explicitly create our items
   237                 Size cbSize = new Size(0, 0);
   238                 foreach (string name in aInfo.PropertyType.GetEnumNames())
   239                 {
   240                     ctrl.Items.Add(name.ToString());
   241                     Graphics g = this.CreateGraphics();
   242                     //Since combobox autosize would not work we need to get measure text ourselves
   243                     SizeF size = g.MeasureString(name.ToString(), ctrl.Font);
   244                     cbSize.Width = Math.Max(cbSize.Width, (int)size.Width);
   245                     cbSize.Height = Math.Max(cbSize.Height, (int)size.Height);
   246                 }
   247 
   248                 //Make sure our combobox is large enough
   249                 ctrl.MinimumSize = cbSize;
   250 
   251                 // Instantiate our enum
   252                 object enumValue = Activator.CreateInstance(aInfo.PropertyType);
   253                 enumValue = aInfo.GetValue(aObject);
   254                 //Set the current item
   255                 ctrl.SelectedItem = enumValue.ToString();
   256                 // Hook-in change notification after setting the value 
   257                 ctrl.SelectedIndexChanged += ControlValueChanged;
   258 
   259                 return ctrl;
   260             }
   261             else if (aInfo.PropertyType == typeof(bool))
   262             {
   263                 CheckBox ctrl = new CheckBox();
   264                 ctrl.AutoSize = true;
   265                 ctrl.Text = aAttribute.Description;
   266                 ctrl.Checked = (bool)aInfo.GetValue(aObject);
   267                 // Hook-in change notification after setting the value 
   268                 ctrl.CheckedChanged += ControlValueChanged;
   269                 return ctrl;
   270             }
   271             else if (aInfo.PropertyType == typeof(string))
   272             {
   273                 TextBox ctrl = new TextBox();
   274                 ctrl.AutoSize = true;
   275                 ctrl.Text = (string)aInfo.GetValue(aObject);
   276                 // Hook-in change notification after setting the value 
   277                 ctrl.TextChanged += ControlValueChanged;
   278                 return ctrl;
   279             }
   280             else if (aInfo.PropertyType == typeof(PropertyFile))
   281             {
   282                 // We have a file property
   283                 // Create a button that will trigger the open file dialog to select our file.
   284                 Button ctrl = new Button();
   285                 ctrl.AutoSize = true;
   286                 ctrl.Text = ((PropertyFile)aInfo.GetValue(aObject)).FullPath;
   287                 // Add lambda expression to Click event
   288                 ctrl.Click += (sender, e) =>
   289                 {
   290                     // Create open file dialog
   291                     OpenFileDialog ofd = new OpenFileDialog();
   292                     ofd.RestoreDirectory = true;
   293                     // Use file filter specified by our property
   294                     ofd.Filter = aAttribute.Filter;
   295                     // Show our dialog
   296                     if (DlgBox.ShowDialog(ofd) == DialogResult.OK)
   297                     {
   298                         // Fetch selected file name
   299                         ctrl.Text = ofd.FileName;
   300                     }
   301                 };
   302 
   303                 // Hook-in change notification after setting the value 
   304                 ctrl.TextChanged += ControlValueChanged;
   305                 return ctrl;
   306             }
   307             else if (aInfo.PropertyType == typeof(PropertyComboBox))
   308             {
   309                 //ComboBox property
   310                 ComboBox ctrl = new ComboBox();
   311                 ctrl.AutoSize = true;
   312                 ctrl.Sorted = true;
   313                 ctrl.DropDownStyle = ComboBoxStyle.DropDownList;
   314                 //Data source is such a pain to set the current item
   315                 //ctrl.DataSource = ((PropertyComboBox)aInfo.GetValue(aObject)).Items;                
   316 
   317                 PropertyComboBox pcb = ((PropertyComboBox)aInfo.GetValue(aObject));
   318                 foreach (string item in pcb.Items)
   319                 {
   320                     ctrl.Items.Add(item);
   321                 }
   322 
   323                 ctrl.SelectedItem = ((PropertyComboBox)aInfo.GetValue(aObject)).CurrentItem;
   324                 //
   325                 return ctrl;
   326             }
   327             //TODO: add support for other control type here
   328             return null;
   329         }
   330 
   331         /// <summary>
   332         /// Don't forget to extend that one and adding types
   333         /// </summary>
   334         /// <returns></returns>
   335         private bool IsPropertyTypeSupported(PropertyInfo aInfo)
   336         {
   337             if (aInfo.PropertyType == typeof(int))
   338             {
   339                 return true;
   340             }
   341             else if (aInfo.PropertyType.IsEnum)
   342             {
   343                 return true;
   344             }
   345             else if (aInfo.PropertyType == typeof(bool))
   346             {
   347                 return true;
   348             }
   349             else if (aInfo.PropertyType == typeof(string))
   350             {
   351                 return true;
   352             }
   353             else if (aInfo.PropertyType == typeof(PropertyFile))
   354             {
   355                 return true;
   356             }
   357             else if (aInfo.PropertyType == typeof(PropertyComboBox))
   358             {
   359                 return true;
   360             }
   361 
   362             //TODO: add support for other type here
   363 
   364             return false;
   365         }
   366 
   367         /// <summary>
   368         /// Update our table layout.
   369         /// Will instantiated every field control as defined by our object.
   370         /// </summary>
   371         /// <param name="aLayout"></param>
   372         private void UpdateControls()
   373         {
   374 
   375             toolTip.RemoveAll();
   376             //Debug.Print("UpdateTableLayoutPanel")
   377             //First clean our current panel
   378             iTableLayoutPanel.Controls.Clear();
   379             iTableLayoutPanel.RowStyles.Clear();
   380             iTableLayoutPanel.ColumnStyles.Clear();
   381             iTableLayoutPanel.RowCount = 0;
   382 
   383             //We always want two columns: one for label and one for the field
   384             iTableLayoutPanel.ColumnCount = 2;
   385             iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
   386             iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
   387 
   388 
   389             if (Object == null)
   390             {
   391                 //Just drop it
   392                 return;
   393             }
   394 
   395             UpdateStaticControls();
   396 
   397             //IEnumerable<PropertyInfo> properties = aObject.GetType().GetProperties().Where(
   398             //    prop => Attribute.IsDefined(prop, typeof(AttributeObjectProperty)));
   399 
   400             //TODO: Do this whenever a field changes
   401             iLabelBrief.Text = Object.Brief();
   402 
   403 
   404             foreach (PropertyInfo pi in Object.GetType().GetProperties())
   405             {
   406                 AttributeObjectProperty[] attributes = ((AttributeObjectProperty[])pi.GetCustomAttributes(typeof(AttributeObjectProperty), true));
   407                 if (attributes.Length != 1)
   408                 {
   409                     continue;
   410                 }
   411 
   412                 AttributeObjectProperty attribute = attributes[0];
   413 
   414                 //Before anything we need to check if that kind of property is supported by our UI
   415                 //Create the editor
   416                 Control ctrl = CreateControlForProperty(pi, attribute, Object);
   417                 if (ctrl == null)
   418                 {
   419                     //Property type not supported
   420                     continue;
   421                 }
   422 
   423                 //Add a new row
   424                 iTableLayoutPanel.RowCount++;
   425                 iTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
   426                 //Create the label
   427                 Label label = new Label();
   428                 label.AutoSize = true;
   429                 label.Dock = DockStyle.Fill;
   430                 label.TextAlign = ContentAlignment.MiddleCenter;
   431                 label.Text = attribute.Name;
   432                 toolTip.SetToolTip(label, attribute.Description);
   433                 iTableLayoutPanel.Controls.Add(label, 0, iTableLayoutPanel.RowCount-1);
   434 
   435                 //Add our editor to our form
   436                 iTableLayoutPanel.Controls.Add(ctrl, 1, iTableLayoutPanel.RowCount - 1);
   437                 //Add tooltip to editor too
   438                 toolTip.SetToolTip(ctrl, attribute.Description);
   439 
   440             }
   441 
   442             //Entrer object edit mode
   443             Object.CurrentState = SharpLib.Ear.Object.State.Edit;
   444             Object.PropertyChanged += PropertyChangedEventHandlerThreadSafe;
   445         }
   446 
   447         /// <summary>
   448         /// 
   449         /// </summary>
   450         /// <param name="sender"></param>
   451         /// <param name="e"></param>
   452         void PropertyChangedEventHandlerThreadSafe(object sender, PropertyChangedEventArgs e)
   453         {
   454             if (this.InvokeRequired)
   455             {
   456                 //Not in the proper thread, invoke ourselves
   457                 PropertyChangedEventHandler d = new PropertyChangedEventHandler(PropertyChangedEventHandlerThreadSafe);
   458                 this.Invoke(d, new object[] { sender, e });
   459             }
   460             else
   461             {
   462                 // We could test the name of the property that has changed as follow
   463                 // It's currently not needed though
   464                 //if (e.PropertyName == "Brief")
   465 
   466                 // Our object has changed behind our back.
   467                 // That's currently only the case for HID events that are listening for inputs.
   468                 UpdateStaticControls();
   469             }
   470         }
   471 
   472         private void buttonTest_Click(object sender, EventArgs e)
   473         {
   474             FetchPropertiesValue(Object);
   475 
   476             //If our object has a test method with no parameters just run it then
   477             MethodInfo info = Object.GetType().GetMethod("Test");
   478             if ( info != null && info.GetParameters().Length==0)
   479             {
   480                 info.Invoke(Object,null);
   481             }
   482 
   483         }
   484 
   485 
   486         /// <summary>
   487         /// 
   488         /// </summary>
   489         /// <param name="sender"></param>
   490         /// <param name="e"></param>
   491         private void ControlValueChanged(object sender, EventArgs e)
   492         {
   493             UpdateObject();
   494         }
   495 
   496         /// <summary>
   497         /// 
   498         /// </summary>
   499         private void UpdateObject()
   500         {
   501             // Update our object with the content of our controls
   502             FetchPropertiesValue(Object);
   503 
   504             UpdateStaticControls();
   505             //
   506             //PerformLayout();
   507         }
   508 
   509         /// <summary>
   510         /// 
   511         /// </summary>
   512         private void UpdateStaticControls()
   513         {
   514             // Update OK and test button status
   515             iButtonOk.Enabled = Object.IsValid();
   516             iButtonTest.Enabled = iButtonOk.Enabled;
   517 
   518             // Update brief title
   519             iLabelBrief.Text = Object.Brief();
   520 
   521             // Update object description
   522             iLabelDescription.Text = Object.Description;
   523         }
   524 
   525         /// <summary>
   526         /// 
   527         /// </summary>
   528         /// <param name="sender"></param>
   529         /// <param name="e"></param>
   530         private void iComboBoxObjectType_KeyPress(object sender, KeyPressEventArgs e)
   531         {
   532             //Special case for HID events
   533             if (Object is EventHid)
   534             {
   535                 //Disable handling of key input as we are using key input for changing our event
   536                 e.Handled = true;
   537             }
   538         }
   539     }
   540 }