PageRenderTime 64ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/DictManage/FormMain.cs

https://gitlab.com/lzlzlz911/PanGu4Net
C# | 413 lines | 322 code | 75 blank | 16 comment | 41 complexity | 2edb89d44ac925d962f5a023a9d0c497 MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. using System;
  18. using System.Collections.Generic;
  19. using System.ComponentModel;
  20. using System.Data;
  21. using System.Drawing;
  22. using System.Text;
  23. using System.Windows.Forms;
  24. using PanGu;
  25. using PanGu.Dict;
  26. namespace DictManage
  27. {
  28. public partial class FormMain : Form
  29. {
  30. WordDictionary _WordDict = null;
  31. String m_DictFileName;
  32. string _Version = "";
  33. private int Count
  34. {
  35. get
  36. {
  37. if (_WordDict != null)
  38. {
  39. return _WordDict.Count;
  40. }
  41. else
  42. {
  43. return 0;
  44. }
  45. }
  46. }
  47. public FormMain()
  48. {
  49. InitializeComponent();
  50. }
  51. private void ShowCount()
  52. {
  53. labelCount.Text = Count.ToString();
  54. }
  55. private void openBinDictFile13ToolStripMenuItem_Click(object sender, EventArgs e)
  56. {
  57. openFileDialogDict.RestoreDirectory = true;
  58. openFileDialogDict.FileName = "Dict.dct";
  59. openFileDialogDict.Filter = "Dictionay file|*.dct";
  60. if (openFileDialogDict.ShowDialog() == DialogResult.OK)
  61. {
  62. try
  63. {
  64. DateTime old = DateTime.Now;
  65. _WordDict = new WordDictionary();
  66. _WordDict.Load(openFileDialogDict.FileName, out _Version);
  67. TimeSpan s = DateTime.Now - old;
  68. statusStrip.Items[0].Text = s.TotalMilliseconds.ToString() + "ms";
  69. }
  70. catch (Exception e1)
  71. {
  72. MessageBox.Show(String.Format("Can not open dictionary, errmsg:{0}", e1.Message));
  73. return;
  74. }
  75. panelMain.Enabled = true;
  76. m_DictFileName = openFileDialogDict.FileName;
  77. this.Text = "V" + _Version + " " + openFileDialogDict.FileName;
  78. ShowCount();
  79. }
  80. }
  81. private void saveBinDictFile13ToolStripMenuItem_Click(object sender, EventArgs e)
  82. {
  83. if (_WordDict == null)
  84. {
  85. return;
  86. }
  87. saveFileDialogDict.RestoreDirectory = true;
  88. saveFileDialogDict.FileName = "Dict.dct";
  89. saveFileDialogDict.Filter = "Dictionay file|*.dct";
  90. if (saveFileDialogDict.ShowDialog() == DialogResult.OK)
  91. {
  92. FormInputDictVersion frmInputDictVersion = new FormInputDictVersion();
  93. frmInputDictVersion.Version = _Version;
  94. if (frmInputDictVersion.ShowDialog() == DialogResult.OK)
  95. {
  96. _WordDict.Save(saveFileDialogDict.FileName,
  97. frmInputDictVersion.Version);
  98. }
  99. }
  100. }
  101. private void buttonSearch_Click(object sender, EventArgs e)
  102. {
  103. if (textBoxSearch.Text.Trim() == "")
  104. {
  105. return;
  106. }
  107. List<SearchWordResult> result = _WordDict.Search(textBoxSearch.Text.Trim());
  108. result.Sort();
  109. listBoxList.Items.Clear();
  110. foreach (SearchWordResult word in result)
  111. {
  112. listBoxList.Items.Add(word);
  113. }
  114. label.Text = "符合条件的记录数:" + result.Count.ToString();
  115. }
  116. private void listBoxList_SelectedIndexChanged(object sender, EventArgs e)
  117. {
  118. int index = listBoxList.SelectedIndex;
  119. if (index < 0)
  120. {
  121. return;
  122. }
  123. object obj = listBoxList.Items[index];
  124. SearchWordResult word = (SearchWordResult)obj;
  125. textBoxWord.Text = word.Word.Word;
  126. numericUpDownFrequency.Value = (decimal)word.Word.Frequency;
  127. posCtrl.Pos = (int)word.Word.Pos;
  128. }
  129. private void textBoxWord_TextChanged(object sender, EventArgs e)
  130. {
  131. String word = textBoxWord.Text.Trim();
  132. if (word == "")
  133. {
  134. buttonUpdate.Enabled = false;
  135. buttonInsert.Enabled = false;
  136. buttonDelete.Enabled = false;
  137. return;
  138. }
  139. WordAttribute selWord = _WordDict.GetWordAttr(word);
  140. if (selWord != null)
  141. {
  142. buttonUpdate.Enabled = true;
  143. buttonInsert.Enabled = false;
  144. buttonDelete.Enabled = true;
  145. numericUpDownFrequency.Value = (decimal)selWord.Frequency;
  146. posCtrl.Pos = (int)selWord.Pos;
  147. }
  148. else
  149. {
  150. buttonUpdate.Enabled = false;
  151. buttonInsert.Enabled = true;
  152. buttonDelete.Enabled = false;
  153. numericUpDownFrequency.Value = 0;
  154. posCtrl.Pos = 0;
  155. }
  156. }
  157. private void buttonInsert_Click(object sender, EventArgs e)
  158. {
  159. _WordDict.InsertWord(textBoxWord.Text.Trim(), (double)numericUpDownFrequency.Value, (POS)posCtrl.Pos);
  160. MessageBox.Show("增加成功", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
  161. ShowCount();
  162. textBoxWord_TextChanged(sender, e);
  163. }
  164. private void buttonUpdate_Click(object sender, EventArgs e)
  165. {
  166. _WordDict.UpdateWord(textBoxWord.Text.Trim(), (double)numericUpDownFrequency.Value, (POS)posCtrl.Pos);
  167. MessageBox.Show("修改成功", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
  168. ShowCount();
  169. textBoxWord_TextChanged(sender, e);
  170. }
  171. private void buttonDelete_Click(object sender, EventArgs e)
  172. {
  173. if (MessageBox.Show("确认删除改单词?", "删除提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information)
  174. == DialogResult.Yes)
  175. {
  176. _WordDict.DeleteWord(textBoxWord.Text.Trim());
  177. MessageBox.Show("删除成功", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
  178. }
  179. ShowCount();
  180. textBoxWord_TextChanged(sender, e);
  181. }
  182. private void BatchInsert(String fileName, String encoder)
  183. {
  184. String content = PanGu.Framework.File.ReadFileToString(fileName, Encoding.GetEncoding(encoder));
  185. String[] words = PanGu.Framework.Regex.Split(content, @"\r\n");
  186. bool allUse = false;
  187. WordAttribute lstWord = null;
  188. foreach (String word in words)
  189. {
  190. if (word == null)
  191. {
  192. continue;
  193. }
  194. if (word.Trim() == "")
  195. {
  196. continue;
  197. }
  198. FormBatchInsert frmBatchInsert = new FormBatchInsert();
  199. if (!allUse || lstWord == null)
  200. {
  201. frmBatchInsert.Word.Word = word.Trim();
  202. if (frmBatchInsert.ShowDialog() == DialogResult.OK)
  203. {
  204. lstWord = frmBatchInsert.Word;
  205. allUse = frmBatchInsert.AllUse;
  206. _WordDict.InsertWord(lstWord.Word, lstWord.Frequency, lstWord.Pos);
  207. }
  208. }
  209. else
  210. {
  211. lstWord.Word = word.Trim();
  212. _WordDict.InsertWord(lstWord.Word, lstWord.Frequency, lstWord.Pos);
  213. }
  214. }
  215. }
  216. private void buttonBatchInsert_Click(object sender, EventArgs e)
  217. {
  218. openFileDialogDict.RestoreDirectory = true;
  219. openFileDialogDict.FileName = "*.txt";
  220. openFileDialogDict.Filter = "Text files|*.txt";
  221. if (openFileDialogDict.ShowDialog() == DialogResult.OK)
  222. {
  223. try
  224. {
  225. FormEncoder frmEncoder = new FormEncoder();
  226. if (frmEncoder.ShowDialog() != DialogResult.OK)
  227. {
  228. return;
  229. }
  230. BatchInsert(openFileDialogDict.FileName, frmEncoder.Encoding);
  231. MessageBox.Show("批量增加成功,注意只有保存字典后,导入的单词才会生效!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
  232. ShowCount();
  233. }
  234. catch(Exception e1)
  235. {
  236. MessageBox.Show(e1.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  237. }
  238. }
  239. }
  240. private void ListWordsByPos(int pos)
  241. {
  242. List<SearchWordResult> result = _WordDict.SearchByPos((POS)pos);
  243. result.Sort();
  244. listBoxList.Items.Clear();
  245. foreach (SearchWordResult word in result)
  246. {
  247. listBoxList.Items.Add(word);
  248. }
  249. label.Text = "符合条件的记录数:" + result.Count.ToString();
  250. }
  251. private void ListWordsByLength(int length)
  252. {
  253. List<SearchWordResult> result = _WordDict.SearchByLength(length);
  254. result.Sort();
  255. listBoxList.Items.Clear();
  256. foreach (SearchWordResult word in result)
  257. {
  258. listBoxList.Items.Add(word);
  259. }
  260. label.Text = "符合条件的记录数:" + result.Count.ToString();
  261. }
  262. private void findToolStripMenuItem_Click(object sender, EventArgs e)
  263. {
  264. FormFind frmFind = new FormFind();
  265. frmFind.ShowDialog();
  266. switch (frmFind.Mode)
  267. {
  268. case FormFind.SearchMode.None:
  269. listBoxList.Items.Clear();
  270. break;
  271. case FormFind.SearchMode.ByPos:
  272. ListWordsByPos(frmFind.POS);
  273. break;
  274. case FormFind.SearchMode.ByLength:
  275. ListWordsByLength(frmFind.Length);
  276. break;
  277. }
  278. }
  279. private void exportToolStripMenuItem_Click(object sender, EventArgs e)
  280. {
  281. if (saveFileDialogText.ShowDialog() == DialogResult.OK)
  282. {
  283. StringBuilder str = new StringBuilder();
  284. foreach (object text in listBoxList.Items)
  285. {
  286. str.AppendLine(text.ToString());
  287. }
  288. PanGu.Framework.File.WriteString(saveFileDialogText.FileName, str.ToString(), Encoding.UTF8);
  289. MessageBox.Show("Save OK!");
  290. }
  291. }
  292. private void textBoxSearch_TextChanged(object sender, EventArgs e)
  293. {
  294. buttonSearch_Click(sender, e);
  295. }
  296. private void OpenAsTextToolStripMenuItem_Click(object sender, EventArgs e)
  297. {
  298. openFileDialogDict.RestoreDirectory = true;
  299. openFileDialogDict.FileName = "Dict.txt";
  300. openFileDialogDict.Filter = "Dictionay file|*.txt";
  301. if (openFileDialogDict.ShowDialog() == DialogResult.OK)
  302. {
  303. _Version = "";
  304. try
  305. {
  306. DateTime old = DateTime.Now;
  307. _WordDict = new WordDictionary();
  308. _WordDict.Load(openFileDialogDict.FileName, true, out _Version);
  309. TimeSpan s = DateTime.Now - old;
  310. statusStrip.Items[0].Text = s.TotalMilliseconds.ToString() + "ms";
  311. }
  312. catch (Exception e1)
  313. {
  314. MessageBox.Show(String.Format("Can not open dictionary, errmsg:{0}", e1.Message));
  315. return;
  316. }
  317. panelMain.Enabled = true;
  318. m_DictFileName = openFileDialogDict.FileName;
  319. this.Text = "V" + _Version + " " + openFileDialogDict.FileName;
  320. ShowCount();
  321. }
  322. }
  323. private void SaveAsTextToolStripMenuItem_Click(object sender, EventArgs e)
  324. {
  325. if (_WordDict == null)
  326. {
  327. return;
  328. }
  329. saveFileDialogDict.RestoreDirectory = true;
  330. saveFileDialogDict.FileName = "Dict.txt";
  331. saveFileDialogDict.Filter = "Dictionay file|*.txt";
  332. if (saveFileDialogDict.ShowDialog() == DialogResult.OK)
  333. {
  334. _WordDict.SaveToText(saveFileDialogDict.FileName);
  335. }
  336. }
  337. }
  338. }