-
Notifications
You must be signed in to change notification settings - Fork 80
/
EmployeeDetailForm.cs
92 lines (91 loc) · 3.37 KB
/
EmployeeDetailForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.Security;
using DevExpress.Xpo;
using DevExpress.XtraEditors;
using DevExpress.XtraLayout;
using BusinessObjectsLibrary.BusinessObjects;
namespace WindowsFormsApplication {
public partial class EmployeeDetailForm : DevExpress.XtraBars.Ribbon.RibbonForm {
private IObjectSpace securedObjectSpace;
private SecurityStrategyComplex security;
private IObjectSpaceProvider objectSpaceProvider;
private Employee employee;
private Dictionary<string, string> visibleMembers;
public EmployeeDetailForm(Employee employee) {
InitializeComponent();
this.employee = employee;
visibleMembers = new Dictionary<string, string> {
{ nameof(Employee.FirstName), "First Name:" },
{ nameof(Employee.LastName), "Last Name:" },
{ nameof(Employee.Department), "Department:" }
};
}
private void EmployeeDetailForm_Load(object sender, EventArgs e) {
security = ((MainForm)MdiParent).Security;
objectSpaceProvider = ((MainForm)MdiParent).ObjectSpaceProvider;
securedObjectSpace = objectSpaceProvider.CreateObjectSpace();
if(employee == null) {
employee = securedObjectSpace.CreateObject<Employee>();
}
else {
employee = securedObjectSpace.GetObject(employee);
deleteBarButtonItem.Enabled = security.CanDelete(employee);
}
employeeBindingSource.DataSource = employee;
AddControls();
}
private void AddControls() {
foreach(KeyValuePair<string, string> pair in visibleMembers) {
string memberName = pair.Key;
string caption = pair.Value;
AddControl(dataLayoutControl1.AddItem(), employee, memberName, caption);
}
}
private void AddControl(LayoutControlItem layout, object targetObject, string memberName, string caption) {
layout.Text = caption;
Type type = targetObject.GetType();
BaseEdit control;
if(security.CanRead(targetObject, memberName)) {
control = GetControl(type, memberName);
if(control != null) {
control.DataBindings.Add(new Binding("EditValue", employeeBindingSource, memberName, true, DataSourceUpdateMode.OnPropertyChanged));
control.Enabled = security.CanWrite(targetObject, memberName);
}
}
else {
control = new ProtectedContentEdit();
control.Enabled = false;
}
dataLayoutControl1.Controls.Add(control);
layout.Control = control;
}
private BaseEdit GetControl(Type type, string memberName) {
BaseEdit control = null;
ITypeInfo typeInfo = securedObjectSpace.TypesInfo.FindTypeInfo(type);
IMemberInfo memberInfo = typeInfo.FindMember(memberName);
if(memberInfo != null) {
if(memberInfo.IsAssociation) {
control = new ComboBoxEdit();
((ComboBoxEdit)control).Properties.Items.AddRange(securedObjectSpace.GetObjects<Department>() as XPCollection<Department>);
}
else {
control = new TextEdit();
}
}
return control;
}
private void SaveBarButtonItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
securedObjectSpace.CommitChanges();
Close();
}
private void CloseBarButtonItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
Close();
}
private void DeleteBarButtonItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
securedObjectSpace.Delete(employee);
securedObjectSpace.CommitChanges();
Close();
}
}
}