Adding support for Display Messages.
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);
115 else if (aInfo.PropertyType == typeof(string))
117 TextBox ctrl = (TextBox)aControl;
118 aInfo.SetValue(aAction, ctrl.Text);
120 //TODO: add support for other types here
126 /// <param name="aInfo"></param>
127 /// <param name="action"></param>
128 private Control CreateControlForProperty(PropertyInfo aInfo, AttributeActionProperty aAttribute, SharpLib.Ear.Action aAction)
130 if (aInfo.PropertyType == typeof(int))
132 //Integer properties are using numeric editor
133 NumericUpDown ctrl = new NumericUpDown();
134 ctrl.AutoSize = true;
135 ctrl.Minimum = Int32.Parse(aAttribute.Minimum);
136 ctrl.Maximum = Int32.Parse(aAttribute.Maximum);
137 ctrl.Increment = Int32.Parse(aAttribute.Increment);
138 ctrl.Value = (int)aInfo.GetValue(aAction);
141 else if (aInfo.PropertyType.IsEnum)
143 //Enum properties are using combo box
144 ComboBox ctrl = new ComboBox();
145 ctrl.AutoSize = true;
147 ctrl.DropDownStyle = ComboBoxStyle.DropDownList;
148 //Data source is fine but it gives us duplicate entries for duplicated enum values
149 //ctrl.DataSource = Enum.GetValues(aInfo.PropertyType);
151 //Therefore we need to explicitly create our items
152 Size cbSize = new Size(0,0);
153 foreach (string name in aInfo.PropertyType.GetEnumNames())
155 ctrl.Items.Add(name.ToString());
156 Graphics g = this.CreateGraphics();
157 //Since combobox autosize would not work we need to get measure text ourselves
158 SizeF size=g.MeasureString(name.ToString(), ctrl.Font);
159 cbSize.Width = Math.Max(cbSize.Width,(int)size.Width);
160 cbSize.Height = Math.Max(cbSize.Height, (int)size.Height);
163 //Make sure our combobox is large enough
164 ctrl.MinimumSize = cbSize;
166 // Instantiate our enum
167 object enumValue = Activator.CreateInstance(aInfo.PropertyType);
168 enumValue = aInfo.GetValue(aAction);
169 //Set the current item
170 ctrl.SelectedItem = enumValue.ToString();
174 else if (aInfo.PropertyType == typeof(bool))
176 CheckBox ctrl = new CheckBox();
177 ctrl.AutoSize = true;
178 ctrl.Text = aAttribute.Description;
179 ctrl.Checked = (bool)aInfo.GetValue(aAction);
182 else if (aInfo.PropertyType == typeof(string))
184 TextBox ctrl = new TextBox();
185 ctrl.AutoSize = true;
186 ctrl.Text = (string)aInfo.GetValue(aAction);
189 //TODO: add support for other control type here
195 /// Don't forget to extend that one and adding types
197 /// <returns></returns>
198 private bool IsPropertyTypeSupported(PropertyInfo aInfo)
200 if (aInfo.PropertyType == typeof(int))
204 else if (aInfo.PropertyType.IsEnum)
208 else if (aInfo.PropertyType == typeof(bool))
212 else if (aInfo.PropertyType == typeof(string))
216 //TODO: add support for other type here
222 /// Update our table layout.
223 /// Will instantiated every field control as defined by our action.
224 /// Fields must be specified by rows from the left.
226 /// <param name="aLayout"></param>
227 private void UpdateTableLayoutPanel(SharpLib.Ear.Action aAction)
230 //Debug.Print("UpdateTableLayoutPanel")
231 //First clean our current panel
232 iTableLayoutPanel.Controls.Clear();
233 iTableLayoutPanel.RowStyles.Clear();
234 iTableLayoutPanel.ColumnStyles.Clear();
235 iTableLayoutPanel.RowCount = 0;
237 //We always want two columns: one for label and one for the field
238 iTableLayoutPanel.ColumnCount = 2;
239 iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
240 iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
249 //IEnumerable<PropertyInfo> properties = aAction.GetType().GetProperties().Where(
250 // prop => Attribute.IsDefined(prop, typeof(AttributeActionProperty)));
253 foreach (PropertyInfo pi in aAction.GetType().GetProperties())
255 AttributeActionProperty[] attributes = ((AttributeActionProperty[])pi.GetCustomAttributes(typeof(AttributeActionProperty), true));
256 if (attributes.Length != 1)
261 AttributeActionProperty attribute = attributes[0];
263 //Before anything we need to check if that kind of property is supported by our UI
265 Control ctrl = CreateControlForProperty(pi, attribute, aAction);
268 //Property type not supported
273 iTableLayoutPanel.RowCount++;
274 iTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
276 Label label = new Label();
277 label.AutoSize = true;
278 label.Dock = DockStyle.Fill;
279 label.TextAlign = ContentAlignment.MiddleCenter;
280 label.Text = attribute.Name;
281 toolTip.SetToolTip(label, attribute.Description);
282 iTableLayoutPanel.Controls.Add(label, 0, iTableLayoutPanel.RowCount-1);
284 //Add our editor to our form
285 iTableLayoutPanel.Controls.Add(ctrl, 1, iTableLayoutPanel.RowCount - 1);
286 //Add tooltip to editor too
287 toolTip.SetToolTip(ctrl, attribute.Description);