Server/FormEditAction.cs
author StephaneLenclud
Wed, 27 Jul 2016 11:07:35 +0200
changeset 222 0e8c6c2f4777
parent 221 5770478e1fe3
child 223 f6272f65d8fc
permissions -rw-r--r--
Adding send and release CEC key actions.
Edit action ComboBox now sorted and selecting proper item.
     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 
    15 namespace SharpDisplayManager
    16 {
    17     /// <summary>
    18     /// Action edit dialog form.
    19     /// </summary>
    20     public partial class FormEditAction : Form
    21     {
    22         public SharpLib.Ear.Action Action = null;
    23 
    24         public FormEditAction()
    25         {
    26             InitializeComponent();
    27         }
    28 
    29         private void FormEditAction_Load(object sender, EventArgs e)
    30         {
    31             //Populate registered actions
    32             foreach (string key in ManagerEventAction.Current.ActionTypes.Keys)
    33             {
    34                 ItemActionType item = new ItemActionType(ManagerEventAction.Current.ActionTypes[key]);
    35                 comboBoxActionType.Items.Add(item);
    36             }
    37 
    38             comboBoxActionType.SelectedIndex = 0;
    39         }
    40 
    41         private void buttonOk_Click(object sender, EventArgs e)
    42         {
    43             FetchPropertiesValue(Action);
    44         }
    45 
    46         private void FormEditAction_Validating(object sender, CancelEventArgs e)
    47         {
    48 
    49         }
    50 
    51         private void comboBoxActionType_SelectedIndexChanged(object sender, EventArgs e)
    52         {
    53             //Instantiate an action corresponding to our type
    54             Action = (SharpLib.Ear.Action)Activator.CreateInstance(((ItemActionType)comboBoxActionType.SelectedItem).Type);
    55 
    56             //Create input fields
    57             UpdateTableLayoutPanel(Action);
    58         }
    59 
    60 
    61         /// <summary>
    62         /// Get properties values from our generated input fields
    63         /// </summary>
    64         private void FetchPropertiesValue(SharpLib.Ear.Action aAction)
    65         {
    66             int ctrlIndex = 0;
    67             foreach (PropertyInfo pi in aAction.GetType().GetProperties())
    68             {
    69                 AttributeActionProperty[] attributes =
    70                     ((AttributeActionProperty[]) pi.GetCustomAttributes(typeof(AttributeActionProperty), true));
    71                 if (attributes.Length != 1)
    72                 {
    73                     continue;
    74                 }
    75 
    76                 AttributeActionProperty attribute = attributes[0];
    77 
    78                 if (!IsPropertyTypeSupported(pi))
    79                 {
    80                     continue;
    81                 }
    82 
    83                 GetPropertyValueFromControl(iTableLayoutPanel.Controls[ctrlIndex+1], pi, aAction); //+1 otherwise we get the label
    84 
    85                 ctrlIndex+=2; //Jump over the label too
    86             }
    87         }
    88 
    89         /// <summary>
    90         /// Extend this function to support reading new types of properties.
    91         /// </summary>
    92         /// <param name="aAction"></param>
    93         private void GetPropertyValueFromControl(Control aControl, PropertyInfo aInfo, SharpLib.Ear.Action aAction)
    94         {
    95             if (aInfo.PropertyType == typeof(int))
    96             {
    97                 NumericUpDown ctrl=(NumericUpDown)aControl;
    98                 aInfo.SetValue(aAction,(int)ctrl.Value);
    99             }
   100             else if (aInfo.PropertyType.IsEnum)
   101             {
   102                 // Instantiate our enum
   103                 object enumValue= Activator.CreateInstance(aInfo.PropertyType);
   104                 // Parse our enum from combo box
   105                 enumValue = Enum.Parse(aInfo.PropertyType,((ComboBox)aControl).SelectedItem.ToString());
   106                 //enumValue = ((ComboBox)aControl).SelectedValue;
   107                 // Set enum value
   108                 aInfo.SetValue(aAction, enumValue);
   109             }
   110             else if (aInfo.PropertyType == typeof(bool))
   111             {
   112                 CheckBox ctrl = (CheckBox)aControl;
   113                 aInfo.SetValue(aAction, ctrl.Checked);
   114             }
   115         }
   116 
   117         /// <summary>
   118         /// 
   119         /// </summary>
   120         /// <param name="aInfo"></param>
   121         /// <param name="action"></param>
   122         private Control CreateControlForProperty(PropertyInfo aInfo, AttributeActionProperty aAttribute, SharpLib.Ear.Action aAction)
   123         {
   124             if (aInfo.PropertyType == typeof(int))
   125             {
   126                 //Integer properties are using numeric editor
   127                 NumericUpDown ctrl = new NumericUpDown();
   128                 ctrl.AutoSize = true;
   129                 ctrl.Minimum = Int32.Parse(aAttribute.Minimum);
   130                 ctrl.Maximum = Int32.Parse(aAttribute.Maximum);
   131                 ctrl.Increment = Int32.Parse(aAttribute.Increment);
   132                 ctrl.Value = (int)aInfo.GetValue(aAction);
   133                 return ctrl;
   134             }
   135             else if (aInfo.PropertyType.IsEnum)
   136             {
   137                 //Enum properties are using combo box
   138                 ComboBox ctrl = new ComboBox();
   139                 ctrl.AutoSize = true;
   140                 ctrl.Sorted = true;
   141                 ctrl.DropDownStyle = ComboBoxStyle.DropDownList;
   142                 //Data source is fine but it gives us duplicate entries for duplicated enum values
   143                 //ctrl.DataSource = Enum.GetValues(aInfo.PropertyType);
   144 
   145                 //Therefore we need to explicitly create our items  
   146                 foreach (string name in aInfo.PropertyType.GetEnumNames())
   147                 {
   148                     ctrl.Items.Add(name.ToString());
   149                 }
   150 
   151                 // Instantiate our enum
   152                 object enumValue = Activator.CreateInstance(aInfo.PropertyType);
   153                 enumValue = aInfo.GetValue(aAction);
   154                 //Set the current item
   155                 ctrl.SelectedItem = enumValue.ToString();
   156 
   157                 return ctrl;
   158             }
   159             else if (aInfo.PropertyType == typeof(bool))
   160             {
   161                 CheckBox ctrl = new CheckBox();
   162                 ctrl.AutoSize = true;
   163                 ctrl.Text = aAttribute.Description;
   164                 ctrl.Checked = (bool)aInfo.GetValue(aAction);                
   165                 return ctrl;
   166             }
   167 
   168             return null;
   169         }
   170 
   171         /// <summary>
   172         /// Don't forget to extend that one and adding types
   173         /// </summary>
   174         /// <returns></returns>
   175         private bool IsPropertyTypeSupported(PropertyInfo aInfo)
   176         {
   177             if (aInfo.PropertyType == typeof(int))
   178             {
   179                 return true;
   180             }
   181             else if (aInfo.PropertyType.IsEnum)
   182             {
   183                 return true;
   184             }
   185             else if (aInfo.PropertyType == typeof(bool))
   186             {
   187                 return true;
   188             }
   189 
   190             return false;
   191         }
   192 
   193         /// <summary>
   194         /// Update our table layout.
   195         /// Will instantiated every field control as defined by our action.
   196         /// Fields must be specified by rows from the left.
   197         /// </summary>
   198         /// <param name="aLayout"></param>
   199         private void UpdateTableLayoutPanel(SharpLib.Ear.Action aAction)
   200         {
   201             toolTip.RemoveAll();
   202             //Debug.Print("UpdateTableLayoutPanel")
   203             //First clean our current panel
   204             iTableLayoutPanel.Controls.Clear();
   205             iTableLayoutPanel.RowStyles.Clear();
   206             iTableLayoutPanel.ColumnStyles.Clear();
   207             iTableLayoutPanel.RowCount = 0;
   208 
   209             //We always want two columns: one for label and one for the field
   210             iTableLayoutPanel.ColumnCount = 2;
   211             iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
   212             iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
   213 
   214 
   215             if (aAction == null)
   216             {
   217                 //Just drop it
   218                 return;
   219             }
   220             
   221             //IEnumerable<PropertyInfo> properties = aAction.GetType().GetProperties().Where(
   222             //    prop => Attribute.IsDefined(prop, typeof(AttributeActionProperty)));
   223 
   224 
   225             foreach (PropertyInfo pi in aAction.GetType().GetProperties())
   226             {
   227                 AttributeActionProperty[] attributes = ((AttributeActionProperty[])pi.GetCustomAttributes(typeof(AttributeActionProperty), true));
   228                 if (attributes.Length != 1)
   229                 {
   230                     continue;
   231                 }
   232 
   233                 AttributeActionProperty attribute = attributes[0];
   234 
   235                 //Before anything we need to check if that kind of property is supported by our UI
   236                 //Create the editor
   237                 Control ctrl = CreateControlForProperty(pi, attribute, aAction);
   238                 if (ctrl == null)
   239                 {
   240                     //Property type not supported
   241                     continue;
   242                 }
   243 
   244                 //Add a new row
   245                 iTableLayoutPanel.RowCount++;
   246                 iTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
   247                 //Create the label
   248                 Label label = new Label();
   249                 label.Text = attribute.Name;
   250                 toolTip.SetToolTip(label, attribute.Description);
   251                 iTableLayoutPanel.Controls.Add(label, 0, iTableLayoutPanel.RowCount-1);
   252 
   253                 //Add our editor to our form
   254                 iTableLayoutPanel.Controls.Add(ctrl, 1, iTableLayoutPanel.RowCount - 1);
   255 
   256 
   257             }        
   258 
   259         }
   260 
   261     }
   262 }