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