FileNamingRules.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. using Resources;
  2. using SmartCoalApplication.Base.CommTool;
  3. using SmartCoalApplication.Base.FunctionModel;
  4. using SmartCoalApplication.Base.SettingModel;
  5. using SmartCoalApplication.Core;
  6. using SmartCoalApplication.Core.DbOpreate.DbModel;
  7. using SmartCoalApplication.Resources;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Data;
  12. using System.Drawing;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Text.RegularExpressions;
  17. using System.Threading.Tasks;
  18. using System.Windows.Forms;
  19. namespace SmartCoalApplication.MeasureProcedure
  20. {
  21. internal partial class FileNamingRules : PdnBaseForm
  22. {
  23. private AppWorkspace appWorkspace;
  24. private string pattern = @"^\d+(\.\d)?$";
  25. private FileNameRule fileNameRule = Program.instance.fileNameRule;
  26. private string filePath = Application.StartupPath + "\\Config\\" + Program.instance.SettingPrefix + "\\FileNameRule.xml";
  27. public FileNamingRules(AppWorkspace appWorkspace)
  28. {
  29. InitializeComponent();
  30. setLanguege();
  31. this.Icon = PdnInfo.AppIcon;
  32. this.appWorkspace = appWorkspace;
  33. NameRules();
  34. }
  35. private void setLanguege()
  36. {
  37. this.Text = PdnResources.GetString("FileNamingRules");
  38. this.groupBox19.Text = PdnResources.GetString("FileNamingRules.groupBox19");
  39. this.label29.Text = PdnResources.GetString("FileNamingRules.label29"); // 名称
  40. this.groupBox1.Text = PdnResources.GetString("FileNamingRules.groupBox1"); // 预览
  41. this.label1.Text = PdnResources.GetString("FileNamingRules.label1"); // 名称
  42. this.label14.Text = PdnResources.GetString("FileNamingRules.label14"); // 名称格式
  43. this.label15.Text = PdnResources.GetString("FileNamingRules.label15"); // 数值位数
  44. this.radioButton1.Text = PdnResources.GetString("FileNamingRules.radioButton1"); // 延续
  45. }
  46. #region [命名规则]
  47. /// <summary>
  48. /// 初始化
  49. /// </summary>
  50. public void NameRules()
  51. {
  52. //
  53. // 设置comboBox1的集合和属性
  54. //
  55. comboBox1.Items.AddRange(InvariantData.numberNum);
  56. comboBox1.SelectedIndex = 0;
  57. #region [设置listView1]
  58. listView1.View = View.Details;
  59. listView1.Columns.Add("1", 0, HorizontalAlignment.Center);
  60. listView1.Columns.Add(PdnResources.GetString("Menu.Set.Generalsettings.format.text"), 60, HorizontalAlignment.Center);
  61. listView1.Columns.Add(PdnResources.GetString("Menu.instruction.text"), 316, HorizontalAlignment.Center);
  62. listView1.BeginUpdate();
  63. var dict = InvariantData.parameterList;
  64. foreach (var s in dict)
  65. {
  66. ListViewItem lvi1 = new ListViewItem();
  67. lvi1.Text = s.Key;
  68. lvi1.SubItems.Add(s.Key);
  69. lvi1.SubItems.Add(s.Value);
  70. listView1.Items.Add(lvi1);
  71. }
  72. listView1.EndUpdate();
  73. #endregion [设置listView1]
  74. #region [读取XMl]
  75. listNames.DataSource = fileNameRule.NameRuleList;
  76. listNames.DisplayMember = "Name";
  77. #endregion
  78. }
  79. /// <summary>
  80. /// 校验名称
  81. /// </summary>
  82. /// <returns></returns>
  83. private bool Inspect()
  84. {
  85. bool check = false;
  86. if (string.IsNullOrEmpty(textBox2.Text))
  87. {
  88. MessageBox.Show(PdnResources.GetString("Menu.pleaseNameformat.text"));
  89. check = true;
  90. return check;
  91. }
  92. else
  93. {
  94. string[] arrs = Regex.Split(textBox2.Text.Trim(), "%");
  95. List<string> strList = InvariantData.parameterList.Keys.ToList();
  96. if (arrs.Length == 1)
  97. {
  98. if (!textBox2.Text.Contains("%"))
  99. {
  100. MessageBox.Show(PdnResources.GetString("ResultViewNameRule2.illigal"));
  101. check = true;
  102. return check;
  103. }
  104. }
  105. foreach (string text in arrs)
  106. {
  107. if (text == "") continue;
  108. if (!strList.Contains("%" + text))
  109. {
  110. MessageBox.Show(PdnResources.GetString("ResultViewNameRule2.illigal"));
  111. check = true;
  112. return check;
  113. }
  114. int index = this.textBox2.Text.IndexOf(text);
  115. if (index == 0)
  116. {
  117. string str = this.textBox2.Text.Substring(0, 1);
  118. if (str != "%")
  119. {
  120. MessageBox.Show(PdnResources.GetString("ResultViewNameRule2.illigal"));
  121. check = true;
  122. return check;
  123. }
  124. }
  125. else if (index == this.textBox2.Text.Length - 1)
  126. {
  127. string str = this.textBox2.Text.Substring(this.textBox2.Text.Length - 2, 1);
  128. if (str != "%")
  129. {
  130. MessageBox.Show(PdnResources.GetString("ResultViewNameRule2.illigal"));
  131. check = true;
  132. return check;
  133. }
  134. }
  135. else
  136. {
  137. string str = this.textBox2.Text.Substring(index - 1, 1);
  138. string str1 = this.textBox2.Text.Substring(index + 1, 1);
  139. if (str != "%" && str1 != "%")
  140. {
  141. MessageBox.Show(PdnResources.GetString("ResultViewNameRule2.illigal"));
  142. check = true;
  143. return check;
  144. }
  145. }
  146. }
  147. if (Islegal())
  148. {
  149. MessageBox.Show(PdnResources.GetString("ResultViewNameRule2.illigalSymbol"));
  150. check = true;
  151. return check;
  152. }
  153. }
  154. if (radioButton1.Checked && !(Regex.IsMatch(textBox3.Text, pattern)))
  155. {
  156. MessageBox.Show(PdnResources.GetString("Menu.Numberscanbeentet.text") + "!");
  157. check = true;
  158. }
  159. return check;
  160. }
  161. /// <summary>
  162. /// 是否符合window命名规则不
  163. /// </summary>
  164. /// <returns></returns>
  165. public bool Islegal()
  166. {
  167. char[] charstr = { '\\', '/', ':', '*', '?', '"', '<', '>', '|' };
  168. char[] textstr = (textBox7.Text.Trim() + textBox8.Text.Trim()).ToString().Trim().ToCharArray();
  169. int count = 0;
  170. for (int i = 0; i < charstr.Length; i++)
  171. {
  172. for (int j = 0; j < textstr.Length; j++)
  173. {
  174. if (charstr[i] == textstr[j])
  175. {
  176. count++;
  177. }
  178. }
  179. }
  180. if (count > 0)
  181. return true;
  182. else
  183. return false;
  184. }
  185. #endregion
  186. #region [操作]
  187. /// <summary>
  188. /// 取消
  189. /// </summary>
  190. /// <param name="sender"></param>
  191. /// <param name="e"></param>
  192. private void button2_Click(object sender, EventArgs e)
  193. {
  194. this.Close();
  195. }
  196. /// <summary>
  197. /// 新增
  198. /// </summary>
  199. /// <param name="sender"></param>
  200. /// <param name="e"></param>
  201. private void button1_Click(object sender, EventArgs e)
  202. {
  203. this.textBox2.TextChanged -= new EventHandler(textBox2_TextChanged);
  204. this.textBox1.Text = string.Empty;
  205. this.textBox4.Text = string.Empty;
  206. this.textBox2.Text = string.Empty;
  207. this.textBox7.Text = string.Empty;
  208. this.textBox8.Text = string.Empty;
  209. this.comboBox1.SelectedIndex = 0;
  210. this.radioButton1.Checked = true;
  211. this.radioButton2.Checked = false;
  212. this.textBox3.Text = "1";
  213. this.textBox5.Text = string.Empty;
  214. this.listNames.SelectedIndex = -1;
  215. this.textBox2.TextChanged += new EventHandler(textBox2_TextChanged);
  216. }
  217. /// <summary>
  218. /// 保存
  219. /// </summary>
  220. /// <param name="sender"></param>
  221. /// <param name="e"></param>
  222. private void btn_Save_Click(object sender, EventArgs e)
  223. {
  224. if (string.IsNullOrEmpty(this.textBox1.Text))
  225. {
  226. MessageBox.Show(PdnResources.GetString("ResultNameView2.InputName"));
  227. return;
  228. }
  229. //先校验
  230. bool pass = Inspect();
  231. if (pass)
  232. {
  233. return;
  234. }
  235. NameRuleListDetails nameRuleListDetails;
  236. string id = Guid.NewGuid().ToString();
  237. string name = this.textBox1.Text;
  238. int index;
  239. if (this.listNames.SelectedItem == null || this.listNames.SelectedIndex == -1)
  240. {
  241. nameRuleListDetails = new NameRuleListDetails();
  242. nameRuleListDetails.ID = id;
  243. var repeatData = fileNameRule.NameRuleList.Where(m => m.Name == name && m.ID != id).FirstOrDefault();
  244. if (repeatData != null)
  245. {
  246. MessageBox.Show(PdnResources.GetString("ResultNameView2.nameRepeat"));
  247. return;
  248. }
  249. fileNameRule.NameRuleList.Add(nameRuleListDetails);
  250. index = fileNameRule.NameRuleList.Count - 1;
  251. }
  252. else {
  253. index = this.listNames.SelectedIndex;
  254. var nameRuleListDetailsB = (NameRuleListDetails)this.listNames.SelectedItem;
  255. var repeatData = fileNameRule.NameRuleList.Where(m => m.Name == name && m.ID != nameRuleListDetailsB.ID).FirstOrDefault();
  256. if (repeatData != null)
  257. {
  258. MessageBox.Show(PdnResources.GetString("ResultNameView2.nameRepeat"));
  259. return;
  260. }
  261. nameRuleListDetails = fileNameRule.NameRuleList.Where(m => m.ID == nameRuleListDetailsB.ID).FirstOrDefault();
  262. }
  263. nameRuleListDetails.Name = this.textBox1.Text;
  264. nameRuleListDetails.Text = this.textBox4.Text;
  265. nameRuleListDetails.NameFormat = this.textBox2.Text;
  266. nameRuleListDetails.ZTextOne = this.textBox7.Text;
  267. nameRuleListDetails.ZTextTwo = this.textBox8.Text;
  268. try
  269. {
  270. nameRuleListDetails.DigitNum = Convert.ToInt32(this.comboBox1.SelectedItem);
  271. }
  272. catch (Exception ex)
  273. {
  274. nameRuleListDetails.DigitNum = 1;
  275. }
  276. try
  277. {
  278. nameRuleListDetails.ContinueNumber = Convert.ToInt32(this.textBox3.Text);
  279. }
  280. catch (Exception ex)
  281. {
  282. nameRuleListDetails.ContinueNumber = 1;
  283. }
  284. nameRuleListDetails.NowNumber = nameRuleListDetails.ContinueNumber;
  285. nameRuleListDetails.isContinue = this.radioButton1.Checked ? true : false;
  286. WriteXML();
  287. this.listNames.SelectedIndexChanged -= new System.EventHandler(this.listNames_SelectedChange);
  288. listNames.DataSource = null;
  289. listNames.DataSource = this.fileNameRule.NameRuleList;
  290. listNames.DisplayMember = "Name";
  291. this.listNames.SelectedIndexChanged += new System.EventHandler(this.listNames_SelectedChange);
  292. listNames.SelectedIndex = index;
  293. MessageBox.Show("保存成功!");
  294. }
  295. /// <summary>
  296. /// 删除
  297. /// </summary>
  298. /// <param name="sender"></param>
  299. /// <param name="e"></param>
  300. private void btn_Del_Click(object sender, EventArgs e)
  301. {
  302. if (this.listNames.SelectedItem == null || this.listNames.SelectedIndex == -1)
  303. {
  304. return;
  305. }
  306. DialogResult result = MessageBox.Show(PdnResources.GetString("ResultViewNameRule2.delete"), PdnResources.GetString("ResultViewNameRule2.deleteTitle"), MessageBoxButtons.OKCancel);
  307. if (result == DialogResult.OK)
  308. {
  309. var nameRuleListDetails = (NameRuleListDetails)this.listNames.SelectedItem;
  310. this.fileNameRule.NameRuleList.Remove(nameRuleListDetails);
  311. this.listNames.SelectedIndexChanged -= new System.EventHandler(this.listNames_SelectedChange);
  312. listNames.DataSource = null;
  313. listNames.DataSource = this.fileNameRule.NameRuleList;
  314. listNames.DisplayMember = "Name";
  315. this.listNames.SelectedIndexChanged += new System.EventHandler(this.listNames_SelectedChange);
  316. WriteXML();
  317. if (this.listNames.Items.Count > 0)
  318. {
  319. listNames.SelectedIndex = 0;
  320. }
  321. else
  322. {
  323. button1_Click(null, null);
  324. }
  325. }
  326. }
  327. /// <summary>
  328. /// 右侧列表选择
  329. /// </summary>
  330. /// <param name="sender"></param>
  331. /// <param name="e"></param>
  332. private void listNames_SelectedChange(object sender, EventArgs e)
  333. {
  334. if (this.listNames.SelectedItem == null || this.listNames.SelectedIndex == -1)
  335. {
  336. return;
  337. }
  338. NameRuleListDetails nameRuleListDetails = (NameRuleListDetails)this.listNames.SelectedItem;
  339. this.textBox1.Text = nameRuleListDetails.Name;
  340. this.textBox4.Text = nameRuleListDetails.Text;
  341. this.textBox2.Text = nameRuleListDetails.NameFormat;
  342. this.textBox7.Text = nameRuleListDetails.ZTextOne;
  343. this.textBox8.Text = nameRuleListDetails.ZTextTwo;
  344. this.comboBox1.SelectedIndex = nameRuleListDetails.DigitNum - 1;
  345. this.textBox3.Text = nameRuleListDetails.ContinueNumber.ToString();
  346. this.radioButton1.Checked = nameRuleListDetails.isContinue ? true : false;
  347. this.radioButton2.Checked = !this.radioButton1.Checked;
  348. }
  349. /// <summary>
  350. /// 保存XML文件
  351. /// </summary>
  352. private void WriteXML()
  353. {
  354. Program.instance.fileNameRule = this.fileNameRule;
  355. string stageModelXml = XmlSerializeHelper.XmlSerialize<FileNameRule>(fileNameRule);
  356. FileOperationHelper.WriteStringToFile(stageModelXml, filePath, FileMode.Create);
  357. }
  358. /// <summary>
  359. ///
  360. /// </summary>
  361. /// <param name="sender"></param>
  362. /// <param name="e"></param>
  363. private void radioButton2_CheckedChanged(object sender, EventArgs e)
  364. {
  365. if (radioButton2.Checked)
  366. {
  367. textBox3.ReadOnly = true;
  368. }
  369. else {
  370. textBox3.ReadOnly = false;
  371. }
  372. if (!string.IsNullOrEmpty(textBox2.Text))
  373. {
  374. textBox2_TextChanged(null, null);
  375. }
  376. }
  377. private void listView1_DoubleClick(object sender, EventArgs e)
  378. {
  379. if (this.listView1.FocusedItem != null && this.listView1.FocusedItem.Index != -1)
  380. {
  381. var dict = InvariantData.parameterList;
  382. int start = this.textBox2.SelectionStart;
  383. this.textBox2.Text = this.textBox2.Text.Insert(start, dict.Keys.ToList()[this.listView1.FocusedItem.Index]);
  384. this.textBox2.SelectionStart = this.textBox2.Text.Length;
  385. }
  386. }
  387. #endregion
  388. /// <summary>
  389. /// 预览展示
  390. /// </summary>
  391. /// <param name="sender"></param>
  392. /// <param name="e"></param>
  393. private void textBox2_TextChanged(object sender, EventArgs e)
  394. {
  395. if (string.IsNullOrEmpty(this.textBox2.Text))
  396. {
  397. this.textBox5.Text = string.Empty;
  398. return;
  399. }
  400. //先校验
  401. bool pass = Inspect();
  402. if (pass)
  403. {
  404. return;
  405. }
  406. string ruleName = string.Empty;
  407. string ruleGain = string.Empty;
  408. List<mic_rulers> list = Program.instance.mic_rulersAll;
  409. var ruler = list.Where(m => m.id.Equals(Program.instance.configModel.RulerId)).FirstOrDefault();
  410. if (ruler != null)
  411. {
  412. ruleName = ruler.ruler_name;
  413. ruleGain = ruler.gain_multiple.ToString();
  414. }
  415. NamingRuleHelper namingRuleHelper = new NamingRuleHelper();
  416. NameRuleListDetails nameRuleListDetails = new NameRuleListDetails();
  417. nameRuleListDetails.Name = this.textBox1.Text;
  418. nameRuleListDetails.Text = this.textBox4.Text;
  419. nameRuleListDetails.NameFormat = this.textBox2.Text;
  420. nameRuleListDetails.ZTextOne = this.textBox7.Text;
  421. nameRuleListDetails.ZTextTwo = this.textBox8.Text;
  422. nameRuleListDetails.isContinue = this.radioButton1.Checked;
  423. try
  424. {
  425. nameRuleListDetails.DigitNum = Convert.ToInt32(this.comboBox1.SelectedItem);
  426. }
  427. catch (Exception ex)
  428. {
  429. nameRuleListDetails.DigitNum = 1;
  430. }
  431. try
  432. {
  433. nameRuleListDetails.ContinueNumber = Convert.ToInt32(this.textBox3.Text);
  434. }
  435. catch (Exception ex)
  436. {
  437. nameRuleListDetails.ContinueNumber = 1;
  438. }
  439. nameRuleListDetails.NowNumber = this.radioButton1.Checked ? nameRuleListDetails.ContinueNumber : 1;
  440. this.textBox5.Text = namingRuleHelper.GetNameFromXml(nameRuleListDetails, ruleName, ruleGain, null, this.filePath);
  441. }
  442. /// <summary>
  443. ///
  444. /// </summary>
  445. /// <param name="sender"></param>
  446. /// <param name="e"></param>
  447. private void text_change(object sender, EventArgs e)
  448. {
  449. if (!string.IsNullOrEmpty(textBox2.Text))
  450. {
  451. textBox2_TextChanged(null, null);
  452. }
  453. }
  454. }
  455. }