tbf/TBF/BenchesDlg.cs

215 lines
8.1 KiB
C#

///
/// Copyright (c) 2013-2018 Sensus Slovensko a.s.
///
using System;
using System.Windows.Forms;
using TBF.Resources;
namespace TBF
{
/// <summary>
/// Dialog to edit local settings that specify database parameters (S1.2)
/// </summary>
public partial class BenchesDlg : Form
{
// Local copy of 'local settings' initialized to Program.localSettings.
// Dialog modifies this copy and updates Program.localSettings just
// when 'OK' is pressed.
LocalSettings localSettings;
public bool CurrentDBChanged;
/// <summary>
/// Constructor, initializes the dialog
/// </summary>
public BenchesDlg()
{
Program.LocalSettings.Save();
localSettings = LocalSettings.Load(Program.LocalSettingsFileName);
if (localSettings == null) throw new Exception("Cannot load local settings");
InitializeComponent();
RedrawTestBenchesListBox();
CurrentDBChanged = false;
}
private void BenchesDlg_Load(object sender, EventArgs e)
{
Text = Strings.Database_Settings;
label1.Text = Strings.Test_Benches_;
addButton.Text = Strings.AddBtnText;
copyButton.Text = Strings.CopyBtnText;
editButton.Text = Strings.EditBtnText;
removeButton.Text = Strings.RemoveBtnText;
okButton.Text = Strings.OkBtnText;
cancelButton.Text = Strings.CancelBtnText;
}
void RedrawTestBenchesListBox()
{
testBenchesListBox.Items.Clear();
if (localSettings.TestBenches != null)
{
int nrBenches = localSettings.TestBenches.GetLength(0);
for (int i = 0; i < nrBenches; i++)
{
testBenchesListBox.Items.Add(localSettings.TestBenches[i].BenchName);
}
}
}
/// <summary>
/// Create a new test bench DB specification and append it to the list
/// </summary>
private void addButton_Click(object sender, EventArgs e)
{
Config.DatabaseSettings bench = new Config.DatabaseSettings();
Forms.DatabaseSettingsDlg dlg = new Forms.DatabaseSettingsDlg(bench,
Config.Data.CurrentBench.ProceduresDBSettings.ConnectionString,
Config.Data.CurrentBench.WaterMetersDBSettings.ConnectionString);
DialogResult dr = dlg.ShowDialog();
CurrentDBChanged |= dlg.CurrentDBChanged;
if (dr == DialogResult.OK)
{
int nrBenches =
(localSettings.TestBenches == null) ? 0 : localSettings.TestBenches.GetLength(0);
if (nrBenches == 0)
{
localSettings.TestBenches = new Config.DatabaseSettings[1];
localSettings.TestBenches[0] = bench;
}
else
{
Config.DatabaseSettings[] newTestBenches = new Config.DatabaseSettings[nrBenches + 1];
for (int i = 0; i < nrBenches; i++)
{
newTestBenches[i] = localSettings.TestBenches[i];
}
newTestBenches[nrBenches] = bench;
localSettings.TestBenches = newTestBenches;
}
testBenchesListBox.Items.Add(bench.BenchName);
}
}
/// <summary>
/// Create a new test bench DB specification,which is a copy of the
/// selected bench, and append it to the list
/// </summary>
private void copyButton_Click(object sender, EventArgs e)
{
if (testBenchesListBox.SelectedIndex >= 0)
{
Config.DatabaseSettings ori = localSettings.TestBenches[testBenchesListBox.SelectedIndex];
Config.DatabaseSettings bench = (Config.DatabaseSettings)ori.Clone();
Forms.DatabaseSettingsDlg dlg = new Forms.DatabaseSettingsDlg(bench,
Config.Data.CurrentBench.ProceduresDBSettings.ConnectionString,
Config.Data.CurrentBench.WaterMetersDBSettings.ConnectionString);
DialogResult dr = dlg.ShowDialog();
CurrentDBChanged |= dlg.CurrentDBChanged;
if (dr == DialogResult.OK)
{
int nrBenches =
(localSettings.TestBenches == null) ? 0 : localSettings.TestBenches.GetLength(0);
Config.DatabaseSettings[] newTestBenches = new Config.DatabaseSettings[nrBenches + 1];
for (int i = 0; i < nrBenches; i++)
{
newTestBenches[i] = localSettings.TestBenches[i];
}
newTestBenches[nrBenches] = bench;
localSettings.TestBenches = newTestBenches;
testBenchesListBox.Items.Add(bench.BenchName);
}
}
}
/// <summary>
/// Edit selected test bench DB specification
/// </summary>
private void editButton_Click(object sender, EventArgs e)
{
if (testBenchesListBox.SelectedIndex >= 0)
{
Forms.DatabaseSettingsDlg dlg = new Forms.DatabaseSettingsDlg(localSettings.TestBenches[testBenchesListBox.SelectedIndex],
Config.Data.CurrentBench.ProceduresDBSettings.ConnectionString,
Config.Data.CurrentBench.WaterMetersDBSettings.ConnectionString);
DialogResult dr = dlg.ShowDialog();
CurrentDBChanged |= dlg.CurrentDBChanged;
if (dr == DialogResult.OK)
{
// Update the item name
testBenchesListBox.Items[testBenchesListBox.SelectedIndex] =
localSettings.TestBenches[testBenchesListBox.SelectedIndex].BenchName;
}
}
}
/// <summary>
/// Remove selected test bench DB specification from the list
/// </summary>
private void removeButton_Click(object sender, EventArgs e)
{
int selectedIx = testBenchesListBox.SelectedIndex;
if (selectedIx >= 0)
{
int nrBenches =
(localSettings.TestBenches == null) ? 0 : localSettings.TestBenches.GetLength(0);
if (nrBenches == 1)
{
localSettings.TestBenches = null;
}
else
{
Config.DatabaseSettings[] newTestBenches = new Config.DatabaseSettings[nrBenches - 1];
for (int i = 0; i < selectedIx; i++)
{
newTestBenches[i] = localSettings.TestBenches[i];
}
for (int i = selectedIx + 1; i < nrBenches; i++)
{
newTestBenches[i - 1] = localSettings.TestBenches[i];
}
localSettings.TestBenches = newTestBenches;
}
RedrawTestBenchesListBox();
}
}
/// <summary>
/// Update setting, return 'OK'
/// </summary>
private void okButton_Click(object sender, EventArgs e)
{
Program.LocalSettings.TestBenches = localSettings.TestBenches;
Program.LocalSettings.Save();
DialogResult = DialogResult.OK;
Close();
return;
}
/// <summary>
/// Throw away changes, return 'Cancel'
/// </summary>
private void cancelButton_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
return;
}
/// <summary>
/// Double click on an item = edit item
/// </summary>
private void testBenchesListBox_DoubleClick(object sender, EventArgs e)
{
editButton_Click(sender, e);
}
}
}