345 lines
12 KiB
C#
345 lines
12 KiB
C#
///
|
|
/// 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 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,
|
|
WaterMeters,
|
|
HeatMeters,
|
|
}
|
|
|
|
/// Used when re-scaling the dialog: 100% = 96dpi, 125% = 120dpi, 150% = 144dpi
|
|
public readonly int Dpi;
|
|
public const int Dpi100pct = 96;
|
|
|
|
public IList<BenchControl.Generic.IComponent> TbfComponents;
|
|
public IList<IValve> Valves;
|
|
public IList<IRegulValve> FeedingRegulValves;
|
|
public ISession Session; /// One common DB session passed also to the controls inside tab pages
|
|
public IList<Procedure> Procedures;
|
|
public IList<Test> Tests;
|
|
|
|
#if HEAT_METERS
|
|
private System.Windows.Forms.TabPage heatMetersTabPage;
|
|
private UiControls.PathsHeatMetersCtrl pathsHeatMetersCtrl;
|
|
#endif
|
|
|
|
public PathsDlg()
|
|
{
|
|
/// Detect display setting: 100% = 96dpi, 125% = 120dpi, 150% = 144dpi.
|
|
Dpi = (int)this.CreateGraphics().DpiX;
|
|
|
|
InitializeComponent();
|
|
|
|
/// SharedDlgButtons configuration
|
|
sharedButtons.RequiredGroupMembership = new Users.Grp.GID[] { Users.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(Users.Entities.DBKind.Config));
|
|
|
|
/// Find all master valves
|
|
Valves = BenchControl.GenericDevices.ValveBase.MasterValves(TbfComponents);
|
|
|
|
/// Find all feeding regulation valves
|
|
FeedingRegulValves = new List<IRegulValve>();
|
|
foreach (var cmpnt in TbfComponents)
|
|
{
|
|
IRegulValve regValve = cmpnt as IRegulValve;
|
|
if (regValve != null && (regValve.Category == TBF.BenchControl.ValveCategory.Feeding || regValve.Category == TBF.BenchControl.ValveCategory.All))
|
|
{
|
|
FeedingRegulValves.Add(regValve);
|
|
}
|
|
}
|
|
|
|
/// Create the database session for paths
|
|
Session = Config.FluentCommon.CreateSession(Users.Entities.DBKind.Config);
|
|
|
|
Procedures = Session.QueryOver<Procedure>().List();
|
|
Tests = Session.QueryOver<Test>().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 TBF.UiControls.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();
|
|
LoadFormPosition();
|
|
}
|
|
|
|
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<string, string>[] renameInfo = new Dictionary<string, string>[pathsTabControl.TabPages.Count];
|
|
|
|
for (int i = 0; i < pathsTabControl.TabPages.Count; i++)
|
|
{
|
|
TabPage tab = pathsTabControl.TabPages[i];
|
|
renameInfo[i] = new Dictionary<string, string>();
|
|
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;
|
|
|
|
SaveFormPosition();
|
|
DialogResult = DialogResult.OK;
|
|
Close();
|
|
}
|
|
|
|
private void cancelButton_Click(object sender, EventArgs e)
|
|
{
|
|
foreach (TabPage tab in pathsTabControl.TabPages)
|
|
{
|
|
(tab.Tag as ITabWithListViewEx).CancelBtnClicked();
|
|
}
|
|
SaveFormPosition();
|
|
DialogResult = DialogResult.Cancel;
|
|
Close();
|
|
}
|
|
|
|
private void SaveFormPosition()
|
|
{
|
|
TBF.LocalSettings ls = Program.LocalSettings;
|
|
if (WindowState == FormWindowState.Normal)
|
|
{
|
|
ls.PathsDlgLeft = Location.X;
|
|
ls.PathsDlgTop = Location.Y;
|
|
ls.PathsDlgWidth = Size.Width;
|
|
ls.PathsDlgHeight = Size.Height;
|
|
}
|
|
else
|
|
{
|
|
ls.PathsDlgLeft = RestoreBounds.Left;
|
|
ls.PathsDlgTop = RestoreBounds.Top;
|
|
ls.PathsDlgWidth = RestoreBounds.Width;
|
|
ls.PathsDlgHeight = RestoreBounds.Height;
|
|
}
|
|
ls.Save();
|
|
}
|
|
|
|
private void LoadFormPosition()
|
|
{
|
|
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 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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Common part for three xxxListViewEx_SelectedIndexChanged event handlers
|
|
/// </summary>
|
|
/// <param name="listViewEx"></param>
|
|
void SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
Results.Forms.ListViewEx listViewEx = sender as Results.Forms.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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tab page changed - item is unselected
|
|
/// </summary>
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|