Files
123123/ZA.CoreService.ESBCertificateManager/Setup/AddContainerForm.cs
T

236 lines
6.8 KiB
C#

using ZA.CoreService.ESBCertificateManager.Models;
namespace ZA.CoreService.ESBCertificateManager.Setup;
public sealed class AddContainerForm : Form
{
private readonly SetupCoordinator _coordinator;
private readonly ComboBox _cmbCompany;
private readonly ComboBox _cmbSystem;
private readonly TextBox _txtName;
private readonly TextBox _txtDisplay;
private readonly TextBox _txtTimeout;
private readonly Button _btnSave;
private bool _running;
public int? CreatedSonicContainerId { get; private set; }
public AddContainerForm(
SetupCoordinator coordinator,
int? preferredSonicConnectionId = null)
{
_coordinator = coordinator;
Text = "Container hinzufügen";
StartPosition = FormStartPosition.CenterParent;
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
MinimizeBox = false;
ShowInTaskbar = false;
ClientSize = new Size(540, 470);
BackColor = SettingsUi.Background;
ForeColor = SettingsUi.Text;
Font = new Font("Segoe UI", 10f);
Label title = new()
{
Text = "Neuer Container",
Location = new Point(28, 22),
AutoSize = true,
Font = new Font("Segoe UI Semibold", 18f, FontStyle.Bold),
ForeColor = SettingsUi.Text
};
Label subtitle = new()
{
Text =
"Container einer Company und einem Sonic-System zuordnen.",
Location = new Point(30, 58),
AutoSize = true,
ForeColor = SettingsUi.Muted
};
Panel card = SettingsUi.CreateCard();
card.Location = new Point(28, 95);
card.Size = new Size(484, 290);
card.Paint += SettingsUi.PaintCardBorder;
_cmbCompany = SettingsUi.CreateComboBox();
_cmbSystem = SettingsUi.CreateComboBox();
_txtName = SettingsUi.CreateTextBox();
_txtDisplay = SettingsUi.CreateTextBox();
_txtTimeout = SettingsUi.CreateTextBox();
_txtTimeout.Text = "180";
AddField(card, "Company", _cmbCompany, 18, 18, 440);
AddField(card, "Sonic-System", _cmbSystem, 18, 78, 440);
AddField(card, "Container-Name", _txtName, 18, 138, 440);
AddField(card, "Anzeigename (optional)", _txtDisplay, 18, 198, 280);
AddField(card, "Timeout (s)", _txtTimeout, 318, 198, 140);
_btnSave = SettingsUi.CreatePrimaryButton("Container anlegen");
_btnSave.Location = new Point(292, 405);
_btnSave.Size = new Size(220, 40);
_btnSave.Click += async (_, _) => await SaveAsync();
Button cancel = SettingsUi.CreateGhostButton("Abbrechen");
cancel.Location = new Point(170, 405);
cancel.Size = new Size(110, 40);
cancel.Click += (_, _) =>
{
DialogResult = DialogResult.Cancel;
Close();
};
Controls.Add(title);
Controls.Add(subtitle);
Controls.Add(card);
Controls.Add(cancel);
Controls.Add(_btnSave);
Shown += async (_, _) =>
await LoadLookupsAsync(preferredSonicConnectionId);
}
private async Task LoadLookupsAsync(int? preferredSonicConnectionId)
{
try
{
IReadOnlyList<CompanyOption> companies =
await _coordinator.LoadCompaniesAsync();
IReadOnlyList<SonicSystemOption> systems =
await _coordinator.LoadSystemsAsync();
_cmbCompany.Items.Clear();
foreach (CompanyOption company in companies)
{
_cmbCompany.Items.Add(company);
}
if (_cmbCompany.Items.Count > 0)
{
_cmbCompany.SelectedIndex = 0;
}
_cmbSystem.Items.Clear();
int selected = 0;
for (int index = 0; index < systems.Count; index++)
{
_cmbSystem.Items.Add(systems[index]);
if (preferredSonicConnectionId is int id
&& systems[index].SonicConnectionId == id)
{
selected = index;
}
}
if (_cmbSystem.Items.Count > 0)
{
_cmbSystem.SelectedIndex = selected;
}
}
catch (Exception ex)
{
MessageBox.Show(
this,
ex.Message,
"Container",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private async Task SaveAsync()
{
if (_running)
{
return;
}
if (_cmbCompany.SelectedItem is not CompanyOption company)
{
MessageBox.Show(
this,
"Bitte eine Company wählen.",
"Container",
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
return;
}
if (_cmbSystem.SelectedItem is not SonicSystemOption system)
{
MessageBox.Show(
this,
"Bitte ein Sonic-System wählen.",
"Container",
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
return;
}
if (!int.TryParse(_txtTimeout.Text.Trim(), out int timeout))
{
MessageBox.Show(
this,
"Bitte einen gültigen Timeout eingeben.",
"Container",
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
return;
}
_running = true;
_btnSave.Enabled = false;
Cursor = Cursors.WaitCursor;
try
{
CreatedSonicContainerId =
await _coordinator.AddSonicContainerAsync(
company.CompanyId,
system.SonicConnectionId,
_txtName.Text,
_txtDisplay.Text,
timeout);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
MessageBox.Show(
this,
ex.Message,
"Container",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
finally
{
_running = false;
_btnSave.Enabled = true;
Cursor = Cursors.Default;
}
}
private static void AddField(
Control parent,
string label,
Control input,
int x,
int y,
int width)
{
Label fieldLabel = SettingsUi.CreateFieldLabel(label);
fieldLabel.Location = new Point(x, y);
input.Location = new Point(x, y + 22);
input.Width = width;
parent.Controls.Add(fieldLabel);
parent.Controls.Add(input);
}
}