StephaneLenclud@231
|
1 |
using System;
|
StephaneLenclud@231
|
2 |
using System.Collections.Generic;
|
StephaneLenclud@231
|
3 |
using System.ComponentModel;
|
StephaneLenclud@231
|
4 |
using System.Data;
|
StephaneLenclud@231
|
5 |
using System.Diagnostics;
|
StephaneLenclud@231
|
6 |
using System.Drawing;
|
StephaneLenclud@231
|
7 |
using System.Linq;
|
StephaneLenclud@231
|
8 |
using System.Text;
|
StephaneLenclud@231
|
9 |
using System.Threading.Tasks;
|
StephaneLenclud@231
|
10 |
using System.Windows.Forms;
|
StephaneLenclud@231
|
11 |
using SharpLib.Display;
|
StephaneLenclud@231
|
12 |
using SharpLib.Ear;
|
StephaneLenclud@231
|
13 |
using System.Reflection;
|
StephaneLenclud@231
|
14 |
using Microsoft.VisualBasic.CompilerServices;
|
StephaneLenclud@231
|
15 |
using SharpLib.Utils;
|
StephaneLenclud@238
|
16 |
using CodeProject.Dialog;
|
StephaneLenclud@239
|
17 |
using System.IO;
|
StephaneLenclud@231
|
18 |
|
StephaneLenclud@231
|
19 |
namespace SharpDisplayManager
|
StephaneLenclud@231
|
20 |
{
|
StephaneLenclud@231
|
21 |
/// <summary>
|
StephaneLenclud@231
|
22 |
/// Object edit dialog form.
|
StephaneLenclud@231
|
23 |
/// </summary>
|
StephaneLenclud@239
|
24 |
public partial class FormEditObject<T> : Form where T: SharpLib.Ear.Object
|
StephaneLenclud@231
|
25 |
{
|
StephaneLenclud@231
|
26 |
public T Object = null;
|
StephaneLenclud@231
|
27 |
|
StephaneLenclud@231
|
28 |
public FormEditObject()
|
StephaneLenclud@231
|
29 |
{
|
StephaneLenclud@231
|
30 |
InitializeComponent();
|
StephaneLenclud@231
|
31 |
}
|
StephaneLenclud@231
|
32 |
|
StephaneLenclud@231
|
33 |
/// <summary>
|
StephaneLenclud@231
|
34 |
///
|
StephaneLenclud@231
|
35 |
/// </summary>
|
StephaneLenclud@231
|
36 |
/// <param name="sender"></param>
|
StephaneLenclud@231
|
37 |
/// <param name="e"></param>
|
StephaneLenclud@231
|
38 |
private void FormEditAction_Load(object sender, EventArgs e)
|
StephaneLenclud@231
|
39 |
{
|
StephaneLenclud@236
|
40 |
// Populate registered object types
|
StephaneLenclud@231
|
41 |
IEnumerable < Type > types = Reflection.GetConcreteClassesDerivedFrom<T>();
|
StephaneLenclud@231
|
42 |
foreach (Type type in types)
|
StephaneLenclud@231
|
43 |
{
|
StephaneLenclud@231
|
44 |
ItemObjectType item = new ItemObjectType(type);
|
StephaneLenclud@247
|
45 |
iComboBoxObjectType.Items.Add(item);
|
StephaneLenclud@231
|
46 |
}
|
StephaneLenclud@231
|
47 |
|
StephaneLenclud@231
|
48 |
if (Object == null)
|
StephaneLenclud@231
|
49 |
{
|
StephaneLenclud@231
|
50 |
// Creating new issue, select our first item
|
StephaneLenclud@247
|
51 |
iComboBoxObjectType.SelectedIndex = 0;
|
StephaneLenclud@231
|
52 |
}
|
StephaneLenclud@231
|
53 |
else
|
StephaneLenclud@231
|
54 |
{
|
StephaneLenclud@231
|
55 |
// Editing existing object
|
StephaneLenclud@246
|
56 |
// Look up our item in our object type combobox
|
StephaneLenclud@247
|
57 |
foreach (ItemObjectType item in iComboBoxObjectType.Items)
|
StephaneLenclud@231
|
58 |
{
|
StephaneLenclud@231
|
59 |
if (item.Type == Object.GetType())
|
StephaneLenclud@231
|
60 |
{
|
StephaneLenclud@247
|
61 |
iComboBoxObjectType.SelectedItem = item;
|
StephaneLenclud@231
|
62 |
}
|
StephaneLenclud@231
|
63 |
}
|
StephaneLenclud@246
|
64 |
|
StephaneLenclud@239
|
65 |
}
|
StephaneLenclud@231
|
66 |
}
|
StephaneLenclud@231
|
67 |
|
StephaneLenclud@247
|
68 |
/// <summary>
|
StephaneLenclud@247
|
69 |
///
|
StephaneLenclud@247
|
70 |
/// </summary>
|
StephaneLenclud@247
|
71 |
/// <param name="sender"></param>
|
StephaneLenclud@247
|
72 |
/// <param name="e"></param>
|
StephaneLenclud@231
|
73 |
private void buttonOk_Click(object sender, EventArgs e)
|
StephaneLenclud@231
|
74 |
{
|
StephaneLenclud@231
|
75 |
FetchPropertiesValue(Object);
|
StephaneLenclud@239
|
76 |
if (!Object.IsValid())
|
StephaneLenclud@239
|
77 |
{
|
StephaneLenclud@247
|
78 |
// Tell closing event to cancel
|
StephaneLenclud@239
|
79 |
DialogResult = DialogResult.None;
|
StephaneLenclud@239
|
80 |
}
|
StephaneLenclud@231
|
81 |
}
|
StephaneLenclud@231
|
82 |
|
StephaneLenclud@239
|
83 |
|
StephaneLenclud@247
|
84 |
/// <summary>
|
StephaneLenclud@247
|
85 |
///
|
StephaneLenclud@247
|
86 |
/// </summary>
|
StephaneLenclud@247
|
87 |
/// <param name="sender"></param>
|
StephaneLenclud@247
|
88 |
/// <param name="e"></param>
|
StephaneLenclud@239
|
89 |
private void FormEditObject_FormClosing(object sender, FormClosingEventArgs e)
|
StephaneLenclud@231
|
90 |
{
|
StephaneLenclud@247
|
91 |
//Check if we need to cancel the closing of our form.
|
StephaneLenclud@239
|
92 |
e.Cancel = DialogResult == DialogResult.None;
|
StephaneLenclud@246
|
93 |
|
StephaneLenclud@246
|
94 |
if (!e.Cancel)
|
StephaneLenclud@246
|
95 |
{
|
StephaneLenclud@246
|
96 |
//Exit edit mode
|
StephaneLenclud@246
|
97 |
Object.CurrentState = SharpLib.Ear.Object.State.Rest;
|
StephaneLenclud@246
|
98 |
Object.PropertyChanged -= PropertyChangedEventHandlerThreadSafe;
|
StephaneLenclud@246
|
99 |
}
|
StephaneLenclud@231
|
100 |
}
|
StephaneLenclud@231
|
101 |
|
StephaneLenclud@247
|
102 |
/// <summary>
|
StephaneLenclud@247
|
103 |
///
|
StephaneLenclud@247
|
104 |
/// </summary>
|
StephaneLenclud@247
|
105 |
/// <param name="sender"></param>
|
StephaneLenclud@247
|
106 |
/// <param name="e"></param>
|
StephaneLenclud@231
|
107 |
private void comboBoxActionType_SelectedIndexChanged(object sender, EventArgs e)
|
StephaneLenclud@231
|
108 |
{
|
StephaneLenclud@231
|
109 |
//Instantiate an action corresponding to our type
|
StephaneLenclud@247
|
110 |
Type actionType = ((ItemObjectType) iComboBoxObjectType.SelectedItem).Type;
|
StephaneLenclud@231
|
111 |
//Create another type of action only if needed
|
StephaneLenclud@231
|
112 |
if (Object == null || Object.GetType() != actionType)
|
StephaneLenclud@231
|
113 |
{
|
StephaneLenclud@231
|
114 |
Object = (T)Activator.CreateInstance(actionType);
|
StephaneLenclud@231
|
115 |
}
|
StephaneLenclud@239
|
116 |
|
StephaneLenclud@231
|
117 |
//Create input fields
|
StephaneLenclud@247
|
118 |
UpdateControls();
|
StephaneLenclud@231
|
119 |
}
|
StephaneLenclud@231
|
120 |
|
StephaneLenclud@231
|
121 |
|
StephaneLenclud@231
|
122 |
/// <summary>
|
StephaneLenclud@231
|
123 |
/// Get properties values from our generated input fields
|
StephaneLenclud@231
|
124 |
/// </summary>
|
StephaneLenclud@236
|
125 |
private void FetchPropertiesValue(T aObject)
|
StephaneLenclud@231
|
126 |
{
|
StephaneLenclud@231
|
127 |
int ctrlIndex = 0;
|
Stephane@243
|
128 |
//For each of our properties
|
StephaneLenclud@236
|
129 |
foreach (PropertyInfo pi in aObject.GetType().GetProperties())
|
StephaneLenclud@231
|
130 |
{
|
Stephane@243
|
131 |
//Get our property attribute
|
Stephane@243
|
132 |
AttributeObjectProperty[] attributes = ((AttributeObjectProperty[]) pi.GetCustomAttributes(typeof(AttributeObjectProperty), true));
|
StephaneLenclud@231
|
133 |
if (attributes.Length != 1)
|
StephaneLenclud@231
|
134 |
{
|
Stephane@243
|
135 |
//No attribute, skip this property then.
|
StephaneLenclud@231
|
136 |
continue;
|
StephaneLenclud@231
|
137 |
}
|
StephaneLenclud@231
|
138 |
AttributeObjectProperty attribute = attributes[0];
|
StephaneLenclud@231
|
139 |
|
Stephane@243
|
140 |
//Check that we support this type of property
|
StephaneLenclud@231
|
141 |
if (!IsPropertyTypeSupported(pi))
|
StephaneLenclud@231
|
142 |
{
|
StephaneLenclud@231
|
143 |
continue;
|
StephaneLenclud@231
|
144 |
}
|
StephaneLenclud@231
|
145 |
|
Stephane@243
|
146 |
//Now fetch our property value
|
StephaneLenclud@236
|
147 |
GetPropertyValueFromControl(iTableLayoutPanel.Controls[ctrlIndex+1], pi, aObject); //+1 otherwise we get the label
|
StephaneLenclud@231
|
148 |
|
StephaneLenclud@231
|
149 |
ctrlIndex+=2; //Jump over the label too
|
StephaneLenclud@231
|
150 |
}
|
StephaneLenclud@231
|
151 |
}
|
StephaneLenclud@231
|
152 |
|
StephaneLenclud@231
|
153 |
/// <summary>
|
StephaneLenclud@231
|
154 |
/// Extend this function to support reading new types of properties.
|
StephaneLenclud@231
|
155 |
/// </summary>
|
StephaneLenclud@236
|
156 |
/// <param name="aObject"></param>
|
StephaneLenclud@236
|
157 |
private void GetPropertyValueFromControl(Control aControl, PropertyInfo aInfo, T aObject)
|
StephaneLenclud@231
|
158 |
{
|
StephaneLenclud@231
|
159 |
if (aInfo.PropertyType == typeof(int))
|
StephaneLenclud@231
|
160 |
{
|
StephaneLenclud@231
|
161 |
NumericUpDown ctrl=(NumericUpDown)aControl;
|
StephaneLenclud@236
|
162 |
aInfo.SetValue(aObject,(int)ctrl.Value);
|
StephaneLenclud@231
|
163 |
}
|
StephaneLenclud@231
|
164 |
else if (aInfo.PropertyType.IsEnum)
|
StephaneLenclud@231
|
165 |
{
|
StephaneLenclud@231
|
166 |
// Instantiate our enum
|
StephaneLenclud@231
|
167 |
object enumValue= Activator.CreateInstance(aInfo.PropertyType);
|
StephaneLenclud@231
|
168 |
// Parse our enum from combo box
|
StephaneLenclud@231
|
169 |
enumValue = Enum.Parse(aInfo.PropertyType,((ComboBox)aControl).SelectedItem.ToString());
|
StephaneLenclud@231
|
170 |
//enumValue = ((ComboBox)aControl).SelectedValue;
|
StephaneLenclud@231
|
171 |
// Set enum value
|
StephaneLenclud@236
|
172 |
aInfo.SetValue(aObject, enumValue);
|
StephaneLenclud@231
|
173 |
}
|
StephaneLenclud@231
|
174 |
else if (aInfo.PropertyType == typeof(bool))
|
StephaneLenclud@231
|
175 |
{
|
StephaneLenclud@231
|
176 |
CheckBox ctrl = (CheckBox)aControl;
|
StephaneLenclud@236
|
177 |
aInfo.SetValue(aObject, ctrl.Checked);
|
StephaneLenclud@231
|
178 |
}
|
StephaneLenclud@231
|
179 |
else if (aInfo.PropertyType == typeof(string))
|
StephaneLenclud@231
|
180 |
{
|
StephaneLenclud@231
|
181 |
TextBox ctrl = (TextBox)aControl;
|
StephaneLenclud@236
|
182 |
aInfo.SetValue(aObject, ctrl.Text);
|
StephaneLenclud@231
|
183 |
}
|
StephaneLenclud@238
|
184 |
else if (aInfo.PropertyType == typeof(PropertyFile))
|
StephaneLenclud@238
|
185 |
{
|
StephaneLenclud@238
|
186 |
Button ctrl = (Button)aControl;
|
StephaneLenclud@238
|
187 |
PropertyFile value = new PropertyFile {FullPath=ctrl.Text};
|
StephaneLenclud@238
|
188 |
aInfo.SetValue(aObject, value);
|
StephaneLenclud@238
|
189 |
}
|
Stephane@243
|
190 |
else if (aInfo.PropertyType == typeof(PropertyComboBox))
|
Stephane@243
|
191 |
{
|
Stephane@243
|
192 |
ComboBox ctrl = (ComboBox)aControl;
|
Stephane@243
|
193 |
string currentItem = ctrl.SelectedItem.ToString();
|
StephaneLenclud@244
|
194 |
PropertyComboBox value = (PropertyComboBox)aInfo.GetValue(aObject);
|
StephaneLenclud@244
|
195 |
value.CurrentItem = currentItem;
|
StephaneLenclud@244
|
196 |
//Not strictly needed but makes sure the set method is called
|
StephaneLenclud@244
|
197 |
aInfo.SetValue(aObject, value);
|
Stephane@243
|
198 |
}
|
StephaneLenclud@250
|
199 |
else if (aInfo.PropertyType == typeof(PropertyButton))
|
StephaneLenclud@250
|
200 |
{
|
StephaneLenclud@250
|
201 |
Button ctrl = (Button)aControl;
|
StephaneLenclud@250
|
202 |
PropertyButton value = new PropertyButton { Text = ctrl.Text };
|
StephaneLenclud@250
|
203 |
aInfo.SetValue(aObject, value);
|
StephaneLenclud@250
|
204 |
}
|
Stephane@243
|
205 |
|
StephaneLenclud@231
|
206 |
//TODO: add support for other types here
|
StephaneLenclud@231
|
207 |
}
|
StephaneLenclud@231
|
208 |
|
StephaneLenclud@236
|
209 |
|
StephaneLenclud@231
|
210 |
/// <summary>
|
StephaneLenclud@236
|
211 |
/// Create a control for the given property.
|
StephaneLenclud@231
|
212 |
/// </summary>
|
StephaneLenclud@231
|
213 |
/// <param name="aInfo"></param>
|
StephaneLenclud@236
|
214 |
/// <param name="aAttribute"></param>
|
StephaneLenclud@236
|
215 |
/// <param name="aObject"></param>
|
StephaneLenclud@236
|
216 |
/// <returns></returns>
|
StephaneLenclud@236
|
217 |
private Control CreateControlForProperty(PropertyInfo aInfo, AttributeObjectProperty aAttribute, T aObject)
|
StephaneLenclud@231
|
218 |
{
|
StephaneLenclud@231
|
219 |
if (aInfo.PropertyType == typeof(int))
|
StephaneLenclud@231
|
220 |
{
|
StephaneLenclud@231
|
221 |
//Integer properties are using numeric editor
|
StephaneLenclud@231
|
222 |
NumericUpDown ctrl = new NumericUpDown();
|
StephaneLenclud@231
|
223 |
ctrl.AutoSize = true;
|
StephaneLenclud@231
|
224 |
ctrl.Minimum = Int32.Parse(aAttribute.Minimum);
|
StephaneLenclud@231
|
225 |
ctrl.Maximum = Int32.Parse(aAttribute.Maximum);
|
StephaneLenclud@231
|
226 |
ctrl.Increment = Int32.Parse(aAttribute.Increment);
|
StephaneLenclud@236
|
227 |
ctrl.Value = (int)aInfo.GetValue(aObject);
|
StephaneLenclud@247
|
228 |
// Hook-in change notification after setting the value
|
StephaneLenclud@247
|
229 |
ctrl.ValueChanged += ControlValueChanged;
|
StephaneLenclud@231
|
230 |
return ctrl;
|
StephaneLenclud@231
|
231 |
}
|
StephaneLenclud@231
|
232 |
else if (aInfo.PropertyType.IsEnum)
|
StephaneLenclud@231
|
233 |
{
|
StephaneLenclud@231
|
234 |
//Enum properties are using combo box
|
StephaneLenclud@231
|
235 |
ComboBox ctrl = new ComboBox();
|
Stephane@243
|
236 |
ctrl.AutoSize = true;
|
Stephane@243
|
237 |
ctrl.Sorted = true;
|
StephaneLenclud@231
|
238 |
ctrl.DropDownStyle = ComboBoxStyle.DropDownList;
|
StephaneLenclud@231
|
239 |
//Data source is fine but it gives us duplicate entries for duplicated enum values
|
StephaneLenclud@231
|
240 |
//ctrl.DataSource = Enum.GetValues(aInfo.PropertyType);
|
StephaneLenclud@231
|
241 |
|
StephaneLenclud@231
|
242 |
//Therefore we need to explicitly create our items
|
Stephane@243
|
243 |
Size cbSize = new Size(0, 0);
|
StephaneLenclud@231
|
244 |
foreach (string name in aInfo.PropertyType.GetEnumNames())
|
StephaneLenclud@231
|
245 |
{
|
StephaneLenclud@231
|
246 |
ctrl.Items.Add(name.ToString());
|
StephaneLenclud@231
|
247 |
Graphics g = this.CreateGraphics();
|
StephaneLenclud@231
|
248 |
//Since combobox autosize would not work we need to get measure text ourselves
|
Stephane@243
|
249 |
SizeF size = g.MeasureString(name.ToString(), ctrl.Font);
|
Stephane@243
|
250 |
cbSize.Width = Math.Max(cbSize.Width, (int)size.Width);
|
StephaneLenclud@231
|
251 |
cbSize.Height = Math.Max(cbSize.Height, (int)size.Height);
|
StephaneLenclud@231
|
252 |
}
|
StephaneLenclud@231
|
253 |
|
StephaneLenclud@231
|
254 |
//Make sure our combobox is large enough
|
StephaneLenclud@231
|
255 |
ctrl.MinimumSize = cbSize;
|
StephaneLenclud@231
|
256 |
|
StephaneLenclud@231
|
257 |
// Instantiate our enum
|
StephaneLenclud@231
|
258 |
object enumValue = Activator.CreateInstance(aInfo.PropertyType);
|
StephaneLenclud@236
|
259 |
enumValue = aInfo.GetValue(aObject);
|
StephaneLenclud@231
|
260 |
//Set the current item
|
StephaneLenclud@231
|
261 |
ctrl.SelectedItem = enumValue.ToString();
|
StephaneLenclud@247
|
262 |
// Hook-in change notification after setting the value
|
StephaneLenclud@247
|
263 |
ctrl.SelectedIndexChanged += ControlValueChanged;
|
StephaneLenclud@231
|
264 |
|
StephaneLenclud@231
|
265 |
return ctrl;
|
StephaneLenclud@231
|
266 |
}
|
StephaneLenclud@231
|
267 |
else if (aInfo.PropertyType == typeof(bool))
|
StephaneLenclud@231
|
268 |
{
|
StephaneLenclud@231
|
269 |
CheckBox ctrl = new CheckBox();
|
StephaneLenclud@231
|
270 |
ctrl.AutoSize = true;
|
StephaneLenclud@231
|
271 |
ctrl.Text = aAttribute.Description;
|
Stephane@243
|
272 |
ctrl.Checked = (bool)aInfo.GetValue(aObject);
|
StephaneLenclud@247
|
273 |
// Hook-in change notification after setting the value
|
StephaneLenclud@247
|
274 |
ctrl.CheckedChanged += ControlValueChanged;
|
StephaneLenclud@231
|
275 |
return ctrl;
|
StephaneLenclud@231
|
276 |
}
|
StephaneLenclud@231
|
277 |
else if (aInfo.PropertyType == typeof(string))
|
StephaneLenclud@231
|
278 |
{
|
StephaneLenclud@231
|
279 |
TextBox ctrl = new TextBox();
|
StephaneLenclud@231
|
280 |
ctrl.AutoSize = true;
|
StephaneLenclud@236
|
281 |
ctrl.Text = (string)aInfo.GetValue(aObject);
|
StephaneLenclud@247
|
282 |
// Hook-in change notification after setting the value
|
StephaneLenclud@247
|
283 |
ctrl.TextChanged += ControlValueChanged;
|
StephaneLenclud@231
|
284 |
return ctrl;
|
StephaneLenclud@231
|
285 |
}
|
StephaneLenclud@238
|
286 |
else if (aInfo.PropertyType == typeof(PropertyFile))
|
StephaneLenclud@238
|
287 |
{
|
StephaneLenclud@238
|
288 |
// We have a file property
|
StephaneLenclud@238
|
289 |
// Create a button that will trigger the open file dialog to select our file.
|
StephaneLenclud@238
|
290 |
Button ctrl = new Button();
|
StephaneLenclud@238
|
291 |
ctrl.AutoSize = true;
|
StephaneLenclud@238
|
292 |
ctrl.Text = ((PropertyFile)aInfo.GetValue(aObject)).FullPath;
|
StephaneLenclud@238
|
293 |
// Add lambda expression to Click event
|
StephaneLenclud@238
|
294 |
ctrl.Click += (sender, e) =>
|
StephaneLenclud@238
|
295 |
{
|
StephaneLenclud@238
|
296 |
// Create open file dialog
|
StephaneLenclud@238
|
297 |
OpenFileDialog ofd = new OpenFileDialog();
|
StephaneLenclud@238
|
298 |
ofd.RestoreDirectory = true;
|
StephaneLenclud@238
|
299 |
// Use file filter specified by our property
|
StephaneLenclud@238
|
300 |
ofd.Filter = aAttribute.Filter;
|
StephaneLenclud@238
|
301 |
// Show our dialog
|
StephaneLenclud@238
|
302 |
if (DlgBox.ShowDialog(ofd) == DialogResult.OK)
|
StephaneLenclud@238
|
303 |
{
|
StephaneLenclud@238
|
304 |
// Fetch selected file name
|
StephaneLenclud@238
|
305 |
ctrl.Text = ofd.FileName;
|
StephaneLenclud@238
|
306 |
}
|
StephaneLenclud@238
|
307 |
};
|
StephaneLenclud@238
|
308 |
|
StephaneLenclud@247
|
309 |
// Hook-in change notification after setting the value
|
StephaneLenclud@247
|
310 |
ctrl.TextChanged += ControlValueChanged;
|
StephaneLenclud@238
|
311 |
return ctrl;
|
StephaneLenclud@238
|
312 |
}
|
Stephane@243
|
313 |
else if (aInfo.PropertyType == typeof(PropertyComboBox))
|
Stephane@243
|
314 |
{
|
Stephane@243
|
315 |
//ComboBox property
|
Stephane@243
|
316 |
ComboBox ctrl = new ComboBox();
|
Stephane@243
|
317 |
ctrl.AutoSize = true;
|
Stephane@243
|
318 |
ctrl.Sorted = true;
|
Stephane@243
|
319 |
ctrl.DropDownStyle = ComboBoxStyle.DropDownList;
|
Stephane@243
|
320 |
//Data source is such a pain to set the current item
|
Stephane@243
|
321 |
//ctrl.DataSource = ((PropertyComboBox)aInfo.GetValue(aObject)).Items;
|
Stephane@243
|
322 |
|
Stephane@243
|
323 |
PropertyComboBox pcb = ((PropertyComboBox)aInfo.GetValue(aObject));
|
Stephane@243
|
324 |
foreach (string item in pcb.Items)
|
Stephane@243
|
325 |
{
|
Stephane@243
|
326 |
ctrl.Items.Add(item);
|
Stephane@243
|
327 |
}
|
Stephane@243
|
328 |
|
Stephane@243
|
329 |
ctrl.SelectedItem = ((PropertyComboBox)aInfo.GetValue(aObject)).CurrentItem;
|
Stephane@243
|
330 |
//
|
Stephane@243
|
331 |
return ctrl;
|
Stephane@243
|
332 |
}
|
StephaneLenclud@250
|
333 |
else if (aInfo.PropertyType == typeof(PropertyButton))
|
StephaneLenclud@250
|
334 |
{
|
StephaneLenclud@250
|
335 |
// We have a button property
|
StephaneLenclud@250
|
336 |
// Create a button that will trigger the custom action.
|
StephaneLenclud@250
|
337 |
Button ctrl = new Button();
|
StephaneLenclud@250
|
338 |
ctrl.AutoSize = true;
|
StephaneLenclud@250
|
339 |
ctrl.Text = ((PropertyButton)aInfo.GetValue(aObject)).Text;
|
StephaneLenclud@250
|
340 |
// Hook in click event
|
StephaneLenclud@250
|
341 |
ctrl.Click += ((PropertyButton)aInfo.GetValue(aObject)).ClickEventHandler;
|
StephaneLenclud@250
|
342 |
// Hook-in change notification after setting the value
|
StephaneLenclud@250
|
343 |
ctrl.TextChanged += ControlValueChanged;
|
StephaneLenclud@250
|
344 |
return ctrl;
|
StephaneLenclud@250
|
345 |
}
|
StephaneLenclud@250
|
346 |
|
StephaneLenclud@231
|
347 |
//TODO: add support for other control type here
|
StephaneLenclud@231
|
348 |
return null;
|
StephaneLenclud@231
|
349 |
}
|
StephaneLenclud@231
|
350 |
|
StephaneLenclud@231
|
351 |
/// <summary>
|
StephaneLenclud@231
|
352 |
/// Don't forget to extend that one and adding types
|
StephaneLenclud@231
|
353 |
/// </summary>
|
StephaneLenclud@231
|
354 |
/// <returns></returns>
|
StephaneLenclud@231
|
355 |
private bool IsPropertyTypeSupported(PropertyInfo aInfo)
|
StephaneLenclud@231
|
356 |
{
|
StephaneLenclud@231
|
357 |
if (aInfo.PropertyType == typeof(int))
|
StephaneLenclud@231
|
358 |
{
|
StephaneLenclud@231
|
359 |
return true;
|
StephaneLenclud@231
|
360 |
}
|
StephaneLenclud@231
|
361 |
else if (aInfo.PropertyType.IsEnum)
|
StephaneLenclud@231
|
362 |
{
|
StephaneLenclud@231
|
363 |
return true;
|
StephaneLenclud@231
|
364 |
}
|
StephaneLenclud@231
|
365 |
else if (aInfo.PropertyType == typeof(bool))
|
StephaneLenclud@231
|
366 |
{
|
StephaneLenclud@231
|
367 |
return true;
|
StephaneLenclud@231
|
368 |
}
|
StephaneLenclud@231
|
369 |
else if (aInfo.PropertyType == typeof(string))
|
StephaneLenclud@231
|
370 |
{
|
StephaneLenclud@231
|
371 |
return true;
|
StephaneLenclud@231
|
372 |
}
|
StephaneLenclud@238
|
373 |
else if (aInfo.PropertyType == typeof(PropertyFile))
|
StephaneLenclud@238
|
374 |
{
|
StephaneLenclud@238
|
375 |
return true;
|
StephaneLenclud@238
|
376 |
}
|
Stephane@243
|
377 |
else if (aInfo.PropertyType == typeof(PropertyComboBox))
|
Stephane@243
|
378 |
{
|
Stephane@243
|
379 |
return true;
|
Stephane@243
|
380 |
}
|
StephaneLenclud@250
|
381 |
else if (aInfo.PropertyType == typeof(PropertyButton))
|
StephaneLenclud@250
|
382 |
{
|
StephaneLenclud@250
|
383 |
return true;
|
StephaneLenclud@250
|
384 |
}
|
Stephane@243
|
385 |
|
StephaneLenclud@231
|
386 |
//TODO: add support for other type here
|
StephaneLenclud@231
|
387 |
|
StephaneLenclud@231
|
388 |
return false;
|
StephaneLenclud@231
|
389 |
}
|
StephaneLenclud@231
|
390 |
|
StephaneLenclud@231
|
391 |
/// <summary>
|
StephaneLenclud@231
|
392 |
/// Update our table layout.
|
StephaneLenclud@246
|
393 |
/// Will instantiated every field control as defined by our object.
|
StephaneLenclud@231
|
394 |
/// </summary>
|
StephaneLenclud@231
|
395 |
/// <param name="aLayout"></param>
|
StephaneLenclud@247
|
396 |
private void UpdateControls()
|
StephaneLenclud@231
|
397 |
{
|
StephaneLenclud@247
|
398 |
|
StephaneLenclud@231
|
399 |
toolTip.RemoveAll();
|
StephaneLenclud@231
|
400 |
//Debug.Print("UpdateTableLayoutPanel")
|
StephaneLenclud@231
|
401 |
//First clean our current panel
|
StephaneLenclud@231
|
402 |
iTableLayoutPanel.Controls.Clear();
|
StephaneLenclud@231
|
403 |
iTableLayoutPanel.RowStyles.Clear();
|
StephaneLenclud@231
|
404 |
iTableLayoutPanel.ColumnStyles.Clear();
|
StephaneLenclud@231
|
405 |
iTableLayoutPanel.RowCount = 0;
|
StephaneLenclud@231
|
406 |
|
StephaneLenclud@231
|
407 |
//We always want two columns: one for label and one for the field
|
StephaneLenclud@231
|
408 |
iTableLayoutPanel.ColumnCount = 2;
|
StephaneLenclud@231
|
409 |
iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
|
StephaneLenclud@231
|
410 |
iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
|
StephaneLenclud@231
|
411 |
|
StephaneLenclud@231
|
412 |
|
StephaneLenclud@247
|
413 |
if (Object == null)
|
StephaneLenclud@231
|
414 |
{
|
StephaneLenclud@231
|
415 |
//Just drop it
|
StephaneLenclud@231
|
416 |
return;
|
StephaneLenclud@231
|
417 |
}
|
StephaneLenclud@246
|
418 |
|
StephaneLenclud@247
|
419 |
UpdateStaticControls();
|
StephaneLenclud@247
|
420 |
|
StephaneLenclud@236
|
421 |
//IEnumerable<PropertyInfo> properties = aObject.GetType().GetProperties().Where(
|
StephaneLenclud@231
|
422 |
// prop => Attribute.IsDefined(prop, typeof(AttributeObjectProperty)));
|
StephaneLenclud@231
|
423 |
|
StephaneLenclud@246
|
424 |
//TODO: Do this whenever a field changes
|
StephaneLenclud@247
|
425 |
iLabelBrief.Text = Object.Brief();
|
StephaneLenclud@246
|
426 |
|
StephaneLenclud@231
|
427 |
|
StephaneLenclud@247
|
428 |
foreach (PropertyInfo pi in Object.GetType().GetProperties())
|
StephaneLenclud@231
|
429 |
{
|
StephaneLenclud@231
|
430 |
AttributeObjectProperty[] attributes = ((AttributeObjectProperty[])pi.GetCustomAttributes(typeof(AttributeObjectProperty), true));
|
StephaneLenclud@231
|
431 |
if (attributes.Length != 1)
|
StephaneLenclud@231
|
432 |
{
|
StephaneLenclud@231
|
433 |
continue;
|
StephaneLenclud@231
|
434 |
}
|
StephaneLenclud@231
|
435 |
|
StephaneLenclud@231
|
436 |
AttributeObjectProperty attribute = attributes[0];
|
StephaneLenclud@231
|
437 |
|
StephaneLenclud@231
|
438 |
//Before anything we need to check if that kind of property is supported by our UI
|
StephaneLenclud@231
|
439 |
//Create the editor
|
StephaneLenclud@247
|
440 |
Control ctrl = CreateControlForProperty(pi, attribute, Object);
|
StephaneLenclud@231
|
441 |
if (ctrl == null)
|
StephaneLenclud@231
|
442 |
{
|
StephaneLenclud@231
|
443 |
//Property type not supported
|
StephaneLenclud@231
|
444 |
continue;
|
StephaneLenclud@231
|
445 |
}
|
StephaneLenclud@231
|
446 |
|
StephaneLenclud@231
|
447 |
//Add a new row
|
StephaneLenclud@231
|
448 |
iTableLayoutPanel.RowCount++;
|
StephaneLenclud@231
|
449 |
iTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
|
StephaneLenclud@231
|
450 |
//Create the label
|
StephaneLenclud@231
|
451 |
Label label = new Label();
|
StephaneLenclud@231
|
452 |
label.AutoSize = true;
|
StephaneLenclud@231
|
453 |
label.Dock = DockStyle.Fill;
|
StephaneLenclud@231
|
454 |
label.TextAlign = ContentAlignment.MiddleCenter;
|
StephaneLenclud@231
|
455 |
label.Text = attribute.Name;
|
StephaneLenclud@231
|
456 |
toolTip.SetToolTip(label, attribute.Description);
|
StephaneLenclud@231
|
457 |
iTableLayoutPanel.Controls.Add(label, 0, iTableLayoutPanel.RowCount-1);
|
StephaneLenclud@231
|
458 |
|
StephaneLenclud@231
|
459 |
//Add our editor to our form
|
StephaneLenclud@231
|
460 |
iTableLayoutPanel.Controls.Add(ctrl, 1, iTableLayoutPanel.RowCount - 1);
|
StephaneLenclud@231
|
461 |
//Add tooltip to editor too
|
StephaneLenclud@231
|
462 |
toolTip.SetToolTip(ctrl, attribute.Description);
|
StephaneLenclud@231
|
463 |
|
StephaneLenclud@246
|
464 |
}
|
StephaneLenclud@231
|
465 |
|
StephaneLenclud@246
|
466 |
//Entrer object edit mode
|
StephaneLenclud@246
|
467 |
Object.CurrentState = SharpLib.Ear.Object.State.Edit;
|
StephaneLenclud@246
|
468 |
Object.PropertyChanged += PropertyChangedEventHandlerThreadSafe;
|
StephaneLenclud@246
|
469 |
}
|
StephaneLenclud@246
|
470 |
|
StephaneLenclud@247
|
471 |
/// <summary>
|
StephaneLenclud@247
|
472 |
///
|
StephaneLenclud@247
|
473 |
/// </summary>
|
StephaneLenclud@247
|
474 |
/// <param name="sender"></param>
|
StephaneLenclud@247
|
475 |
/// <param name="e"></param>
|
StephaneLenclud@246
|
476 |
void PropertyChangedEventHandlerThreadSafe(object sender, PropertyChangedEventArgs e)
|
StephaneLenclud@246
|
477 |
{
|
StephaneLenclud@246
|
478 |
if (this.InvokeRequired)
|
StephaneLenclud@246
|
479 |
{
|
StephaneLenclud@246
|
480 |
//Not in the proper thread, invoke ourselves
|
StephaneLenclud@246
|
481 |
PropertyChangedEventHandler d = new PropertyChangedEventHandler(PropertyChangedEventHandlerThreadSafe);
|
StephaneLenclud@246
|
482 |
this.Invoke(d, new object[] { sender, e });
|
StephaneLenclud@246
|
483 |
}
|
StephaneLenclud@246
|
484 |
else
|
StephaneLenclud@246
|
485 |
{
|
StephaneLenclud@247
|
486 |
// We could test the name of the property that has changed as follow
|
StephaneLenclud@247
|
487 |
// It's currently not needed though
|
StephaneLenclud@247
|
488 |
//if (e.PropertyName == "Brief")
|
StephaneLenclud@246
|
489 |
|
StephaneLenclud@247
|
490 |
// Our object has changed behind our back.
|
StephaneLenclud@247
|
491 |
// That's currently only the case for HID events that are listening for inputs.
|
StephaneLenclud@250
|
492 |
if (Object is EventHid)
|
StephaneLenclud@250
|
493 |
{
|
StephaneLenclud@250
|
494 |
//HID can't do full control updates for some reason
|
StephaneLenclud@250
|
495 |
//We are getting spammed with HID events after a few clicks
|
StephaneLenclud@250
|
496 |
//We need to investigate, HID bug?
|
StephaneLenclud@250
|
497 |
UpdateStaticControls();
|
StephaneLenclud@250
|
498 |
}
|
StephaneLenclud@250
|
499 |
else
|
StephaneLenclud@250
|
500 |
{
|
StephaneLenclud@250
|
501 |
UpdateControls();
|
StephaneLenclud@250
|
502 |
}
|
StephaneLenclud@246
|
503 |
}
|
StephaneLenclud@231
|
504 |
}
|
StephaneLenclud@231
|
505 |
|
StephaneLenclud@231
|
506 |
private void buttonTest_Click(object sender, EventArgs e)
|
StephaneLenclud@231
|
507 |
{
|
StephaneLenclud@231
|
508 |
FetchPropertiesValue(Object);
|
StephaneLenclud@231
|
509 |
|
StephaneLenclud@231
|
510 |
//If our object has a test method with no parameters just run it then
|
StephaneLenclud@231
|
511 |
MethodInfo info = Object.GetType().GetMethod("Test");
|
StephaneLenclud@231
|
512 |
if ( info != null && info.GetParameters().Length==0)
|
StephaneLenclud@231
|
513 |
{
|
StephaneLenclud@231
|
514 |
info.Invoke(Object,null);
|
StephaneLenclud@231
|
515 |
}
|
StephaneLenclud@231
|
516 |
|
StephaneLenclud@231
|
517 |
}
|
StephaneLenclud@247
|
518 |
|
StephaneLenclud@247
|
519 |
|
StephaneLenclud@247
|
520 |
/// <summary>
|
StephaneLenclud@247
|
521 |
///
|
StephaneLenclud@247
|
522 |
/// </summary>
|
StephaneLenclud@247
|
523 |
/// <param name="sender"></param>
|
StephaneLenclud@247
|
524 |
/// <param name="e"></param>
|
StephaneLenclud@247
|
525 |
private void ControlValueChanged(object sender, EventArgs e)
|
StephaneLenclud@247
|
526 |
{
|
StephaneLenclud@247
|
527 |
UpdateObject();
|
StephaneLenclud@247
|
528 |
}
|
StephaneLenclud@247
|
529 |
|
StephaneLenclud@247
|
530 |
/// <summary>
|
StephaneLenclud@247
|
531 |
///
|
StephaneLenclud@247
|
532 |
/// </summary>
|
StephaneLenclud@247
|
533 |
private void UpdateObject()
|
StephaneLenclud@247
|
534 |
{
|
StephaneLenclud@247
|
535 |
// Update our object with the content of our controls
|
StephaneLenclud@247
|
536 |
FetchPropertiesValue(Object);
|
StephaneLenclud@247
|
537 |
|
StephaneLenclud@247
|
538 |
UpdateStaticControls();
|
StephaneLenclud@247
|
539 |
//
|
StephaneLenclud@247
|
540 |
//PerformLayout();
|
StephaneLenclud@247
|
541 |
}
|
StephaneLenclud@247
|
542 |
|
StephaneLenclud@247
|
543 |
/// <summary>
|
StephaneLenclud@247
|
544 |
///
|
StephaneLenclud@247
|
545 |
/// </summary>
|
StephaneLenclud@247
|
546 |
private void UpdateStaticControls()
|
StephaneLenclud@247
|
547 |
{
|
StephaneLenclud@247
|
548 |
// Update OK and test button status
|
StephaneLenclud@247
|
549 |
iButtonOk.Enabled = Object.IsValid();
|
StephaneLenclud@247
|
550 |
iButtonTest.Enabled = iButtonOk.Enabled;
|
StephaneLenclud@247
|
551 |
|
StephaneLenclud@247
|
552 |
// Update brief title
|
StephaneLenclud@247
|
553 |
iLabelBrief.Text = Object.Brief();
|
StephaneLenclud@247
|
554 |
|
StephaneLenclud@247
|
555 |
// Update object description
|
StephaneLenclud@247
|
556 |
iLabelDescription.Text = Object.Description;
|
StephaneLenclud@247
|
557 |
}
|
StephaneLenclud@247
|
558 |
|
StephaneLenclud@247
|
559 |
/// <summary>
|
StephaneLenclud@247
|
560 |
///
|
StephaneLenclud@247
|
561 |
/// </summary>
|
StephaneLenclud@247
|
562 |
/// <param name="sender"></param>
|
StephaneLenclud@247
|
563 |
/// <param name="e"></param>
|
StephaneLenclud@247
|
564 |
private void iComboBoxObjectType_KeyPress(object sender, KeyPressEventArgs e)
|
StephaneLenclud@247
|
565 |
{
|
StephaneLenclud@247
|
566 |
//Special case for HID events
|
StephaneLenclud@247
|
567 |
if (Object is EventHid)
|
StephaneLenclud@247
|
568 |
{
|
StephaneLenclud@247
|
569 |
//Disable handling of key input as we are using key input for changing our event
|
StephaneLenclud@247
|
570 |
e.Handled = true;
|
StephaneLenclud@247
|
571 |
}
|
StephaneLenclud@247
|
572 |
}
|
StephaneLenclud@231
|
573 |
}
|
StephaneLenclud@231
|
574 |
}
|