# HG changeset patch # User StephaneLenclud # Date 1469538357 -7200 # Node ID e5910d7b6a815195d7de24393b7fc5acac915d81 # Parent 99c407831232a510f2e4c063c7392e893bd5302f Adding support for enumerated action property edition. diff -r 99c407831232 -r e5910d7b6a81 Server/Actions/ActionCecDevice.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Server/Actions/ActionCecDevice.cs Tue Jul 26 15:05:57 2016 +0200 @@ -0,0 +1,26 @@ +using CecSharp; +using SharpLib.Ear; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace SharpDisplayManager +{ + [DataContract] + public abstract class ActionCecDevice: SharpLib.Ear.Action + { + [DataMember] + [AttributeActionProperty + ( + Id = "CEC.Device", + Name = "Device", + Description = "The logical address used by this action." + ) + ] + public CecLogicalAddress Device { get; set; } + } +} diff -r 99c407831232 -r e5910d7b6a81 Server/Actions/ActionCecDevicePowerOn.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Server/Actions/ActionCecDevicePowerOn.cs Tue Jul 26 15:05:57 2016 +0200 @@ -0,0 +1,40 @@ +using CecSharp; +using SharpLib.Ear; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace SharpDisplayManager +{ + [DataContract] + [AttributeAction(Id = "Cec.Device.PowerOn", Name = "CEC Device Power On", Description = "Turns on the specified CEC device on your HDMI bus.")] + public class ActionCecDevicePowerOn : ActionCecDevice + { + /// + /// + /// + /// + public override string Brief() + { + return "CEC Power On " + Device.ToString(); + } + + /// + /// + /// + public override void Execute() + { + if (Cec.Client.Static == null) + { + Console.WriteLine("WARNING: No CEC client installed."); + return; + } + + Cec.Client.Static.Lib.PowerOnDevices(Device); + } + } +} diff -r 99c407831232 -r e5910d7b6a81 Server/Actions/ActionCecDeviceStandby.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Server/Actions/ActionCecDeviceStandby.cs Tue Jul 26 15:05:57 2016 +0200 @@ -0,0 +1,41 @@ +using CecSharp; +using SharpLib.Ear; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace SharpDisplayManager +{ + [DataContract] + [AttributeAction(Id = "Cec.Device.Standby", Name = "CEC Device Standby", Description = "Puts on standby the specified CEC device on your HDMI bus.")] + public class ActionCecDeviceStandby : ActionCecDevice + { + /// + /// + /// + /// + public override string Brief() + { + return "CEC Standby " + Device.ToString(); + } + + /// + /// + /// + public override void Execute() + { + if (Cec.Client.Static == null) + { + Console.WriteLine("WARNING: No CEC client installed."); + return; + } + + Cec.Client.Static.Lib.StandbyDevices(Device); + } + + } +} diff -r 99c407831232 -r e5910d7b6a81 Server/FormEditAction.Designer.cs --- a/Server/FormEditAction.Designer.cs Tue Jul 26 11:51:50 2016 +0200 +++ b/Server/FormEditAction.Designer.cs Tue Jul 26 15:05:57 2016 +0200 @@ -60,7 +60,7 @@ // this.buttonOk.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.buttonOk.DialogResult = System.Windows.Forms.DialogResult.OK; - this.buttonOk.Location = new System.Drawing.Point(12, 347); + this.buttonOk.Location = new System.Drawing.Point(12, 72); this.buttonOk.Name = "buttonOk"; this.buttonOk.Size = new System.Drawing.Size(75, 23); this.buttonOk.TabIndex = 21; @@ -72,7 +72,7 @@ // this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; - this.buttonCancel.Location = new System.Drawing.Point(93, 347); + this.buttonCancel.Location = new System.Drawing.Point(93, 72); this.buttonCancel.Name = "buttonCancel"; this.buttonCancel.Size = new System.Drawing.Size(75, 23); this.buttonCancel.TabIndex = 22; @@ -81,6 +81,10 @@ // // iTableLayoutPanel // + this.iTableLayoutPanel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.iTableLayoutPanel.AutoSize = true; this.iTableLayoutPanel.ColumnCount = 2; this.iTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.iTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); @@ -89,7 +93,7 @@ this.iTableLayoutPanel.RowCount = 2; this.iTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.iTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F)); - this.iTableLayoutPanel.Size = new System.Drawing.Size(373, 114); + this.iTableLayoutPanel.Size = new System.Drawing.Size(373, 16); this.iTableLayoutPanel.TabIndex = 23; // // FormEditAction @@ -97,8 +101,9 @@ this.AcceptButton = this.buttonOk; this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.AutoSize = true; this.CancelButton = this.buttonCancel; - this.ClientSize = new System.Drawing.Size(400, 382); + this.ClientSize = new System.Drawing.Size(400, 107); this.Controls.Add(this.iTableLayoutPanel); this.Controls.Add(this.buttonCancel); this.Controls.Add(this.buttonOk); diff -r 99c407831232 -r e5910d7b6a81 Server/FormEditAction.cs --- a/Server/FormEditAction.cs Tue Jul 26 11:51:50 2016 +0200 +++ b/Server/FormEditAction.cs Tue Jul 26 15:05:57 2016 +0200 @@ -40,7 +40,7 @@ private void buttonOk_Click(object sender, EventArgs e) { - + FetchPropertiesValue(Action); } private void FormEditAction_Validating(object sender, CancelEventArgs e) @@ -57,6 +57,105 @@ UpdateTableLayoutPanel(Action); } + + /// + /// Get properties values from our generated input fields + /// + private void FetchPropertiesValue(SharpLib.Ear.Action aAction) + { + int ctrlIndex = 0; + foreach (PropertyInfo pi in aAction.GetType().GetProperties()) + { + AttributeActionProperty[] attributes = + ((AttributeActionProperty[]) pi.GetCustomAttributes(typeof(AttributeActionProperty), true)); + if (attributes.Length != 1) + { + continue; + } + + AttributeActionProperty attribute = attributes[0]; + + if (!IsPropertyTypeSupported(pi)) + { + continue; + } + + GetPropertyValueFromControl(iTableLayoutPanel.Controls[ctrlIndex+1], pi, aAction); //+1 otherwise we get the label + + ctrlIndex+=2; //Jump over the label too + } + } + + /// + /// Extend this function to support reading new types of properties. + /// + /// + private void GetPropertyValueFromControl(Control aControl, PropertyInfo aInfo, SharpLib.Ear.Action aAction) + { + if (aInfo.PropertyType == typeof(int)) + { + NumericUpDown ctrl=(NumericUpDown)aControl; + aInfo.SetValue(aAction,(int)ctrl.Value); + } + else if (aInfo.PropertyType.IsEnum) + { + // Instantiate our enum + object enumValue= Activator.CreateInstance(aInfo.PropertyType); + // Parse our enum from combo box + //enumValue = Enum.Parse(aInfo.PropertyType,((ComboBox)aControl).SelectedValue.ToString()); + enumValue = ((ComboBox)aControl).SelectedValue; + // Set enum value + aInfo.SetValue(aAction, enumValue); + } + } + + /// + /// + /// + /// + /// + private Control CreateControlForProperty(PropertyInfo aInfo, AttributeActionProperty aAttribute, SharpLib.Ear.Action aAction) + { + if (aInfo.PropertyType == typeof(int)) + { + //Integer properties are using numeric editor + NumericUpDown ctrl = new NumericUpDown(); + ctrl.Minimum = Int32.Parse(aAttribute.Minimum); + ctrl.Maximum = Int32.Parse(aAttribute.Maximum); + ctrl.Increment = Int32.Parse(aAttribute.Increment); + ctrl.Value = (int)aInfo.GetValue(aAction); + return ctrl; + } + else if (aInfo.PropertyType.IsEnum) + { + //Enum properties are using combo box + ComboBox ctrl = new ComboBox(); + ctrl.DropDownStyle = ComboBoxStyle.DropDownList; + ctrl.DataSource = Enum.GetValues(aInfo.PropertyType); + return ctrl; + } + + return null; + } + + /// + /// Don't forget to extend that one and adding types + /// + /// + private bool IsPropertyTypeSupported(PropertyInfo aInfo) + { + if (aInfo.PropertyType == typeof(int)) + { + return true; + } + else if (aInfo.PropertyType.IsEnum) + { + return true; + } + + return false; + } + /// /// Update our table layout. /// Will instantiated every field control as defined by our action. @@ -98,6 +197,16 @@ } AttributeActionProperty attribute = attributes[0]; + + //Before anything we need to check if that kind of property is supported by our UI + //Create the editor + Control ctrl = CreateControlForProperty(pi, attribute, aAction); + if (ctrl == null) + { + //Property type not supported + continue; + } + //Add a new row iTableLayoutPanel.RowCount++; iTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize)); @@ -107,67 +216,11 @@ toolTip.SetToolTip(label, attribute.Description); iTableLayoutPanel.Controls.Add(label, 0, iTableLayoutPanel.RowCount-1); + //Add our editor to our form + iTableLayoutPanel.Controls.Add(ctrl, 1, iTableLayoutPanel.RowCount - 1); - //Create the editor - } - - - /* - //Then recreate our rows... - while (iTableLayoutPanel.RowCount < layout.Rows.Count) - { - iTableLayoutPanel.RowCount++; - } - - // ...and columns - while (iTableLayoutPanel.ColumnCount < layout.Columns.Count) - { - iTableLayoutPanel.ColumnCount++; - } - - //For each column - for (int i = 0; i < iTableLayoutPanel.ColumnCount; i++) - { - //Create our column styles - this.iTableLayoutPanel.ColumnStyles.Add(layout.Columns[i]); - - //For each rows - for (int j = 0; j < iTableLayoutPanel.RowCount; j++) - { - if (i == 0) - { - //Create our row styles - this.iTableLayoutPanel.RowStyles.Add(layout.Rows[j]); - } - else - { - continue; - } - } - } - - //For each field - foreach (DataField field in aClient.Fields) - { - if (!field.IsTableField) - { - //That field is not taking part in our table layout skip it - continue; - } - - TableField tableField = (TableField)field; - - //Create a control corresponding to the field specified for that cell - Control control = CreateControlForDataField(tableField); - - //Add newly created control to our table layout at the specified row and column - iTableLayoutPanel.Controls.Add(control, tableField.Column, tableField.Row); - //Make sure we specify column and row span for that new control - iTableLayoutPanel.SetColumnSpan(control, tableField.ColumnSpan); - iTableLayoutPanel.SetRowSpan(control, tableField.RowSpan); - } - */ + } } diff -r 99c407831232 -r e5910d7b6a81 Server/MainForm.cs --- a/Server/MainForm.cs Tue Jul 26 11:51:50 2016 +0200 +++ b/Server/MainForm.cs Tue Jul 26 15:05:57 2016 +0200 @@ -324,7 +324,7 @@ // Add our actions for that event foreach (SharpLib.Ear.Action a in e.Actions) { - TreeNode actionNode = actionsNodes.Nodes.Add(a.Name); + TreeNode actionNode = actionsNodes.Nodes.Add(a.Brief()); actionNode.Tag = a; } } diff -r 99c407831232 -r e5910d7b6a81 Server/SharpDisplayManager.csproj --- a/Server/SharpDisplayManager.csproj Tue Jul 26 11:51:50 2016 +0200 +++ b/Server/SharpDisplayManager.csproj Tue Jul 26 15:05:57 2016 +0200 @@ -154,12 +154,15 @@ + + + diff -r 99c407831232 -r e5910d7b6a81 SharpLibEar/Action.cs --- a/SharpLibEar/Action.cs Tue Jul 26 11:51:50 2016 +0200 +++ b/SharpLibEar/Action.cs Tue Jul 26 15:05:57 2016 +0200 @@ -20,6 +20,11 @@ private set { } } + public virtual string Brief() + { + return Name; + } + public int CompareTo(object obj) { //Sort by action name diff -r 99c407831232 -r e5910d7b6a81 SharpLibEar/ActionSleep.cs --- a/SharpLibEar/ActionSleep.cs Tue Jul 26 11:51:50 2016 +0200 +++ b/SharpLibEar/ActionSleep.cs Tue Jul 26 15:05:57 2016 +0200 @@ -14,8 +14,16 @@ public class ActionSleep : Action { [DataMember] - [AttributeActionProperty(Id = "Thread.Sleep.Timeout", Name = "Timeout", - Description = "Specifies the number of milliseconds this action will sleep for.")] + [AttributeActionProperty + ( + Id = "Thread.Sleep.Timeout", + Name = "Timeout (ms)", + Description = "Specifies the number of milliseconds this action will sleep for.", + Minimum = "0", + Maximum = "10000", + Increment = "1" + ) + ] public int TimeoutInMilliseconds { get; set; } public ActionSleep() @@ -29,6 +37,12 @@ TimeoutInMilliseconds = aMillisecondsTimeout; } + public override string Brief() + { + return Name + " for " + TimeoutInMilliseconds + " ms"; + } + + public override void Execute() { Thread.Sleep(TimeoutInMilliseconds); diff -r 99c407831232 -r e5910d7b6a81 SharpLibEar/AttributeActionProperty.cs --- a/SharpLibEar/AttributeActionProperty.cs Tue Jul 26 11:51:50 2016 +0200 +++ b/SharpLibEar/AttributeActionProperty.cs Tue Jul 26 15:05:57 2016 +0200 @@ -15,5 +15,13 @@ public string Id; public string Name; public string Description; + // For numerics + public string Minimum; + public string Maximum; + public string Increment; } + + + + }