/// /// Copyright (c) 2013-2015 Sensus Metering Systems /// using System; using System.Collections.Generic; using System.Globalization; using System.Windows.Forms; using NHibernate; using log4net; using TBF.UiControls; using TBF.BenchControl.GenericDevices; using TBF.Resources; namespace TBF { public partial class PathsDlg : Form, IParentOfListViewEx { static readonly ILog log = LogManager.GetLogger(typeof(PathsDlg)); enum PathsDlgTabs { Feeding, Bench, Output, Meters, } /// Used when re-scaling the dialog: 100% = 96dpi, 125% = 120dpi, 150% = 144dpi public readonly int Dpi; public const int Dpi100pct = 96; public IList TbfComponents; public IList Valves; public ISession Session; /// One common DB session passed also to the controls inside tab pages public PathsDlg() { /// Detect display setting: 100% = 96dpi, 125% = 120dpi, 150% = 144dpi. Dpi = (int)this.CreateGraphics().DpiX; InitializeComponent(); /// SharedDlgButtons configuration sharedButtons.RequiredGroupMembership = Config.Grp.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 TbfComponents = BenchControl.TbfComponents.LoadComponentsFromDB(Config.FluentCommon.CreateSession(Config.Entities.DBKind.Config)); /// Find all master valves Valves = BenchControl.GenericDevices.ValveBase.MasterValves(TbfComponents); /// Create the database session for paths Session = Config.FluentCommon.CreateSession(Config.Entities.DBKind.Config); 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.Meters].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.Meters]); } private void PathsDlg_Load(object sender, EventArgs e) { Localize(); TBF.LocalSettings ls = Program.LocalSettings; 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 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.Meters].Text = Strings.MetersTabPageTitle; } 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) { foreach (TabPage tab in pathsTabControl.TabPages) { (tab.Tag as ITabWithListViewEx).OkBtnClicked(); } Program.LocalSettings.PathsDlgLeft = Location.X; Program.LocalSettings.PathsDlgTop = Location.Y; Program.LocalSettings.PathsDlgWidth = Size.Width; Program.LocalSettings.PathsDlgHeight = Size.Height; Program.LocalSettings.Save(); DialogResult = DialogResult.OK; Close(); } private void cancelButton_Click(object sender, EventArgs e) { Program.LocalSettings.PathsDlgLeft = Location.X; Program.LocalSettings.PathsDlgTop = Location.Y; Program.LocalSettings.PathsDlgWidth = Size.Width; Program.LocalSettings.PathsDlgHeight = Size.Height; Program.LocalSettings.Save(); foreach (TabPage tab in pathsTabControl.TabPages) { (tab.Tag as ITabWithListViewEx).CancelBtnClicked(); } DialogResult = DialogResult.Cancel; Close(); } 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); } } } }