Adding send and release CEC key actions.
Edit action ComboBox now sorted and selecting proper item.
2 using System.Collections.Generic;
3 using System.ComponentModel;
5 using System.Diagnostics;
9 using System.Threading.Tasks;
10 using System.Windows.Forms;
11 using SharpLib.Display;
13 using System.Reflection;
15 namespace SharpDisplayManager
18 /// Action edit dialog form.
20 public partial class FormEditAction : Form
22 public SharpLib.Ear.Action Action = null;
24 public FormEditAction()
26 InitializeComponent();
29 private void FormEditAction_Load(object sender, EventArgs e)
31 //Populate registered actions
32 foreach (string key in ManagerEventAction.Current.ActionTypes.Keys)
34 ItemActionType item = new ItemActionType(ManagerEventAction.Current.ActionTypes[key]);
35 comboBoxActionType.Items.Add(item);
38 comboBoxActionType.SelectedIndex = 0;
41 private void buttonOk_Click(object sender, EventArgs e)
43 FetchPropertiesValue(Action);
46 private void FormEditAction_Validating(object sender, CancelEventArgs e)
51 private void comboBoxActionType_SelectedIndexChanged(object sender, EventArgs e)
53 //Instantiate an action corresponding to our type
54 Action = (SharpLib.Ear.Action)Activator.CreateInstance(((ItemActionType)comboBoxActionType.SelectedItem).Type);
57 UpdateTableLayoutPanel(Action);
62 /// Get properties values from our generated input fields
64 private void FetchPropertiesValue(SharpLib.Ear.Action aAction)
67 foreach (PropertyInfo pi in aAction.GetType().GetProperties())
69 AttributeActionProperty[] attributes =
70 ((AttributeActionProperty[]) pi.GetCustomAttributes(typeof(AttributeActionProperty), true));
71 if (attributes.Length != 1)
76 AttributeActionProperty attribute = attributes[0];
78 if (!IsPropertyTypeSupported(pi))
83 GetPropertyValueFromControl(iTableLayoutPanel.Controls[ctrlIndex+1], pi, aAction); //+1 otherwise we get the label
85 ctrlIndex+=2; //Jump over the label too
90 /// Extend this function to support reading new types of properties.
92 /// <param name="aAction"></param>
93 private void GetPropertyValueFromControl(Control aControl, PropertyInfo aInfo, SharpLib.Ear.Action aAction)
95 if (aInfo.PropertyType == typeof(int))
97 NumericUpDown ctrl=(NumericUpDown)aControl;
98 aInfo.SetValue(aAction,(int)ctrl.Value);
100 else if (aInfo.PropertyType.IsEnum)
102 // Instantiate our enum
103 object enumValue= Activator.CreateInstance(aInfo.PropertyType);
104 // Parse our enum from combo box
105 enumValue = Enum.Parse(aInfo.PropertyType,((ComboBox)aControl).SelectedItem.ToString());
106 //enumValue = ((ComboBox)aControl).SelectedValue;
108 aInfo.SetValue(aAction, enumValue);
110 else if (aInfo.PropertyType == typeof(bool))
112 CheckBox ctrl = (CheckBox)aControl;
113 aInfo.SetValue(aAction, ctrl.Checked);
120 /// <param name="aInfo"></param>
121 /// <param name="action"></param>
122 private Control CreateControlForProperty(PropertyInfo aInfo, AttributeActionProperty aAttribute, SharpLib.Ear.Action aAction)
124 if (aInfo.PropertyType == typeof(int))
126 //Integer properties are using numeric editor
127 NumericUpDown ctrl = new NumericUpDown();
128 ctrl.AutoSize = true;
129 ctrl.Minimum = Int32.Parse(aAttribute.Minimum);
130 ctrl.Maximum = Int32.Parse(aAttribute.Maximum);
131 ctrl.Increment = Int32.Parse(aAttribute.Increment);
132 ctrl.Value = (int)aInfo.GetValue(aAction);
135 else if (aInfo.PropertyType.IsEnum)
137 //Enum properties are using combo box
138 ComboBox ctrl = new ComboBox();
139 ctrl.AutoSize = true;
141 ctrl.DropDownStyle = ComboBoxStyle.DropDownList;
142 //Data source is fine but it gives us duplicate entries for duplicated enum values
143 //ctrl.DataSource = Enum.GetValues(aInfo.PropertyType);
145 //Therefore we need to explicitly create our items
146 foreach (string name in aInfo.PropertyType.GetEnumNames())
148 ctrl.Items.Add(name.ToString());
151 // Instantiate our enum
152 object enumValue = Activator.CreateInstance(aInfo.PropertyType);
153 enumValue = aInfo.GetValue(aAction);
154 //Set the current item
155 ctrl.SelectedItem = enumValue.ToString();
159 else if (aInfo.PropertyType == typeof(bool))
161 CheckBox ctrl = new CheckBox();
162 ctrl.AutoSize = true;
163 ctrl.Text = aAttribute.Description;
164 ctrl.Checked = (bool)aInfo.GetValue(aAction);
172 /// Don't forget to extend that one and adding types
174 /// <returns></returns>
175 private bool IsPropertyTypeSupported(PropertyInfo aInfo)
177 if (aInfo.PropertyType == typeof(int))
181 else if (aInfo.PropertyType.IsEnum)
185 else if (aInfo.PropertyType == typeof(bool))
194 /// Update our table layout.
195 /// Will instantiated every field control as defined by our action.
196 /// Fields must be specified by rows from the left.
198 /// <param name="aLayout"></param>
199 private void UpdateTableLayoutPanel(SharpLib.Ear.Action aAction)
202 //Debug.Print("UpdateTableLayoutPanel")
203 //First clean our current panel
204 iTableLayoutPanel.Controls.Clear();
205 iTableLayoutPanel.RowStyles.Clear();
206 iTableLayoutPanel.ColumnStyles.Clear();
207 iTableLayoutPanel.RowCount = 0;
209 //We always want two columns: one for label and one for the field
210 iTableLayoutPanel.ColumnCount = 2;
211 iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
212 iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
221 //IEnumerable<PropertyInfo> properties = aAction.GetType().GetProperties().Where(
222 // prop => Attribute.IsDefined(prop, typeof(AttributeActionProperty)));
225 foreach (PropertyInfo pi in aAction.GetType().GetProperties())
227 AttributeActionProperty[] attributes = ((AttributeActionProperty[])pi.GetCustomAttributes(typeof(AttributeActionProperty), true));
228 if (attributes.Length != 1)
233 AttributeActionProperty attribute = attributes[0];
235 //Before anything we need to check if that kind of property is supported by our UI
237 Control ctrl = CreateControlForProperty(pi, attribute, aAction);
240 //Property type not supported
245 iTableLayoutPanel.RowCount++;
246 iTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
248 Label label = new Label();
249 label.Text = attribute.Name;
250 toolTip.SetToolTip(label, attribute.Description);
251 iTableLayoutPanel.Controls.Add(label, 0, iTableLayoutPanel.RowCount-1);
253 //Add our editor to our form
254 iTableLayoutPanel.Controls.Add(ctrl, 1, iTableLayoutPanel.RowCount - 1);