StephaneLenclud@211
|
1 |
using System;
|
StephaneLenclud@211
|
2 |
using System.Collections.Generic;
|
StephaneLenclud@211
|
3 |
using System.ComponentModel;
|
StephaneLenclud@211
|
4 |
using System.Data;
|
StephaneLenclud@219
|
5 |
using System.Diagnostics;
|
StephaneLenclud@211
|
6 |
using System.Drawing;
|
StephaneLenclud@211
|
7 |
using System.Linq;
|
StephaneLenclud@211
|
8 |
using System.Text;
|
StephaneLenclud@211
|
9 |
using System.Threading.Tasks;
|
StephaneLenclud@211
|
10 |
using System.Windows.Forms;
|
StephaneLenclud@219
|
11 |
using SharpLib.Display;
|
StephaneLenclud@211
|
12 |
using SharpLib.Ear;
|
StephaneLenclud@219
|
13 |
using System.Reflection;
|
StephaneLenclud@211
|
14 |
|
StephaneLenclud@211
|
15 |
namespace SharpDisplayManager
|
StephaneLenclud@211
|
16 |
{
|
StephaneLenclud@214
|
17 |
/// <summary>
|
StephaneLenclud@214
|
18 |
/// Action edit dialog form.
|
StephaneLenclud@214
|
19 |
/// </summary>
|
StephaneLenclud@211
|
20 |
public partial class FormEditAction : Form
|
StephaneLenclud@211
|
21 |
{
|
StephaneLenclud@211
|
22 |
public SharpLib.Ear.Action Action = null;
|
StephaneLenclud@211
|
23 |
|
StephaneLenclud@211
|
24 |
public FormEditAction()
|
StephaneLenclud@211
|
25 |
{
|
StephaneLenclud@211
|
26 |
InitializeComponent();
|
StephaneLenclud@211
|
27 |
}
|
StephaneLenclud@211
|
28 |
|
StephaneLenclud@211
|
29 |
private void FormEditAction_Load(object sender, EventArgs e)
|
StephaneLenclud@211
|
30 |
{
|
StephaneLenclud@211
|
31 |
//Populate registered actions
|
Stephane@212
|
32 |
foreach (string key in ManagerEventAction.Current.ActionTypes.Keys)
|
StephaneLenclud@211
|
33 |
{
|
StephaneLenclud@214
|
34 |
ItemActionType item = new ItemActionType(ManagerEventAction.Current.ActionTypes[key]);
|
StephaneLenclud@214
|
35 |
comboBoxActionType.Items.Add(item);
|
StephaneLenclud@211
|
36 |
}
|
StephaneLenclud@211
|
37 |
|
StephaneLenclud@211
|
38 |
comboBoxActionType.SelectedIndex = 0;
|
StephaneLenclud@211
|
39 |
}
|
StephaneLenclud@211
|
40 |
|
StephaneLenclud@211
|
41 |
private void buttonOk_Click(object sender, EventArgs e)
|
StephaneLenclud@211
|
42 |
{
|
StephaneLenclud@220
|
43 |
FetchPropertiesValue(Action);
|
StephaneLenclud@211
|
44 |
}
|
StephaneLenclud@211
|
45 |
|
StephaneLenclud@211
|
46 |
private void FormEditAction_Validating(object sender, CancelEventArgs e)
|
StephaneLenclud@211
|
47 |
{
|
StephaneLenclud@211
|
48 |
|
StephaneLenclud@211
|
49 |
}
|
StephaneLenclud@219
|
50 |
|
StephaneLenclud@219
|
51 |
private void comboBoxActionType_SelectedIndexChanged(object sender, EventArgs e)
|
StephaneLenclud@219
|
52 |
{
|
StephaneLenclud@219
|
53 |
//Instantiate an action corresponding to our type
|
StephaneLenclud@219
|
54 |
Action = (SharpLib.Ear.Action)Activator.CreateInstance(((ItemActionType)comboBoxActionType.SelectedItem).Type);
|
StephaneLenclud@219
|
55 |
|
StephaneLenclud@219
|
56 |
//Create input fields
|
StephaneLenclud@219
|
57 |
UpdateTableLayoutPanel(Action);
|
StephaneLenclud@219
|
58 |
}
|
StephaneLenclud@219
|
59 |
|
StephaneLenclud@220
|
60 |
|
StephaneLenclud@220
|
61 |
/// <summary>
|
StephaneLenclud@220
|
62 |
/// Get properties values from our generated input fields
|
StephaneLenclud@220
|
63 |
/// </summary>
|
StephaneLenclud@220
|
64 |
private void FetchPropertiesValue(SharpLib.Ear.Action aAction)
|
StephaneLenclud@220
|
65 |
{
|
StephaneLenclud@220
|
66 |
int ctrlIndex = 0;
|
StephaneLenclud@220
|
67 |
foreach (PropertyInfo pi in aAction.GetType().GetProperties())
|
StephaneLenclud@220
|
68 |
{
|
StephaneLenclud@220
|
69 |
AttributeActionProperty[] attributes =
|
StephaneLenclud@220
|
70 |
((AttributeActionProperty[]) pi.GetCustomAttributes(typeof(AttributeActionProperty), true));
|
StephaneLenclud@220
|
71 |
if (attributes.Length != 1)
|
StephaneLenclud@220
|
72 |
{
|
StephaneLenclud@220
|
73 |
continue;
|
StephaneLenclud@220
|
74 |
}
|
StephaneLenclud@220
|
75 |
|
StephaneLenclud@220
|
76 |
AttributeActionProperty attribute = attributes[0];
|
StephaneLenclud@220
|
77 |
|
StephaneLenclud@220
|
78 |
if (!IsPropertyTypeSupported(pi))
|
StephaneLenclud@220
|
79 |
{
|
StephaneLenclud@220
|
80 |
continue;
|
StephaneLenclud@220
|
81 |
}
|
StephaneLenclud@220
|
82 |
|
StephaneLenclud@220
|
83 |
GetPropertyValueFromControl(iTableLayoutPanel.Controls[ctrlIndex+1], pi, aAction); //+1 otherwise we get the label
|
StephaneLenclud@220
|
84 |
|
StephaneLenclud@220
|
85 |
ctrlIndex+=2; //Jump over the label too
|
StephaneLenclud@220
|
86 |
}
|
StephaneLenclud@220
|
87 |
}
|
StephaneLenclud@220
|
88 |
|
StephaneLenclud@220
|
89 |
/// <summary>
|
StephaneLenclud@220
|
90 |
/// Extend this function to support reading new types of properties.
|
StephaneLenclud@220
|
91 |
/// </summary>
|
StephaneLenclud@220
|
92 |
/// <param name="aAction"></param>
|
StephaneLenclud@220
|
93 |
private void GetPropertyValueFromControl(Control aControl, PropertyInfo aInfo, SharpLib.Ear.Action aAction)
|
StephaneLenclud@220
|
94 |
{
|
StephaneLenclud@220
|
95 |
if (aInfo.PropertyType == typeof(int))
|
StephaneLenclud@220
|
96 |
{
|
StephaneLenclud@220
|
97 |
NumericUpDown ctrl=(NumericUpDown)aControl;
|
StephaneLenclud@220
|
98 |
aInfo.SetValue(aAction,(int)ctrl.Value);
|
StephaneLenclud@220
|
99 |
}
|
StephaneLenclud@220
|
100 |
else if (aInfo.PropertyType.IsEnum)
|
StephaneLenclud@220
|
101 |
{
|
StephaneLenclud@220
|
102 |
// Instantiate our enum
|
StephaneLenclud@220
|
103 |
object enumValue= Activator.CreateInstance(aInfo.PropertyType);
|
StephaneLenclud@220
|
104 |
// Parse our enum from combo box
|
StephaneLenclud@221
|
105 |
enumValue = Enum.Parse(aInfo.PropertyType,((ComboBox)aControl).SelectedItem.ToString());
|
StephaneLenclud@221
|
106 |
//enumValue = ((ComboBox)aControl).SelectedValue;
|
StephaneLenclud@220
|
107 |
// Set enum value
|
StephaneLenclud@220
|
108 |
aInfo.SetValue(aAction, enumValue);
|
StephaneLenclud@220
|
109 |
}
|
StephaneLenclud@222
|
110 |
else if (aInfo.PropertyType == typeof(bool))
|
StephaneLenclud@222
|
111 |
{
|
StephaneLenclud@222
|
112 |
CheckBox ctrl = (CheckBox)aControl;
|
StephaneLenclud@222
|
113 |
aInfo.SetValue(aAction, ctrl.Checked);
|
StephaneLenclud@222
|
114 |
}
|
StephaneLenclud@225
|
115 |
else if (aInfo.PropertyType == typeof(string))
|
StephaneLenclud@225
|
116 |
{
|
StephaneLenclud@225
|
117 |
TextBox ctrl = (TextBox)aControl;
|
StephaneLenclud@225
|
118 |
aInfo.SetValue(aAction, ctrl.Text);
|
StephaneLenclud@225
|
119 |
}
|
StephaneLenclud@225
|
120 |
//TODO: add support for other types here
|
StephaneLenclud@220
|
121 |
}
|
StephaneLenclud@220
|
122 |
|
StephaneLenclud@220
|
123 |
/// <summary>
|
StephaneLenclud@220
|
124 |
///
|
StephaneLenclud@220
|
125 |
/// </summary>
|
StephaneLenclud@220
|
126 |
/// <param name="aInfo"></param>
|
StephaneLenclud@220
|
127 |
/// <param name="action"></param>
|
StephaneLenclud@220
|
128 |
private Control CreateControlForProperty(PropertyInfo aInfo, AttributeActionProperty aAttribute, SharpLib.Ear.Action aAction)
|
StephaneLenclud@220
|
129 |
{
|
StephaneLenclud@220
|
130 |
if (aInfo.PropertyType == typeof(int))
|
StephaneLenclud@220
|
131 |
{
|
StephaneLenclud@220
|
132 |
//Integer properties are using numeric editor
|
StephaneLenclud@222
|
133 |
NumericUpDown ctrl = new NumericUpDown();
|
StephaneLenclud@222
|
134 |
ctrl.AutoSize = true;
|
StephaneLenclud@220
|
135 |
ctrl.Minimum = Int32.Parse(aAttribute.Minimum);
|
StephaneLenclud@220
|
136 |
ctrl.Maximum = Int32.Parse(aAttribute.Maximum);
|
StephaneLenclud@220
|
137 |
ctrl.Increment = Int32.Parse(aAttribute.Increment);
|
StephaneLenclud@220
|
138 |
ctrl.Value = (int)aInfo.GetValue(aAction);
|
StephaneLenclud@220
|
139 |
return ctrl;
|
StephaneLenclud@220
|
140 |
}
|
StephaneLenclud@220
|
141 |
else if (aInfo.PropertyType.IsEnum)
|
StephaneLenclud@220
|
142 |
{
|
StephaneLenclud@220
|
143 |
//Enum properties are using combo box
|
StephaneLenclud@220
|
144 |
ComboBox ctrl = new ComboBox();
|
StephaneLenclud@224
|
145 |
ctrl.AutoSize = true;
|
StephaneLenclud@224
|
146 |
ctrl.Sorted = true;
|
StephaneLenclud@220
|
147 |
ctrl.DropDownStyle = ComboBoxStyle.DropDownList;
|
StephaneLenclud@221
|
148 |
//Data source is fine but it gives us duplicate entries for duplicated enum values
|
StephaneLenclud@221
|
149 |
//ctrl.DataSource = Enum.GetValues(aInfo.PropertyType);
|
StephaneLenclud@221
|
150 |
|
StephaneLenclud@223
|
151 |
//Therefore we need to explicitly create our items
|
StephaneLenclud@224
|
152 |
Size cbSize = new Size(0,0);
|
StephaneLenclud@221
|
153 |
foreach (string name in aInfo.PropertyType.GetEnumNames())
|
StephaneLenclud@221
|
154 |
{
|
StephaneLenclud@221
|
155 |
ctrl.Items.Add(name.ToString());
|
StephaneLenclud@224
|
156 |
Graphics g = this.CreateGraphics();
|
StephaneLenclud@224
|
157 |
//Since combobox autosize would not work we need to get measure text ourselves
|
StephaneLenclud@224
|
158 |
SizeF size=g.MeasureString(name.ToString(), ctrl.Font);
|
StephaneLenclud@224
|
159 |
cbSize.Width = Math.Max(cbSize.Width,(int)size.Width);
|
StephaneLenclud@224
|
160 |
cbSize.Height = Math.Max(cbSize.Height, (int)size.Height);
|
StephaneLenclud@221
|
161 |
}
|
StephaneLenclud@221
|
162 |
|
StephaneLenclud@224
|
163 |
//Make sure our combobox is large enough
|
StephaneLenclud@224
|
164 |
ctrl.MinimumSize = cbSize;
|
StephaneLenclud@224
|
165 |
|
StephaneLenclud@222
|
166 |
// Instantiate our enum
|
StephaneLenclud@222
|
167 |
object enumValue = Activator.CreateInstance(aInfo.PropertyType);
|
StephaneLenclud@222
|
168 |
enumValue = aInfo.GetValue(aAction);
|
StephaneLenclud@222
|
169 |
//Set the current item
|
StephaneLenclud@222
|
170 |
ctrl.SelectedItem = enumValue.ToString();
|
StephaneLenclud@221
|
171 |
|
StephaneLenclud@220
|
172 |
return ctrl;
|
StephaneLenclud@220
|
173 |
}
|
StephaneLenclud@222
|
174 |
else if (aInfo.PropertyType == typeof(bool))
|
StephaneLenclud@222
|
175 |
{
|
StephaneLenclud@222
|
176 |
CheckBox ctrl = new CheckBox();
|
StephaneLenclud@222
|
177 |
ctrl.AutoSize = true;
|
StephaneLenclud@222
|
178 |
ctrl.Text = aAttribute.Description;
|
StephaneLenclud@222
|
179 |
ctrl.Checked = (bool)aInfo.GetValue(aAction);
|
StephaneLenclud@222
|
180 |
return ctrl;
|
StephaneLenclud@222
|
181 |
}
|
StephaneLenclud@225
|
182 |
else if (aInfo.PropertyType == typeof(string))
|
StephaneLenclud@225
|
183 |
{
|
StephaneLenclud@225
|
184 |
TextBox ctrl = new TextBox();
|
StephaneLenclud@225
|
185 |
ctrl.AutoSize = true;
|
StephaneLenclud@225
|
186 |
ctrl.Text = (string)aInfo.GetValue(aAction);
|
StephaneLenclud@225
|
187 |
return ctrl;
|
StephaneLenclud@225
|
188 |
}
|
StephaneLenclud@225
|
189 |
//TODO: add support for other control type here
|
StephaneLenclud@220
|
190 |
|
StephaneLenclud@220
|
191 |
return null;
|
StephaneLenclud@220
|
192 |
}
|
StephaneLenclud@220
|
193 |
|
StephaneLenclud@220
|
194 |
/// <summary>
|
StephaneLenclud@220
|
195 |
/// Don't forget to extend that one and adding types
|
StephaneLenclud@220
|
196 |
/// </summary>
|
StephaneLenclud@220
|
197 |
/// <returns></returns>
|
StephaneLenclud@220
|
198 |
private bool IsPropertyTypeSupported(PropertyInfo aInfo)
|
StephaneLenclud@220
|
199 |
{
|
StephaneLenclud@220
|
200 |
if (aInfo.PropertyType == typeof(int))
|
StephaneLenclud@220
|
201 |
{
|
StephaneLenclud@220
|
202 |
return true;
|
StephaneLenclud@220
|
203 |
}
|
StephaneLenclud@220
|
204 |
else if (aInfo.PropertyType.IsEnum)
|
StephaneLenclud@220
|
205 |
{
|
StephaneLenclud@220
|
206 |
return true;
|
StephaneLenclud@220
|
207 |
}
|
StephaneLenclud@222
|
208 |
else if (aInfo.PropertyType == typeof(bool))
|
StephaneLenclud@222
|
209 |
{
|
StephaneLenclud@222
|
210 |
return true;
|
StephaneLenclud@222
|
211 |
}
|
StephaneLenclud@225
|
212 |
else if (aInfo.PropertyType == typeof(string))
|
StephaneLenclud@225
|
213 |
{
|
StephaneLenclud@225
|
214 |
return true;
|
StephaneLenclud@225
|
215 |
}
|
StephaneLenclud@225
|
216 |
//TODO: add support for other type here
|
StephaneLenclud@220
|
217 |
|
StephaneLenclud@220
|
218 |
return false;
|
StephaneLenclud@220
|
219 |
}
|
StephaneLenclud@220
|
220 |
|
StephaneLenclud@219
|
221 |
/// <summary>
|
StephaneLenclud@219
|
222 |
/// Update our table layout.
|
StephaneLenclud@219
|
223 |
/// Will instantiated every field control as defined by our action.
|
StephaneLenclud@219
|
224 |
/// Fields must be specified by rows from the left.
|
StephaneLenclud@219
|
225 |
/// </summary>
|
StephaneLenclud@219
|
226 |
/// <param name="aLayout"></param>
|
StephaneLenclud@219
|
227 |
private void UpdateTableLayoutPanel(SharpLib.Ear.Action aAction)
|
StephaneLenclud@219
|
228 |
{
|
StephaneLenclud@219
|
229 |
toolTip.RemoveAll();
|
StephaneLenclud@219
|
230 |
//Debug.Print("UpdateTableLayoutPanel")
|
StephaneLenclud@219
|
231 |
//First clean our current panel
|
StephaneLenclud@219
|
232 |
iTableLayoutPanel.Controls.Clear();
|
StephaneLenclud@219
|
233 |
iTableLayoutPanel.RowStyles.Clear();
|
StephaneLenclud@219
|
234 |
iTableLayoutPanel.ColumnStyles.Clear();
|
StephaneLenclud@219
|
235 |
iTableLayoutPanel.RowCount = 0;
|
StephaneLenclud@219
|
236 |
|
StephaneLenclud@219
|
237 |
//We always want two columns: one for label and one for the field
|
StephaneLenclud@219
|
238 |
iTableLayoutPanel.ColumnCount = 2;
|
StephaneLenclud@219
|
239 |
iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
|
StephaneLenclud@219
|
240 |
iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
|
StephaneLenclud@219
|
241 |
|
StephaneLenclud@219
|
242 |
|
StephaneLenclud@219
|
243 |
if (aAction == null)
|
StephaneLenclud@219
|
244 |
{
|
StephaneLenclud@219
|
245 |
//Just drop it
|
StephaneLenclud@219
|
246 |
return;
|
StephaneLenclud@219
|
247 |
}
|
StephaneLenclud@219
|
248 |
|
StephaneLenclud@219
|
249 |
//IEnumerable<PropertyInfo> properties = aAction.GetType().GetProperties().Where(
|
StephaneLenclud@219
|
250 |
// prop => Attribute.IsDefined(prop, typeof(AttributeActionProperty)));
|
StephaneLenclud@219
|
251 |
|
StephaneLenclud@219
|
252 |
|
StephaneLenclud@219
|
253 |
foreach (PropertyInfo pi in aAction.GetType().GetProperties())
|
StephaneLenclud@219
|
254 |
{
|
StephaneLenclud@219
|
255 |
AttributeActionProperty[] attributes = ((AttributeActionProperty[])pi.GetCustomAttributes(typeof(AttributeActionProperty), true));
|
StephaneLenclud@219
|
256 |
if (attributes.Length != 1)
|
StephaneLenclud@219
|
257 |
{
|
StephaneLenclud@219
|
258 |
continue;
|
StephaneLenclud@219
|
259 |
}
|
StephaneLenclud@219
|
260 |
|
StephaneLenclud@219
|
261 |
AttributeActionProperty attribute = attributes[0];
|
StephaneLenclud@220
|
262 |
|
StephaneLenclud@220
|
263 |
//Before anything we need to check if that kind of property is supported by our UI
|
StephaneLenclud@220
|
264 |
//Create the editor
|
StephaneLenclud@220
|
265 |
Control ctrl = CreateControlForProperty(pi, attribute, aAction);
|
StephaneLenclud@220
|
266 |
if (ctrl == null)
|
StephaneLenclud@220
|
267 |
{
|
StephaneLenclud@220
|
268 |
//Property type not supported
|
StephaneLenclud@220
|
269 |
continue;
|
StephaneLenclud@220
|
270 |
}
|
StephaneLenclud@220
|
271 |
|
StephaneLenclud@219
|
272 |
//Add a new row
|
StephaneLenclud@219
|
273 |
iTableLayoutPanel.RowCount++;
|
StephaneLenclud@219
|
274 |
iTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
|
StephaneLenclud@219
|
275 |
//Create the label
|
StephaneLenclud@219
|
276 |
Label label = new Label();
|
StephaneLenclud@224
|
277 |
label.AutoSize = true;
|
StephaneLenclud@224
|
278 |
label.Dock = DockStyle.Fill;
|
StephaneLenclud@224
|
279 |
label.TextAlign = ContentAlignment.MiddleCenter;
|
StephaneLenclud@219
|
280 |
label.Text = attribute.Name;
|
StephaneLenclud@219
|
281 |
toolTip.SetToolTip(label, attribute.Description);
|
StephaneLenclud@219
|
282 |
iTableLayoutPanel.Controls.Add(label, 0, iTableLayoutPanel.RowCount-1);
|
StephaneLenclud@219
|
283 |
|
StephaneLenclud@220
|
284 |
//Add our editor to our form
|
StephaneLenclud@220
|
285 |
iTableLayoutPanel.Controls.Add(ctrl, 1, iTableLayoutPanel.RowCount - 1);
|
StephaneLenclud@223
|
286 |
//Add tooltip to editor too
|
StephaneLenclud@223
|
287 |
toolTip.SetToolTip(ctrl, attribute.Description);
|
StephaneLenclud@219
|
288 |
|
StephaneLenclud@220
|
289 |
}
|
StephaneLenclud@219
|
290 |
|
StephaneLenclud@219
|
291 |
}
|
StephaneLenclud@219
|
292 |
|
StephaneLenclud@211
|
293 |
}
|
StephaneLenclud@211
|
294 |
}
|