Files
UserStringProperty/UserStringProperty/UI/XmlImportDialog.cs
2026-05-02 11:37:32 +08:00

206 lines
6.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using UserStringProperty.Core;
namespace UserStringProperty.UI
{
public enum ImportMode
{
Append, // 添加新的(如果同名已存在则提示替换)
Replace // 完全替代现有同名属性集
}
public class XmlImportDialog : Form
{
private XmlPropertyConfig _incomingConfig;
private ComboBox _cmbCmig;
private ComboBox _cmbPropertySet;
private Label _lblIncomingInfo;
private Label _lblConflictWarning;
private Button _btnOK;
private Button _btnCancel;
private string _pluginFolder;
public ImportMode SelectedMode { get; private set; }
public string SelectedCmigName { get; private set; }
public string SelectedPropertySetName { get; private set; }
public bool ShouldReplace { get; private set; }
public XmlImportDialog(XmlPropertyConfig incoming, string pluginFolder)
{
_incomingConfig = incoming;
_pluginFolder = pluginFolder;
InitializeComponent();
UpdateConflictWarning();
}
private void InitializeComponent()
{
this.Text = "导入属性集";
this.Size = new Size(550, 250);
this.StartPosition = FormStartPosition.CenterParent;
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
int y = 15;
// 选择 Cmig
var lblCmig = new Label
{
Text = "模板类型 (cmig):",
Location = new Point(15, y),
Size = new Size(120, 20)
};
this.Controls.Add(lblCmig);
_cmbCmig = new ComboBox
{
Location = new Point(140, y),
Size = new Size(380, 20),
DropDownStyle = ComboBoxStyle.DropDownList
};
_cmbCmig.Items.Add(_incomingConfig.CmigName);
_cmbCmig.SelectedIndex = 0;
this.Controls.Add(_cmbCmig);
y += 30;
// 选择 PropertySet
var lblPropertySet = new Label
{
Text = "属性集 (propertyset):",
Location = new Point(15, y),
Size = new Size(120, 20)
};
this.Controls.Add(lblPropertySet);
_cmbPropertySet = new ComboBox
{
Location = new Point(140, y),
Size = new Size(380, 20),
DropDownStyle = ComboBoxStyle.DropDownList
};
foreach (var ps in _incomingConfig.PropertySets)
{
_cmbPropertySet.Items.Add(ps.Name);
}
if (_cmbPropertySet.Items.Count > 0)
_cmbPropertySet.SelectedIndex = 0;
_cmbPropertySet.SelectedIndexChanged += (s, e) => UpdateConflictWarning();
this.Controls.Add(_cmbPropertySet);
y += 35;
// 即将导入的信息
_lblIncomingInfo = new Label
{
Text = "",
Location = new Point(15, y),
Size = new Size(500, 20),
ForeColor = Color.DarkGreen,
Font = new Font(Font.FontFamily, 9, FontStyle.Bold)
};
this.Controls.Add(_lblIncomingInfo);
y += 25;
// 冲突警告
_lblConflictWarning = new Label
{
Text = "",
Location = new Point(15, y),
Size = new Size(500, 20)
};
this.Controls.Add(_lblConflictWarning);
y += 40;
// 按钮
_btnOK = new Button
{
Text = "确定导入",
Location = new Point(350, y),
Size = new Size(90, 30)
};
_btnOK.Click += (s, e) =>
{
var cmigName = _cmbCmig.SelectedItem?.ToString() ?? "";
var psName = _cmbPropertySet.SelectedItem?.ToString() ?? "";
var fileName = $"{cmigName}-{psName}.xml";
var existingPath = Path.Combine(_pluginFolder, fileName);
if (File.Exists(existingPath))
{
var result = MessageBox.Show(
$"文件 {fileName} 已存在,是否覆盖?",
"确认覆盖",
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning);
if (result != DialogResult.Yes)
return;
}
SelectedCmigName = cmigName;
SelectedPropertySetName = psName;
this.DialogResult = DialogResult.OK;
this.Close();
};
this.Controls.Add(_btnOK);
_btnCancel = new Button
{
Text = "取消",
Location = new Point(245, y),
Size = new Size(90, 30)
};
_btnCancel.Click += (s, e) =>
{
this.DialogResult = DialogResult.Cancel;
this.Close();
};
this.Controls.Add(_btnCancel);
}
private void UpdateConflictWarning()
{
var cmigName = _cmbCmig.SelectedItem?.ToString() ?? "";
var psName = _cmbPropertySet.SelectedItem?.ToString() ?? "";
if (string.IsNullOrEmpty(cmigName) || string.IsNullOrEmpty(psName))
{
_lblIncomingInfo.Text = "";
_lblConflictWarning.Text = "";
return;
}
var fileName = $"{cmigName}-{psName}.xml";
var existingPath = Path.Combine(_pluginFolder, fileName);
var exists = File.Exists(existingPath);
_lblIncomingInfo.Text = $"待导入: {cmigName}-{psName}";
if (exists)
{
_lblConflictWarning.Text = $"⚠ 同名属性集已存在,将覆盖现有文件";
_lblConflictWarning.ForeColor = Color.DarkOrange;
}
else
{
_lblConflictWarning.Text = $"✓ 同名属性集不存在,将作为新文件保存";
_lblConflictWarning.ForeColor = Color.DarkGreen;
}
}
public bool CheckReplace()
{
var fileName = $"{SelectedCmigName}-{SelectedPropertySetName}.xml";
var existingPath = Path.Combine(_pluginFolder, fileName);
return File.Exists(existingPath);
}
}
}