Files
rssotto-csharp-client/ModVersionChecker/forms/SourceDetailForm.cs
2025-09-04 10:14:30 +02:00

111 lines
4.2 KiB
C#

using ModVersionChecker.data.model;
using ModVersionChecker.managers.interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ModVersionChecker.forms
{
// Simple editor form for SourceDef
public class SourceDetailForm : Form
{
private readonly IFormFactory _formFactory;
private readonly ISourcesDefManager _sourceManager;
public SourceDef SourceDef { get; set; }
public Boolean IsEditable => !string.IsNullOrWhiteSpace(SourceDef?.Id);
private TextBox _idField, _nameField, _typeField, _defaultsField;
private Button _okButton, _cancelButton;
public event EventHandler<string>? OnSourceChanged;
public SourceDetailForm(IFormFactory formFactory, ISourcesDefManager sourceManager)
{
_formFactory = formFactory ?? throw new ArgumentNullException(nameof(formFactory));
_sourceManager = sourceManager ?? throw new ArgumentNullException(nameof(sourceManager));
InitializeComponent();
_formFactory = formFactory;
}
private void InitializeComponent()
{
Text = "Edit SourceDef";
Size = new Size(400, 300);
StartPosition = FormStartPosition.CenterParent;
Padding = new Padding(10);
var layout = new TableLayoutPanel
{
Dock = DockStyle.Fill,
RowCount = 5,
ColumnCount = 2,
Padding = new Padding(10)
};
layout.Controls.Add(new Label { Text = "Id:", Width = 80 }, 0, 0);
_idField = new TextBox { Text = "", Width = 200 };
layout.Controls.Add(_idField, 1, 0);
layout.Controls.Add(new Label { Text = "Name:", Width = 80 }, 0, 1);
_nameField = new TextBox { Text = "", Width = 200 };
layout.Controls.Add(_nameField, 1, 1);
layout.Controls.Add(new Label { Text = "Type:", Width = 80 }, 0, 2);
_typeField = new TextBox { Text = "", Width = 200 };
layout.Controls.Add(_typeField, 1, 2);
layout.Controls.Add(new Label { Text = "Defaults (key=value, comma separated):", Width = 80 }, 0, 3);
_defaultsField = new TextBox { Text = "", Width = 200 };
layout.Controls.Add(_defaultsField, 1, 3);
var buttonPanel = new FlowLayoutPanel { FlowDirection = FlowDirection.RightToLeft, Dock = DockStyle.Fill };
_okButton = new Button { Text = "OK", DialogResult = DialogResult.OK };
_cancelButton = new Button { Text = "Cancel", DialogResult = DialogResult.Cancel };
buttonPanel.Controls.Add(_okButton);
buttonPanel.Controls.Add(_cancelButton);
layout.Controls.Add(buttonPanel, 0, 4);
layout.SetColumnSpan(buttonPanel, 2);
Controls.Add(layout);
_okButton.Click += (s, e) =>
{
SourceDef.Id = _idField.Text.Trim();
SourceDef.Name = _nameField.Text.Trim();
SourceDef.Type = _typeField.Text.Trim();
SourceDef.Defaults = ParseDefaults(_defaultsField.Text);
DialogResult = DialogResult.OK;
Close();
};
_cancelButton.Click += (s, e) => { DialogResult = DialogResult.Cancel; Close(); };
}
public void UpdateFields()
{
if (SourceDef != null)
{
_idField.Text = SourceDef.Id;
_nameField.Text = SourceDef.Name;
_typeField.Text = SourceDef.Type;
_defaultsField.Text = string.Join(", ", SourceDef.Defaults.Select(d => $"{d.Key}={d.Value}"));
}
}
private Dictionary<string, string> ParseDefaults(string text)
{
var dict = new Dictionary<string, string>();
var pairs = text.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var pair in pairs)
{
var kv = pair.Split(new[] { '=' }, 2);
if (kv.Length == 2)
dict[kv[0].Trim()] = kv[1].Trim();
}
return dict;
}
}
}