using System; using System.ComponentModel; namespace Netron.Diagramming.Core { /// /// An inherited property descriptor which can be created dynamically on the basis of a specification. /// internal class PropertySpecDescriptor : PropertyDescriptor { #region events /// /// Occurs when the property grid accesses the value of the property /// public event EventHandler OnGetValue; /// /// Occurs when the property grid tries to set the value of the property /// public event EventHandler OnSetValue; #endregion #region Fields private PropertySpec item; #endregion #region Constructor /// /// Initializes a new instance of the class. /// /// The item. /// the attributes to be used on the descriptor. Note that the attributes of the have to be previously taken over and overloaded. public PropertySpecDescriptor(PropertySpec item, Attribute[] attributes) : base(item.Name, attributes) { this.item = item; } #endregion #region Methods /// /// When overridden in a derived class, gets the type of the component this property is bound to. /// /// /// A that represents the type of component this property is bound to. When the or methods are invoked, the object specified might be an instance of this type. public override Type ComponentType { get { return item.GetType(); } } /// /// When overridden in a derived class, gets a value indicating whether this property is read-only. /// /// /// true if the property is read-only; otherwise, false. public override bool IsReadOnly { get { return (Attributes.Matches(ReadOnlyAttribute.Yes)); } } /// /// When overridden in a derived class, gets the type of the property. /// /// /// A that represents the type of the property. public override Type PropertyType { get { return Type.GetType(item.TypeName); } } /// /// When overridden in a derived class, returns whether resetting an object changes its value. /// /// The component to test for reset capability. /// /// true if resetting the component changes its value; otherwise, false. /// public override bool CanResetValue(object component) { if (item.DefaultValue == null) return false; else return !this.GetValue(component).Equals(item.DefaultValue); } /// /// When overridden in a derived class, gets the current value of the property on a component. /// /// The component with the property for which to retrieve the value. /// /// The value of a property for a given component. /// public override object GetValue(object component) { // Have the property bag raise an event to get the current value // of the property. PropertyEventArgs e = new PropertyEventArgs(component, base.Name, null); RaiseOnGetValue(e); return e.Value; } /// /// Raises the on get value. /// /// The instance containing the event data. private void RaiseOnGetValue(PropertyEventArgs e) { EventHandler handler = OnGetValue; if (handler != null) handler(this, e); } /// /// Raises the on set value. /// /// The instance containing the event data. private void RaiseOnSetValue(PropertyEventArgs e) { EventHandler handler = OnSetValue; if (handler != null) handler(this, e); } /// /// When overridden in a derived class, resets the value for this property of the component to the default value. /// /// The component with the property value that is to be reset to the default value. public override void ResetValue(object component) { SetValue(component, item.DefaultValue); } /// /// When overridden in a derived class, sets the value of the component to a different value. /// /// The component with the property value that is to be set. /// The new value. public override void SetValue(object component, object value) { // Have the property bag raise an event to set the current value // of the property. PropertyEventArgs e = new PropertyEventArgs(component, Name, value); RaiseOnSetValue(e); } /// /// When overridden in a derived class, determines a value indicating whether the value of this property needs to be persisted. /// /// The component with the property to be examined for persistence. /// /// true if the property should be persisted; otherwise, false. /// public override bool ShouldSerializeValue(object component) { object val = this.GetValue(component); if (item.DefaultValue == null && val == null) return false; else return !val.Equals(item.DefaultValue); } #endregion } }