1.1 --- a/Server/FormEditAction.cs Sun Jul 31 12:03:52 2016 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,325 +0,0 @@
1.4 -using System;
1.5 -using System.Collections.Generic;
1.6 -using System.ComponentModel;
1.7 -using System.Data;
1.8 -using System.Diagnostics;
1.9 -using System.Drawing;
1.10 -using System.Linq;
1.11 -using System.Text;
1.12 -using System.Threading.Tasks;
1.13 -using System.Windows.Forms;
1.14 -using SharpLib.Display;
1.15 -using SharpLib.Ear;
1.16 -using System.Reflection;
1.17 -
1.18 -namespace SharpDisplayManager
1.19 -{
1.20 - /// <summary>
1.21 - /// Action edit dialog form.
1.22 - /// </summary>
1.23 - public partial class FormEditAction : Form
1.24 - {
1.25 - public SharpLib.Ear.Action Action = null;
1.26 -
1.27 - public FormEditAction()
1.28 - {
1.29 - InitializeComponent();
1.30 - }
1.31 -
1.32 - /// <summary>
1.33 - ///
1.34 - /// </summary>
1.35 - /// <param name="sender"></param>
1.36 - /// <param name="e"></param>
1.37 - private void FormEditAction_Load(object sender, EventArgs e)
1.38 - {
1.39 - // Populate registered actions
1.40 - foreach (string key in ManagerEventAction.Current.ActionTypes.Keys)
1.41 - {
1.42 - ItemActionType item = new ItemActionType(ManagerEventAction.Current.ActionTypes[key]);
1.43 - comboBoxActionType.Items.Add(item);
1.44 - }
1.45 -
1.46 - if (Action == null)
1.47 - {
1.48 - // Creating new issue, select our first item
1.49 - comboBoxActionType.SelectedIndex = 0;
1.50 - }
1.51 - else
1.52 - {
1.53 - // Editing existing issue
1.54 - // Look up our item in our combobox
1.55 - foreach (ItemActionType item in comboBoxActionType.Items)
1.56 - {
1.57 - if (item.Type == Action.GetType())
1.58 - {
1.59 - comboBoxActionType.SelectedItem = item;
1.60 - }
1.61 - }
1.62 - }
1.63 - }
1.64 -
1.65 - private void buttonOk_Click(object sender, EventArgs e)
1.66 - {
1.67 - FetchPropertiesValue(Action);
1.68 - }
1.69 -
1.70 - private void FormEditAction_Validating(object sender, CancelEventArgs e)
1.71 - {
1.72 -
1.73 - }
1.74 -
1.75 - private void comboBoxActionType_SelectedIndexChanged(object sender, EventArgs e)
1.76 - {
1.77 - //Instantiate an action corresponding to our type
1.78 - Type actionType = ((ItemActionType) comboBoxActionType.SelectedItem).Type;
1.79 - //Create another type of action only if needed
1.80 - if (Action == null || Action.GetType() != actionType)
1.81 - {
1.82 - Action = (SharpLib.Ear.Action)Activator.CreateInstance(actionType);
1.83 - }
1.84 -
1.85 - //Create input fields
1.86 - UpdateTableLayoutPanel(Action);
1.87 - }
1.88 -
1.89 -
1.90 - /// <summary>
1.91 - /// Get properties values from our generated input fields
1.92 - /// </summary>
1.93 - private void FetchPropertiesValue(SharpLib.Ear.Action aAction)
1.94 - {
1.95 - int ctrlIndex = 0;
1.96 - foreach (PropertyInfo pi in aAction.GetType().GetProperties())
1.97 - {
1.98 - AttributeActionProperty[] attributes =
1.99 - ((AttributeActionProperty[]) pi.GetCustomAttributes(typeof(AttributeActionProperty), true));
1.100 - if (attributes.Length != 1)
1.101 - {
1.102 - continue;
1.103 - }
1.104 -
1.105 - AttributeActionProperty attribute = attributes[0];
1.106 -
1.107 - if (!IsPropertyTypeSupported(pi))
1.108 - {
1.109 - continue;
1.110 - }
1.111 -
1.112 - GetPropertyValueFromControl(iTableLayoutPanel.Controls[ctrlIndex+1], pi, aAction); //+1 otherwise we get the label
1.113 -
1.114 - ctrlIndex+=2; //Jump over the label too
1.115 - }
1.116 - }
1.117 -
1.118 - /// <summary>
1.119 - /// Extend this function to support reading new types of properties.
1.120 - /// </summary>
1.121 - /// <param name="aAction"></param>
1.122 - private void GetPropertyValueFromControl(Control aControl, PropertyInfo aInfo, SharpLib.Ear.Action aAction)
1.123 - {
1.124 - if (aInfo.PropertyType == typeof(int))
1.125 - {
1.126 - NumericUpDown ctrl=(NumericUpDown)aControl;
1.127 - aInfo.SetValue(aAction,(int)ctrl.Value);
1.128 - }
1.129 - else if (aInfo.PropertyType.IsEnum)
1.130 - {
1.131 - // Instantiate our enum
1.132 - object enumValue= Activator.CreateInstance(aInfo.PropertyType);
1.133 - // Parse our enum from combo box
1.134 - enumValue = Enum.Parse(aInfo.PropertyType,((ComboBox)aControl).SelectedItem.ToString());
1.135 - //enumValue = ((ComboBox)aControl).SelectedValue;
1.136 - // Set enum value
1.137 - aInfo.SetValue(aAction, enumValue);
1.138 - }
1.139 - else if (aInfo.PropertyType == typeof(bool))
1.140 - {
1.141 - CheckBox ctrl = (CheckBox)aControl;
1.142 - aInfo.SetValue(aAction, ctrl.Checked);
1.143 - }
1.144 - else if (aInfo.PropertyType == typeof(string))
1.145 - {
1.146 - TextBox ctrl = (TextBox)aControl;
1.147 - aInfo.SetValue(aAction, ctrl.Text);
1.148 - }
1.149 - //TODO: add support for other types here
1.150 - }
1.151 -
1.152 - /// <summary>
1.153 - ///
1.154 - /// </summary>
1.155 - /// <param name="aInfo"></param>
1.156 - /// <param name="action"></param>
1.157 - private Control CreateControlForProperty(PropertyInfo aInfo, AttributeActionProperty aAttribute, SharpLib.Ear.Action aAction)
1.158 - {
1.159 - if (aInfo.PropertyType == typeof(int))
1.160 - {
1.161 - //Integer properties are using numeric editor
1.162 - NumericUpDown ctrl = new NumericUpDown();
1.163 - ctrl.AutoSize = true;
1.164 - ctrl.Minimum = Int32.Parse(aAttribute.Minimum);
1.165 - ctrl.Maximum = Int32.Parse(aAttribute.Maximum);
1.166 - ctrl.Increment = Int32.Parse(aAttribute.Increment);
1.167 - ctrl.Value = (int)aInfo.GetValue(aAction);
1.168 - return ctrl;
1.169 - }
1.170 - else if (aInfo.PropertyType.IsEnum)
1.171 - {
1.172 - //Enum properties are using combo box
1.173 - ComboBox ctrl = new ComboBox();
1.174 - ctrl.AutoSize = true;
1.175 - ctrl.Sorted = true;
1.176 - ctrl.DropDownStyle = ComboBoxStyle.DropDownList;
1.177 - //Data source is fine but it gives us duplicate entries for duplicated enum values
1.178 - //ctrl.DataSource = Enum.GetValues(aInfo.PropertyType);
1.179 -
1.180 - //Therefore we need to explicitly create our items
1.181 - Size cbSize = new Size(0,0);
1.182 - foreach (string name in aInfo.PropertyType.GetEnumNames())
1.183 - {
1.184 - ctrl.Items.Add(name.ToString());
1.185 - Graphics g = this.CreateGraphics();
1.186 - //Since combobox autosize would not work we need to get measure text ourselves
1.187 - SizeF size=g.MeasureString(name.ToString(), ctrl.Font);
1.188 - cbSize.Width = Math.Max(cbSize.Width,(int)size.Width);
1.189 - cbSize.Height = Math.Max(cbSize.Height, (int)size.Height);
1.190 - }
1.191 -
1.192 - //Make sure our combobox is large enough
1.193 - ctrl.MinimumSize = cbSize;
1.194 -
1.195 - // Instantiate our enum
1.196 - object enumValue = Activator.CreateInstance(aInfo.PropertyType);
1.197 - enumValue = aInfo.GetValue(aAction);
1.198 - //Set the current item
1.199 - ctrl.SelectedItem = enumValue.ToString();
1.200 -
1.201 - return ctrl;
1.202 - }
1.203 - else if (aInfo.PropertyType == typeof(bool))
1.204 - {
1.205 - CheckBox ctrl = new CheckBox();
1.206 - ctrl.AutoSize = true;
1.207 - ctrl.Text = aAttribute.Description;
1.208 - ctrl.Checked = (bool)aInfo.GetValue(aAction);
1.209 - return ctrl;
1.210 - }
1.211 - else if (aInfo.PropertyType == typeof(string))
1.212 - {
1.213 - TextBox ctrl = new TextBox();
1.214 - ctrl.AutoSize = true;
1.215 - ctrl.Text = (string)aInfo.GetValue(aAction);
1.216 - return ctrl;
1.217 - }
1.218 - //TODO: add support for other control type here
1.219 -
1.220 - return null;
1.221 - }
1.222 -
1.223 - /// <summary>
1.224 - /// Don't forget to extend that one and adding types
1.225 - /// </summary>
1.226 - /// <returns></returns>
1.227 - private bool IsPropertyTypeSupported(PropertyInfo aInfo)
1.228 - {
1.229 - if (aInfo.PropertyType == typeof(int))
1.230 - {
1.231 - return true;
1.232 - }
1.233 - else if (aInfo.PropertyType.IsEnum)
1.234 - {
1.235 - return true;
1.236 - }
1.237 - else if (aInfo.PropertyType == typeof(bool))
1.238 - {
1.239 - return true;
1.240 - }
1.241 - else if (aInfo.PropertyType == typeof(string))
1.242 - {
1.243 - return true;
1.244 - }
1.245 - //TODO: add support for other type here
1.246 -
1.247 - return false;
1.248 - }
1.249 -
1.250 - /// <summary>
1.251 - /// Update our table layout.
1.252 - /// Will instantiated every field control as defined by our action.
1.253 - /// Fields must be specified by rows from the left.
1.254 - /// </summary>
1.255 - /// <param name="aLayout"></param>
1.256 - private void UpdateTableLayoutPanel(SharpLib.Ear.Action aAction)
1.257 - {
1.258 - toolTip.RemoveAll();
1.259 - //Debug.Print("UpdateTableLayoutPanel")
1.260 - //First clean our current panel
1.261 - iTableLayoutPanel.Controls.Clear();
1.262 - iTableLayoutPanel.RowStyles.Clear();
1.263 - iTableLayoutPanel.ColumnStyles.Clear();
1.264 - iTableLayoutPanel.RowCount = 0;
1.265 -
1.266 - //We always want two columns: one for label and one for the field
1.267 - iTableLayoutPanel.ColumnCount = 2;
1.268 - iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
1.269 - iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
1.270 -
1.271 -
1.272 - if (aAction == null)
1.273 - {
1.274 - //Just drop it
1.275 - return;
1.276 - }
1.277 -
1.278 - //IEnumerable<PropertyInfo> properties = aAction.GetType().GetProperties().Where(
1.279 - // prop => Attribute.IsDefined(prop, typeof(AttributeActionProperty)));
1.280 -
1.281 -
1.282 - foreach (PropertyInfo pi in aAction.GetType().GetProperties())
1.283 - {
1.284 - AttributeActionProperty[] attributes = ((AttributeActionProperty[])pi.GetCustomAttributes(typeof(AttributeActionProperty), true));
1.285 - if (attributes.Length != 1)
1.286 - {
1.287 - continue;
1.288 - }
1.289 -
1.290 - AttributeActionProperty attribute = attributes[0];
1.291 -
1.292 - //Before anything we need to check if that kind of property is supported by our UI
1.293 - //Create the editor
1.294 - Control ctrl = CreateControlForProperty(pi, attribute, aAction);
1.295 - if (ctrl == null)
1.296 - {
1.297 - //Property type not supported
1.298 - continue;
1.299 - }
1.300 -
1.301 - //Add a new row
1.302 - iTableLayoutPanel.RowCount++;
1.303 - iTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
1.304 - //Create the label
1.305 - Label label = new Label();
1.306 - label.AutoSize = true;
1.307 - label.Dock = DockStyle.Fill;
1.308 - label.TextAlign = ContentAlignment.MiddleCenter;
1.309 - label.Text = attribute.Name;
1.310 - toolTip.SetToolTip(label, attribute.Description);
1.311 - iTableLayoutPanel.Controls.Add(label, 0, iTableLayoutPanel.RowCount-1);
1.312 -
1.313 - //Add our editor to our form
1.314 - iTableLayoutPanel.Controls.Add(ctrl, 1, iTableLayoutPanel.RowCount - 1);
1.315 - //Add tooltip to editor too
1.316 - toolTip.SetToolTip(ctrl, attribute.Description);
1.317 -
1.318 - }
1.319 -
1.320 - }
1.321 -
1.322 - private void buttonTest_Click(object sender, EventArgs e)
1.323 - {
1.324 - FetchPropertiesValue(Action);
1.325 - Action.Execute();
1.326 - }
1.327 - }
1.328 -}