/// /// Copyright (c) 2016 Sensus Metering Systems /// using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using log4net; using TBF.BenchControl.GenericDevices; using TBF.Resources; using TBF.UI.Shared; namespace TBF.BenchControl.TestMethods.Endurance { public partial class CycleStepsCtrl : BaseWithListViewEx, ITabWithListViewEx { static readonly ILog log = LogManager.GetLogger(typeof(CycleStepsCtrl)); /// /// Fixed ListViewEx columns /// enum Column { Duration, //Message, FixedColumnsCount, } readonly int fixedCount = (int)Column.FixedColumnsCount; IList sequence; CycleDlg parent; Control parentControl; Control[] editors; int vCount; /// Number of valves public CycleStepsCtrl() : base() { } public void Initialize(IList sequence, CycleDlg parent, Control parentControl) { this.sequence = sequence; this.parent = parent; this.parentControl = parentControl; Name = "Dummy"; MyItems = sequence; SubItemClicked += new Results.Forms.SubItemEventHandler(listViewEx_SubItemClicked); SubItemRightClicked += new Results.Forms.SubItemEventHandler(listViewEx_SubItemRightClicked); SubItemEndEditing += new Results.Forms.SubItemEndEditingEventHandler(listViewEx_SubItemEndEditing); /// /// ListViewEx columns /// Columns.Add(Strings.Duration_ms, 100 * parent.Dpi / UI.Constants.Dpi100pct); //Columns.Add(Strings.Message, 100 * parent.Dpi / UI.Constants.Dpi100pct); /// vCount = 0; foreach (var vlv in parent.Valves) { Columns.Add(vlv.Cfg.Name, 50 * parent.Dpi / UI.Constants.Dpi100pct); vCount++; } /// /// Prepare valve combo-boxes /// TextBox tb; ComboBox cb; editors = new Control[fixedCount + vCount]; /// Duration editors[(int)Column.Duration] = tb = new TextBox(); tb.Visible = false; parent.Controls.Add(tb); /// Message //editors[(int)Column.Message] = tb = new TextBox(); //tb.Visible = false; //parent.Controls.Add(tb); /// Valves for (int i = fixedCount; i < fixedCount + vCount; i++) { editors[i] = cb = new ComboBox(); cb.Visible = false; cb.Items.Add("---"); cb.Items.Add(Strings.open); cb.Items.Add(Strings.close); parent.Controls.Add(cb); } base.RedrawAll(); } void listViewEx_SubItemClicked(object sender, Results.Forms.SubItemEventArgs e) { if (!Unlocked || e.SubItem >= editors.Length) return; StartEditing(editors[e.SubItem], e.Item, e.SubItem); } void listViewEx_SubItemRightClicked(object sender, Results.Forms.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; System.Drawing.Color backColor = System.Drawing.Color.White; foreach (ListViewItem item in Items) { if (value != null) { 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, Results.Forms.SubItemEndEditingEventArgs e) { if (e.SubItem == (int)Column.Duration) { uint duration; if (!uint.TryParse(e.DisplayText, out duration) || (duration % 10) != 0 || duration == 0) { e.DisplayText = e.Item.Text; e.Cancel = true; } } //else if (e.SubItem == (int)Column.Message) //{ // if (e.DisplayText.Length >= 255) // { // // Message too long // e.DisplayText = e.Item.SubItems[e.SubItem].Text; // e.Cancel = true; // } //} else if ((e.SubItem >= fixedCount) && (e.SubItem < fixedCount + vCount)) { if ((e.DisplayText != Strings.open) && (e.DisplayText != Strings.close) && (e.DisplayText != "---")) { e.DisplayText = e.Item.SubItems[e.SubItem].Text; e.Cancel = true; } else { e.Item.SubItems[e.SubItem].BackColor = e.DisplayText == Strings.open ? Color.LightGray : Color.White; } } } public override void DrawOne(object myItem) { CycleStep step = (CycleStep)myItem; ListViewItem lvi = new ListViewItem(step.Duration.ToString()); lvi.UseItemStyleForSubItems = false; //lvi.SubItems.Add(string.IsNullOrEmpty(step.Message) ? string.Empty : step.Message); for (int i = 0; i < parent.Valves.Count; i++) { if ((step.ValvesOpen & (1 << i)) != 0) { lvi.SubItems.Add(Strings.open); lvi.SubItems[lvi.SubItems.Count - 1].BackColor = Color.LightGray; } else if ((step.ValvesClose & (1 << i)) != 0) { 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 = step; Items.Add(lvi); } public override void UpdateOne(ListViewItem lvi, object myItem) { CycleStep step = (CycleStep)myItem; /// /// Duration /// uint duration; if (uint.TryParse(lvi.Text, out duration) && (duration % 10) == 0 && duration != 0) step.Duration = duration; else lvi.Text = step.Duration.ToString(); /// /// Message /// //if (lvi.SubItems[(int)Column.Message].Text.Length == 0) // step.Message = string.Empty; //else // step.Message = lvi.SubItems[(int)Column.Message].Text; /// /// Valves /// int k = 0; uint valvesOpen = 0; uint valvesClose = 0; /// for (int i = 0; i < parent.Valves.Count; i++) { IValve valve = parent.Valves[i]; string lviSubitText = lvi.SubItems[fixedCount + k++].Text; if (lviSubitText.Equals(Strings.open)) { valvesOpen += ((uint)1 << i); } if (lviSubitText.Equals(Strings.close)) { valvesClose += ((uint)1 << i); } } step.ValvesOpen = valvesOpen; step.ValvesClose = valvesClose; } public void AddOne() { base.AddOne(new CycleStep()); } public new void RemoveSelected() { bool lastItemRemoved = base.RemoveSelected(); if (lastItemRemoved && parent != null) { parent.UpdateButtonStates(SharedButtons.SelectedItemPos.None); } } public string GetWarningsBeforeSaving(ref Dictionary renmInfo) { return string.Empty; } public void UpdateRelatedItems(Dictionary renameInfo) { } } }