Removing JSON type converter from ear manager.
Using bindings for some application settings.
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;
14 using Microsoft.VisualBasic.CompilerServices;
17 namespace SharpDisplayManager
20 /// Object edit dialog form.
22 public partial class FormEditObject<T> : Form where T : class
24 public T Object = null;
26 public FormEditObject()
28 InitializeComponent();
34 /// <param name="sender"></param>
35 /// <param name="e"></param>
36 private void FormEditAction_Load(object sender, EventArgs e)
38 // Populate registered actions
39 IEnumerable < Type > types = Reflection.GetConcreteClassesDerivedFrom<T>();
40 foreach (Type type in types)
42 ItemObjectType item = new ItemObjectType(type);
43 comboBoxActionType.Items.Add(item);
48 // Creating new issue, select our first item
49 comboBoxActionType.SelectedIndex = 0;
53 // Editing existing object
54 // Look up our item in our combobox
55 foreach (ItemObjectType item in comboBoxActionType.Items)
57 if (item.Type == Object.GetType())
59 comboBoxActionType.SelectedItem = item;
65 private void buttonOk_Click(object sender, EventArgs e)
67 FetchPropertiesValue(Object);
70 private void FormEditAction_Validating(object sender, CancelEventArgs e)
75 private void comboBoxActionType_SelectedIndexChanged(object sender, EventArgs e)
77 //Instantiate an action corresponding to our type
78 Type actionType = ((ItemObjectType) comboBoxActionType.SelectedItem).Type;
79 //Create another type of action only if needed
80 if (Object == null || Object.GetType() != actionType)
82 Object = (T)Activator.CreateInstance(actionType);
86 UpdateTableLayoutPanel(Object);
91 /// Get properties values from our generated input fields
93 private void FetchPropertiesValue(T aAction)
96 foreach (PropertyInfo pi in aAction.GetType().GetProperties())
98 AttributeObjectProperty[] attributes =
99 ((AttributeObjectProperty[]) pi.GetCustomAttributes(typeof(AttributeObjectProperty), true));
100 if (attributes.Length != 1)
105 AttributeObjectProperty attribute = attributes[0];
107 if (!IsPropertyTypeSupported(pi))
112 GetPropertyValueFromControl(iTableLayoutPanel.Controls[ctrlIndex+1], pi, aAction); //+1 otherwise we get the label
114 ctrlIndex+=2; //Jump over the label too
119 /// Extend this function to support reading new types of properties.
121 /// <param name="aAction"></param>
122 private void GetPropertyValueFromControl(Control aControl, PropertyInfo aInfo, T aAction)
124 if (aInfo.PropertyType == typeof(int))
126 NumericUpDown ctrl=(NumericUpDown)aControl;
127 aInfo.SetValue(aAction,(int)ctrl.Value);
129 else if (aInfo.PropertyType.IsEnum)
131 // Instantiate our enum
132 object enumValue= Activator.CreateInstance(aInfo.PropertyType);
133 // Parse our enum from combo box
134 enumValue = Enum.Parse(aInfo.PropertyType,((ComboBox)aControl).SelectedItem.ToString());
135 //enumValue = ((ComboBox)aControl).SelectedValue;
137 aInfo.SetValue(aAction, enumValue);
139 else if (aInfo.PropertyType == typeof(bool))
141 CheckBox ctrl = (CheckBox)aControl;
142 aInfo.SetValue(aAction, ctrl.Checked);
144 else if (aInfo.PropertyType == typeof(string))
146 TextBox ctrl = (TextBox)aControl;
147 aInfo.SetValue(aAction, ctrl.Text);
149 //TODO: add support for other types here
155 /// <param name="aInfo"></param>
156 /// <param name="action"></param>
157 private Control CreateControlForProperty(PropertyInfo aInfo, AttributeObjectProperty aAttribute, T aAction)
159 if (aInfo.PropertyType == typeof(int))
161 //Integer properties are using numeric editor
162 NumericUpDown ctrl = new NumericUpDown();
163 ctrl.AutoSize = true;
164 ctrl.Minimum = Int32.Parse(aAttribute.Minimum);
165 ctrl.Maximum = Int32.Parse(aAttribute.Maximum);
166 ctrl.Increment = Int32.Parse(aAttribute.Increment);
167 ctrl.Value = (int)aInfo.GetValue(aAction);
170 else if (aInfo.PropertyType.IsEnum)
172 //Enum properties are using combo box
173 ComboBox ctrl = new ComboBox();
174 ctrl.AutoSize = true;
176 ctrl.DropDownStyle = ComboBoxStyle.DropDownList;
177 //Data source is fine but it gives us duplicate entries for duplicated enum values
178 //ctrl.DataSource = Enum.GetValues(aInfo.PropertyType);
180 //Therefore we need to explicitly create our items
181 Size cbSize = new Size(0,0);
182 foreach (string name in aInfo.PropertyType.GetEnumNames())
184 ctrl.Items.Add(name.ToString());
185 Graphics g = this.CreateGraphics();
186 //Since combobox autosize would not work we need to get measure text ourselves
187 SizeF size=g.MeasureString(name.ToString(), ctrl.Font);
188 cbSize.Width = Math.Max(cbSize.Width,(int)size.Width);
189 cbSize.Height = Math.Max(cbSize.Height, (int)size.Height);
192 //Make sure our combobox is large enough
193 ctrl.MinimumSize = cbSize;
195 // Instantiate our enum
196 object enumValue = Activator.CreateInstance(aInfo.PropertyType);
197 enumValue = aInfo.GetValue(aAction);
198 //Set the current item
199 ctrl.SelectedItem = enumValue.ToString();
203 else if (aInfo.PropertyType == typeof(bool))
205 CheckBox ctrl = new CheckBox();
206 ctrl.AutoSize = true;
207 ctrl.Text = aAttribute.Description;
208 ctrl.Checked = (bool)aInfo.GetValue(aAction);
211 else if (aInfo.PropertyType == typeof(string))
213 TextBox ctrl = new TextBox();
214 ctrl.AutoSize = true;
215 ctrl.Text = (string)aInfo.GetValue(aAction);
218 //TODO: add support for other control type here
224 /// Don't forget to extend that one and adding types
226 /// <returns></returns>
227 private bool IsPropertyTypeSupported(PropertyInfo aInfo)
229 if (aInfo.PropertyType == typeof(int))
233 else if (aInfo.PropertyType.IsEnum)
237 else if (aInfo.PropertyType == typeof(bool))
241 else if (aInfo.PropertyType == typeof(string))
245 //TODO: add support for other type here
251 /// Update our table layout.
252 /// Will instantiated every field control as defined by our action.
253 /// Fields must be specified by rows from the left.
255 /// <param name="aLayout"></param>
256 private void UpdateTableLayoutPanel(T aAction)
259 //Debug.Print("UpdateTableLayoutPanel")
260 //First clean our current panel
261 iTableLayoutPanel.Controls.Clear();
262 iTableLayoutPanel.RowStyles.Clear();
263 iTableLayoutPanel.ColumnStyles.Clear();
264 iTableLayoutPanel.RowCount = 0;
266 //We always want two columns: one for label and one for the field
267 iTableLayoutPanel.ColumnCount = 2;
268 iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
269 iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
278 //IEnumerable<PropertyInfo> properties = aAction.GetType().GetProperties().Where(
279 // prop => Attribute.IsDefined(prop, typeof(AttributeObjectProperty)));
282 foreach (PropertyInfo pi in aAction.GetType().GetProperties())
284 AttributeObjectProperty[] attributes = ((AttributeObjectProperty[])pi.GetCustomAttributes(typeof(AttributeObjectProperty), true));
285 if (attributes.Length != 1)
290 AttributeObjectProperty attribute = attributes[0];
292 //Before anything we need to check if that kind of property is supported by our UI
294 Control ctrl = CreateControlForProperty(pi, attribute, aAction);
297 //Property type not supported
302 iTableLayoutPanel.RowCount++;
303 iTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
305 Label label = new Label();
306 label.AutoSize = true;
307 label.Dock = DockStyle.Fill;
308 label.TextAlign = ContentAlignment.MiddleCenter;
309 label.Text = attribute.Name;
310 toolTip.SetToolTip(label, attribute.Description);
311 iTableLayoutPanel.Controls.Add(label, 0, iTableLayoutPanel.RowCount-1);
313 //Add our editor to our form
314 iTableLayoutPanel.Controls.Add(ctrl, 1, iTableLayoutPanel.RowCount - 1);
315 //Add tooltip to editor too
316 toolTip.SetToolTip(ctrl, attribute.Description);
322 private void buttonTest_Click(object sender, EventArgs e)
324 FetchPropertiesValue(Object);
326 //If our object has a test method with no parameters just run it then
327 MethodInfo info = Object.GetType().GetMethod("Test");
328 if ( info != null && info.GetParameters().Length==0)
330 info.Invoke(Object,null);