chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
using OpenTap;
using OpenTap.Addin.Annotation;
using OpenTapEditor.UI;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Data;
using System.Xml.Serialization;
 
namespace OpenTapEditor.Provider
{
    public class DutControlProvider
    {
        private static ITypeData resourceTypeData = TypeData.FromType(typeof(IResource));
 
 
        public static object? GetItemValue(AnnotationCollection x)
        {
            return (x.Get<IAvailableValuesAnnotation>() as IStringReadOnlyValueAnnotation)?.Value
                                ?? x.Get<IStringValueAnnotation>()?.Value
                                ?? x.Get<IStringReadOnlyValueAnnotation>()?.Value
                                ?? x.Get<IAvailableValuesAnnotationProxy>()?.SelectedValue?.Source?.ToString()
                                ?? x.Get<IObjectValueAnnotation>()?.Value;
        }
 
        public static AnnotationCollection[]? getMembers(AnnotationCollection annotations)
        {
            return annotations?.Get<IMembersAnnotation>()?.Members
                .Where(x =>
                {
                    var member = x.Get<IMemberAnnotation>().Member;
                    if (member.DeclaringType.DescendsTo(TypeData.FromType(typeof(IResource))) && member.Name == nameof(Resource.Name))
                        return true;
 
                    return x.Get<IAccessAnnotation>()?.IsVisible ?? false;
                })
                .Where(x =>
                {
                    var member = x.Get<IMemberAnnotation>()?.Member;
                    if (member == null || (member.GetAttribute<SubmitAttribute>() != null && x.Get<IAvailableValuesAnnotationProxy>() != null)) return false;
                    return FilterMember(member);
                })
                .ToArray();
        }
 
        public static StackPanel test(AnnotationCollection annotation)
        {
            var valueAnnotation = annotation.Get<IObjectValueAnnotation>();
            if (valueAnnotation == null) return null;
            StackPanel stack = new StackPanel()
            {
                Orientation = Orientation.Horizontal
            };
            stack.Children.Add(new TextBlock { Text = annotation.Get<DisplayAttribute>().Name });
 
            /// 列表
            if (annotation.Get<ICollectionAnnotation>() is ICollectionAnnotation caa)
            {
                if (annotation.Get<ReadOnlyMemberAnnotation>() != null) return null;
                var fixedSize = annotation.Get<IFixedSizeCollectionAnnotation>()?.IsFixedSize ?? false;
                var items = caa.AnnotatedElements.Select(a => a.Source).ToArray();
                var dataType = caa.AnnotatedElements.GetType();
                //Type genericArgument = dataType.GetGenericArguments()[0];
 
                bool placeholderElementAdded = false;
                var dataGrid = new DataGrid();
 
                if (annotation.Get<BindingListAttribute>() is BindingListAttribute bla)
                {
                    BindingOperations.SetBinding(dataGrid,
                        DataGrid.ItemsSourceProperty,
                        new Binding(bla.BindingName)
                        {
                            Source = annotation.Source,
                            Mode = BindingMode.TwoWay,
                        });
                    dataGrid.CanUserAddRows = bla.CanAdd;
                    dataGrid.AutoGenerateColumns = false;
 
                    Type propertyType = annotation?.Source?.GetType()?.GetProperty(bla.BindingName)?.PropertyType;
                    if (propertyType?.IsGenericType == true)
                    {
                        DataGridHelper.SetDataGridColumns(dataGrid, propertyType.GetGenericArguments()[0]);
                    }
                    else
                    {
                        DataGridHelper.SetDataGridColumns(dataGrid, propertyType);
                    }
                }
                else
                {
                    dataGrid.ItemsSource = items;
                }
                stack.Children.Add(dataGrid);
                return stack;
            }
 
            // StringValue是正常的输入
            // IAvailableValuesAnnotation 是枚举
            // IStringReadOnlyValueAnnotation是只读
            var stringValueAnnotation = annotation.Get<IStringValueAnnotation>();
            if (stringValueAnnotation == null) return null;
 
            var selectAnnotation = annotation.Get<IAvailableValuesAnnotationProxy>();
            if (selectAnnotation != null)
            {
                var source = selectAnnotation.AvailableValues;
                var comboBox = new ComboBox
                {
                    DisplayMemberPath = "Source.Name",
                    ItemsSource = source
                };
                BindingOperations.SetBinding(comboBox, ComboBox.SelectedItemProperty,
 
                new Binding(nameof(IAvailableValuesAnnotationProxy.SelectedValue))
                {
                    Source = selectAnnotation,
                    Mode = BindingMode.TwoWay,
                    NotifyOnSourceUpdated = true,
                });
                comboBox.SourceUpdated += (sender, e) =>
                {
                    annotation.Write();
                    annotation.Read();
                };
                stack.Children.Add(comboBox);
                return stack;
            }
 
            if (annotation.Get<FilePathAttribute>() is FilePathAttribute fpa)
            {
                FileSelector fs = new FileSelector();
                stack.Children.Add(fs);
                BindingOperations.SetBinding(fs, FileSelector.FilePathProperty,
                    new Binding(nameof(IStringValueAnnotation.Value))
                    {
                        Source = stringValueAnnotation,
                        Mode = BindingMode.TwoWay,
                        NotifyOnSourceUpdated = true
                    });
                fs.SourceUpdated += (sender, e) =>
                {
                    annotation.Write();
                    annotation.Read();
                };
                return stack;
            }
 
 
            if (annotation.Get<DynamicSelectAttribute>() is DynamicSelectAttribute dsa)
            {
                ComboBox combox = new ComboBox();
                stack.Children.Add(combox);
 
                BindingOperations.SetBinding(combox, ComboBox.ItemsSourceProperty,
                    new Binding(dsa.SourceName)
                    {
                        Source = dsa,
                        Mode = BindingMode.OneWay
                    });
 
                BindingOperations.SetBinding(combox, ComboBox.TextProperty,
                new Binding(nameof(stringValueAnnotation.Value))
                {
                    Source = stringValueAnnotation,
                    Mode = BindingMode.TwoWay,
                    NotifyOnSourceUpdated = true
                });
                combox.SourceUpdated += (sender, e) =>
                {
                    annotation.Write();
                    annotation.Read();
                };
                return stack;
            }
 
            TextBox tb = new TextBox();
            stack.Children.Add(tb);
            BindingOperations.SetBinding(tb, TextBox.TextProperty,
                new Binding(nameof(IStringValueAnnotation.Value))
                {
                    Source = stringValueAnnotation,
                    Mode = BindingMode.TwoWay,
                    NotifyOnSourceUpdated = true
                });
            tb.SourceUpdated += (sender, e) =>
            {
                annotation.Write();
                annotation.Read();
            };
 
            //var update = annotation.Get<UpdateMonitor>(true);
            //if (update != null)
            //{
            //    update.RegisterSourceUpdated(tb, () => tb.GetBindingExpression(TextBox.TextProperty).UpdateTarget());
            //}
            return stack;
        }
 
