alielie
2026-09-10 9ec80cafc23f5ee3278cacce3c9f86f4087b435a
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
using AddInPlugin;
using AddInPlugin.Entity;
using CommunityToolkit.Mvvm.ComponentModel;
using OpenTap;
using OpenTap.Addin.Util;
using OpenTapEditor.Provider;
using OpenTapEditor.Util;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
 
namespace OpenTapEditor
{
    /// <summary>
    /// AdapterSetting.xaml 的交互逻辑
    /// </summary>
    [ObservableObject]
    public partial class AdapterSetting : Window
    {
        [ObservableProperty]
        private string stepName;
 
        [ObservableProperty]
        private string stepGroup;
 
        [ObservableProperty]
        private string typeName;
 
        [ObservableProperty]
        private List<string> groupSource = new List<string>();
 
        private ITestStep step;
 
        private AdapterData data;
 
        private bool isEdit;
 
        private IList<AdapterData> datasource;
 
        public AdapterSetting()
        {
            DataContext = this;
            InitializeComponent();
            GroupSource = StepTypeLoader.CustomSteps.Select(s => s.GroupBy)
                .Distinct().ToList();
        }
 
        public bool? Open(ITestStep step, IList<AdapterData> datasource)
        {
            try
            {
                this.datasource = datasource;
                TypeName = step.GetType().Name;
                this.step = step;
                data = new AdapterData();
                isEdit = false;
                UpdateSource(this.step);
            }
            catch (Exception ex)
            {
 
            }
            return this.ShowDialog();
        }
 
        public bool? Open(AdapterData data, IList<AdapterData> datasource, bool isEdit)
        {
            this.isEdit = isEdit;
            StepGroup = data.Group;
            if (isEdit)
            {
                StepName = data.Name;
                this.data = data;
            }
            else
            {
                this.data = new AdapterData();
            }
 
            this.datasource = datasource;
            TapSerializer serializer = new TapSerializer();
            ITestStep step = (ITestStep)serializer.DeserializeFromString(data.Xml, TypeData.FromType(typeof(ITestStep)));
            this.step = step;
            UpdateSource(this.step);
            return this.ShowDialog();
        }
 
        private void UpdateSource(object obj)
        {
            //var annotations = AnnotationCollection.Annotate(obj);
            view.Children.Clear();
            //view.Children.Add(DutControlProvider.GetControl(annotations));
            view.Children.Add(FormGroupProvider.GetControl(obj, null));
        }
 
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var btn = (Button)sender;
            if (btn.IsCancel)
            {
                this.Close();
                return;
            }
 
            try
            {
                if (string.IsNullOrEmpty(StepName))
                {
                    FgName.ValidateResult = Panuon.WPF.UI.ValidateResult.Error;
                    FgName.Message = "请输入名称";
                    return;
                }
                var temp = datasource.FirstOrDefault(e => e.Name == this.StepName);
 
                if (temp != null && temp != data)
                {
                    FgName.ValidateResult = Panuon.WPF.UI.ValidateResult.Error;
                    FgName.Message = "名称不能重复";
                    return;
                }
 
                TapSerializer serializer = new TapSerializer();
                using (var str = new MemoryStream())
                {
                    serializer.Serialize(str, this.step);
                    var xmlCache = Encoding.UTF8.GetString(str.ToArray());
                    //XmlDocument doc = new XmlDocument();
                    //doc.LoadXml(xmlCache);
                    //XmlNode myNode = doc.DocumentElement;
 
                    data.Name = this.StepName;
                    data.Group = this.StepGroup;
                    data.Xml = xmlCache;
                    data.DataValue = new SerializableDictionary<string, string>();
                    if (this.step is VariableTestStep vts && vts.MethodVariables != null)
                    {
                        foreach (var mv in vts.MethodVariables)
                        {
                            data.DataValue[mv.Name] = mv.ValueStr;
                        }
                    }
                    if (!isEdit)
                    {
                        datasource.Add(data);
                    }
                    StepTypeLoader.SaveAdapterStep();
                    this.Close();
                }
            }
            catch (Exception ex)
            {
 
            }
        }
 
        private void ComboBox_GotFocus(object sender, RoutedEventArgs e)
        {
            var comboBox = (ComboBox)sender;
            comboBox.IsDropDownOpen = true;
        }
 
        private void ComboBox_Loaded(object sender, RoutedEventArgs e)
        {
            var comboBox = (ComboBox)sender;
            comboBox.AddHandler(TextBoxBase.TextChangedEvent, new TextChangedEventHandler(ComboBox_GotFocus));
            comboBox.Loaded -= ComboBox_Loaded;
        }
    }
}