Chronos Plugins 5.4.0
This documentation covers the plugin interfaces definitions and an example implementation.
Loading...
Searching...
No Matches
MockDynamicProperties.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Diagnostics.CodeAnalysis;
5using System.Linq;
6using System.Windows.Media;
8
9namespace MockPlugin.Tasks
10{
15 [ScheduleDiagramColor(nameof(Colors.RoyalBlue))]
16 [SuppressMessage("ReSharper", "UnusedMember.Global")]
17 public class FlexibleArguments : CustomTypeDescriptor, ITask
18 {
19 #region ITask implementation, does nothing but show the parameter list in the time table
20
21 public void PreValidate()
22 {
23 // nothing
24 }
25
26 public void PostValidate()
27 {
28 // nothing
29 }
30
31 public void Execute()
32 {
33 // nothing
34 }
35
36 private string GetArgList()
37 {
38 var sb = new System.Text.StringBuilder();
39 foreach (var key in mPropsValues.Keys)
40 {
41 sb.AppendFormat("{0}={1};", key, mPropsValues[key]);
42 }
43 if (sb.Length > 0)
44 {
45 sb.Length -= 1;
46 }
47 else
48 {
49 return "No properties";
50 }
51 return sb.ToString();
52 }
53
54 public string GetTaskAction()
55 {
56 return GetArgList();
57 }
58
59 #endregion ITask implementation, does nothing but show the parameter list in the time table
60
64 private readonly Dictionary<string, object> mPropsValues = new Dictionary<string, object>();
65
67 {
68 // if we don't return this when asked, the ScheduleDiagramColor declaration above will not be seen by Chronos.
69 mAttrs = TypeDescriptor.GetAttributes(this, noCustomTypeDesc: true);
70 // Default descriptor for the only real property
71 var reflProp = TypeDescriptor.GetProperties(this, noCustomTypeDesc: true).Find(nameof(PropCount), false);
72 mProps.Add(reflProp);
73 // Add some fake properties to start with
74 // This one has a unit:
75 mProps.Add(new MyPropertyDescriptor("SomeIntParam", typeof(int), "s"));
76 var resProp = new MyPropertyDescriptor("SomeCalculationResult", typeof(int));
77 resProp.AddAttribute(new ReadOnlyAttribute(true));
78 mProps.Add(resProp);
79 mProps.Add(new MyPropertyDescriptor("SomeStringParam", typeof(string)));
80 mProps.Add(new MyPropertyDescriptor("SomeBoolParam", typeof(bool)));
81 PropCount = (uint)mProps.Count;
82 }
83
84 #region Type descriptor implementation
85
90 private class MyMemberDescriptor : MemberDescriptor
91 {
92 public MyMemberDescriptor(string name)
93 : base(name)
94 { }
95 }
96
100 private class MyPropertyDescriptor : PropertyDescriptor
101 {
102 private readonly List<Attribute> mExtraAttributes = new List<Attribute>();
103 private readonly string mName;
104
105 public MyPropertyDescriptor(string name, Type proptype, string unit = null)
106 : base(new MyMemberDescriptor(name))
107 {
108 mName = name;
109 PropertyType = proptype;
111 }
112
113 public void AddAttribute(Attribute someAttr)
114 {
115 mExtraAttributes.Add(someAttr);
116 }
117
118 protected override void FillAttributes(System.Collections.IList attributeList)
119 {
120 base.FillAttributes(attributeList);
121 foreach (var someAttr in mExtraAttributes)
122 {
123 attributeList.Add(someAttr);
124 }
125 }
126
127 public override Type PropertyType { get; }
128
129 public override Type ComponentType => typeof(FlexibleArguments);
130
131 public override void SetValue(object component, object value)
132 {
133 ((FlexibleArguments) component).mPropsValues[mName] = value;
134 }
135
136 public override object GetValue(object component)
137 {
138 ((FlexibleArguments) component).mPropsValues.TryGetValue(mName, out var retval);
139 return retval;
140 }
141
142 // dont't care about the rest
143 public override bool CanResetValue(object component)
144 {
145 return false;
146 }
147
148 public override void ResetValue(object component)
149 {
150 // nothing
151 }
152
153 public override bool IsReadOnly => mExtraAttributes.OfType<ReadOnlyAttribute>().Any();
154
155 public override bool ShouldSerializeValue(object component)
156 {
157 return true;
158 }
159 }
160
166 public override object GetPropertyOwner(PropertyDescriptor pd)
167 {
168 return mProps.Contains(pd) ? this : null;
169 }
170
174 private PropertyDescriptorCollection mPropDescColl;
175
176 public override PropertyDescriptorCollection GetProperties()
177 {
178 return mPropDescColl;
179 }
180
181 public override AttributeCollection GetAttributes() => mAttrs;
182
183 #endregion Type descriptor implementation
184
185 private readonly List<PropertyDescriptor> mProps = new List<PropertyDescriptor>();
186 private readonly AttributeCollection mAttrs;
187 private uint mPropCount;
188
189 [DynamicPropertyMaster]
190 public uint PropCount
191 {
192 get => mPropCount;
193 set
194 {
195 var oldCount = mPropCount;
196 // fill up to whatever the user wants, but do not let him hide PropCount
197 mPropCount = Math.Max(1, value);
198 if (oldCount != mPropCount)
199 {
200 for (int i = mProps.Count; i < mPropCount; ++i)
201 {
202 mProps.Add(new MyPropertyDescriptor($"FakeProp{i}", typeof(string)));
203 }
204 // when the number is decreased, we just present a part of our internal list
205 mPropDescColl = new PropertyDescriptorCollection(mProps.Take((int)mPropCount).ToArray());
206
207 // the list of dynamic properties has just changed,
208 // alert the reflection manager so the time table can access the new properties
209 TypeDescriptor.Refresh(this);
210 }
211 }
212 }
213 }
214}
Classes and interfaces that are meant for plugins. The classes and interfaces below this namespace ar...
Example task implementations. Since there are lots of things that can be done from a task,...
To be implemented by the "task" part of a Chronos plugin. Public properties of the implementing type ...
Use this on your task property if you want the method editor to show a default unit.
This task demonstrates how to use a custom type descriptor to implement a dynamic list of properties....
override object GetPropertyOwner(PropertyDescriptor pd)
Must be overridden, else you'll get NullRefrences when trying to work with the descriptor.
override PropertyDescriptorCollection GetProperties()
PropertyDescriptorCollection mPropDescColl
Caches the list resulting from set_PropCount.
void Execute()
Do whatever you have to do with your parameters.
string GetTaskAction()
Description of the tasks's action (for hints/time table)
readonly AttributeCollection mAttrs
override AttributeCollection GetAttributes()
readonly Dictionary< string, object > mPropsValues
Storage for fake property names and values.
void PostValidate()
Called after the schedule construction is completed.
void PreValidate()
Called before the schedule construction is completed.
readonly List< PropertyDescriptor > mProps
Member descriptor "implementation" - the defaults are ok for us.
Custom property descriptor, redirecting get/set into our mPropsValues dictionary.
override void FillAttributes(System.Collections.IList attributeList)
override void SetValue(object component, object value)
MyPropertyDescriptor(string name, Type proptype, string unit=null)