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

136 lines
5.0 KiB
C#

using ModVersionChecker.data.model;
using ModVersionChecker.managers.interfaces;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace ModVersionChecker.forms
{
public class SourcesConfigForm : Form
{
private List<SourceDef> _sourceDefs;
private ListView _listView;
private Button _addButton, _editButton, _deleteButton, _closeButton;
private TableLayoutPanel _mainLayout;
private readonly ISourcesDefManager _sourcesManager;
private readonly IFormFactory _formFactory;
public event EventHandler<string>? OnSourcesChanged;
public List<SourceDef> SourceDefs => _sourceDefs;
public SourcesConfigForm(IFormFactory formFactory, ISourcesDefManager sourcesManager)
{
_sourcesManager = sourcesManager ?? throw new ArgumentNullException(nameof(sourcesManager));
_formFactory = formFactory ?? throw new ArgumentNullException(nameof(formFactory));
_sourceDefs = _sourcesManager.List() ?? new List<SourceDef>();
Padding = new Padding(20);
InitializeComponent();
}
private void InitializeComponent()
{
Text = "Source Definitions";
Size = new Size(800, 400);
StartPosition = FormStartPosition.CenterParent;
_mainLayout = new TableLayoutPanel
{
Dock = DockStyle.Fill,
RowCount = 2,
ColumnCount = 1,
Padding = new Padding(10)
};
_mainLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 80));
_mainLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 20));
_listView = new ListView
{
Dock = DockStyle.Fill,
View = View.Details,
FullRowSelect = true,
MultiSelect = false,
GridLines = true
};
_listView.Columns.Add("Id", 100);
_listView.Columns.Add("Name", 150);
_listView.Columns.Add("Type", 100);
_listView.Columns.Add("Defaults", -2);
UpdateListView();
_mainLayout.Controls.Add(_listView, 0, 0);
var buttonPanel = new FlowLayoutPanel { Dock = DockStyle.Fill, FlowDirection = FlowDirection.LeftToRight };
_addButton = new Button { Text = "Add" };
_editButton = new Button { Text = "Edit", Enabled = false };
_deleteButton = new Button { Text = "Delete", Enabled = false };
_closeButton = new Button { Text = "Close", DialogResult = DialogResult.OK };
_addButton.Click += (s, e) => AddSourceDef();
_editButton.Click += (s, e) => EditSourceDef();
_deleteButton.Click += (s, e) => DeleteSourceDef();
_closeButton.Click += (s, e) => Close();
_listView.SelectedIndexChanged += (s, e) =>
{
bool hasSelection = _listView.SelectedItems.Count > 0;
_editButton.Enabled = hasSelection;
_deleteButton.Enabled = hasSelection;
};
buttonPanel.Controls.AddRange(new Control[] { _addButton, _editButton, _deleteButton, _closeButton });
_mainLayout.Controls.Add(buttonPanel, 0, 1);
Controls.Add(_mainLayout);
}
private void UpdateListView()
{
_listView.Items.Clear();
foreach (var src in _sourceDefs)
{
var item = new ListViewItem(src.Id);
item.SubItems.Add(src.Name);
item.SubItems.Add(src.Type);
item.SubItems.Add(string.Join(", ", src.Defaults.Select(d => $"{d.Key}={d.Value}")));
item.Tag = src;
_listView.Items.Add(item);
}
}
private void AddSourceDef()
{
EventHandler<string>? handler = (s, e) => MessageBox.Show("Source Changed");
var editor = _formFactory.CreateSourceDetailForm(null, handler);
if (editor.ShowDialog() == DialogResult.OK)
{
_sourceDefs.Add(editor.SourceDef);
UpdateListView();
}
}
private void EditSourceDef()
{
if (_listView.SelectedItems.Count == 0) return;
var src = _listView.SelectedItems[0].Tag as SourceDef;
EventHandler<string>? handler = (s, e) => MessageBox.Show("Source Changed");
var editor = _formFactory.CreateSourceDetailForm(src, handler);
if (editor.ShowDialog() == DialogResult.OK)
{
//int idx = _sourceDefs.IndexOf(src);
//_sourceDefs[idx] = editor.SourceDef;
//UpdateListView();
}
}
private void DeleteSourceDef()
{
if (_listView.SelectedItems.Count == 0) return;
var src = _listView.SelectedItems[0].Tag as SourceDef;
_sourceDefs.Remove(src);
UpdateListView();
}
}
}