1 | using System;
|
---|
2 | using System.ComponentModel;
|
---|
3 | namespace Netron.Diagramming.Core {
|
---|
4 | /// <summary>
|
---|
5 | /// An inherited property descriptor which can be created dynamically on the basis of a <see cref="PropertySpec"/> specification.
|
---|
6 | /// </summary>
|
---|
7 | internal class PropertySpecDescriptor : PropertyDescriptor {
|
---|
8 | #region events
|
---|
9 | /// <summary>
|
---|
10 | /// Occurs when the property grid accesses the value of the property
|
---|
11 | /// </summary>
|
---|
12 | public event EventHandler<PropertyEventArgs> OnGetValue;
|
---|
13 | /// <summary>
|
---|
14 | /// Occurs when the property grid tries to set the value of the property
|
---|
15 | /// </summary>
|
---|
16 | public event EventHandler<PropertyEventArgs> OnSetValue;
|
---|
17 |
|
---|
18 | #endregion
|
---|
19 |
|
---|
20 | #region Fields
|
---|
21 | private PropertySpec item;
|
---|
22 | #endregion
|
---|
23 |
|
---|
24 | #region Constructor
|
---|
25 | /// <summary>
|
---|
26 | /// Initializes a new instance of the <see cref="T:PropertySpecDescriptor"/> class.
|
---|
27 | /// </summary>
|
---|
28 | /// <param name="item">The item.</param>
|
---|
29 | /// <param name="attributes">the attributes to be used on the descriptor. Note that the attributes of the <see cref="PropertySpec"/> have to be previously taken over and overloaded.</param>
|
---|
30 | public PropertySpecDescriptor(PropertySpec item, Attribute[] attributes)
|
---|
31 | : base(item.Name, attributes) {
|
---|
32 | this.item = item;
|
---|
33 | }
|
---|
34 | #endregion
|
---|
35 |
|
---|
36 | #region Methods
|
---|
37 |
|
---|
38 | /// <summary>
|
---|
39 | /// When overridden in a derived class, gets the type of the component this property is bound to.
|
---|
40 | /// </summary>
|
---|
41 | /// <value></value>
|
---|
42 | /// <returns>A <see cref="T:System.Type"></see> that represents the type of component this property is bound to. When the <see cref="M:System.ComponentModel.PropertyDescriptor.GetValue(System.Object)"></see> or <see cref="M:System.ComponentModel.PropertyDescriptor.SetValue(System.Object,System.Object)"></see> methods are invoked, the object specified might be an instance of this type.</returns>
|
---|
43 | public override Type ComponentType {
|
---|
44 | get {
|
---|
45 | return item.GetType();
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | /// <summary>
|
---|
50 | /// When overridden in a derived class, gets a value indicating whether this property is read-only.
|
---|
51 | /// </summary>
|
---|
52 | /// <value></value>
|
---|
53 | /// <returns>true if the property is read-only; otherwise, false.</returns>
|
---|
54 | public override bool IsReadOnly {
|
---|
55 | get {
|
---|
56 | return (Attributes.Matches(ReadOnlyAttribute.Yes));
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | /// <summary>
|
---|
61 | /// When overridden in a derived class, gets the type of the property.
|
---|
62 | /// </summary>
|
---|
63 | /// <value></value>
|
---|
64 | /// <returns>A <see cref="T:System.Type"></see> that represents the type of the property.</returns>
|
---|
65 | public override Type PropertyType {
|
---|
66 | get {
|
---|
67 | return Type.GetType(item.TypeName);
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | /// <summary>
|
---|
72 | /// When overridden in a derived class, returns whether resetting an object changes its value.
|
---|
73 | /// </summary>
|
---|
74 | /// <param name="component">The component to test for reset capability.</param>
|
---|
75 | /// <returns>
|
---|
76 | /// true if resetting the component changes its value; otherwise, false.
|
---|
77 | /// </returns>
|
---|
78 | public override bool CanResetValue(object component) {
|
---|
79 | if (item.DefaultValue == null)
|
---|
80 | return false;
|
---|
81 | else
|
---|
82 | return !this.GetValue(component).Equals(item.DefaultValue);
|
---|
83 | }
|
---|
84 |
|
---|
85 | /// <summary>
|
---|
86 | /// When overridden in a derived class, gets the current value of the property on a component.
|
---|
87 | /// </summary>
|
---|
88 | /// <param name="component">The component with the property for which to retrieve the value.</param>
|
---|
89 | /// <returns>
|
---|
90 | /// The value of a property for a given component.
|
---|
91 | /// </returns>
|
---|
92 | public override object GetValue(object component) {
|
---|
93 | // Have the property bag raise an event to get the current value
|
---|
94 | // of the property.
|
---|
95 |
|
---|
96 | PropertyEventArgs e = new PropertyEventArgs(component, base.Name, null);
|
---|
97 | RaiseOnGetValue(e);
|
---|
98 | return e.Value;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /// <summary>
|
---|
102 | /// Raises the on get value.
|
---|
103 | /// </summary>
|
---|
104 | /// <param name="e">The <see cref="T:Netron.Diagramming.Core.PropertyEventArgs"/> instance containing the event data.</param>
|
---|
105 | private void RaiseOnGetValue(PropertyEventArgs e) {
|
---|
106 | EventHandler<PropertyEventArgs> handler = OnGetValue;
|
---|
107 | if (handler != null)
|
---|
108 | handler(this, e);
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | /// <summary>
|
---|
113 | /// Raises the on set value.
|
---|
114 | /// </summary>
|
---|
115 | /// <param name="e">The <see cref="T:Netron.Diagramming.Core.PropertyEventArgs"/> instance containing the event data.</param>
|
---|
116 | private void RaiseOnSetValue(PropertyEventArgs e) {
|
---|
117 | EventHandler<PropertyEventArgs> handler = OnSetValue;
|
---|
118 | if (handler != null)
|
---|
119 | handler(this, e);
|
---|
120 | }
|
---|
121 |
|
---|
122 | /// <summary>
|
---|
123 | /// When overridden in a derived class, resets the value for this property of the component to the default value.
|
---|
124 | /// </summary>
|
---|
125 | /// <param name="component">The component with the property value that is to be reset to the default value.</param>
|
---|
126 | public override void ResetValue(object component) {
|
---|
127 | SetValue(component, item.DefaultValue);
|
---|
128 | }
|
---|
129 |
|
---|
130 | /// <summary>
|
---|
131 | /// When overridden in a derived class, sets the value of the component to a different value.
|
---|
132 | /// </summary>
|
---|
133 | /// <param name="component">The component with the property value that is to be set.</param>
|
---|
134 | /// <param name="value">The new value.</param>
|
---|
135 | public override void SetValue(object component, object value) {
|
---|
136 | // Have the property bag raise an event to set the current value
|
---|
137 | // of the property.
|
---|
138 |
|
---|
139 | PropertyEventArgs e = new PropertyEventArgs(component, Name, value);
|
---|
140 | RaiseOnSetValue(e);
|
---|
141 | }
|
---|
142 |
|
---|
143 | /// <summary>
|
---|
144 | /// When overridden in a derived class, determines a value indicating whether the value of this property needs to be persisted.
|
---|
145 | /// </summary>
|
---|
146 | /// <param name="component">The component with the property to be examined for persistence.</param>
|
---|
147 | /// <returns>
|
---|
148 | /// true if the property should be persisted; otherwise, false.
|
---|
149 | /// </returns>
|
---|
150 | public override bool ShouldSerializeValue(object component) {
|
---|
151 | object val = this.GetValue(component);
|
---|
152 |
|
---|
153 | if (item.DefaultValue == null && val == null)
|
---|
154 | return false;
|
---|
155 | else
|
---|
156 | return !val.Equals(item.DefaultValue);
|
---|
157 | }
|
---|
158 |
|
---|
159 | #endregion
|
---|
160 | }
|
---|
161 | }
|
---|