Optical drive eject action now functional.
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;
16 using CodeProject.Dialog;
19 namespace SharpDisplayManager
22 /// Object edit dialog form.
24 public partial class FormEditObject<T> : Form where T: SharpLib.Ear.Object
26 public T Object = null;
28 public FormEditObject()
30 InitializeComponent();
36 /// <param name="sender"></param>
37 /// <param name="e"></param>
38 private void FormEditAction_Load(object sender, EventArgs e)
40 // Populate registered object types
41 IEnumerable < Type > types = Reflection.GetConcreteClassesDerivedFrom<T>();
42 foreach (Type type in types)
44 ItemObjectType item = new ItemObjectType(type);
45 comboBoxActionType.Items.Add(item);
50 // Creating new issue, select our first item
51 comboBoxActionType.SelectedIndex = 0;
55 // Editing existing object
56 // Look up our item in our combobox
57 foreach (ItemObjectType item in comboBoxActionType.Items)
59 if (item.Type == Object.GetType())
61 comboBoxActionType.SelectedItem = item;
67 private void buttonOk_Click(object sender, EventArgs e)
69 FetchPropertiesValue(Object);
70 if (!Object.IsValid())
72 // Tell for closing event to abort
73 DialogResult = DialogResult.None;
78 private void FormEditObject_FormClosing(object sender, FormClosingEventArgs e)
80 e.Cancel = DialogResult == DialogResult.None;
83 private void comboBoxActionType_SelectedIndexChanged(object sender, EventArgs e)
85 //Instantiate an action corresponding to our type
86 Type actionType = ((ItemObjectType) comboBoxActionType.SelectedItem).Type;
87 //Create another type of action only if needed
88 if (Object == null || Object.GetType() != actionType)
90 Object = (T)Activator.CreateInstance(actionType);
93 //Disable ok button if our object is not valid
94 buttonOk.Enabled = Object.IsValid();
97 UpdateTableLayoutPanel(Object);
102 /// Get properties values from our generated input fields
104 private void FetchPropertiesValue(T aObject)
107 //For each of our properties
108 foreach (PropertyInfo pi in aObject.GetType().GetProperties())
110 //Get our property attribute
111 AttributeObjectProperty[] attributes = ((AttributeObjectProperty[]) pi.GetCustomAttributes(typeof(AttributeObjectProperty), true));
112 if (attributes.Length != 1)
114 //No attribute, skip this property then.
117 AttributeObjectProperty attribute = attributes[0];
119 //Check that we support this type of property
120 if (!IsPropertyTypeSupported(pi))
125 //Now fetch our property value
126 GetPropertyValueFromControl(iTableLayoutPanel.Controls[ctrlIndex+1], pi, aObject); //+1 otherwise we get the label
128 ctrlIndex+=2; //Jump over the label too
133 /// Extend this function to support reading new types of properties.
135 /// <param name="aObject"></param>
136 private void GetPropertyValueFromControl(Control aControl, PropertyInfo aInfo, T aObject)
138 if (aInfo.PropertyType == typeof(int))
140 NumericUpDown ctrl=(NumericUpDown)aControl;
141 aInfo.SetValue(aObject,(int)ctrl.Value);
143 else if (aInfo.PropertyType.IsEnum)
145 // Instantiate our enum
146 object enumValue= Activator.CreateInstance(aInfo.PropertyType);
147 // Parse our enum from combo box
148 enumValue = Enum.Parse(aInfo.PropertyType,((ComboBox)aControl).SelectedItem.ToString());
149 //enumValue = ((ComboBox)aControl).SelectedValue;
151 aInfo.SetValue(aObject, enumValue);
153 else if (aInfo.PropertyType == typeof(bool))
155 CheckBox ctrl = (CheckBox)aControl;
156 aInfo.SetValue(aObject, ctrl.Checked);
158 else if (aInfo.PropertyType == typeof(string))
160 TextBox ctrl = (TextBox)aControl;
161 aInfo.SetValue(aObject, ctrl.Text);
163 else if (aInfo.PropertyType == typeof(PropertyFile))
165 Button ctrl = (Button)aControl;
166 PropertyFile value = new PropertyFile {FullPath=ctrl.Text};
167 aInfo.SetValue(aObject, value);
169 else if (aInfo.PropertyType == typeof(PropertyComboBox))
171 ComboBox ctrl = (ComboBox)aControl;
172 string currentItem = ctrl.SelectedItem.ToString();
173 PropertyComboBox pcb = (PropertyComboBox)aInfo.GetValue(aObject);
174 pcb.CurrentItem = currentItem;
177 //TODO: add support for other types here
182 /// Create a control for the given property.
184 /// <param name="aInfo"></param>
185 /// <param name="aAttribute"></param>
186 /// <param name="aObject"></param>
187 /// <returns></returns>
188 private Control CreateControlForProperty(PropertyInfo aInfo, AttributeObjectProperty aAttribute, T aObject)
190 if (aInfo.PropertyType == typeof(int))
192 //Integer properties are using numeric editor
193 NumericUpDown ctrl = new NumericUpDown();
194 ctrl.AutoSize = true;
195 ctrl.Minimum = Int32.Parse(aAttribute.Minimum);
196 ctrl.Maximum = Int32.Parse(aAttribute.Maximum);
197 ctrl.Increment = Int32.Parse(aAttribute.Increment);
198 ctrl.Value = (int)aInfo.GetValue(aObject);
201 else if (aInfo.PropertyType.IsEnum)
203 //Enum properties are using combo box
204 ComboBox ctrl = new ComboBox();
205 ctrl.AutoSize = true;
207 ctrl.DropDownStyle = ComboBoxStyle.DropDownList;
208 //Data source is fine but it gives us duplicate entries for duplicated enum values
209 //ctrl.DataSource = Enum.GetValues(aInfo.PropertyType);
211 //Therefore we need to explicitly create our items
212 Size cbSize = new Size(0, 0);
213 foreach (string name in aInfo.PropertyType.GetEnumNames())
215 ctrl.Items.Add(name.ToString());
216 Graphics g = this.CreateGraphics();
217 //Since combobox autosize would not work we need to get measure text ourselves
218 SizeF size = g.MeasureString(name.ToString(), ctrl.Font);
219 cbSize.Width = Math.Max(cbSize.Width, (int)size.Width);
220 cbSize.Height = Math.Max(cbSize.Height, (int)size.Height);
223 //Make sure our combobox is large enough
224 ctrl.MinimumSize = cbSize;
226 // Instantiate our enum
227 object enumValue = Activator.CreateInstance(aInfo.PropertyType);
228 enumValue = aInfo.GetValue(aObject);
229 //Set the current item
230 ctrl.SelectedItem = enumValue.ToString();
234 else if (aInfo.PropertyType == typeof(bool))
236 CheckBox ctrl = new CheckBox();
237 ctrl.AutoSize = true;
238 ctrl.Text = aAttribute.Description;
239 ctrl.Checked = (bool)aInfo.GetValue(aObject);
242 else if (aInfo.PropertyType == typeof(string))
244 TextBox ctrl = new TextBox();
245 ctrl.AutoSize = true;
246 ctrl.Text = (string)aInfo.GetValue(aObject);
249 else if (aInfo.PropertyType == typeof(PropertyFile))
251 // We have a file property
252 // Create a button that will trigger the open file dialog to select our file.
253 Button ctrl = new Button();
254 ctrl.AutoSize = true;
255 ctrl.Text = ((PropertyFile)aInfo.GetValue(aObject)).FullPath;
257 // Add lambda expression to Click event
258 ctrl.Click += (sender, e) =>
260 // Create open file dialog
261 OpenFileDialog ofd = new OpenFileDialog();
262 ofd.RestoreDirectory = true;
263 // Use file filter specified by our property
264 ofd.Filter = aAttribute.Filter;
266 if (DlgBox.ShowDialog(ofd) == DialogResult.OK)
268 // Fetch selected file name
269 ctrl.Text = ofd.FileName;
270 //Enable Ok button then
271 buttonOk.Enabled = Object.IsValid();
277 else if (aInfo.PropertyType == typeof(PropertyComboBox))
280 ComboBox ctrl = new ComboBox();
281 ctrl.AutoSize = true;
283 ctrl.DropDownStyle = ComboBoxStyle.DropDownList;
284 //Data source is such a pain to set the current item
285 //ctrl.DataSource = ((PropertyComboBox)aInfo.GetValue(aObject)).Items;
287 PropertyComboBox pcb = ((PropertyComboBox)aInfo.GetValue(aObject));
288 foreach (string item in pcb.Items)
290 ctrl.Items.Add(item);
293 ctrl.SelectedItem = ((PropertyComboBox)aInfo.GetValue(aObject)).CurrentItem;
297 //TODO: add support for other control type here
302 /// Don't forget to extend that one and adding types
304 /// <returns></returns>
305 private bool IsPropertyTypeSupported(PropertyInfo aInfo)
307 if (aInfo.PropertyType == typeof(int))
311 else if (aInfo.PropertyType.IsEnum)
315 else if (aInfo.PropertyType == typeof(bool))
319 else if (aInfo.PropertyType == typeof(string))
323 else if (aInfo.PropertyType == typeof(PropertyFile))
327 else if (aInfo.PropertyType == typeof(PropertyComboBox))
332 //TODO: add support for other type here
338 /// Update our table layout.
339 /// Will instantiated every field control as defined by our action.
340 /// Fields must be specified by rows from the left.
342 /// <param name="aLayout"></param>
343 private void UpdateTableLayoutPanel(T aObject)
346 //Debug.Print("UpdateTableLayoutPanel")
347 //First clean our current panel
348 iTableLayoutPanel.Controls.Clear();
349 iTableLayoutPanel.RowStyles.Clear();
350 iTableLayoutPanel.ColumnStyles.Clear();
351 iTableLayoutPanel.RowCount = 0;
353 //We always want two columns: one for label and one for the field
354 iTableLayoutPanel.ColumnCount = 2;
355 iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
356 iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
365 //IEnumerable<PropertyInfo> properties = aObject.GetType().GetProperties().Where(
366 // prop => Attribute.IsDefined(prop, typeof(AttributeObjectProperty)));
369 foreach (PropertyInfo pi in aObject.GetType().GetProperties())
371 AttributeObjectProperty[] attributes = ((AttributeObjectProperty[])pi.GetCustomAttributes(typeof(AttributeObjectProperty), true));
372 if (attributes.Length != 1)
377 AttributeObjectProperty attribute = attributes[0];
379 //Before anything we need to check if that kind of property is supported by our UI
381 Control ctrl = CreateControlForProperty(pi, attribute, aObject);
384 //Property type not supported
389 iTableLayoutPanel.RowCount++;
390 iTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
392 Label label = new Label();
393 label.AutoSize = true;
394 label.Dock = DockStyle.Fill;
395 label.TextAlign = ContentAlignment.MiddleCenter;
396 label.Text = attribute.Name;
397 toolTip.SetToolTip(label, attribute.Description);
398 iTableLayoutPanel.Controls.Add(label, 0, iTableLayoutPanel.RowCount-1);
400 //Add our editor to our form
401 iTableLayoutPanel.Controls.Add(ctrl, 1, iTableLayoutPanel.RowCount - 1);
402 //Add tooltip to editor too
403 toolTip.SetToolTip(ctrl, attribute.Description);
409 private void buttonTest_Click(object sender, EventArgs e)
411 FetchPropertiesValue(Object);
413 //If our object has a test method with no parameters just run it then
414 MethodInfo info = Object.GetType().GetMethod("Test");
415 if ( info != null && info.GetParameters().Length==0)
417 info.Invoke(Object,null);