Server/FormEditObject.cs
changeset 247 afdbe76ab03b
parent 246 30a221eecc06
child 250 b2121d03f6f0
     1.1 --- a/Server/FormEditObject.cs	Sat Aug 20 21:00:35 2016 +0200
     1.2 +++ b/Server/FormEditObject.cs	Sun Aug 21 16:11:40 2016 +0200
     1.3 @@ -42,42 +42,53 @@
     1.4              foreach (Type type in types)
     1.5              {
     1.6                  ItemObjectType item = new ItemObjectType(type);
     1.7 -                comboBoxActionType.Items.Add(item);
     1.8 +                iComboBoxObjectType.Items.Add(item);
     1.9              }
    1.10  
    1.11              if (Object == null)
    1.12              {
    1.13                  // Creating new issue, select our first item
    1.14 -                comboBoxActionType.SelectedIndex = 0;
    1.15 +                iComboBoxObjectType.SelectedIndex = 0;
    1.16              }
    1.17              else
    1.18              {
    1.19                  // Editing existing object
    1.20                  // Look up our item in our object type combobox
    1.21 -                foreach (ItemObjectType item in comboBoxActionType.Items)
    1.22 +                foreach (ItemObjectType item in iComboBoxObjectType.Items)
    1.23                  {
    1.24                      if (item.Type == Object.GetType())
    1.25                      {
    1.26 -                        comboBoxActionType.SelectedItem = item;
    1.27 +                        iComboBoxObjectType.SelectedItem = item;
    1.28                      }
    1.29                  }
    1.30  
    1.31              }
    1.32          }
    1.33  
    1.34 +        /// <summary>
    1.35 +        /// 
    1.36 +        /// </summary>
    1.37 +        /// <param name="sender"></param>
    1.38 +        /// <param name="e"></param>
    1.39          private void buttonOk_Click(object sender, EventArgs e)
    1.40          {
    1.41              FetchPropertiesValue(Object);
    1.42              if (!Object.IsValid())
    1.43              {
    1.44 -                // Tell for closing event to abort
    1.45 +                // Tell closing event to cancel
    1.46                  DialogResult = DialogResult.None;
    1.47              }
    1.48          }
    1.49  
    1.50  
    1.51 +        /// <summary>
    1.52 +        /// 
    1.53 +        /// </summary>
    1.54 +        /// <param name="sender"></param>
    1.55 +        /// <param name="e"></param>
    1.56          private void FormEditObject_FormClosing(object sender, FormClosingEventArgs e)
    1.57          {
    1.58 +            //Check if we need to cancel the closing of our form.
    1.59              e.Cancel = DialogResult == DialogResult.None;
    1.60  
    1.61              if (!e.Cancel)
    1.62 @@ -88,21 +99,23 @@
    1.63              }
    1.64          }
    1.65  
    1.66 +        /// <summary>
    1.67 +        /// 
    1.68 +        /// </summary>
    1.69 +        /// <param name="sender"></param>
    1.70 +        /// <param name="e"></param>
    1.71          private void comboBoxActionType_SelectedIndexChanged(object sender, EventArgs e)
    1.72          {
    1.73              //Instantiate an action corresponding to our type
    1.74 -            Type actionType = ((ItemObjectType) comboBoxActionType.SelectedItem).Type;
    1.75 +            Type actionType = ((ItemObjectType) iComboBoxObjectType.SelectedItem).Type;
    1.76              //Create another type of action only if needed
    1.77              if (Object == null || Object.GetType() != actionType)
    1.78              {
    1.79                  Object = (T)Activator.CreateInstance(actionType);
    1.80              }
    1.81  
    1.82 -            //Disable ok button if our object is not valid
    1.83 -            buttonOk.Enabled = Object.IsValid();
    1.84 -
    1.85              //Create input fields
    1.86 -            UpdateTableLayoutPanel(Object);
    1.87 +            UpdateControls();
    1.88          }
    1.89  
    1.90  
    1.91 @@ -206,6 +219,8 @@
    1.92                  ctrl.Maximum = Int32.Parse(aAttribute.Maximum);
    1.93                  ctrl.Increment = Int32.Parse(aAttribute.Increment);
    1.94                  ctrl.Value = (int)aInfo.GetValue(aObject);
    1.95 +                // Hook-in change notification after setting the value 
    1.96 +                ctrl.ValueChanged += ControlValueChanged;
    1.97                  return ctrl;
    1.98              }
    1.99              else if (aInfo.PropertyType.IsEnum)
   1.100 @@ -238,6 +253,8 @@
   1.101                  enumValue = aInfo.GetValue(aObject);
   1.102                  //Set the current item
   1.103                  ctrl.SelectedItem = enumValue.ToString();
   1.104 +                // Hook-in change notification after setting the value 
   1.105 +                ctrl.SelectedIndexChanged += ControlValueChanged;
   1.106  
   1.107                  return ctrl;
   1.108              }
   1.109 @@ -247,6 +264,8 @@
   1.110                  ctrl.AutoSize = true;
   1.111                  ctrl.Text = aAttribute.Description;
   1.112                  ctrl.Checked = (bool)aInfo.GetValue(aObject);
   1.113 +                // Hook-in change notification after setting the value 
   1.114 +                ctrl.CheckedChanged += ControlValueChanged;
   1.115                  return ctrl;
   1.116              }
   1.117              else if (aInfo.PropertyType == typeof(string))
   1.118 @@ -254,6 +273,8 @@
   1.119                  TextBox ctrl = new TextBox();
   1.120                  ctrl.AutoSize = true;
   1.121                  ctrl.Text = (string)aInfo.GetValue(aObject);
   1.122 +                // Hook-in change notification after setting the value 
   1.123 +                ctrl.TextChanged += ControlValueChanged;
   1.124                  return ctrl;
   1.125              }
   1.126              else if (aInfo.PropertyType == typeof(PropertyFile))
   1.127 @@ -263,7 +284,6 @@
   1.128                  Button ctrl = new Button();
   1.129                  ctrl.AutoSize = true;
   1.130                  ctrl.Text = ((PropertyFile)aInfo.GetValue(aObject)).FullPath;
   1.131 -
   1.132                  // Add lambda expression to Click event
   1.133                  ctrl.Click += (sender, e) =>
   1.134                  {
   1.135 @@ -277,11 +297,11 @@
   1.136                      {
   1.137                          // Fetch selected file name
   1.138                          ctrl.Text = ofd.FileName;
   1.139 -                        //Enable Ok button then
   1.140 -                        buttonOk.Enabled = Object.IsValid();
   1.141                      }
   1.142                  };
   1.143  
   1.144 +                // Hook-in change notification after setting the value 
   1.145 +                ctrl.TextChanged += ControlValueChanged;
   1.146                  return ctrl;
   1.147              }
   1.148              else if (aInfo.PropertyType == typeof(PropertyComboBox))
   1.149 @@ -349,8 +369,9 @@
   1.150          /// Will instantiated every field control as defined by our object.
   1.151          /// </summary>
   1.152          /// <param name="aLayout"></param>
   1.153 -        private void UpdateTableLayoutPanel(T aObject)
   1.154 +        private void UpdateControls()
   1.155          {
   1.156 +
   1.157              toolTip.RemoveAll();
   1.158              //Debug.Print("UpdateTableLayoutPanel")
   1.159              //First clean our current panel
   1.160 @@ -365,20 +386,22 @@
   1.161              iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
   1.162  
   1.163  
   1.164 -            if (aObject == null)
   1.165 +            if (Object == null)
   1.166              {
   1.167                  //Just drop it
   1.168                  return;
   1.169              }
   1.170  
   1.171 +            UpdateStaticControls();
   1.172 +
   1.173              //IEnumerable<PropertyInfo> properties = aObject.GetType().GetProperties().Where(
   1.174              //    prop => Attribute.IsDefined(prop, typeof(AttributeObjectProperty)));
   1.175  
   1.176              //TODO: Do this whenever a field changes
   1.177 -            labelBrief.Text = Object.Brief();
   1.178 +            iLabelBrief.Text = Object.Brief();
   1.179  
   1.180  
   1.181 -            foreach (PropertyInfo pi in aObject.GetType().GetProperties())
   1.182 +            foreach (PropertyInfo pi in Object.GetType().GetProperties())
   1.183              {
   1.184                  AttributeObjectProperty[] attributes = ((AttributeObjectProperty[])pi.GetCustomAttributes(typeof(AttributeObjectProperty), true));
   1.185                  if (attributes.Length != 1)
   1.186 @@ -390,7 +413,7 @@
   1.187  
   1.188                  //Before anything we need to check if that kind of property is supported by our UI
   1.189                  //Create the editor
   1.190 -                Control ctrl = CreateControlForProperty(pi, attribute, aObject);
   1.191 +                Control ctrl = CreateControlForProperty(pi, attribute, Object);
   1.192                  if (ctrl == null)
   1.193                  {
   1.194                      //Property type not supported
   1.195 @@ -421,6 +444,11 @@
   1.196              Object.PropertyChanged += PropertyChangedEventHandlerThreadSafe;
   1.197          }
   1.198  
   1.199 +        /// <summary>
   1.200 +        /// 
   1.201 +        /// </summary>
   1.202 +        /// <param name="sender"></param>
   1.203 +        /// <param name="e"></param>
   1.204          void PropertyChangedEventHandlerThreadSafe(object sender, PropertyChangedEventArgs e)
   1.205          {
   1.206              if (this.InvokeRequired)
   1.207 @@ -431,16 +459,13 @@
   1.208              }
   1.209              else
   1.210              {
   1.211 -                //Disable ok button if our object is not valid
   1.212 -                buttonOk.Enabled = Object.IsValid();
   1.213 +                // We could test the name of the property that has changed as follow
   1.214 +                // It's currently not needed though
   1.215 +                //if (e.PropertyName == "Brief")
   1.216  
   1.217 -                if (e.PropertyName == "Brief")
   1.218 -                {
   1.219 -                    labelBrief.Text = Object.Brief();
   1.220 -                }
   1.221 -
   1.222 -                //Create input fields
   1.223 -                //UpdateTableLayoutPanel(Object);
   1.224 +                // Our object has changed behind our back.
   1.225 +                // That's currently only the case for HID events that are listening for inputs.
   1.226 +                UpdateStaticControls();
   1.227              }
   1.228          }
   1.229  
   1.230 @@ -456,5 +481,60 @@
   1.231              }
   1.232  
   1.233          }
   1.234 +
   1.235 +
   1.236 +        /// <summary>
   1.237 +        /// 
   1.238 +        /// </summary>
   1.239 +        /// <param name="sender"></param>
   1.240 +        /// <param name="e"></param>
   1.241 +        private void ControlValueChanged(object sender, EventArgs e)
   1.242 +        {
   1.243 +            UpdateObject();
   1.244 +        }
   1.245 +
   1.246 +        /// <summary>
   1.247 +        /// 
   1.248 +        /// </summary>
   1.249 +        private void UpdateObject()
   1.250 +        {
   1.251 +            // Update our object with the content of our controls
   1.252 +            FetchPropertiesValue(Object);
   1.253 +
   1.254 +            UpdateStaticControls();
   1.255 +            //
   1.256 +            //PerformLayout();
   1.257 +        }
   1.258 +
   1.259 +        /// <summary>
   1.260 +        /// 
   1.261 +        /// </summary>
   1.262 +        private void UpdateStaticControls()
   1.263 +        {
   1.264 +            // Update OK and test button status
   1.265 +            iButtonOk.Enabled = Object.IsValid();
   1.266 +            iButtonTest.Enabled = iButtonOk.Enabled;
   1.267 +
   1.268 +            // Update brief title
   1.269 +            iLabelBrief.Text = Object.Brief();
   1.270 +
   1.271 +            // Update object description
   1.272 +            iLabelDescription.Text = Object.Description;
   1.273 +        }
   1.274 +
   1.275 +        /// <summary>
   1.276 +        /// 
   1.277 +        /// </summary>
   1.278 +        /// <param name="sender"></param>
   1.279 +        /// <param name="e"></param>
   1.280 +        private void iComboBoxObjectType_KeyPress(object sender, KeyPressEventArgs e)
   1.281 +        {
   1.282 +            //Special case for HID events
   1.283 +            if (Object is EventHid)
   1.284 +            {
   1.285 +                //Disable handling of key input as we are using key input for changing our event
   1.286 +                e.Handled = true;
   1.287 +            }
   1.288 +        }
   1.289      }
   1.290  }