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