ResultViewNameRule2.cs 22 KB

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