using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Threading; using System.Windows.Forms; using DevExpress.XtraEditors; using GCAS.Code; using GCAS.Dto; using GCAS.Localization; using GCAS.Model; namespace GCAS { public partial class DeviceForm : DevExpress.XtraEditors.XtraForm { private List L; private DeviceRepository deviceRepository; public OperatorModel currentUser; private ActionsRepository actionsRepository; public DeviceForm() { InitializeComponent(); L = LocalizationHelper.GetSource(Thread.CurrentThread.CurrentUICulture); } private void DeviceForm_Load(object sender, System.EventArgs e) { actionsRepository = new ActionsRepository(); deviceRepository = new DeviceRepository(); InitDataView(); } private void InitDataView(bool isPageLoad = true) { gridView1.LoadingPanelVisible = true; var list = deviceRepository.GetList(); gridControl1.DataSource = list; gridView1.LoadingPanelVisible = false; if (!isPageLoad) { var realForm = MdiParent.MdiChildren.FirstOrDefault(c => c.Name == "RealTimeForm") as RealTimeForm; realForm.ChangeDeviceInfo(); } if (currentUser.Role == 1) { contextMenuStrip1.Enabled = false; } } private async void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { DeviceModel rowData; switch (e.ClickedItem.Name) { case "menu_new": using (NewDeviceForm form = new NewDeviceForm()) { form.Text = L.GetString("new"); form.currentUser = currentUser; if (form.ShowDialog() == DialogResult.OK) InitDataView(false); } break; case "menu_edit": rowData = gridView1.GetFocusedRow() as DeviceModel; if (rowData != null) { using (NewDeviceForm form = new NewDeviceForm()) { form.Text = L.GetString("edit"); form.deviceModel = rowData; form.currentUser = currentUser; if (form.ShowDialog() == DialogResult.OK) InitDataView(false); } } break; case "menu_delete": rowData = gridView1.GetFocusedRow() as DeviceModel; if (rowData != null) { if (XtraMessageBox.Show(string.Format(L.GetString("DeleteDeviceMessage"), rowData.Name), L.GetString("notification"), MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { if (await deviceRepository.Delete(rowData.Id) > 0) { await actionsRepository.Insert(new ActionsModel { Info = string.Format(L.GetString("deleteEquipmentLog"), rowData.Name), Operator = currentUser.Name, RoleName = currentUser.RoleName, Time = DateTime.Now }); InitDataView(false); } } } break; } } private void gridView1_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e) { if (e.HitInfo.InRow) { int index = gridView1.FocusedRowHandle; if (index >= 0) { menu_edit.Enabled = true; } else { menu_edit.Enabled = false; } } else { menu_edit.Enabled = false; } } private void gridView1_MouseDown(object sender, MouseEventArgs e) { if (currentUser.Role == 1) return; if (e.Clicks != 2 || e.Button != MouseButtons.Left) return; var rowData = gridView1.GetRow(gridView1.CalcHitInfo(new Point(e.X, e.Y)).RowHandle) as DeviceModel; if (rowData != null) { using (NewDeviceForm form = new NewDeviceForm()) { form.Text = L.GetString("Edit"); form.deviceModel = rowData; form.currentUser = currentUser; if (form.ShowDialog() == DialogResult.OK) InitDataView(false); } } } } }