        public static bool FilterMember(IMemberData member)
        {
            if (member.DeclaringType.DescendsTo(resourceTypeData) && member.Name == nameof(Resource.Name))
                return true;
            if (member.GetAttribute<BrowsableAttribute>() is BrowsableAttribute attr)
                return attr.Browsable;
            if (member.HasAttribute<OutputAttribute>())
                return true;
            return member.Attributes.Any(a => a is XmlIgnoreAttribute) == false && member.Writable;
        }
 
        public static StackPanel GetControl(AnnotationCollection annotation)
        {
            var stack = new StackPanel()
            {
 
            };
 
            var groupCache = new Dictionary<string, Expander>();
 
            Expander GetExpander(List<string> groups)
            {
                Expander groupNode = null;
                Expander lastGroup = null;
                string groupKey = null;
                foreach (var group in groups)
                {
                    groupKey = string.IsNullOrEmpty(groupKey) ? group : (groupKey + "." + group);
                    groupNode = null;
                    if (groupCache.TryGetValue(groupKey, out groupNode) == false)
                    {
                        // add the group
                        groupNode = new Expander
                        {
                            IsExpanded = true,
                            Header = groupKey,
                            Content = new StackPanel()
                        };
                        groupCache[groupKey] = groupNode;
 
                        if (lastGroup != null)
                        {
                            ((StackPanel)lastGroup.Content).Children.Add(groupNode);
                        }
                        else
                        {
                            stack.Children.Add(groupNode);
                        }
                    }
                    lastGroup = groupNode;
                }
                return groupNode;
            }
 
            var members = getMembers(annotation);
            foreach (var ann in members)
            {
                var control = test(ann);
                if (control == null) continue;
                var _groups = ann?.Get<DisplayAttribute>().Group.ToList();
                if (_groups == null || _groups.Count <= 0)
                {
                    stack.Children.Add(control);
                }
                else
                {
                    var expander = GetExpander(_groups);
                    ((StackPanel)expander.Content).Children.Add(control);
                }
            }
            return stack;
        }
    }
}