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