206 lines
6.2 KiB
C#
206 lines
6.2 KiB
C#
using ZA.CoreService.ESBCertificateManager.Models;
|
|
|
|
namespace ZA.CoreService.ESBCertificateManager.Setup;
|
|
|
|
public sealed class AddSonicSystemForm : Form
|
|
{
|
|
private readonly SetupCoordinator _coordinator;
|
|
private readonly SonicSystemOption? _existing;
|
|
private readonly TextBox _txtName;
|
|
private readonly TextBox _txtHost;
|
|
private readonly TextBox _txtPort;
|
|
private readonly TextBox _txtDomain;
|
|
private readonly ComboBox _cmbProtocol;
|
|
private readonly Button _btnSave;
|
|
private bool _running;
|
|
|
|
public int? CreatedSonicConnectionId { get; private set; }
|
|
|
|
public bool IsEditMode => _existing is not null;
|
|
|
|
public AddSonicSystemForm(
|
|
SetupCoordinator coordinator,
|
|
SonicSystemOption? existing = null)
|
|
{
|
|
_coordinator = coordinator;
|
|
_existing = existing;
|
|
|
|
Text = IsEditMode
|
|
? "Sonic-System bearbeiten"
|
|
: "Sonic-System hinzufügen";
|
|
StartPosition = FormStartPosition.CenterParent;
|
|
FormBorderStyle = FormBorderStyle.FixedDialog;
|
|
MaximizeBox = false;
|
|
MinimizeBox = false;
|
|
ShowInTaskbar = false;
|
|
ClientSize = new Size(520, 430);
|
|
BackColor = SettingsUi.Background;
|
|
ForeColor = SettingsUi.Text;
|
|
Font = new Font("Segoe UI", 10f);
|
|
|
|
Label title = new()
|
|
{
|
|
Text = IsEditMode
|
|
? "Sonic-System bearbeiten"
|
|
: "Neues Sonic-System",
|
|
Location = new Point(28, 22),
|
|
AutoSize = true,
|
|
Font = new Font("Segoe UI Semibold", 18f, FontStyle.Bold),
|
|
ForeColor = SettingsUi.Text
|
|
};
|
|
|
|
Label subtitle = new()
|
|
{
|
|
Text = "Management-Verbindung für Container-Neustarts",
|
|
Location = new Point(30, 58),
|
|
AutoSize = true,
|
|
ForeColor = SettingsUi.Muted
|
|
};
|
|
|
|
Panel card = SettingsUi.CreateCard();
|
|
card.Location = new Point(28, 95);
|
|
card.Size = new Size(464, 250);
|
|
card.Paint += SettingsUi.PaintCardBorder;
|
|
|
|
_txtName = SettingsUi.CreateTextBox();
|
|
_txtHost = SettingsUi.CreateTextBox();
|
|
_txtPort = SettingsUi.CreateTextBox();
|
|
_txtDomain = SettingsUi.CreateTextBox();
|
|
_cmbProtocol = SettingsUi.CreateComboBox();
|
|
_cmbProtocol.Items.AddRange(["tcp", "ssl"]);
|
|
_cmbProtocol.SelectedIndex = 0;
|
|
_txtPort.Text = "13070";
|
|
|
|
if (_existing is not null)
|
|
{
|
|
_txtName.Text = _existing.ConnectionName;
|
|
_txtHost.Text = _existing.ManagementHost;
|
|
_txtPort.Text = _existing.ManagementPort.ToString();
|
|
_txtDomain.Text = _existing.DomainName;
|
|
|
|
string protocol = _existing.ConnectionProtocol
|
|
.Trim()
|
|
.TrimEnd(':', '/')
|
|
.ToLowerInvariant();
|
|
|
|
int protocolIndex = _cmbProtocol.Items.IndexOf(protocol);
|
|
_cmbProtocol.SelectedIndex = protocolIndex >= 0 ? protocolIndex : 0;
|
|
}
|
|
|
|
AddField(card, "Verbindungsname", _txtName, 18, 18, 420);
|
|
AddField(card, "Management-Host", _txtHost, 18, 78, 280);
|
|
AddField(card, "Port", _txtPort, 316, 78, 122);
|
|
AddField(card, "Domain", _txtDomain, 18, 138, 420);
|
|
AddField(card, "Protokoll", _cmbProtocol, 18, 198, 160);
|
|
|
|
_btnSave = SettingsUi.CreatePrimaryButton(
|
|
IsEditMode ? "Änderungen speichern" : "System anlegen");
|
|
_btnSave.Location = new Point(292, 365);
|
|
_btnSave.Size = new Size(200, 40);
|
|
_btnSave.Click += async (_, _) => await SaveAsync();
|
|
|
|
Button cancel = SettingsUi.CreateGhostButton("Abbrechen");
|
|
cancel.Location = new Point(170, 365);
|
|
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);
|
|
AcceptButton = _btnSave;
|
|
CancelButton = cancel;
|
|
}
|
|
|
|
private async Task SaveAsync()
|
|
{
|
|
if (_running)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!int.TryParse(_txtPort.Text.Trim(), out int port))
|
|
{
|
|
MessageBox.Show(
|
|
this,
|
|
"Ungültiger Port.",
|
|
"Sonic-System",
|
|
MessageBoxButtons.OK,
|
|
MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
|
|
_running = true;
|
|
_btnSave.Enabled = false;
|
|
Cursor = Cursors.WaitCursor;
|
|
|
|
try
|
|
{
|
|
string protocol =
|
|
_cmbProtocol.SelectedItem?.ToString() ?? "tcp";
|
|
|
|
if (_existing is not null)
|
|
{
|
|
await _coordinator.UpdateSonicConnectionAsync(
|
|
_existing.SonicConnectionId,
|
|
_txtName.Text,
|
|
_txtHost.Text,
|
|
port,
|
|
_txtDomain.Text,
|
|
protocol);
|
|
|
|
CreatedSonicConnectionId = _existing.SonicConnectionId;
|
|
}
|
|
else
|
|
{
|
|
CreatedSonicConnectionId =
|
|
await _coordinator.AddSonicConnectionAsync(
|
|
_txtName.Text,
|
|
_txtHost.Text,
|
|
port,
|
|
_txtDomain.Text,
|
|
protocol);
|
|
}
|
|
|
|
DialogResult = DialogResult.OK;
|
|
Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(
|
|
this,
|
|
ex.Message,
|
|
"Sonic-System",
|
|
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);
|
|
}
|
|
}
|