using System; using System.Drawing; using System.Linq; using System.Windows.Forms; using CadParamPluging.Common; namespace CadParamPluging.UI { public class ParamCatalogForm : Form { private readonly ListBox _list; private ParamCatalog _catalog; public ParamCatalogForm(ParamCatalog initial) { Text = "参数总表"; Size = new Size(640, 520); StartPosition = FormStartPosition.CenterParent; FormBorderStyle = FormBorderStyle.FixedDialog; MaximizeBox = false; MinimizeBox = false; ShowInTaskbar = false; _catalog = (initial ?? ParamCatalog.CreateDefault()).Clone(); _catalog.Normalize(); var layout = new TableLayoutPanel { Dock = DockStyle.Fill, Padding = new Padding(10), ColumnCount = 2, RowCount = 2 }; layout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100f)); layout.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize)); layout.RowStyles.Add(new RowStyle(SizeType.Percent, 100f)); layout.RowStyles.Add(new RowStyle(SizeType.AutoSize)); _list = new ListBox { Dock = DockStyle.Fill }; ReloadList(); var actions = new FlowLayoutPanel { Dock = DockStyle.Fill, FlowDirection = FlowDirection.TopDown, WrapContents = false, AutoSize = true }; var btnAdd = new Button { Text = "新增", AutoSize = true }; btnAdd.Click += (_, __) => OnAdd(); var btnEdit = new Button { Text = "修改", AutoSize = true }; btnEdit.Click += (_, __) => OnEdit(); var btnDelete = new Button { Text = "删除", AutoSize = true }; btnDelete.Click += (_, __) => OnDelete(); actions.Controls.Add(btnAdd); actions.Controls.Add(btnEdit); actions.Controls.Add(btnDelete); var btnPanel = new FlowLayoutPanel { Dock = DockStyle.Fill, FlowDirection = FlowDirection.RightToLeft, AutoSize = true }; var btnClose = new Button { Text = "关闭", DialogResult = DialogResult.Cancel }; var btnSave = new Button { Text = "保存" }; btnSave.Click += (_, __) => OnSave(); btnPanel.Controls.Add(btnClose); btnPanel.Controls.Add(btnSave); layout.Controls.Add(_list, 0, 0); layout.Controls.Add(actions, 1, 0); layout.Controls.Add(btnPanel, 0, 1); layout.SetColumnSpan(btnPanel, 2); Controls.Add(layout); CancelButton = btnClose; } private void ReloadList() { _list.Items.Clear(); foreach (var it in (_catalog.Items ?? Enumerable.Empty()).OrderBy(x => x.Order)) { _list.Items.Add(Format(it)); } if (_list.Items.Count > 0) { _list.SelectedIndex = 0; } } private static string Format(ParamDefinition def) { if (def == null) { return "(null)"; } var label = string.IsNullOrWhiteSpace(def.Label) ? def.Key : def.Label; return $"{label} ({def.Key}) [{def.Type}]"; } private ParamDefinition GetSelected() { var idx = _list.SelectedIndex; if (idx < 0 || _catalog.Items == null) { return null; } var ordered = _catalog.Items.OrderBy(x => x.Order).ThenBy(x => x.Key, StringComparer.OrdinalIgnoreCase).ToList(); if (idx >= ordered.Count) { return null; } var selKey = ordered[idx].Key; return _catalog.FindByKey(selKey); } private void OnAdd() { using (var f = new ParamDefinitionForm("新增参数", new ParamDefinition { Type = ParamValueType.String }, isEdit: false)) { if (f.ShowDialog(this) != DialogResult.OK) { return; } var def = f.Result; if (_catalog.FindByKey(def.Key) != null) { MessageBox.Show(this, $"Key 已存在:{def.Key}", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } _catalog.Items.Add(def); _catalog.Normalize(); ReloadList(); } } private void OnEdit() { var selected = GetSelected(); if (selected == null) { MessageBox.Show(this, "请先选择一项再修改。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } using (var f = new ParamDefinitionForm("修改参数", selected, isEdit: true)) { if (f.ShowDialog(this) != DialogResult.OK) { return; } var updated = f.Result; var existing = _catalog.FindByKey(updated.Key); if (existing == null) { _catalog.Items.Add(updated); } else { existing.Label = updated.Label; existing.Type = updated.Type; existing.Required = updated.Required; existing.Hint = updated.Hint; existing.DefaultValue = updated.DefaultValue; existing.Min = updated.Min; existing.Max = updated.Max; existing.EnumOptions = updated.EnumOptions; existing.Group = updated.Group; existing.Order = updated.Order; existing.Normalize(); } _catalog.Normalize(); ReloadList(); } } private void OnDelete() { var selected = GetSelected(); if (selected == null) { MessageBox.Show(this, "请先选择一项再删除。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } var confirm = MessageBox.Show(this, $"确认删除参数:{selected.Label} ({selected.Key})?", "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (confirm != DialogResult.Yes) { return; } _catalog.Items = (_catalog.Items ?? Enumerable.Empty()) .Where(x => !string.Equals(x.Key, selected.Key, StringComparison.OrdinalIgnoreCase)) .ToList(); _catalog.Normalize(); ReloadList(); } private void OnSave() { try { ParamCatalogStore.Save(_catalog); MessageBox.Show(this, "已保存。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(this, $"保存失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }