# HG changeset patch # User StephaneLenclud # Date 1471533201 -7200 # Node ID dd7770b97916aebf721225e96f0c5de748a11a22 # Parent c92587ddabcdd4da1984d03612b5263ea6fb1121 Improved object editor dialog validation. Added EAR support for object validation. diff -r c92587ddabcd -r dd7770b97916 Server/FormEditObject.Designer.cs --- a/Server/FormEditObject.Designer.cs Thu Aug 18 14:35:50 2016 +0200 +++ b/Server/FormEditObject.Designer.cs Thu Aug 18 17:13:21 2016 +0200 @@ -112,7 +112,7 @@ this.buttonTest.UseVisualStyleBackColor = true; this.buttonTest.Click += new System.EventHandler(this.buttonTest_Click); // - // FormEditAction + // FormEditObject // this.AcceptButton = this.buttonOk; this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -130,10 +130,10 @@ this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.MaximizeBox = false; this.MinimizeBox = false; - this.Name = "FormEditAction"; + this.Name = "FormEditObject"; this.Text = "Edit action"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FormEditObject_FormClosing); this.Load += new System.EventHandler(this.FormEditAction_Load); - this.Validating += new System.ComponentModel.CancelEventHandler(this.FormEditAction_Validating); this.ResumeLayout(false); this.PerformLayout(); diff -r c92587ddabcd -r dd7770b97916 Server/FormEditObject.cs --- a/Server/FormEditObject.cs Thu Aug 18 14:35:50 2016 +0200 +++ b/Server/FormEditObject.cs Thu Aug 18 17:13:21 2016 +0200 @@ -14,13 +14,14 @@ using Microsoft.VisualBasic.CompilerServices; using SharpLib.Utils; using CodeProject.Dialog; +using System.IO; namespace SharpDisplayManager { /// /// Object edit dialog form. /// - public partial class FormEditObject : Form where T : class + public partial class FormEditObject : Form where T: SharpLib.Ear.Object { public T Object = null; @@ -52,7 +53,7 @@ else { // Editing existing object - // Look up our item in our combobox + // Look up our item in our combobox foreach (ItemObjectType item in comboBoxActionType.Items) { if (item.Type == Object.GetType()) @@ -60,17 +61,23 @@ comboBoxActionType.SelectedItem = item; } } - } + } } private void buttonOk_Click(object sender, EventArgs e) { FetchPropertiesValue(Object); + if (!Object.IsValid()) + { + // Tell for closing event to abort + DialogResult = DialogResult.None; + } } - private void FormEditAction_Validating(object sender, CancelEventArgs e) + + private void FormEditObject_FormClosing(object sender, FormClosingEventArgs e) { - + e.Cancel = DialogResult == DialogResult.None; } private void comboBoxActionType_SelectedIndexChanged(object sender, EventArgs e) @@ -82,7 +89,10 @@ { Object = (T)Activator.CreateInstance(actionType); } - + + //Disable ok button if our object is not valid + buttonOk.Enabled = Object.IsValid(); + //Create input fields UpdateTableLayoutPanel(Object); } @@ -232,6 +242,7 @@ Button ctrl = new Button(); ctrl.AutoSize = true; ctrl.Text = ((PropertyFile)aInfo.GetValue(aObject)).FullPath; + // Add lambda expression to Click event ctrl.Click += (sender, e) => { @@ -245,6 +256,8 @@ { // Fetch selected file name ctrl.Text = ofd.FileName; + //Enable Ok button then + buttonOk.Enabled = Object.IsValid(); } }; diff -r c92587ddabcd -r dd7770b97916 SharpLibEar/Action.cs --- a/SharpLibEar/Action.cs Thu Aug 18 14:35:50 2016 +0200 +++ b/SharpLibEar/Action.cs Thu Aug 18 17:13:21 2016 +0200 @@ -25,6 +25,12 @@ public void Execute() { Console.WriteLine("Action executing: " + Brief()); + if (!IsValid()) + { + Console.WriteLine($"WARNING: action invalid, aborting execution."); + return; + } + DoExecute(); } diff -r c92587ddabcd -r dd7770b97916 SharpLibEar/ActionLaunchApp.cs --- a/SharpLibEar/ActionLaunchApp.cs Thu Aug 18 14:35:50 2016 +0200 +++ b/SharpLibEar/ActionLaunchApp.cs Thu Aug 18 17:13:21 2016 +0200 @@ -49,6 +49,12 @@ return Name + ": " + Path.GetFileName(File.FullPath); } + public override bool IsValid() + { + // This is a valid configuration only if our file exists + return System.IO.File.Exists(File.FullPath); + } + [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SwitchToThisWindow")] public static extern void SwitchToThisWindow([System.Runtime.InteropServices.InAttribute()] System.IntPtr hwnd, [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] bool fUnknown); diff -r c92587ddabcd -r dd7770b97916 SharpLibEar/Object.cs --- a/SharpLibEar/Object.cs Thu Aug 18 14:35:50 2016 +0200 +++ b/SharpLibEar/Object.cs Thu Aug 18 17:13:21 2016 +0200 @@ -21,7 +21,9 @@ [KnownType("DerivedTypes")] public abstract class Object: IComparable { - + /// + /// Static object name. + /// public string Name { //Get the name of this object attribute @@ -29,6 +31,9 @@ private set { } } + /// + /// Static object description. + /// public string Description { //Get the description of this object attribute @@ -36,11 +41,20 @@ private set { } } + /// + /// Dynamic object description. + /// + /// public virtual string Brief() { return Name; } + /// + /// Needed to make sure our sorting makes sense + /// + /// + /// public int CompareTo(object obj) { //Sort by object name @@ -48,6 +62,15 @@ } /// + /// Tells whether the current object configuration is valid. + /// + /// + public virtual bool IsValid() + { + return true; + } + + /// /// So that data contract knows all our types. /// /// diff -r c92587ddabcd -r dd7770b97916 SharpLibEar/PropertyFile.cs --- a/SharpLibEar/PropertyFile.cs Thu Aug 18 14:35:50 2016 +0200 +++ b/SharpLibEar/PropertyFile.cs Thu Aug 18 17:13:21 2016 +0200 @@ -15,6 +15,6 @@ public class PropertyFile : Object { [DataMember] - public string FullPath { get; set; } = ""; + public string FullPath { get; set; } = "Select a file"; } }