/// /// Copyright (c) 2013-2017 Sensus Metering Systems /// using System; using System.Collections.Generic; using System.Globalization; using System.Windows.Forms; using NHibernate; using log4net; using Config.Entities; using Common.Forms; using TBF.Rig; using TBF.Rig.GenericDevices; using TBF.Resources; using TBF.UI.Shared; namespace TBF.UI.Bench.Paths { public partial class PathsDlg : Form, IParentOfListViewEx { static readonly ILog log = LogManager.GetLogger(typeof(PathsDlg)); enum PathsDlgTabs { Feeding, Bench, Output, WaterMeters, HeatMeters, } /// Used when re-scaling the dialog: 100% = 96dpi, 125% = 120dpi, 150% = 144dpi public readonly int Dpi; public IList TbfComponents; public IList Valves; public IList FeedingRegulValves; public IList OutputRegulValves; public ISession Session; /// One common DB session passed also to the controls inside tab pages public IList Procedures; public IList Tests; #if HEAT_METERS private System.Windows.Forms.TabPage heatMetersTabPage; private PathsHeatMetersCtrl pathsHeatMetersCtrl; #endif public PathsDlg() { /// Detect display setting: 100% = 96dpi, 125% = 120dpi, 150% = 144dpi. Dpi = (int)this.CreateGraphics().DpiX; InitializeComponent(); /// SharedDlgButtons configuration sharedButtons.ParentForm = this; sharedButtons.RequiredGroupMembership = new Common.GID[] { Common.GID.Metrologists }; sharedButtons.OptionalButtons = SharedButtons.Buttons.Add | SharedButtons.Buttons.Remove | SharedButtons.Buttons.Up | SharedButtons.Buttons.Down; sharedButtons.Unlocked += Unlocked; sharedButtons.OKClicked += okButton_Click; sharedButtons.CancelClicked += cancelButton_Click; sharedButtons.AddClicked += addButton_Click; sharedButtons.RemoveClicked += removeButton_Click; sharedButtons.UpClicked += upButton_Click; sharedButtons.DownClicked += downButton_Click; /// Load the list of components from the database try { TbfComponents = Rig.TbfComponents.LoadComponentsFromDB(TBF.DB.CreateSession(Common.DBKind.Config)); } catch { TbfComponents = new List(); MessageBox.Show("Error occured when loading components"); } /// Find all master valves Valves = Rig.GenericDevices.ValveBase.MasterValves(TbfComponents); /// Find all feeding regulation valves FeedingRegulValves = new List(); OutputRegulValves = new List(); foreach (var cmpnt in TbfComponents) { IRegValve regValve = cmpnt as IRegValve; if (regValve != null && !(cmpnt is TBF.Rig.Elde.RegulValveTandem.RegulValveTandem)) { switch (regValve.Category) { case ValveCategory.Feeding: FeedingRegulValves.Add(regValve); break; case ValveCategory.Output: OutputRegulValves.Add(regValve); break; } } } /// Create the database session for paths Session = TBF.DB.CreateSession(Common.DBKind.Config); Procedures = Session.QueryOver().List(); Tests = Session.QueryOver().List(); pathsTabControl.TabPages[(int)PathsDlgTabs.Feeding].Tag = pathsFeedingCtrl; pathsTabControl.TabPages[(int)PathsDlgTabs.Bench].Tag = pathsBenchCtrl; pathsTabControl.TabPages[(int)PathsDlgTabs.Output].Tag = pathsOutputCtrl; pathsTabControl.TabPages[(int)PathsDlgTabs.WaterMeters].Tag = pathsMetersCtrl; // Requires 'Session' to be created pathsFeedingCtrl.Initialize(this, pathsTabControl.TabPages[(int)PathsDlgTabs.Feeding]); pathsBenchCtrl.Initialize(this, pathsTabControl.TabPages[(int)PathsDlgTabs.Bench]); pathsOutputCtrl.Initialize(this, pathsTabControl.TabPages[(int)PathsDlgTabs.Output]); pathsMetersCtrl.Initialize(this, pathsTabControl.TabPages[(int)PathsDlgTabs.WaterMeters]); #if HEAT_METERS this.heatMetersTabPage = new System.Windows.Forms.TabPage(); this.pathsHeatMetersCtrl = new PathsHeatMetersCtrl(); this.heatMetersTabPage.SuspendLayout(); this.SuspendLayout(); this.pathsTabControl.Controls.Add(this.heatMetersTabPage); // // heatMetersTabPage // this.heatMetersTabPage.Controls.Add(this.pathsHeatMetersCtrl); this.heatMetersTabPage.Name = "heatMetersTabPage"; this.heatMetersTabPage.UseVisualStyleBackColor = true; // // pathsHeatMetersCtrl // this.pathsHeatMetersCtrl.DoubleClickActivation = false; this.pathsHeatMetersCtrl.FullRowSelect = true; this.pathsHeatMetersCtrl.GridLines = true; this.pathsHeatMetersCtrl.Name = "pathsHeatMetersCtrl"; this.pathsHeatMetersCtrl.UseCompatibleStateImageBehavior = false; this.pathsHeatMetersCtrl.View = System.Windows.Forms.View.Details; this.pathsHeatMetersCtrl.SelectedIndexChanged += new System.EventHandler(this.SelectedIndexChanged); this.heatMetersTabPage.ResumeLayout(false); this.ResumeLayout(false); pathsTabControl.TabPages[(int)PathsDlgTabs.HeatMeters].Tag = pathsHeatMetersCtrl; // Requires 'Session' to be created pathsHeatMetersCtrl.Initialize(this, pathsTabControl.TabPages[(int)PathsDlgTabs.HeatMeters]); #endif } private void PathsDlg_Load(object sender, EventArgs e) { Localize(); LoadUISettings(); } private void Localize() { Text = Strings.PathsDlgTitle; pathsTabControl.TabPages[(int)PathsDlgTabs.Feeding].Text = Strings.FeedingTabPageTitle; pathsTabControl.TabPages[(int)PathsDlgTabs.Bench].Text = Strings.BenchTabPageTitle; pathsTabControl.TabPages[(int)PathsDlgTabs.Output].Text = Strings.OutputTabPageTitle; pathsTabControl.TabPages[(int)PathsDlgTabs.WaterMeters].Text = Strings.MetersTabPageTitle; #if HEAT_METERS pathsTabControl.TabPages[(int)PathsDlgTabs.HeatMeters].Text = Strings.Heat_meter_sensors; /// Localization #endif } private void Unlocked(object sender, EventArgs e) { foreach (TabPage tab in pathsTabControl.TabPages) { (tab.Tag as ITabWithListViewEx).Unlock(); } } private void okButton_Click(object sender, EventArgs e) { string warnings = string.Empty; Dictionary[] renameInfo = new Dictionary[pathsTabControl.TabPages.Count]; for (int i = 0; i < pathsTabControl.TabPages.Count; i++) { TabPage tab = pathsTabControl.TabPages[i]; renameInfo[i] = new Dictionary(); if (tab.Tag is ITabWithListViewEx) { warnings += (tab.Tag as ITabWithListViewEx).GetWarningsBeforeSaving(ref renameInfo[i]); } } bool updateRelatedItems = false; if (!string.IsNullOrEmpty(warnings)) { string message = string.Format("{0}{1}{2}", warnings, Environment.NewLine, Strings.Do_you_want_to_update_related_tests_and_procedures); DialogResult dr = MessageBox.Show(message, Strings.Warning, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation); if (dr == DialogResult.Cancel) { DialogResult = DialogResult.Cancel; return; } else if (dr == DialogResult.Yes) { updateRelatedItems = true; } } Cursor.Current = Cursors.WaitCursor; using (var transaction = Session.BeginTransaction()) { try { if (updateRelatedItems) { for (int i = 0; i < pathsTabControl.TabPages.Count; i++) { TabPage tab = pathsTabControl.TabPages[i]; if (tab.Tag is ITabWithListViewEx) (tab.Tag as ITabWithListViewEx).UpdateRelatedItems(renameInfo[i]); } } foreach (TabPage tab in pathsTabControl.TabPages) { (tab.Tag as ITabWithListViewEx).OkBtnClicked(); } transaction.Commit(); } catch (Exception exc) { transaction.Rollback(); Cursor.Current = Cursors.Default; MessageBox.Show(Strings.Cannot_save_changes, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); log.ErrorFormat("Update of paths in the database failed: {0}", exc.Message); } } Cursor.Current = Cursors.Default; SaveUISettings(); DialogResult = DialogResult.OK; Close(); } private void cancelButton_Click(object sender, EventArgs e) { foreach (TabPage tab in pathsTabControl.TabPages) { (tab.Tag as ITabWithListViewEx).CancelBtnClicked(); } SaveUISettings(); DialogResult = DialogResult.Cancel; Close(); } private void SaveUISettings() { /// Obtain PathsDlg dimensions, etc. bool isMaximized = (WindowState == FormWindowState.Maximized); int left = (WindowState == FormWindowState.Normal) ? Location.X : RestoreBounds.Left; int top = (WindowState == FormWindowState.Normal) ? Location.Y : RestoreBounds.Top; int width = (WindowState == FormWindowState.Normal) ? Size.Width : RestoreBounds.Width; int height = (WindowState == FormWindowState.Normal) ? Size.Height : RestoreBounds.Height; LocalSettings ls = Program.LocalSettings; if (ls != null && (ls.PathsDlgMaximized != isMaximized || ls.PathsDlgLeft != left || ls.PathsDlgTop != top || ls.PathsDlgWidth != width || ls.PathsDlgHeight != height)) { /// At least one PathsDlg dimension differs => Update local settings and save them ls.PathsDlgMaximized = isMaximized; ls.PathsDlgLeft = left; ls.PathsDlgTop = top; ls.PathsDlgWidth = width; ls.PathsDlgHeight = height; ls.Save(); } } private void LoadUISettings() { TBF.LocalSettings ls = Program.LocalSettings; WindowState = (ls.PathsDlgMaximized ? FormWindowState.Maximized : FormWindowState.Normal); Width = (ls.PathsDlgWidth > 0) ? ls.PathsDlgWidth : 1050; Height = (ls.PathsDlgHeight > 0) ? ls.PathsDlgHeight : 360; Left = (ls.PathsDlgLeft != 0) ? ls.PathsDlgLeft : 150; Top = (ls.PathsDlgTop != 0) ? ls.PathsDlgTop : 150; } private void addButton_Click(object sender, EventArgs e) { (pathsTabControl.SelectedTab.Tag as ITabWithListViewEx).AddOne(); } private void removeButton_Click(object sender, EventArgs e) { (pathsTabControl.SelectedTab.Tag as ITabWithListViewEx).RemoveSelected(); } private void upButton_Click(object sender, EventArgs e) { (pathsTabControl.SelectedTab.Tag as ITabWithListViewEx).MoveUpSelected(); } private void downButton_Click(object sender, EventArgs e) { (pathsTabControl.SelectedTab.Tag as ITabWithListViewEx).MoveDownSelected(); } /// /// Common part for three xxxListViewEx_SelectedIndexChanged event handlers /// /// void SelectedIndexChanged(object sender, EventArgs e) { ListViewEx listViewEx = sender as ListViewEx; if (listViewEx.SelectedItems.Count != 1) { /// No item selected sharedButtons.DisableButtons(SharedButtons.Buttons.Remove | SharedButtons.Buttons.Up | SharedButtons.Buttons.Down); } else if (listViewEx.Items.Count == 1) { /// One (the first and the last) item selected sharedButtons.EnableButtons(SharedButtons.Buttons.Remove); sharedButtons.DisableButtons(SharedButtons.Buttons.Up | SharedButtons.Buttons.Down); } else if (listViewEx.SelectedIndices[0] == 0) { /// The first of many items selected sharedButtons.EnableButtons(SharedButtons.Buttons.Remove | SharedButtons.Buttons.Down); sharedButtons.DisableButtons(SharedButtons.Buttons.Up); } else if (listViewEx.SelectedIndices[0] == (listViewEx.Items.Count - 1)) { /// The last of many items selected sharedButtons.EnableButtons(SharedButtons.Buttons.Remove | SharedButtons.Buttons.Up); sharedButtons.DisableButtons(SharedButtons.Buttons.Down); } else { /// One of middle items selected sharedButtons.EnableButtons(SharedButtons.Buttons.Remove | SharedButtons.Buttons.Up | SharedButtons.Buttons.Down); } } /// /// Tab page changed - item is unselected /// private void pathsTabControl_SelectedIndexChanged(object sender, EventArgs e) { UpdateButtonStates(SharedButtons.SelectedItemPos.None); } public void UpdateButtonStates(SharedButtons.SelectedItemPos selectedItemPos) { if (selectedItemPos == SharedButtons.SelectedItemPos.None) { sharedButtons.DisableButtons(SharedButtons.Buttons.Remove | SharedButtons.Buttons.Up | SharedButtons.Buttons.Down); } } private void PathsDlg_FormClosing(object sender, FormClosingEventArgs e) { if (Users.CurrentUser.Restore(this)) Program.MainWnd.UpdateUser(); } } }