Extension.cs
9.1 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Reflection;
using Asa.FaceControl;
using Model;
namespace BLL
{
public class Extension : IDisposable
{
public event IExtension.PrintDelegate Printing;
private readonly IExtension currentExtension;
private readonly Dictionary<string, IExtension> dicExtension;
private readonly Alcoelectro alcoelectro;
private readonly Inventec inventec;
private readonly PanaCIM panaCIM;
private readonly NanRui nanRui;
private readonly KaiFa kaiFa;
private readonly Dictionary<string, Func<ControlBase>> dicControls;
private readonly Dictionary<Type, Action<PropertyInfo, ControlBase, object>> dicControlAttribute;
private readonly List<ExtensionControl> ctlGroup = new();
private int ctlX, ctlY;
private int pnlWidth;
private FacePanel panel;
private System.Drawing.Point startPoint;
public Extension(Config config)
{
LogNet.log.Info("加载扩展:" + config.ExtensionName);
alcoelectro = new(config);
inventec = new(config);
panaCIM = new(config);
nanRui = new(config);
kaiFa = new(config);
dicExtension = new()
{
{ "Bwit", alcoelectro },
{ "Inventec", inventec },
{ "PanaCIM", panaCIM },
{ "NanRui", nanRui },
{ "KaiFa", kaiFa }
};
for (int i = 0; i < dicExtension.Count; i++)
dicExtension.ElementAt(i).Value.Printing += new IExtension.PrintDelegate(
delegate (Dictionary<string, string> content) { Printing?.Invoke(content); });
dicControls = new()
{
{ "Label", () => { return new FaceLabel(); } },
{ "TextBox", () => { return new FaceTextBox(); } },
{ "Button", () => { return new FaceButton(); } },
{ "ComboBox", () => { return new FaceComboBox(); } },
{ "CheckBox", () => { return new FaceCheckBox(); } },
{ "ListBox", () => { return new FaceListBox(); } },
{ "PictureBox", () => { return new FacePictureBox(); } },
{ "NumericUpDown", () => { return new FaceNumericUpDown(); } },
{ "RadioBox", () => { return new FaceRadioBox(); } }
};
dicControlAttribute = new()
{
{ typeof(string), AttributeString },
{ typeof(int), AttributeInt32 },
{ typeof(float), AttributeSingle },
{ typeof(bool), AttributeBoolean },
{ typeof(System.Drawing.Color), AttributeColor },
{ typeof(System.Drawing.ContentAlignment), AttributeContentAlignment },
{ typeof(System.Drawing.Font), AttributeFont },
{ typeof(System.Windows.Forms.HorizontalAlignment), AttributeHorizontalAlignment }
};
currentExtension = dicExtension[config.ExtensionName];
}
public void Dispose()
{
currentExtension.Dispose();
}
public void LoadPanel(FacePanel pnl)
{
panel = pnl;
pnlWidth = pnl.Width;
startPoint = new(6, panel.TextHeight + 6);
if (!System.IO.File.Exists(FilePath.CONFIG_EXTENSION)) return;
string json = System.IO.File.ReadAllText(FilePath.CONFIG_EXTENSION);
JavaScriptSerializer serializer = new();
object[] rows = (object[])serializer.DeserializeObject(json);
ctlY = startPoint.Y;
for (int i = 0; i < rows.Length; i++)
LoadRow(rows[i]);
currentExtension.Load(ctlGroup);
}
public void Clear()
{
currentExtension.Clear();
}
public void SetKey(string[] originalCode, Dictionary<string, string> key)
{
currentExtension.SetKey(originalCode, key);
}
public void Update()
{
currentExtension.Update();
}
private void LoadRow(object row)
{
object[] cols = (object[])row;
int maxHeight = 0;
ctlX = startPoint.X;
for (int i = 0; i < cols.Length; i++)
{
Dictionary<string, object> dic = (Dictionary<string, object>)cols[i];
if (!dic.ContainsKey("Type")) continue;
ControlBase ctl = dicControls[dic["Type"].ToString()].Invoke();
ctl.Location = new System.Drawing.Point(ctlX, ctlY);
panel.Controls.Add(ctl);
ExtensionControl ext = new() { Control = ctl };
PropertyInfo[] info = ext.GetType().GetProperties();
for (int j = 0; j < info.Length; j++)
{
if (dic.ContainsKey(info[j].Name))
{
if (info[j].PropertyType == typeof(bool))
info[j].SetValue(ext, Convert.ToBoolean(dic[info[j].Name]));
else
info[j].SetValue(ext, dic[info[j].Name].ToString());
}
}
ctlGroup.Add(ext);
if (dic.ContainsKey("Attribute"))
LoadAttribute(ctl, (Dictionary<string, object>)dic["Attribute"]);
ctlX += ctl.Width + 6;
if (maxHeight < ctl.Height)
maxHeight = ctl.Height;
if (dic.ContainsKey("Event"))
LoadEvent(ctl, (Dictionary<string, object>)dic["Event"]);
}
ctlY += maxHeight + 6;
}
private void LoadAttribute(ControlBase ctl, Dictionary<string, object> att)
{
Type type = ctl.GetType();
foreach (string key in att.Keys)
{
PropertyInfo info = type.GetProperty(key);
if (info == null) continue;
dicControlAttribute[info.PropertyType].Invoke(info, ctl, att[key]);
}
}
private void LoadEvent(ControlBase ctl, Dictionary<string, object> att)
{
try
{
Type type = ctl.GetType();
foreach (string key in att.Keys)
{
EventInfo info = type.GetEvent(key);
if (info == null) continue;
Delegate handler = Delegate.CreateDelegate(info.EventHandlerType, currentExtension, att[key].ToString());
info.AddEventHandler(ctl, handler);
}
}
catch (Exception ex)
{
LogNet.log.Error("LoadEvent", ex);
}
}
private void AttributeInt32(PropertyInfo info, ControlBase ctl, object obj)
{
if (int.TryParse(obj.ToString(), out int result))
{
if (info.Name == "Width" && result < 1)
info.SetValue(ctl, pnlWidth - ctl.Left - 6);
else
info.SetValue(ctl, result);
}
}
private void AttributeString(PropertyInfo info, ControlBase ctl, object obj)
{
info.SetValue(ctl, obj.ToString());
}
private void AttributeBoolean(PropertyInfo info, ControlBase ctl, object obj)
{
if (bool.TryParse(obj.ToString(), out bool result))
info.SetValue(ctl, result);
}
private void AttributeSingle(PropertyInfo info, ControlBase ctl, object obj)
{
if (float.TryParse(obj.ToString(), out float result))
info.SetValue(ctl, result);
}
private void AttributeContentAlignment(PropertyInfo info, ControlBase ctl, object obj)
{
System.Drawing.ContentAlignment alignment = (System.Drawing.ContentAlignment)Enum.Parse(typeof(System.Drawing.ContentAlignment), obj.ToString());
info.SetValue(ctl, alignment);
}
private void AttributeColor(PropertyInfo info, ControlBase ctl, object obj)
{
string[] s = obj.ToString().Split(',');
if (s.Length != 3) return;
if (!int.TryParse(s[0], out int r)) return;
if (!int.TryParse(s[1], out int g)) return;
if (!int.TryParse(s[2], out int b)) return;
System.Drawing.Color color = System.Drawing.Color.FromArgb(r, g, b);
info.SetValue(ctl, color);
}
private void AttributeFont(PropertyInfo info, ControlBase ctl, object obj)
{
System.Drawing.Font font = ObjConversion.StrToFont(obj.ToString());
info.SetValue(ctl, font);
}
private void AttributeHorizontalAlignment(PropertyInfo info, ControlBase ctl, object obj)
{
System.Windows.Forms.HorizontalAlignment alignment = (System.Windows.Forms.HorizontalAlignment)Enum.Parse(typeof(System.Windows.Forms.HorizontalAlignment), obj.ToString());
info.SetValue(ctl, alignment);
}
}
}