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