using Resources;
using SmartCoalApplication.Base.CommTool;
using SmartCoalApplication.Base.FunctionModel;
using SmartCoalApplication.Base.SettingModel;
using SmartCoalApplication.Core;
using SmartCoalApplication.Core.DbOpreate.DbModel;
using SmartCoalApplication.Resources;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SmartCoalApplication.MeasureProcedure
{
internal partial class FileNamingRules : PdnBaseForm
{
private AppWorkspace appWorkspace;
private string pattern = @"^\d+(\.\d)?$";
private FileNameRule fileNameRule = Program.instance.fileNameRule;
private string filePath = Application.StartupPath + "\\Config\\" + Program.instance.SettingPrefix + "\\FileNameRule.xml";
public FileNamingRules(AppWorkspace appWorkspace)
{
InitializeComponent();
setLanguege();
this.Icon = PdnInfo.AppIcon;
this.appWorkspace = appWorkspace;
NameRules();
}
private void setLanguege()
{
this.Text = PdnResources.GetString("FileNamingRules");
this.groupBox19.Text = PdnResources.GetString("FileNamingRules.groupBox19");
this.label29.Text = PdnResources.GetString("FileNamingRules.label29"); // 名称
this.groupBox1.Text = PdnResources.GetString("FileNamingRules.groupBox1"); // 预览
this.label1.Text = PdnResources.GetString("FileNamingRules.label1"); // 名称
this.label14.Text = PdnResources.GetString("FileNamingRules.label14"); // 名称格式
this.label15.Text = PdnResources.GetString("FileNamingRules.label15"); // 数值位数
this.radioButton1.Text = PdnResources.GetString("FileNamingRules.radioButton1"); // 延续
}
#region [命名规则]
///
/// 初始化
///
public void NameRules()
{
//
// 设置comboBox1的集合和属性
//
comboBox1.Items.AddRange(InvariantData.numberNum);
comboBox1.SelectedIndex = 0;
#region [设置listView1]
listView1.View = View.Details;
listView1.Columns.Add("1", 0, HorizontalAlignment.Center);
listView1.Columns.Add(PdnResources.GetString("Menu.Set.Generalsettings.format.text"), 60, HorizontalAlignment.Center);
listView1.Columns.Add(PdnResources.GetString("Menu.instruction.text"), 316, HorizontalAlignment.Center);
listView1.BeginUpdate();
var dict = InvariantData.parameterList;
foreach (var s in dict)
{
ListViewItem lvi1 = new ListViewItem();
lvi1.Text = s.Key;
lvi1.SubItems.Add(s.Key);
lvi1.SubItems.Add(s.Value);
listView1.Items.Add(lvi1);
}
listView1.EndUpdate();
#endregion [设置listView1]
#region [读取XMl]
listNames.DataSource = fileNameRule.NameRuleList;
listNames.DisplayMember = "Name";
#endregion
}
///
/// 校验名称
///
///
private bool Inspect()
{
bool check = false;
if (string.IsNullOrEmpty(textBox2.Text))
{
MessageBox.Show(PdnResources.GetString("Menu.pleaseNameformat.text"));
check = true;
return check;
}
else
{
string[] arrs = Regex.Split(textBox2.Text.Trim(), "%");
List strList = InvariantData.parameterList.Keys.ToList();
if (arrs.Length == 1)
{
if (!textBox2.Text.Contains("%"))
{
MessageBox.Show(PdnResources.GetString("ResultViewNameRule2.illigal"));
check = true;
return check;
}
}
foreach (string text in arrs)
{
if (text == "") continue;
if (!strList.Contains("%" + text))
{
MessageBox.Show(PdnResources.GetString("ResultViewNameRule2.illigal"));
check = true;
return check;
}
int index = this.textBox2.Text.IndexOf(text);
if (index == 0)
{
string str = this.textBox2.Text.Substring(0, 1);
if (str != "%")
{
MessageBox.Show(PdnResources.GetString("ResultViewNameRule2.illigal"));
check = true;
return check;
}
}
else if (index == this.textBox2.Text.Length - 1)
{
string str = this.textBox2.Text.Substring(this.textBox2.Text.Length - 2, 1);
if (str != "%")
{
MessageBox.Show(PdnResources.GetString("ResultViewNameRule2.illigal"));
check = true;
return check;
}
}
else
{
string str = this.textBox2.Text.Substring(index - 1, 1);
string str1 = this.textBox2.Text.Substring(index + 1, 1);
if (str != "%" && str1 != "%")
{
MessageBox.Show(PdnResources.GetString("ResultViewNameRule2.illigal"));
check = true;
return check;
}
}
}
if (Islegal())
{
MessageBox.Show(PdnResources.GetString("ResultViewNameRule2.illigalSymbol"));
check = true;
return check;
}
}
if (radioButton1.Checked && !(Regex.IsMatch(textBox3.Text, pattern)))
{
MessageBox.Show(PdnResources.GetString("Menu.Numberscanbeentet.text") + "!");
check = true;
}
return check;
}
///
/// 是否符合window命名规则不
///
///
public bool Islegal()
{
char[] charstr = { '\\', '/', ':', '*', '?', '"', '<', '>', '|' };
char[] textstr = (textBox7.Text.Trim() + textBox8.Text.Trim()).ToString().Trim().ToCharArray();
int count = 0;
for (int i = 0; i < charstr.Length; i++)
{
for (int j = 0; j < textstr.Length; j++)
{
if (charstr[i] == textstr[j])
{
count++;
}
}
}
if (count > 0)
return true;
else
return false;
}
#endregion
#region [操作]
///
/// 取消
///
///
///
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
///
/// 新增
///
///
///
private void button1_Click(object sender, EventArgs e)
{
this.textBox2.TextChanged -= new EventHandler(textBox2_TextChanged);
this.textBox1.Text = string.Empty;
this.textBox4.Text = string.Empty;
this.textBox2.Text = string.Empty;
this.textBox7.Text = string.Empty;
this.textBox8.Text = string.Empty;
this.comboBox1.SelectedIndex = 0;
this.radioButton1.Checked = true;
this.radioButton2.Checked = false;
this.textBox3.Text = "1";
this.textBox5.Text = string.Empty;
this.listNames.SelectedIndex = -1;
this.textBox2.TextChanged += new EventHandler(textBox2_TextChanged);
}
///
/// 保存
///
///
///
private void btn_Save_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.textBox1.Text))
{
MessageBox.Show(PdnResources.GetString("ResultNameView2.InputName"));
return;
}
//先校验
bool pass = Inspect();
if (pass)
{
return;
}
NameRuleListDetails nameRuleListDetails;
string id = Guid.NewGuid().ToString();
string name = this.textBox1.Text;
int index;
if (this.listNames.SelectedItem == null || this.listNames.SelectedIndex == -1)
{
nameRuleListDetails = new NameRuleListDetails();
nameRuleListDetails.ID = id;
var repeatData = fileNameRule.NameRuleList.Where(m => m.Name == name && m.ID != id).FirstOrDefault();
if (repeatData != null)
{
MessageBox.Show(PdnResources.GetString("ResultNameView2.nameRepeat"));
return;
}
fileNameRule.NameRuleList.Add(nameRuleListDetails);
index = fileNameRule.NameRuleList.Count - 1;
}
else {
index = this.listNames.SelectedIndex;
var nameRuleListDetailsB = (NameRuleListDetails)this.listNames.SelectedItem;
var repeatData = fileNameRule.NameRuleList.Where(m => m.Name == name && m.ID != nameRuleListDetailsB.ID).FirstOrDefault();
if (repeatData != null)
{
MessageBox.Show(PdnResources.GetString("ResultNameView2.nameRepeat"));
return;
}
nameRuleListDetails = fileNameRule.NameRuleList.Where(m => m.ID == nameRuleListDetailsB.ID).FirstOrDefault();
}
nameRuleListDetails.Name = this.textBox1.Text;
nameRuleListDetails.Text = this.textBox4.Text;
nameRuleListDetails.NameFormat = this.textBox2.Text;
nameRuleListDetails.ZTextOne = this.textBox7.Text;
nameRuleListDetails.ZTextTwo = this.textBox8.Text;
try
{
nameRuleListDetails.DigitNum = Convert.ToInt32(this.comboBox1.SelectedItem);
}
catch (Exception ex)
{
nameRuleListDetails.DigitNum = 1;
}
try
{
nameRuleListDetails.ContinueNumber = Convert.ToInt32(this.textBox3.Text);
}
catch (Exception ex)
{
nameRuleListDetails.ContinueNumber = 1;
}
nameRuleListDetails.NowNumber = nameRuleListDetails.ContinueNumber;
nameRuleListDetails.isContinue = this.radioButton1.Checked ? true : false;
WriteXML();
this.listNames.SelectedIndexChanged -= new System.EventHandler(this.listNames_SelectedChange);
listNames.DataSource = null;
listNames.DataSource = this.fileNameRule.NameRuleList;
listNames.DisplayMember = "Name";
this.listNames.SelectedIndexChanged += new System.EventHandler(this.listNames_SelectedChange);
listNames.SelectedIndex = index;
MessageBox.Show("保存成功!");
}
///
/// 删除
///
///
///
private void btn_Del_Click(object sender, EventArgs e)
{
if (this.listNames.SelectedItem == null || this.listNames.SelectedIndex == -1)
{
return;
}
DialogResult result = MessageBox.Show(PdnResources.GetString("ResultViewNameRule2.delete"), PdnResources.GetString("ResultViewNameRule2.deleteTitle"), MessageBoxButtons.OKCancel);
if (result == DialogResult.OK)
{
var nameRuleListDetails = (NameRuleListDetails)this.listNames.SelectedItem;
this.fileNameRule.NameRuleList.Remove(nameRuleListDetails);
this.listNames.SelectedIndexChanged -= new System.EventHandler(this.listNames_SelectedChange);
listNames.DataSource = null;
listNames.DataSource = this.fileNameRule.NameRuleList;
listNames.DisplayMember = "Name";
this.listNames.SelectedIndexChanged += new System.EventHandler(this.listNames_SelectedChange);
WriteXML();
if (this.listNames.Items.Count > 0)
{
listNames.SelectedIndex = 0;
}
else
{
button1_Click(null, null);
}
}
}
///
/// 右侧列表选择
///
///
///
private void listNames_SelectedChange(object sender, EventArgs e)
{
if (this.listNames.SelectedItem == null || this.listNames.SelectedIndex == -1)
{
return;
}
NameRuleListDetails nameRuleListDetails = (NameRuleListDetails)this.listNames.SelectedItem;
this.textBox1.Text = nameRuleListDetails.Name;
this.textBox4.Text = nameRuleListDetails.Text;
this.textBox2.Text = nameRuleListDetails.NameFormat;
this.textBox7.Text = nameRuleListDetails.ZTextOne;
this.textBox8.Text = nameRuleListDetails.ZTextTwo;
this.comboBox1.SelectedIndex = nameRuleListDetails.DigitNum - 1;
this.textBox3.Text = nameRuleListDetails.ContinueNumber.ToString();
this.radioButton1.Checked = nameRuleListDetails.isContinue ? true : false;
this.radioButton2.Checked = !this.radioButton1.Checked;
}
///
/// 保存XML文件
///
private void WriteXML()
{
Program.instance.fileNameRule = this.fileNameRule;
string stageModelXml = XmlSerializeHelper.XmlSerialize(fileNameRule);
FileOperationHelper.WriteStringToFile(stageModelXml, filePath, FileMode.Create);
}
///
///
///
///
///
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (radioButton2.Checked)
{
textBox3.ReadOnly = true;
}
else {
textBox3.ReadOnly = false;
}
if (!string.IsNullOrEmpty(textBox2.Text))
{
textBox2_TextChanged(null, null);
}
}
private void listView1_DoubleClick(object sender, EventArgs e)
{
if (this.listView1.FocusedItem != null && this.listView1.FocusedItem.Index != -1)
{
var dict = InvariantData.parameterList;
int start = this.textBox2.SelectionStart;
this.textBox2.Text = this.textBox2.Text.Insert(start, dict.Keys.ToList()[this.listView1.FocusedItem.Index]);
this.textBox2.SelectionStart = this.textBox2.Text.Length;
}
}
#endregion
///
/// 预览展示
///
///
///
private void textBox2_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.textBox2.Text))
{
this.textBox5.Text = string.Empty;
return;
}
//先校验
bool pass = Inspect();
if (pass)
{
return;
}
string ruleName = string.Empty;
string ruleGain = string.Empty;
List list = Program.instance.mic_rulersAll;
var ruler = list.Where(m => m.id.Equals(Program.instance.configModel.RulerId)).FirstOrDefault();
if (ruler != null)
{
ruleName = ruler.ruler_name;
ruleGain = ruler.gain_multiple.ToString();
}
NamingRuleHelper namingRuleHelper = new NamingRuleHelper();
NameRuleListDetails nameRuleListDetails = new NameRuleListDetails();
nameRuleListDetails.Name = this.textBox1.Text;
nameRuleListDetails.Text = this.textBox4.Text;
nameRuleListDetails.NameFormat = this.textBox2.Text;
nameRuleListDetails.ZTextOne = this.textBox7.Text;
nameRuleListDetails.ZTextTwo = this.textBox8.Text;
nameRuleListDetails.isContinue = this.radioButton1.Checked;
try
{
nameRuleListDetails.DigitNum = Convert.ToInt32(this.comboBox1.SelectedItem);
}
catch (Exception ex)
{
nameRuleListDetails.DigitNum = 1;
}
try
{
nameRuleListDetails.ContinueNumber = Convert.ToInt32(this.textBox3.Text);
}
catch (Exception ex)
{
nameRuleListDetails.ContinueNumber = 1;
}
nameRuleListDetails.NowNumber = this.radioButton1.Checked ? nameRuleListDetails.ContinueNumber : 1;
this.textBox5.Text = namingRuleHelper.GetNameFromXml(nameRuleListDetails, ruleName, ruleGain, null, this.filePath);
}
///
///
///
///
///
private void text_change(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox2.Text))
{
textBox2_TextChanged(null, null);
}
}
}
}