领胜LDS 键盘AOI检测项目
wells.liu
2020-06-28 19a9489aef9b3353171eca5e6b583aadb2828593
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
using Autofac;
using Bro.Common.Base;
using Bro.Common.Factory;
using Bro.Common.Helper;
using Bro.Common.Interface;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace Bro.Common.Model.Forms
{
    public partial class FrmDeviceOpConfigEditor : Form
    {
        public FrmDeviceOpConfigEditor()
        {
            InitializeComponent();
        }
 
        //private DeviceOpBind bind = null;
        public DeviceOpBind Bind { get; set; }
 
        IOperationConfig backOpConfig = new OperationConfigBase();
        List<IDevice> deviceList = null;
        IDevice currentDevice = null;
        public FrmDeviceOpConfigEditor(DeviceOpBind bind)
        {
            InitializeComponent();
 
            InitialDeviceCbo();
 
            Bind = bind;
            if (!string.IsNullOrWhiteSpace(bind.Device))
            {
                cboDevice.SelectedValue = bind.Device;
 
                currentDevice = deviceList.FirstOrDefault(u => u.Id == bind.Device);
                if (currentDevice != null)
                {
                    var attr = currentDevice.GetType().GetCustomAttribute<DeviceAttribute>();
                    if (attr != null)
                    {
                        backOpConfig = ConfigFactory.GetOperationConfig(attr.TypeCode);
                    }
                }
            }
            else
            {
                if (cboDevice.Items.Count > 0)
                    cboDevice.SelectedIndex = 0;
            }
 
            cboDevice.SelectedIndexChanged += CboDevice_SelectedIndexChanged;
 
            if (bind.OpConfig == null)
            {
                bind.OpConfig = new OperationConfigBase();
 
                if (currentDevice != null)
                {
                    var attr = currentDevice.GetType().GetCustomAttribute<DeviceAttribute>();
                    if (attr != null)
                        bind.OpConfig = ConfigFactory.GetOperationConfig(attr.TypeCode);
                }
            }
 
            backOpConfig.DataFrom(bind.OpConfig);
 
            propGrid.SelectedObject = bind.OpConfig;
        }
 
        private void CboDevice_SelectedIndexChanged(object sender, EventArgs e)
        {
            ChangeOpConfigByDevice();
        }
 
        private void ChangeOpConfigByDevice()
        {
            var device = deviceList.FirstOrDefault(u => u.Id == cboDevice.SelectedValue.ToString());
            if (device != null)
            {
                var attr = device.GetType().GetCustomAttribute<DeviceAttribute>();
                if (attr != null)
                {
                    propGrid.SelectedObject = ConfigFactory.GetOperationConfig(attr.TypeCode);
                }
            }
        }
 
        private void InitialDeviceCbo()
        {
            using (var scope = GlobalVar.Container.BeginLifetimeScope())
            {
                deviceList = scope.Resolve<List<IDevice>>();
                if (deviceList.Count > 0)
                {
                    UIHelper.SetCombo(cboDevice, new List<ISimpleDevice>(deviceList), "Name", "Id");
                }
            }
        }
 
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Bind.OpConfig = backOpConfig;
            this.DialogResult = DialogResult.Cancel;
        }
 
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            Bind.Device = cboDevice.SelectedValue.ToString();
            Bind.OpConfig = propGrid.SelectedObject as IOperationConfig;
            this.DialogResult = DialogResult.OK;
        }
 
        private void btnReset_Click(object sender, EventArgs e)
        {
            ChangeOpConfigByDevice();
        }
    }
}