Published v0.10.2.0.
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();
32 /// <param name="sender"></param>
33 /// <param name="e"></param>
34 private void FormEditAction_Load(object sender, EventArgs e)
36 // Populate registered actions
37 foreach (string key in ManagerEventAction.Current.ActionTypes.Keys)
39 ItemActionType item = new ItemActionType(ManagerEventAction.Current.ActionTypes[key]);
40 comboBoxActionType.Items.Add(item);
45 // Creating new issue, select our first item
46 comboBoxActionType.SelectedIndex = 0;
50 // Editing existing issue
51 // Look up our item in our combobox
52 foreach (ItemActionType item in comboBoxActionType.Items)
54 if (item.Type == Action.GetType())
56 comboBoxActionType.SelectedItem = item;
62 private void buttonOk_Click(object sender, EventArgs e)
64 FetchPropertiesValue(Action);
67 private void FormEditAction_Validating(object sender, CancelEventArgs e)
72 private void comboBoxActionType_SelectedIndexChanged(object sender, EventArgs e)
74 //Instantiate an action corresponding to our type
75 Type actionType = ((ItemActionType) comboBoxActionType.SelectedItem).Type;
76 //Create another type of action only if needed
77 if (Action == null || Action.GetType() != actionType)
79 Action = (SharpLib.Ear.Action)Activator.CreateInstance(actionType);
83 UpdateTableLayoutPanel(Action);
88 /// Get properties values from our generated input fields
90 private void FetchPropertiesValue(SharpLib.Ear.Action aAction)
93 foreach (PropertyInfo pi in aAction.GetType().GetProperties())
95 AttributeActionProperty[] attributes =
96 ((AttributeActionProperty[]) pi.GetCustomAttributes(typeof(AttributeActionProperty), true));
97 if (attributes.Length != 1)
102 AttributeActionProperty attribute = attributes[0];
104 if (!IsPropertyTypeSupported(pi))
109 GetPropertyValueFromControl(iTableLayoutPanel.Controls[ctrlIndex+1], pi, aAction); //+1 otherwise we get the label
111 ctrlIndex+=2; //Jump over the label too
116 /// Extend this function to support reading new types of properties.
118 /// <param name="aAction"></param>
119 private void GetPropertyValueFromControl(Control aControl, PropertyInfo aInfo, SharpLib.Ear.Action aAction)
121 if (aInfo.PropertyType == typeof(int))
123 NumericUpDown ctrl=(NumericUpDown)aControl;
124 aInfo.SetValue(aAction,(int)ctrl.Value);
126 else if (aInfo.PropertyType.IsEnum)
128 // Instantiate our enum
129 object enumValue= Activator.CreateInstance(aInfo.PropertyType);
130 // Parse our enum from combo box
131 enumValue = Enum.Parse(aInfo.PropertyType,((ComboBox)aControl).SelectedItem.ToString());
132 //enumValue = ((ComboBox)aControl).SelectedValue;
134 aInfo.SetValue(aAction, enumValue);
136 else if (aInfo.PropertyType == typeof(bool))
138 CheckBox ctrl = (CheckBox)aControl;
139 aInfo.SetValue(aAction, ctrl.Checked);
141 else if (aInfo.PropertyType == typeof(string))
143 TextBox ctrl = (TextBox)aControl;
144 aInfo.SetValue(aAction, ctrl.Text);
146 //TODO: add support for other types here
152 /// <param name="aInfo"></param>
153 /// <param name="action"></param>
154 private Control CreateControlForProperty(PropertyInfo aInfo, AttributeActionProperty aAttribute, SharpLib.Ear.Action aAction)
156 if (aInfo.PropertyType == typeof(int))
158 //Integer properties are using numeric editor
159 NumericUpDown ctrl = new NumericUpDown();
160 ctrl.AutoSize = true;
161 ctrl.Minimum = Int32.Parse(aAttribute.Minimum);
162 ctrl.Maximum = Int32.Parse(aAttribute.Maximum);
163 ctrl.Increment = Int32.Parse(aAttribute.Increment);
164 ctrl.Value = (int)aInfo.GetValue(aAction);
167 else if (aInfo.PropertyType.IsEnum)
169 //Enum properties are using combo box
170 ComboBox ctrl = new ComboBox();
171 ctrl.AutoSize = true;
173 ctrl.DropDownStyle = ComboBoxStyle.DropDownList;
174 //Data source is fine but it gives us duplicate entries for duplicated enum values
175 //ctrl.DataSource = Enum.GetValues(aInfo.PropertyType);
177 //Therefore we need to explicitly create our items
178 Size cbSize = new Size(0,0);
179 foreach (string name in aInfo.PropertyType.GetEnumNames())
181 ctrl.Items.Add(name.ToString());
182 Graphics g = this.CreateGraphics();
183 //Since combobox autosize would not work we need to get measure text ourselves
184 SizeF size=g.MeasureString(name.ToString(), ctrl.Font);
185 cbSize.Width = Math.Max(cbSize.Width,(int)size.Width);
186 cbSize.Height = Math.Max(cbSize.Height, (int)size.Height);
189 //Make sure our combobox is large enough
190 ctrl.MinimumSize = cbSize;
192 // Instantiate our enum
193 object enumValue = Activator.CreateInstance(aInfo.PropertyType);
194 enumValue = aInfo.GetValue(aAction);
195 //Set the current item
196 ctrl.SelectedItem = enumValue.ToString();
200 else if (aInfo.PropertyType == typeof(bool))
202 CheckBox ctrl = new CheckBox();
203 ctrl.AutoSize = true;
204 ctrl.Text = aAttribute.Description;
205 ctrl.Checked = (bool)aInfo.GetValue(aAction);
208 else if (aInfo.PropertyType == typeof(string))
210 TextBox ctrl = new TextBox();
211 ctrl.AutoSize = true;
212 ctrl.Text = (string)aInfo.GetValue(aAction);
215 //TODO: add support for other control type here
221 /// Don't forget to extend that one and adding types
223 /// <returns></returns>
224 private bool IsPropertyTypeSupported(PropertyInfo aInfo)
226 if (aInfo.PropertyType == typeof(int))
230 else if (aInfo.PropertyType.IsEnum)
234 else if (aInfo.PropertyType == typeof(bool))
238 else if (aInfo.PropertyType == typeof(string))
242 //TODO: add support for other type here
248 /// Update our table layout.
249 /// Will instantiated every field control as defined by our action.
250 /// Fields must be specified by rows from the left.
252 /// <param name="aLayout"></param>
253 private void UpdateTableLayoutPanel(SharpLib.Ear.Action aAction)
256 //Debug.Print("UpdateTableLayoutPanel")
257 //First clean our current panel
258 iTableLayoutPanel.Controls.Clear();
259 iTableLayoutPanel.RowStyles.Clear();
260 iTableLayoutPanel.ColumnStyles.Clear();
261 iTableLayoutPanel.RowCount = 0;
263 //We always want two columns: one for label and one for the field
264 iTableLayoutPanel.ColumnCount = 2;
265 iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
266 iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
275 //IEnumerable<PropertyInfo> properties = aAction.GetType().GetProperties().Where(
276 // prop => Attribute.IsDefined(prop, typeof(AttributeActionProperty)));
279 foreach (PropertyInfo pi in aAction.GetType().GetProperties())
281 AttributeActionProperty[] attributes = ((AttributeActionProperty[])pi.GetCustomAttributes(typeof(AttributeActionProperty), true));
282 if (attributes.Length != 1)
287 AttributeActionProperty attribute = attributes[0];
289 //Before anything we need to check if that kind of property is supported by our UI
291 Control ctrl = CreateControlForProperty(pi, attribute, aAction);
294 //Property type not supported
299 iTableLayoutPanel.RowCount++;
300 iTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
302 Label label = new Label();
303 label.AutoSize = true;
304 label.Dock = DockStyle.Fill;
305 label.TextAlign = ContentAlignment.MiddleCenter;
306 label.Text = attribute.Name;
307 toolTip.SetToolTip(label, attribute.Description);
308 iTableLayoutPanel.Controls.Add(label, 0, iTableLayoutPanel.RowCount-1);
310 //Add our editor to our form
311 iTableLayoutPanel.Controls.Add(ctrl, 1, iTableLayoutPanel.RowCount - 1);
312 //Add tooltip to editor too
313 toolTip.SetToolTip(ctrl, attribute.Description);
319 private void buttonTest_Click(object sender, EventArgs e)
321 FetchPropertiesValue(Action);