10using System.ComponentModel;
11using System.Collections.Generic;
12using System.Windows.Forms;
13using System.Reflection;
23 [DesignerCategory(
"")]
24 [ProvideProperty(
"ApplyAuthorization", typeof(Control))]
25 [ToolboxItem(
true), ToolboxBitmap(typeof(ReadWriteAuthorization),
"Csla.Windows.ReadWriteAuthorization")]
29 private class ControlStatus
31 public bool ApplyAuthorization {
get;
set; }
32 public bool CanRead {
get;
set; }
35 private readonly Dictionary<Control, ControlStatus> _sources =
36 new Dictionary<Control, ControlStatus>();
43 { container.Add(
this); }
56 if (IsPropertyImplemented(extendee,
"ReadOnly")
57 || IsPropertyImplemented(extendee,
"Enabled"))
72 if (_sources.TryGetValue(source, out result))
73 return result.ApplyAuthorization;
88 if (_sources.TryGetValue(source, out status))
89 status.ApplyAuthorization = value;
93 new ControlStatus { ApplyAuthorization = value, CanRead =
true });
111 foreach (var item
in _sources)
112 if (item.Value.ApplyAuthorization)
113 ApplyAuthorizationRules(item.Key);
116 private void ApplyAuthorizationRules(Control control)
118 foreach (Binding binding
in control.DataBindings)
121 if (binding.DataSource is BindingSource)
124 (BindingSource)binding.DataSource;
131 string propertyName =
132 binding.BindingMemberInfo.BindingField;
136 ds.CanReadProperty(propertyName));
139 ds.CanWriteProperty(propertyName));
145 private void ApplyReadRules(
146 Control ctl, Binding binding,
149 var status = GetControlStatus(ctl);
158 binding.Format -= ReturnEmpty;
167 binding.Format += ReturnEmpty;
171 var propertyInfo = ctl.GetType().GetProperty(binding.PropertyName,
172 BindingFlags.FlattenHierarchy |
173 BindingFlags.Instance |
174 BindingFlags.Public);
175 if (propertyInfo !=
null)
177 propertyInfo.SetValue(ctl,
179 Utilities.GetPropertyType(
180 propertyInfo.PropertyType)),
186 status.CanRead = canRead;
189 private void ApplyWriteRules(
190 Control ctl, Binding binding,
193 if (ctl is Label)
return;
196 PropertyInfo propertyInfo =
197 ctl.GetType().GetProperty(
"ReadOnly",
198 BindingFlags.FlattenHierarchy |
199 BindingFlags.Instance |
200 BindingFlags.Public);
201 if (propertyInfo !=
null)
204 (!(bool)propertyInfo.GetValue(
205 ctl,
new object[] { }));
206 propertyInfo.SetValue(
207 ctl, !canWrite,
new object[] { });
208 if ((!couldWrite) && (canWrite))
213 bool couldWrite = ctl.Enabled;
214 ctl.Enabled = canWrite;
215 if ((!couldWrite) && (canWrite))
220 private void ReturnEmpty(
221 object sender, ConvertEventArgs e)
223 e.Value = GetEmptyValue(e.DesiredType);
226 private object GetEmptyValue(Type desiredType)
228 object result =
null;
229 if (desiredType.IsValueType)
230 result = Activator.CreateInstance(desiredType);
234 private static bool IsPropertyImplemented(
235 object obj,
string propertyName)
237 if (obj.GetType().GetProperty(propertyName,
238 BindingFlags.FlattenHierarchy |
239 BindingFlags.Instance |
240 BindingFlags.Public) !=
null)
246 private ControlStatus GetControlStatus(Control control)
248 return _sources[control];
Windows Forms extender control that automatically enables and disables detail form controls based on ...
void ResetControlAuthorization()
Causes the ReadWriteAuthorization control to apply authorization rules from the business object to al...
bool CanExtend(object extendee)
Gets a value indicating whether the extender control can extend the specified control.
bool GetApplyAuthorization(Control source)
Gets the custom ApplyAuthorization extender property added to extended controls.
void SetApplyAuthorization(Control source, bool value)
Sets the custom ApplyAuthorization extender property added to extended controls.
ReadWriteAuthorization(IContainer container)
Creates an instance of the object.
Defines the authorization interface through which an object can indicate which properties the current...