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@231
|
45 |
comboBoxActionType.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@231
|
51 |
comboBoxActionType.SelectedIndex = 0;
|
StephaneLenclud@231
|
52 |
}
|
StephaneLenclud@231
|
53 |
else
|
StephaneLenclud@231
|
54 |
{
|
StephaneLenclud@231
|
55 |
// Editing existing object
|
StephaneLenclud@239
|
56 |
// Look up our item in our combobox
|
StephaneLenclud@231
|
57 |
foreach (ItemObjectType item in comboBoxActionType.Items)
|
StephaneLenclud@231
|
58 |
{
|
StephaneLenclud@231
|
59 |
if (item.Type == Object.GetType())
|
StephaneLenclud@231
|
60 |
{
|
StephaneLenclud@231
|
61 |
comboBoxActionType.SelectedItem = item;
|
StephaneLenclud@231
|
62 |
}
|
StephaneLenclud@231
|
63 |
}
|
StephaneLenclud@239
|
64 |
}
|
StephaneLenclud@231
|
65 |
}
|
StephaneLenclud@231
|
66 |
|
StephaneLenclud@231
|
67 |
private void buttonOk_Click(object sender, EventArgs e)
|
StephaneLenclud@231
|
68 |
{
|
StephaneLenclud@231
|
69 |
FetchPropertiesValue(Object);
|
StephaneLenclud@239
|
70 |
if (!Object.IsValid())
|
StephaneLenclud@239
|
71 |
{
|
StephaneLenclud@239
|
72 |
// Tell for closing event to abort
|
StephaneLenclud@239
|
73 |
DialogResult = DialogResult.None;
|
StephaneLenclud@239
|
74 |
}
|
StephaneLenclud@231
|
75 |
}
|
StephaneLenclud@231
|
76 |
|
StephaneLenclud@239
|
77 |
|
StephaneLenclud@239
|
78 |
private void FormEditObject_FormClosing(object sender, FormClosingEventArgs e)
|
StephaneLenclud@231
|
79 |
{
|
StephaneLenclud@239
|
80 |
e.Cancel = DialogResult == DialogResult.None;
|
StephaneLenclud@231
|
81 |
}
|
StephaneLenclud@231
|
82 |
|
StephaneLenclud@231
|
83 |
private void comboBoxActionType_SelectedIndexChanged(object sender, EventArgs e)
|
StephaneLenclud@231
|
84 |
{
|
StephaneLenclud@231
|
85 |
//Instantiate an action corresponding to our type
|
StephaneLenclud@231
|
86 |
Type actionType = ((ItemObjectType) comboBoxActionType.SelectedItem).Type;
|
StephaneLenclud@231
|
87 |
//Create another type of action only if needed
|
StephaneLenclud@231
|
88 |
if (Object == null || Object.GetType() != actionType)
|
StephaneLenclud@231
|
89 |
{
|
StephaneLenclud@231
|
90 |
Object = (T)Activator.CreateInstance(actionType);
|
StephaneLenclud@231
|
91 |
}
|
StephaneLenclud@239
|
92 |
|
StephaneLenclud@239
|
93 |
//Disable ok button if our object is not valid
|
StephaneLenclud@239
|
94 |
buttonOk.Enabled = Object.IsValid();
|
StephaneLenclud@239
|
95 |
|
StephaneLenclud@231
|
96 |
//Create input fields
|
StephaneLenclud@231
|
97 |
UpdateTableLayoutPanel(Object);
|
StephaneLenclud@231
|
98 |
}
|
StephaneLenclud@231
|
99 |
|
StephaneLenclud@231
|
100 |
|
StephaneLenclud@231
|
101 |
/// <summary>
|
StephaneLenclud@231
|
102 |
/// Get properties values from our generated input fields
|
StephaneLenclud@231
|
103 |
/// </summary>
|
StephaneLenclud@236
|
104 |
private void FetchPropertiesValue(T aObject)
|
StephaneLenclud@231
|
105 |
{
|
StephaneLenclud@231
|
106 |
int ctrlIndex = 0;
|
Stephane@243
|
107 |
//For each of our properties
|
StephaneLenclud@236
|
108 |
foreach (PropertyInfo pi in aObject.GetType().GetProperties())
|
StephaneLenclud@231
|
109 |
{
|
Stephane@243
|
110 |
//Get our property attribute
|
Stephane@243
|
111 |
AttributeObjectProperty[] attributes = ((AttributeObjectProperty[]) pi.GetCustomAttributes(typeof(AttributeObjectProperty), true));
|
StephaneLenclud@231
|
112 |
if (attributes.Length != 1)
|
StephaneLenclud@231
|
113 |
{
|
Stephane@243
|
114 |
//No attribute, skip this property then.
|
StephaneLenclud@231
|
115 |
continue;
|
StephaneLenclud@231
|
116 |
}
|
StephaneLenclud@231
|
117 |
AttributeObjectProperty attribute = attributes[0];
|
StephaneLenclud@231
|
118 |
|
Stephane@243
|
119 |
//Check that we support this type of property
|
StephaneLenclud@231
|
120 |
if (!IsPropertyTypeSupported(pi))
|
StephaneLenclud@231
|
121 |
{
|
StephaneLenclud@231
|
122 |
continue;
|
StephaneLenclud@231
|
123 |
}
|
StephaneLenclud@231
|
124 |
|
Stephane@243
|
125 |
//Now fetch our property value
|
StephaneLenclud@236
|
126 |
GetPropertyValueFromControl(iTableLayoutPanel.Controls[ctrlIndex+1], pi, aObject); //+1 otherwise we get the label
|
StephaneLenclud@231
|
127 |
|
StephaneLenclud@231
|
128 |
ctrlIndex+=2; //Jump over the label too
|
StephaneLenclud@231
|
129 |
}
|
StephaneLenclud@231
|
130 |
}
|
StephaneLenclud@231
|
131 |
|
StephaneLenclud@231
|
132 |
/// <summary>
|
StephaneLenclud@231
|
133 |
/// Extend this function to support reading new types of properties.
|
StephaneLenclud@231
|
134 |
/// </summary>
|
StephaneLenclud@236
|
135 |
/// <param name="aObject"></param>
|
StephaneLenclud@236
|
136 |
private void GetPropertyValueFromControl(Control aControl, PropertyInfo aInfo, T aObject)
|
StephaneLenclud@231
|
137 |
{
|
StephaneLenclud@231
|
138 |
if (aInfo.PropertyType == typeof(int))
|
StephaneLenclud@231
|
139 |
{
|
StephaneLenclud@231
|
140 |
NumericUpDown ctrl=(NumericUpDown)aControl;
|
StephaneLenclud@236
|
141 |
aInfo.SetValue(aObject,(int)ctrl.Value);
|
StephaneLenclud@231
|
142 |
}
|
StephaneLenclud@231
|
143 |
else if (aInfo.PropertyType.IsEnum)
|
StephaneLenclud@231
|
144 |
{
|
StephaneLenclud@231
|
145 |
// Instantiate our enum
|
StephaneLenclud@231
|
146 |
object enumValue= Activator.CreateInstance(aInfo.PropertyType);
|
StephaneLenclud@231
|
147 |
// Parse our enum from combo box
|
StephaneLenclud@231
|
148 |
enumValue = Enum.Parse(aInfo.PropertyType,((ComboBox)aControl).SelectedItem.ToString());
|
StephaneLenclud@231
|
149 |
//enumValue = ((ComboBox)aControl).SelectedValue;
|
StephaneLenclud@231
|
150 |
// Set enum value
|
StephaneLenclud@236
|
151 |
aInfo.SetValue(aObject, enumValue);
|
StephaneLenclud@231
|
152 |
}
|
StephaneLenclud@231
|
153 |
else if (aInfo.PropertyType == typeof(bool))
|
StephaneLenclud@231
|
154 |
{
|
StephaneLenclud@231
|
155 |
CheckBox ctrl = (CheckBox)aControl;
|
StephaneLenclud@236
|
156 |
aInfo.SetValue(aObject, ctrl.Checked);
|
StephaneLenclud@231
|
157 |
}
|
StephaneLenclud@231
|
158 |
else if (aInfo.PropertyType == typeof(string))
|
StephaneLenclud@231
|
159 |
{
|
StephaneLenclud@231
|
160 |
TextBox ctrl = (TextBox)aControl;
|
StephaneLenclud@236
|
161 |
aInfo.SetValue(aObject, ctrl.Text);
|
StephaneLenclud@231
|
162 |
}
|
StephaneLenclud@238
|
163 |
else if (aInfo.PropertyType == typeof(PropertyFile))
|
StephaneLenclud@238
|
164 |
{
|
StephaneLenclud@238
|
165 |
Button ctrl = (Button)aControl;
|
StephaneLenclud@238
|
166 |
PropertyFile value = new PropertyFile {FullPath=ctrl.Text};
|
StephaneLenclud@238
|
167 |
aInfo.SetValue(aObject, value);
|
StephaneLenclud@238
|
168 |
}
|
Stephane@243
|
169 |
else if (aInfo.PropertyType == typeof(PropertyComboBox))
|
Stephane@243
|
170 |
{
|
Stephane@243
|
171 |
ComboBox ctrl = (ComboBox)aControl;
|
Stephane@243
|
172 |
string currentItem = ctrl.SelectedItem.ToString();
|
StephaneLenclud@244
|
173 |
PropertyComboBox value = (PropertyComboBox)aInfo.GetValue(aObject);
|
StephaneLenclud@244
|
174 |
value.CurrentItem = currentItem;
|
StephaneLenclud@244
|
175 |
//Not strictly needed but makes sure the set method is called
|
StephaneLenclud@244
|
176 |
aInfo.SetValue(aObject, value);
|
Stephane@243
|
177 |
}
|
Stephane@243
|
178 |
|
StephaneLenclud@231
|
179 |
//TODO: add support for other types here
|
StephaneLenclud@231
|
180 |
}
|
StephaneLenclud@231
|
181 |
|
StephaneLenclud@236
|
182 |
|
StephaneLenclud@231
|
183 |
/// <summary>
|
StephaneLenclud@236
|
184 |
/// Create a control for the given property.
|
StephaneLenclud@231
|
185 |
/// </summary>
|
StephaneLenclud@231
|
186 |
/// <param name="aInfo"></param>
|
StephaneLenclud@236
|
187 |
/// <param name="aAttribute"></param>
|
StephaneLenclud@236
|
188 |
/// <param name="aObject"></param>
|
StephaneLenclud@236
|
189 |
/// <returns></returns>
|
StephaneLenclud@236
|
190 |
private Control CreateControlForProperty(PropertyInfo aInfo, AttributeObjectProperty aAttribute, T aObject)
|
StephaneLenclud@231
|
191 |
{
|
StephaneLenclud@231
|
192 |
if (aInfo.PropertyType == typeof(int))
|
StephaneLenclud@231
|
193 |
{
|
StephaneLenclud@231
|
194 |
//Integer properties are using numeric editor
|
StephaneLenclud@231
|
195 |
NumericUpDown ctrl = new NumericUpDown();
|
StephaneLenclud@231
|
196 |
ctrl.AutoSize = true;
|
StephaneLenclud@231
|
197 |
ctrl.Minimum = Int32.Parse(aAttribute.Minimum);
|
StephaneLenclud@231
|
198 |
ctrl.Maximum = Int32.Parse(aAttribute.Maximum);
|
StephaneLenclud@231
|
199 |
ctrl.Increment = Int32.Parse(aAttribute.Increment);
|
StephaneLenclud@236
|
200 |
ctrl.Value = (int)aInfo.GetValue(aObject);
|
StephaneLenclud@231
|
201 |
return ctrl;
|
StephaneLenclud@231
|
202 |
}
|
StephaneLenclud@231
|
203 |
else if (aInfo.PropertyType.IsEnum)
|
StephaneLenclud@231
|
204 |
{
|
StephaneLenclud@231
|
205 |
//Enum properties are using combo box
|
StephaneLenclud@231
|
206 |
ComboBox ctrl = new ComboBox();
|
Stephane@243
|
207 |
ctrl.AutoSize = true;
|
Stephane@243
|
208 |
ctrl.Sorted = true;
|
StephaneLenclud@231
|
209 |
ctrl.DropDownStyle = ComboBoxStyle.DropDownList;
|
StephaneLenclud@231
|
210 |
//Data source is fine but it gives us duplicate entries for duplicated enum values
|
StephaneLenclud@231
|
211 |
//ctrl.DataSource = Enum.GetValues(aInfo.PropertyType);
|
StephaneLenclud@231
|
212 |
|
StephaneLenclud@231
|
213 |
//Therefore we need to explicitly create our items
|
Stephane@243
|
214 |
Size cbSize = new Size(0, 0);
|
StephaneLenclud@231
|
215 |
foreach (string name in aInfo.PropertyType.GetEnumNames())
|
StephaneLenclud@231
|
216 |
{
|
StephaneLenclud@231
|
217 |
ctrl.Items.Add(name.ToString());
|
StephaneLenclud@231
|
218 |
Graphics g = this.CreateGraphics();
|
StephaneLenclud@231
|
219 |
//Since combobox autosize would not work we need to get measure text ourselves
|
Stephane@243
|
220 |
SizeF size = g.MeasureString(name.ToString(), ctrl.Font);
|
Stephane@243
|
221 |
cbSize.Width = Math.Max(cbSize.Width, (int)size.Width);
|
StephaneLenclud@231
|
222 |
cbSize.Height = Math.Max(cbSize.Height, (int)size.Height);
|
StephaneLenclud@231
|
223 |
}
|
StephaneLenclud@231
|
224 |
|
StephaneLenclud@231
|
225 |
//Make sure our combobox is large enough
|
StephaneLenclud@231
|
226 |
ctrl.MinimumSize = cbSize;
|
StephaneLenclud@231
|
227 |
|
StephaneLenclud@231
|
228 |
// Instantiate our enum
|
StephaneLenclud@231
|
229 |
object enumValue = Activator.CreateInstance(aInfo.PropertyType);
|
StephaneLenclud@236
|
230 |
enumValue = aInfo.GetValue(aObject);
|
StephaneLenclud@231
|
231 |
//Set the current item
|
StephaneLenclud@231
|
232 |
ctrl.SelectedItem = enumValue.ToString();
|
StephaneLenclud@231
|
233 |
|
StephaneLenclud@231
|
234 |
return ctrl;
|
StephaneLenclud@231
|
235 |
}
|
StephaneLenclud@231
|
236 |
else if (aInfo.PropertyType == typeof(bool))
|
StephaneLenclud@231
|
237 |
{
|
StephaneLenclud@231
|
238 |
CheckBox ctrl = new CheckBox();
|
StephaneLenclud@231
|
239 |
ctrl.AutoSize = true;
|
StephaneLenclud@231
|
240 |
ctrl.Text = aAttribute.Description;
|
Stephane@243
|
241 |
ctrl.Checked = (bool)aInfo.GetValue(aObject);
|
StephaneLenclud@231
|
242 |
return ctrl;
|
StephaneLenclud@231
|
243 |
}
|
StephaneLenclud@231
|
244 |
else if (aInfo.PropertyType == typeof(string))
|
StephaneLenclud@231
|
245 |
{
|
StephaneLenclud@231
|
246 |
TextBox ctrl = new TextBox();
|
StephaneLenclud@231
|
247 |
ctrl.AutoSize = true;
|
StephaneLenclud@236
|
248 |
ctrl.Text = (string)aInfo.GetValue(aObject);
|
StephaneLenclud@231
|
249 |
return ctrl;
|
StephaneLenclud@231
|
250 |
}
|
StephaneLenclud@238
|
251 |
else if (aInfo.PropertyType == typeof(PropertyFile))
|
StephaneLenclud@238
|
252 |
{
|
StephaneLenclud@238
|
253 |
// We have a file property
|
StephaneLenclud@238
|
254 |
// Create a button that will trigger the open file dialog to select our file.
|
StephaneLenclud@238
|
255 |
Button ctrl = new Button();
|
StephaneLenclud@238
|
256 |
ctrl.AutoSize = true;
|
StephaneLenclud@238
|
257 |
ctrl.Text = ((PropertyFile)aInfo.GetValue(aObject)).FullPath;
|
StephaneLenclud@239
|
258 |
|
StephaneLenclud@238
|
259 |
// Add lambda expression to Click event
|
StephaneLenclud@238
|
260 |
ctrl.Click += (sender, e) =>
|
StephaneLenclud@238
|
261 |
{
|
StephaneLenclud@238
|
262 |
// Create open file dialog
|
StephaneLenclud@238
|
263 |
OpenFileDialog ofd = new OpenFileDialog();
|
StephaneLenclud@238
|
264 |
ofd.RestoreDirectory = true;
|
StephaneLenclud@238
|
265 |
// Use file filter specified by our property
|
StephaneLenclud@238
|
266 |
ofd.Filter = aAttribute.Filter;
|
StephaneLenclud@238
|
267 |
// Show our dialog
|
StephaneLenclud@238
|
268 |
if (DlgBox.ShowDialog(ofd) == DialogResult.OK)
|
StephaneLenclud@238
|
269 |
{
|
StephaneLenclud@238
|
270 |
// Fetch selected file name
|
StephaneLenclud@238
|
271 |
ctrl.Text = ofd.FileName;
|
StephaneLenclud@239
|
272 |
//Enable Ok button then
|
StephaneLenclud@239
|
273 |
buttonOk.Enabled = Object.IsValid();
|
StephaneLenclud@238
|
274 |
}
|
StephaneLenclud@238
|
275 |
};
|
StephaneLenclud@238
|
276 |
|
StephaneLenclud@238
|
277 |
return ctrl;
|
StephaneLenclud@238
|
278 |
}
|
Stephane@243
|
279 |
else if (aInfo.PropertyType == typeof(PropertyComboBox))
|
Stephane@243
|
280 |
{
|
Stephane@243
|
281 |
//ComboBox property
|
Stephane@243
|
282 |
ComboBox ctrl = new ComboBox();
|
Stephane@243
|
283 |
ctrl.AutoSize = true;
|
Stephane@243
|
284 |
ctrl.Sorted = true;
|
Stephane@243
|
285 |
ctrl.DropDownStyle = ComboBoxStyle.DropDownList;
|
Stephane@243
|
286 |
//Data source is such a pain to set the current item
|
Stephane@243
|
287 |
//ctrl.DataSource = ((PropertyComboBox)aInfo.GetValue(aObject)).Items;
|
Stephane@243
|
288 |
|
Stephane@243
|
289 |
PropertyComboBox pcb = ((PropertyComboBox)aInfo.GetValue(aObject));
|
Stephane@243
|
290 |
foreach (string item in pcb.Items)
|
Stephane@243
|
291 |
{
|
Stephane@243
|
292 |
ctrl.Items.Add(item);
|
Stephane@243
|
293 |
}
|
Stephane@243
|
294 |
|
Stephane@243
|
295 |
ctrl.SelectedItem = ((PropertyComboBox)aInfo.GetValue(aObject)).CurrentItem;
|
Stephane@243
|
296 |
//
|
Stephane@243
|
297 |
return ctrl;
|
Stephane@243
|
298 |
}
|
StephaneLenclud@231
|
299 |
//TODO: add support for other control type here
|
StephaneLenclud@231
|
300 |
return null;
|
StephaneLenclud@231
|
301 |
}
|
StephaneLenclud@231
|
302 |
|
StephaneLenclud@231
|
303 |
/// <summary>
|
StephaneLenclud@231
|
304 |
/// Don't forget to extend that one and adding types
|
StephaneLenclud@231
|
305 |
/// </summary>
|
StephaneLenclud@231
|
306 |
/// <returns></returns>
|
StephaneLenclud@231
|
307 |
private bool IsPropertyTypeSupported(PropertyInfo aInfo)
|
StephaneLenclud@231
|
308 |
{
|
StephaneLenclud@231
|
309 |
if (aInfo.PropertyType == typeof(int))
|
StephaneLenclud@231
|
310 |
{
|
StephaneLenclud@231
|
311 |
return true;
|
StephaneLenclud@231
|
312 |
}
|
StephaneLenclud@231
|
313 |
else if (aInfo.PropertyType.IsEnum)
|
StephaneLenclud@231
|
314 |
{
|
StephaneLenclud@231
|
315 |
return true;
|
StephaneLenclud@231
|
316 |
}
|
StephaneLenclud@231
|
317 |
else if (aInfo.PropertyType == typeof(bool))
|
StephaneLenclud@231
|
318 |
{
|
StephaneLenclud@231
|
319 |
return true;
|
StephaneLenclud@231
|
320 |
}
|
StephaneLenclud@231
|
321 |
else if (aInfo.PropertyType == typeof(string))
|
StephaneLenclud@231
|
322 |
{
|
StephaneLenclud@231
|
323 |
return true;
|
StephaneLenclud@231
|
324 |
}
|
StephaneLenclud@238
|
325 |
else if (aInfo.PropertyType == typeof(PropertyFile))
|
StephaneLenclud@238
|
326 |
{
|
StephaneLenclud@238
|
327 |
return true;
|
StephaneLenclud@238
|
328 |
}
|
Stephane@243
|
329 |
else if (aInfo.PropertyType == typeof(PropertyComboBox))
|
Stephane@243
|
330 |
{
|
Stephane@243
|
331 |
return true;
|
Stephane@243
|
332 |
}
|
Stephane@243
|
333 |
|
StephaneLenclud@231
|
334 |
//TODO: add support for other type here
|
StephaneLenclud@231
|
335 |
|
StephaneLenclud@231
|
336 |
return false;
|
StephaneLenclud@231
|
337 |
}
|
StephaneLenclud@231
|
338 |
|
StephaneLenclud@231
|
339 |
/// <summary>
|
StephaneLenclud@231
|
340 |
/// Update our table layout.
|
StephaneLenclud@231
|
341 |
/// Will instantiated every field control as defined by our action.
|
StephaneLenclud@231
|
342 |
/// Fields must be specified by rows from the left.
|
StephaneLenclud@231
|
343 |
/// </summary>
|
StephaneLenclud@231
|
344 |
/// <param name="aLayout"></param>
|
StephaneLenclud@236
|
345 |
private void UpdateTableLayoutPanel(T aObject)
|
StephaneLenclud@231
|
346 |
{
|
StephaneLenclud@231
|
347 |
toolTip.RemoveAll();
|
StephaneLenclud@231
|
348 |
//Debug.Print("UpdateTableLayoutPanel")
|
StephaneLenclud@231
|
349 |
//First clean our current panel
|
StephaneLenclud@231
|
350 |
iTableLayoutPanel.Controls.Clear();
|
StephaneLenclud@231
|
351 |
iTableLayoutPanel.RowStyles.Clear();
|
StephaneLenclud@231
|
352 |
iTableLayoutPanel.ColumnStyles.Clear();
|
StephaneLenclud@231
|
353 |
iTableLayoutPanel.RowCount = 0;
|
StephaneLenclud@231
|
354 |
|
StephaneLenclud@231
|
355 |
//We always want two columns: one for label and one for the field
|
StephaneLenclud@231
|
356 |
iTableLayoutPanel.ColumnCount = 2;
|
StephaneLenclud@231
|
357 |
iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
|
StephaneLenclud@231
|
358 |
iTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
|
StephaneLenclud@231
|
359 |
|
StephaneLenclud@231
|
360 |
|
StephaneLenclud@236
|
361 |
if (aObject == null)
|
StephaneLenclud@231
|
362 |
{
|
StephaneLenclud@231
|
363 |
//Just drop it
|
StephaneLenclud@231
|
364 |
return;
|
StephaneLenclud@231
|
365 |
}
|
StephaneLenclud@231
|
366 |
|
StephaneLenclud@236
|
367 |
//IEnumerable<PropertyInfo> properties = aObject.GetType().GetProperties().Where(
|
StephaneLenclud@231
|
368 |
// prop => Attribute.IsDefined(prop, typeof(AttributeObjectProperty)));
|
StephaneLenclud@231
|
369 |
|
StephaneLenclud@231
|
370 |
|
StephaneLenclud@236
|
371 |
foreach (PropertyInfo pi in aObject.GetType().GetProperties())
|
StephaneLenclud@231
|
372 |
{
|
StephaneLenclud@231
|
373 |
AttributeObjectProperty[] attributes = ((AttributeObjectProperty[])pi.GetCustomAttributes(typeof(AttributeObjectProperty), true));
|
StephaneLenclud@231
|
374 |
if (attributes.Length != 1)
|
StephaneLenclud@231
|
375 |
{
|
StephaneLenclud@231
|
376 |
continue;
|
StephaneLenclud@231
|
377 |
}
|
StephaneLenclud@231
|
378 |
|
StephaneLenclud@231
|
379 |
AttributeObjectProperty attribute = attributes[0];
|
StephaneLenclud@231
|
380 |
|
StephaneLenclud@231
|
381 |
//Before anything we need to check if that kind of property is supported by our UI
|
StephaneLenclud@231
|
382 |
//Create the editor
|
StephaneLenclud@236
|
383 |
Control ctrl = CreateControlForProperty(pi, attribute, aObject);
|
StephaneLenclud@231
|
384 |
if (ctrl == null)
|
StephaneLenclud@231
|
385 |
{
|
StephaneLenclud@231
|
386 |
//Property type not supported
|
StephaneLenclud@231
|
387 |
continue;
|
StephaneLenclud@231
|
388 |
}
|
StephaneLenclud@231
|
389 |
|
StephaneLenclud@231
|
390 |
//Add a new row
|
StephaneLenclud@231
|
391 |
iTableLayoutPanel.RowCount++;
|
StephaneLenclud@231
|
392 |
iTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
|
StephaneLenclud@231
|
393 |
//Create the label
|
StephaneLenclud@231
|
394 |
Label label = new Label();
|
StephaneLenclud@231
|
395 |
label.AutoSize = true;
|
StephaneLenclud@231
|
396 |
label.Dock = DockStyle.Fill;
|
StephaneLenclud@231
|
397 |
label.TextAlign = ContentAlignment.MiddleCenter;
|
StephaneLenclud@231
|
398 |
label.Text = attribute.Name;
|
StephaneLenclud@231
|
399 |
toolTip.SetToolTip(label, attribute.Description);
|
StephaneLenclud@231
|
400 |
iTableLayoutPanel.Controls.Add(label, 0, iTableLayoutPanel.RowCount-1);
|
StephaneLenclud@231
|
401 |
|
StephaneLenclud@231
|
402 |
//Add our editor to our form
|
StephaneLenclud@231
|
403 |
iTableLayoutPanel.Controls.Add(ctrl, 1, iTableLayoutPanel.RowCount - 1);
|
StephaneLenclud@231
|
404 |
//Add tooltip to editor too
|
StephaneLenclud@231
|
405 |
toolTip.SetToolTip(ctrl, attribute.Description);
|
StephaneLenclud@231
|
406 |
|
StephaneLenclud@231
|
407 |
}
|
StephaneLenclud@231
|
408 |
|
StephaneLenclud@231
|
409 |
}
|
StephaneLenclud@231
|
410 |
|
StephaneLenclud@231
|
411 |
private void buttonTest_Click(object sender, EventArgs e)
|
StephaneLenclud@231
|
412 |
{
|
StephaneLenclud@231
|
413 |
FetchPropertiesValue(Object);
|
StephaneLenclud@231
|
414 |
|
StephaneLenclud@231
|
415 |
//If our object has a test method with no parameters just run it then
|
StephaneLenclud@231
|
416 |
MethodInfo info = Object.GetType().GetMethod("Test");
|
StephaneLenclud@231
|
417 |
if ( info != null && info.GetParameters().Length==0)
|
StephaneLenclud@231
|
418 |
{
|
StephaneLenclud@231
|
419 |
info.Invoke(Object,null);
|
StephaneLenclud@231
|
420 |
}
|
StephaneLenclud@231
|
421 |
|
StephaneLenclud@231
|
422 |
}
|
StephaneLenclud@231
|
423 |
}
|
StephaneLenclud@231
|
424 |
}
|