tbf/TestBenchFramework/BenchesDlg.cs
Milan Hanajik fc30e160fc - Unhandled exception handler added - it creates a log message.
- config.backup.xml file created and used in case config.xml is corrupted
- LocalSettings.Assign() function avoided
- static LocalSettins LocalSettings.Load(string fname) returns a reference to settings
2015-03-01 06:26:51 +01:00

201 lines
7.2 KiB
C#

using System;
using System.Windows.Forms;
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;
/// <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();
}
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>
/// <param name="sender">Not used</param>
/// <param name="e">Not used</param>
private void addButton_Click(object sender, EventArgs e)
{
DatabaseSettings bench = new DatabaseSettings();
DatabaseSettingsDlg dlg = new DatabaseSettingsDlg(bench);
DialogResult dr = dlg.ShowDialog();
if (dr == DialogResult.OK)
{
int nrBenches =
(localSettings.TestBenches == null) ? 0 : localSettings.TestBenches.GetLength(0);
if (nrBenches == 0)
{
localSettings.TestBenches = new DatabaseSettings[1];
localSettings.TestBenches[0] = bench;
}
else
{
DatabaseSettings[] newTestBenches = new 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>
/// <param name="sender">Not used</param>
/// <param name="e">Not used</param>
private void copyButton_Click(object sender, EventArgs e)
{
if (testBenchesListBox.SelectedIndex >= 0)
{
DatabaseSettings ori = localSettings.TestBenches[testBenchesListBox.SelectedIndex];
DatabaseSettings bench = (DatabaseSettings)ori.Clone();
DatabaseSettingsDlg dlg = new DatabaseSettingsDlg(bench);
DialogResult dr = dlg.ShowDialog();
if (dr == DialogResult.OK)
{
int nrBenches =
(localSettings.TestBenches == null) ? 0 : localSettings.TestBenches.GetLength(0);
DatabaseSettings[] newTestBenches = new 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>
/// <param name="sender">Not used</param>
/// <param name="e">Not used</param>
private void editButton_Click(object sender, EventArgs e)
{
if (testBenchesListBox.SelectedIndex >= 0)
{
DatabaseSettingsDlg dlg = new DatabaseSettingsDlg(localSettings.TestBenches[testBenchesListBox.SelectedIndex]);
DialogResult dr = dlg.ShowDialog();
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>
/// <param name="sender">Not used</param>
/// <param name="e">Not used</param>
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
{
DatabaseSettings[] newTestBenches = new 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>
/// <param name="sender">Not used</param>
/// <param name="e">Not used</param>
private void okButton_Click(object sender, EventArgs e)
{
localSettings.Save();
Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsFileName);
DialogResult = DialogResult.OK;
Close();
return;
}
/// <summary>
/// Throw away changes, return 'Cancel'
/// </summary>
/// <param name="sender">Not used</param>
/// <param name="e">Not used</param>
private void cancelButton_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
return;
}
/// <summary>
/// Double click on an item = edit item
/// </summary>
/// <param name="sender">Not used</param>
/// <param name="e">Not used</param>
private void testBenchesListBox_DoubleClick(object sender, EventArgs e)
{
editButton_Click(sender, e);
}
}
}