Improved object editor dialog validation.
authorStephaneLenclud
Thu, 18 Aug 2016 17:13:21 +0200
changeset 239dd7770b97916
parent 238 c92587ddabcd
child 240 5c4f1e2bf29a
Improved object editor dialog validation.
Added EAR support for object validation.
Server/FormEditObject.Designer.cs
Server/FormEditObject.cs
SharpLibEar/Action.cs
SharpLibEar/ActionLaunchApp.cs
SharpLibEar/Object.cs
SharpLibEar/PropertyFile.cs
     1.1 --- a/Server/FormEditObject.Designer.cs	Thu Aug 18 14:35:50 2016 +0200
     1.2 +++ b/Server/FormEditObject.Designer.cs	Thu Aug 18 17:13:21 2016 +0200
     1.3 @@ -112,7 +112,7 @@
     1.4              this.buttonTest.UseVisualStyleBackColor = true;
     1.5              this.buttonTest.Click += new System.EventHandler(this.buttonTest_Click);
     1.6              // 
     1.7 -            // FormEditAction
     1.8 +            // FormEditObject
     1.9              // 
    1.10              this.AcceptButton = this.buttonOk;
    1.11              this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    1.12 @@ -130,10 +130,10 @@
    1.13              this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
    1.14              this.MaximizeBox = false;
    1.15              this.MinimizeBox = false;
    1.16 -            this.Name = "FormEditAction";
    1.17 +            this.Name = "FormEditObject";
    1.18              this.Text = "Edit action";
    1.19 +            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FormEditObject_FormClosing);
    1.20              this.Load += new System.EventHandler(this.FormEditAction_Load);
    1.21 -            this.Validating += new System.ComponentModel.CancelEventHandler(this.FormEditAction_Validating);
    1.22              this.ResumeLayout(false);
    1.23              this.PerformLayout();
    1.24  
     2.1 --- a/Server/FormEditObject.cs	Thu Aug 18 14:35:50 2016 +0200
     2.2 +++ b/Server/FormEditObject.cs	Thu Aug 18 17:13:21 2016 +0200
     2.3 @@ -14,13 +14,14 @@
     2.4  using Microsoft.VisualBasic.CompilerServices;
     2.5  using SharpLib.Utils;
     2.6  using CodeProject.Dialog;
     2.7 +using System.IO;
     2.8  
     2.9  namespace SharpDisplayManager
    2.10  {
    2.11      /// <summary>
    2.12      /// Object edit dialog form.
    2.13      /// </summary>
    2.14 -    public partial class FormEditObject<T> : Form where T : class
    2.15 +    public partial class FormEditObject<T> : Form where T: SharpLib.Ear.Object
    2.16      {
    2.17          public T Object = null;
    2.18  
    2.19 @@ -52,7 +53,7 @@
    2.20              else
    2.21              {
    2.22                  // Editing existing object
    2.23 -                // Look up our item in our combobox 
    2.24 +                // Look up our item in our combobox
    2.25                  foreach (ItemObjectType item in comboBoxActionType.Items)
    2.26                  {
    2.27                      if (item.Type == Object.GetType())
    2.28 @@ -60,17 +61,23 @@
    2.29                          comboBoxActionType.SelectedItem = item;
    2.30                      }
    2.31                  }
    2.32 -            }            
    2.33 +            }
    2.34          }
    2.35  
    2.36          private void buttonOk_Click(object sender, EventArgs e)
    2.37          {
    2.38              FetchPropertiesValue(Object);
    2.39 +            if (!Object.IsValid())
    2.40 +            {
    2.41 +                // Tell for closing event to abort
    2.42 +                DialogResult = DialogResult.None;
    2.43 +            }
    2.44          }
    2.45  
    2.46 -        private void FormEditAction_Validating(object sender, CancelEventArgs e)
    2.47 +
    2.48 +        private void FormEditObject_FormClosing(object sender, FormClosingEventArgs e)
    2.49          {
    2.50 -
    2.51 +            e.Cancel = DialogResult == DialogResult.None;
    2.52          }
    2.53  
    2.54          private void comboBoxActionType_SelectedIndexChanged(object sender, EventArgs e)
    2.55 @@ -82,7 +89,10 @@
    2.56              {
    2.57                  Object = (T)Activator.CreateInstance(actionType);
    2.58              }
    2.59 -            
    2.60 +
    2.61 +            //Disable ok button if our object is not valid
    2.62 +            buttonOk.Enabled = Object.IsValid();
    2.63 +
    2.64              //Create input fields
    2.65              UpdateTableLayoutPanel(Object);
    2.66          }
    2.67 @@ -232,6 +242,7 @@
    2.68                  Button ctrl = new Button();
    2.69                  ctrl.AutoSize = true;
    2.70                  ctrl.Text = ((PropertyFile)aInfo.GetValue(aObject)).FullPath;
    2.71 +
    2.72                  // Add lambda expression to Click event
    2.73                  ctrl.Click += (sender, e) =>
    2.74                  {
    2.75 @@ -245,6 +256,8 @@
    2.76                      {
    2.77                          // Fetch selected file name
    2.78                          ctrl.Text = ofd.FileName;
    2.79 +                        //Enable Ok button then
    2.80 +                        buttonOk.Enabled = Object.IsValid();
    2.81                      }
    2.82                  };
    2.83  
     3.1 --- a/SharpLibEar/Action.cs	Thu Aug 18 14:35:50 2016 +0200
     3.2 +++ b/SharpLibEar/Action.cs	Thu Aug 18 17:13:21 2016 +0200
     3.3 @@ -25,6 +25,12 @@
     3.4          public void Execute()
     3.5          {
     3.6              Console.WriteLine("Action executing: " + Brief());
     3.7 +            if (!IsValid())
     3.8 +            {
     3.9 +                Console.WriteLine($"WARNING: action invalid, aborting execution.");
    3.10 +                return;
    3.11 +            }
    3.12 +            
    3.13              DoExecute();
    3.14          }
    3.15  
     4.1 --- a/SharpLibEar/ActionLaunchApp.cs	Thu Aug 18 14:35:50 2016 +0200
     4.2 +++ b/SharpLibEar/ActionLaunchApp.cs	Thu Aug 18 17:13:21 2016 +0200
     4.3 @@ -49,6 +49,12 @@
     4.4              return Name + ": " + Path.GetFileName(File.FullPath);
     4.5          }
     4.6  
     4.7 +        public override bool IsValid()
     4.8 +        {
     4.9 +            // This is a valid configuration only if our file exists
    4.10 +            return System.IO.File.Exists(File.FullPath);
    4.11 +        }
    4.12 +
    4.13          [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SwitchToThisWindow")]
    4.14          public static extern void SwitchToThisWindow([System.Runtime.InteropServices.InAttribute()] System.IntPtr hwnd, [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] bool fUnknown);
    4.15  
     5.1 --- a/SharpLibEar/Object.cs	Thu Aug 18 14:35:50 2016 +0200
     5.2 +++ b/SharpLibEar/Object.cs	Thu Aug 18 17:13:21 2016 +0200
     5.3 @@ -21,7 +21,9 @@
     5.4      [KnownType("DerivedTypes")]
     5.5      public abstract class Object: IComparable
     5.6      {
     5.7 -
     5.8 +        /// <summary>
     5.9 +        /// Static object name.
    5.10 +        /// </summary>
    5.11          public string Name
    5.12          {
    5.13              //Get the name of this object attribute
    5.14 @@ -29,6 +31,9 @@
    5.15              private set { }
    5.16          }
    5.17  
    5.18 +        /// <summary>
    5.19 +        /// Static object description.
    5.20 +        /// </summary>
    5.21          public string Description
    5.22          {
    5.23              //Get the description of this object attribute
    5.24 @@ -36,11 +41,20 @@
    5.25              private set { }
    5.26          }
    5.27  
    5.28 +        /// <summary>
    5.29 +        /// Dynamic object description.
    5.30 +        /// </summary>
    5.31 +        /// <returns></returns>
    5.32          public virtual string Brief()
    5.33          {
    5.34              return Name;
    5.35          }
    5.36  
    5.37 +        /// <summary>
    5.38 +        /// Needed to make sure our sorting makes sense
    5.39 +        /// </summary>
    5.40 +        /// <param name="obj"></param>
    5.41 +        /// <returns></returns>
    5.42          public int CompareTo(object obj)
    5.43          {
    5.44              //Sort by object name
    5.45 @@ -48,6 +62,15 @@
    5.46          }
    5.47  
    5.48          /// <summary>
    5.49 +        /// Tells whether the current object configuration is valid.
    5.50 +        /// </summary>
    5.51 +        /// <returns></returns>
    5.52 +        public virtual bool IsValid()
    5.53 +        {
    5.54 +            return true;
    5.55 +        }
    5.56 +
    5.57 +        /// <summary>
    5.58          /// So that data contract knows all our types.
    5.59          /// </summary>
    5.60          /// <returns></returns>
     6.1 --- a/SharpLibEar/PropertyFile.cs	Thu Aug 18 14:35:50 2016 +0200
     6.2 +++ b/SharpLibEar/PropertyFile.cs	Thu Aug 18 17:13:21 2016 +0200
     6.3 @@ -15,6 +15,6 @@
     6.4      public class PropertyFile : Object
     6.5      {
     6.6          [DataMember]
     6.7 -        public string FullPath { get; set; } = "";
     6.8 +        public string FullPath { get; set; } = "Select a file";
     6.9      }
    6.10  }