tbf/TestBenchFramework/Forms/LoginDlgWithBenchSelection.cs
2017-09-26 09:45:18 +02:00

176 lines
5.2 KiB
C#

///
/// Copyright (c) 2013-2017 Sensus Metering Systems
///
using System;
using System.Windows.Forms;
using TBF.Resources;
namespace TBF.Forms
{
/// <summary>
/// Provides login dialog that includes test bench selection.
/// It is used only once on program startup. Does not do authorisation
/// as User.users have not been loadded from the DB yet.
/// </summary>
public partial class LoginDlgWithBenchSelection : Form
{
/// Private fields
string alias;
string password;
string benchName = null;
string legalizator;
/// Readonly public properties to do the authorization.
public string Alias { get { return alias; } }
public string Password { get { return password; } }
public string BenchName { get { return benchName; } }
public Users.Entities.LoginMethod Method;
public string Legalizator { get { return legalizator; } }
/// <summary>
/// Constructor.
/// </summary>
public LoginDlgWithBenchSelection()
{
InitializeComponent();
}
/// <summary>
/// Constructor with a predefined bench.
/// </summary>
public LoginDlgWithBenchSelection(string predefinedBench)
: this()
{
benchName = predefinedBench;
#if LANG_PL
Height = 156;
EnableAndPrepareLegalizatorCombo(legalizatorComboBox);
#else
legalizator = string.Empty;
#endif
}
/// <summary>
/// Load Legalizator order combo box items from the LocalSettings LastLegalizators array
/// </summary>
/// <param name="orderComboBox">Legalizator ComboBox</param>
void EnableAndPrepareLegalizatorCombo(ComboBox comboBox)
{
legalizatorLabel.Visible = true;
legalizatorComboBox.Visible = true;
legalizatorComboBox.Enabled = true;
for (int i = 0; i < Program.LocalSettings.LastLegalizatorsCount; i++)
{
comboBox.Items.Add(Program.LocalSettings.LastLegalizators[i]);
}
legalizator = (comboBox.Items.Count > 0) ? Program.LocalSettings.LastLegalizators[0] : string.Empty;
comboBox.Text = legalizator;
}
private void LoginDlgWithBenchSelection_Load(object sender, EventArgs e)
{
Localize();
#if DEBUG
userNameTextBox.Text = "milan";
passwordTextBox.Text = "napajedla";
#endif
for (int i = 0; i < Program.LocalSettings.BenchesCount; i++)
{
testBenchComboBox.Items.Add(Program.LocalSettings.TestBenches[i].BenchName);
if (benchName != null && benchName == Program.LocalSettings.TestBenches[i].BenchName)
{
testBenchComboBox.Text = benchName;
}
}
}
void Localize()
{
Text = Strings.User_Login + " TBF v." + Program.Version;
aliasLabel.Text = GetAliasLabel();
passwordLabel.Text = Strings.Password;
testBenchLabel.Text = Strings.Test_bench;
okButton.Text = Strings.OkBtnText;
cancelButton.Text = Strings.CancelBtnText;
legalizatorLabel.Text = Strings.Legalizator;
}
string GetAliasLabel()
{
switch (Method)
{
default:
case Users.Entities.LoginMethod.UserName: return Strings.User_name;
case Users.Entities.LoginMethod.FullName: return Strings.Full_name;
case Users.Entities.LoginMethod.Number: return Strings.User_code;
}
}
/// <summary>
/// OK clicked (authorization is done outside this dialog).
/// </summary>
private void okButton_Click(object sender, EventArgs e)
{
if (testBenchComboBox.SelectedItem != null)
{
alias = userNameTextBox.Text;
password = passwordTextBox.Text;
benchName = testBenchComboBox.SelectedItem.ToString();
if (legalizatorComboBox.Enabled)
{
legalizator = legalizatorComboBox.Text;
Program.LocalSettings.UpdateHistory(legalizator, ref Program.LocalSettings.LastLegalizators);
}
DialogResult = DialogResult.OK;
Close();
return;
}
else
{
MessageBox.Show(Strings.PLs_select_testbench, Strings.Warning, MessageBoxButtons.OK);
}
}
/// <summary>
/// Cancel clicked.
/// </summary>
private void cancelButton_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
return;
}
private void passwordTextBox_TextChanged(object sender, EventArgs e)
{
if (passwordTextBox.Text != "")
{
this.AcceptButton = okButton;
}
}
private void userNameTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (Convert.ToInt32(e.KeyChar) == 13)
{
passwordTextBox.Focus();
}
}
private void aliasLabel_Click(object sender, EventArgs e)
{
Method++;
if (Method == Users.Entities.LoginMethod.Count)
Method = 0;
aliasLabel.Text = GetAliasLabel();
}
}
}