/// /// Copyright (c) 2013-2017 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using System.Drawing; using System.Globalization; using System.Windows.Forms; using log4net; using NHibernate; using Results.Forms; using TBF.BenchControl; using TBF.BenchControl.GenericDevices; using TBF.Resources; using TBF.UI.Shared; namespace TBF.UI.Bench.Paths { public class PathsBenchCtrl : BaseWithListViewEx, ITabWithListViewEx { static readonly ILog log = LogManager.GetLogger(typeof(PathsBenchCtrl)); /// /// Fixed ListViewEx columns /// enum Column { Name, Qfrom, Qto, Tup, Tdown, Pup, Pdown, Pdelta, StopBFValve, FixedColumnsCount, } readonly int fixedColumnsCount = (int)Column.FixedColumnsCount; PathsDlg parent; Control parentControl; Control[] editors; public PathsBenchCtrl() : base() { FixedRowsCount = 1; } public void Initialize(PathsDlg parent, Control parentControl) { this.parent = parent; this.parentControl = parentControl; /// Load the paths MyItems = parent.Session.QueryOver() .OrderBy(x => x.ItemNr).Asc .List(); foreach (var path in MyItems) path.OriName = path.Name; SubItemClicked += new SubItemEventHandler(listViewEx_SubItemClicked); SubItemRightClicked += new SubItemEventHandler(listViewEx_SubItemRightClicked); SubItemEndEditing += new SubItemEndEditingEventHandler(listViewEx_SubItemEndEditing); /// ListViewEx columns TBF.LocalSettings ls = Program.LocalSettings; Columns.Add(Strings.Name, (ls.BenchColumnCount > 0) ? ls.BenchColumnWidths[0] : 60 * parent.Dpi / Constants.Dpi100pct); Columns.Add(Strings.Q_from_m3h, (ls.BenchColumnCount > 1) ? ls.BenchColumnWidths[1] : 50 * parent.Dpi / Constants.Dpi100pct); Columns.Add(Strings.Q_to_m3h, (ls.BenchColumnCount > 2) ? ls.BenchColumnWidths[2] : 50 * parent.Dpi / Constants.Dpi100pct); Columns.Add(Strings.T_in, (ls.BenchColumnCount > 3) ? ls.BenchColumnWidths[3] : 50 * parent.Dpi / Constants.Dpi100pct); Columns.Add(Strings.T_out, (ls.BenchColumnCount > 4) ? ls.BenchColumnWidths[4] : 50 * parent.Dpi / Constants.Dpi100pct); Columns.Add(Strings.P_in, (ls.BenchColumnCount > 5) ? ls.BenchColumnWidths[5] : 50 * parent.Dpi / Constants.Dpi100pct); Columns.Add(Strings.P_out, (ls.BenchColumnCount > 6) ? ls.BenchColumnWidths[6] : 50 * parent.Dpi / Constants.Dpi100pct); Columns.Add(Strings.P_delta, (ls.BenchColumnCount > 7) ? ls.BenchColumnWidths[7] : 50 * parent.Dpi / Constants.Dpi100pct); Columns.Add(Strings.StopBFValve_chdr, (ls.BenchColumnCount > 8) ? ls.BenchColumnWidths[8] : 70 * parent.Dpi / Constants.Dpi100pct); int clmn = (int)Column.FixedColumnsCount; foreach (var vlv in parent.Valves) { if (vlv.Category == ValveCategory.All || vlv.Category == ValveCategory.Bench) { Columns.Add(vlv.Cfg.Name, (ls.BenchColumnCount > clmn) ? ls.BenchColumnWidths[clmn] : 50 * parent.Dpi / Constants.Dpi100pct); clmn++; } } /// Temperatures, pressures ComboBox tUpCBox = new ComboBox(); ComboBox tDownCBox = new ComboBox(); ComboBox prUpCBox = new ComboBox(); ComboBox prDownCBox = new ComboBox(); ComboBox prDeltaCBox = new ComboBox(); ComboBox stopBFValveComboBox = new ComboBox(); stopBFValveComboBox.Items.Add("---"); foreach (var cmpnt in parent.TbfComponents) { if (cmpnt is BenchControl.GenericDevices.ITempMeter) { tUpCBox.Items.Add(cmpnt.Cfg.Name); tDownCBox.Items.Add(cmpnt.Cfg.Name); } if (cmpnt is BenchControl.GenericDevices.IPressureMeter) { prUpCBox.Items.Add(cmpnt.Cfg.Name); prDownCBox.Items.Add(cmpnt.Cfg.Name); prDeltaCBox.Items.Add(cmpnt.Cfg.Name); } if (cmpnt is IValve && (cmpnt as IValve).CoupledTo == null) { stopBFValveComboBox.Items.Add(cmpnt.Cfg.Name); } } editors = new Control[fixedColumnsCount + 64]; /// editors[(int)Column.Name] = new TextBox(); /// Name editors[(int)Column.Qfrom] = new TextBox(); /// Q_from editors[(int)Column.Qto] = new TextBox(); /// Q_to editors[(int)Column.Tup] = tUpCBox; editors[(int)Column.Tdown] = tDownCBox; editors[(int)Column.Pup] = prUpCBox; editors[(int)Column.Pdown] = prDownCBox; editors[(int)Column.Pdelta] = prDownCBox; editors[(int)Column.StopBFValve] = stopBFValveComboBox; /// Prepare valve combo-boxes (max. 64 valves) for (int i = 0; i < 64; i++) { ComboBox cb = new ComboBox(); cb.Items.Add("---"); cb.Items.Add(Strings.open); cb.Items.Add(Strings.close); editors[fixedColumnsCount + i] = cb; } /// for (int i = 0; i < editors.Length; i++) { editors[i].Visible = false; parent.Controls.Add(editors[i]); } base.RedrawAll(); } void listViewEx_SubItemClicked(object sender, SubItemEventArgs e) { if (!Unlocked || e.SubItem >= editors.Length) return; if ((e.Item.Tag is Config.Entities.IHasItemNr) && ((e.Item.Tag as Config.Entities.IHasItemNr).ItemNr < FixedRowsCount) && (e.SubItem < fixedColumnsCount)) { return; } StartEditing(editors[e.SubItem], e.Item, e.SubItem); } void listViewEx_SubItemRightClicked(object sender, SubItemEventArgs e) { if (!Unlocked || e.SubItem >= editors.Length) return; if (DialogResult.Yes == MessageBox.Show(Strings.Do_you_want_to_copy_this_value_to_all_cells_below_this_cell, Strings.Confirmation, MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { int columnNr = e.SubItem; string value = null; Color backColor = Color.White; foreach (ListViewItem item in Items) { if (value != null && item.SubItems.Count > columnNr) { item.SubItems[columnNr].Text = value; item.SubItems[columnNr].BackColor = backColor; } else if (item == e.Item) { value = item.SubItems[columnNr].Text; backColor = item.SubItems[columnNr].BackColor; } } } } void listViewEx_SubItemEndEditing(object sender, SubItemEndEditingEventArgs e) { if (e.SubItem >= fixedColumnsCount) { e.Item.SubItems[e.SubItem].BackColor = e.DisplayText == Strings.open ? Color.LightGray : Color.White; } } public override void DrawOne(object myItem) { Config.Entities.BenchPath entity =(Config.Entities.BenchPath)myItem; BenchControl.BenchPath path = new BenchControl.BenchPath(entity, parent.TbfComponents); ListViewItem lvi = new ListViewItem(entity.Name); lvi.UseItemStyleForSubItems = false; lvi.SubItems.Add(path.Qfrom.ToString()); lvi.SubItems.Add(path.Qto.ToString()); lvi.SubItems.Add(path.TempMtrUp != null ? path.TempMtrUp.Cfg.Name : "---"); lvi.SubItems.Add(path.TempMtrDown != null ? path.TempMtrDown.Cfg.Name : "---"); lvi.SubItems.Add(path.PressMtrUp != null ? path.PressMtrUp.Cfg.Name : "---"); lvi.SubItems.Add(path.PressMtrDown != null ? path.PressMtrDown.Cfg.Name : "---"); lvi.SubItems.Add(path.PressMtrDelta != null ? path.PressMtrDelta.Cfg.Name : "---"); lvi.SubItems.Add((path.StopBFValve != null) ? path.StopBFValve.Cfg.Name : "---"); foreach (var valve in parent.Valves) { if (valve.Category == ValveCategory.All || valve.Category == ValveCategory.Bench) { if (path.ValvesOpen != null && path.ValvesOpen.Contains(valve)) { lvi.SubItems.Add(Strings.open); lvi.SubItems[lvi.SubItems.Count - 1].BackColor = Color.LightGray; } else if (path.ValvesClose != null && path.ValvesClose.Contains(valve)) { lvi.SubItems.Add(Strings.close); lvi.SubItems[lvi.SubItems.Count - 1].BackColor = Color.White; } else { lvi.SubItems.Add("---"); lvi.SubItems[lvi.SubItems.Count - 1].BackColor = Color.White; } } } lvi.Tag = entity; Items.Add(lvi); } public override void UpdateOne(ListViewItem lvi, object myItem) { Config.Entities.BenchPath entity = (Config.Entities.BenchPath)myItem; entity.Name = lvi.Text; float Qfrom; bool allOk = Utils.TryParseUFloat(lvi.SubItems[(int)Column.Qfrom].Text, out Qfrom); entity.Qfrom = Qfrom; float Qto; allOk &= Utils.TryParseUFloat(lvi.SubItems[(int)Column.Qto].Text, out Qto); entity.Qto = Qto; entity.TempMtrUp = lvi.SubItems[(int)Column.Tup].Text; entity.TempMtrDown = lvi.SubItems[(int)Column.Tdown].Text; entity.PressMtrUp = lvi.SubItems[(int)Column.Pup].Text; entity.PressMtrDown = lvi.SubItems[(int)Column.Pdown].Text; entity.PressMtrDelta = lvi.SubItems[(int)Column.Pdelta].Text; entity.StopBFValve = lvi.SubItems[(int)Column.StopBFValve].Text.Equals("---") ? null : lvi.SubItems[(int)Column.StopBFValve].Text; string valvesOpen = string.Empty; string valvesClose = string.Empty; int k = 0; for (int j = 0; j < parent.Valves.Count; j++) { if (parent.Valves[j].Category == ValveCategory.All || parent.Valves[j].Category == ValveCategory.Bench) { string text = lvi.SubItems[fixedColumnsCount + k].Text; if (text.Equals(Strings.open)) { if (valvesOpen != string.Empty) valvesOpen += ";"; valvesOpen += parent.Valves[j].Cfg.Name; } if (text.Equals(Strings.close)) { if (valvesClose != string.Empty) valvesClose += ";"; valvesClose += parent.Valves[j].Cfg.Name; } k++; } } entity.ValvesOpen = valvesOpen; entity.ValvesClose = valvesClose; } public void AddOne() { Config.Entities.BenchPath entity; if (MyItems.Count < FixedRowsCount) { entity = new Config.Entities.BenchPath(Strings._none_, MyItems.Count); } else { /// Find an unused test name string newName; for (int nameId = 1; true; nameId++) { newName = Strings.New_bench_path_name + nameId.ToString(); bool notUsed = true; foreach (var t in MyItems) if (t.Name.Equals(newName)) notUsed = false; if (notUsed) break; } entity = new Config.Entities.BenchPath(newName, MyItems.Count); } base.AddOne(entity); } public new void RemoveSelected() { bool lastOneRemoved = base.RemoveSelected(); if (lastOneRemoved && parent != null) { parent.UpdateButtonStates(SharedButtons.SelectedItemPos.None); } } public string GetWarningsBeforeSaving(ref Dictionary renmInfo) { base.OkBtnClicked(); /// = UpdateAll() string warnings = string.Empty; foreach (var entity in ToBeRemovedItems) { if (!string.IsNullOrEmpty(entity.OriName)) { int occurances = 0; foreach (var tst in parent.Tests) if (!string.IsNullOrEmpty(tst.BenchPath) && tst.BenchPath.Equals(entity.OriName)) occurances++; if (occurances > 0) { warnings += string.Format(Strings._0_path_1_used_by_2_tests_will_be_removed_3, Strings.BenchTabPageTitle, entity.Name, occurances, Environment.NewLine); renmInfo.Add(entity.OriName, string.Empty); } } } foreach (var entity in MyItems) { if (!string.IsNullOrEmpty(entity.OriName) && entity.OriName != entity.Name) { int occurances = 0; foreach (var tst in parent.Tests) if (!string.IsNullOrEmpty(tst.BenchPath) && tst.BenchPath.Equals(entity.OriName)) occurances++; if (occurances > 0) { warnings += string.Format(Strings._0_path_1_used_by_2_tests_will_be_renamed_to_3_4, Strings.BenchTabPageTitle, entity.OriName, occurances, entity.Name, Environment.NewLine); renmInfo.Add(entity.OriName, entity.Name); } } } return warnings; } public void UpdateRelatedItems(Dictionary renameInfo) { foreach (var ri in renameInfo) { var tests = parent.Session.QueryOver() .Where(x => x.BenchPath == ri.Key) .List(); foreach (var t in tests) { t.BenchPath = ri.Value; parent.Session.Update(t); } } } /// /// Update the entities and save them to the database /// public new void OkBtnClicked() { base.OkBtnClicked(); /// Update the database foreach (var entity in ToBeRemovedItems) { parent.Session.Delete(entity); } foreach (var entity in MyItems) { parent.Session.SaveOrUpdate(entity); } SaveUISettings(); } void SaveUISettings() { LocalSettings ls = Program.LocalSettings; /// Compare list view column widths with the saved ones bool anyColumnDiffers = false; if (Columns.Count != ls.BenchColumnCount) { anyColumnDiffers = true; } else { for (int i = 0; i < Columns.Count; i++) { if (Columns[i].Width != ls.BenchColumnWidths[i]) { anyColumnDiffers = true; break; } } } if (anyColumnDiffers) { /// Update column widths ls.BenchColumnWidths = new int[Columns.Count]; for (int i = 0; i < Columns.Count; i++) { ls.BenchColumnWidths[i] = Columns[i].Width; } ls.Save(); } } } }