PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/danalyser/danalyser/Interface/Forms/CQLQueriesForm.cs

#
C# | 292 lines | 255 code | 25 blank | 12 comment | 40 complexity | 234d695af0846240b140c7b1b0e6240b MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using CQLQueries;
  9. using CQLQueries.Analyzers;
  10. using daSyntaxAnalyser.SyntaxTree;
  11. using daBase;
  12. using daDataLayer;
  13. using System.IO;
  14. using danalyser.Properties;
  15. using danalyser.Controllers;
  16. namespace danalyser.Interface.Forms
  17. {
  18. public partial class CQLForm : Form
  19. {
  20. public CQLForm()
  21. {
  22. InitializeComponent();
  23. }
  24. public CQLForm(String fn)
  25. {
  26. InitializeComponent();
  27. SourceFiles.Items.Add(fn);
  28. }
  29. private void CQLForm_Load(object sender, EventArgs e)
  30. {
  31. CQL_LA.Instance.ErrorOccuredEvent += new EventHandler<ErrorArgs>(ErrorOccered);
  32. CQL_SA.Instance.ErrorOccuredEvent += new EventHandler<ErrorArgs>(ErrorOccered);
  33. CQL_Semantic.Instance.ErrorOccuredEvent += new EventHandler<ErrorArgs>(ErrorOccered);
  34. CQL_Interpreter.Instance.ErrorOccuredEvent += new EventHandler<ErrorArgs>(ErrorOccered);
  35. CQL_Interpreter.Instance.MsgEvent += new EventHandler<MsgArg>(GetMsg);
  36. }
  37. public void ErrorOccered(object sender, ErrorArgs e)
  38. {
  39. RezulData.Rows.Add();
  40. RezulData.Rows[RezulData.RowCount - 1].Cells[0].Value = Resources.Msg3;
  41. RezulData.Rows[RezulData.RowCount - 1].Cells[1].Value = e.Level + ": " + e.Message;
  42. //RezulData.Rows[RezulData.RowCount - 1].Cells[2].Value = e.Position;
  43. RezulData.Rows[RezulData.RowCount - 1].Cells[4].Value = 4;
  44. RezulData.Rows[RezulData.RowCount - 1].Cells[5].Value = e.BeginTok.ToString();
  45. RezulData.Rows[RezulData.RowCount - 1].Cells[6].Value = e.EndTok.ToString();
  46. }
  47. public void GetMsg(object sender, MsgArg e)
  48. {
  49. RezulData.Rows.Add();
  50. RezulData.Rows[RezulData.RowCount - 1].Cells[0].Value = Resources.Msg2;
  51. RezulData.Rows[RezulData.RowCount-1].Cells[1].Value = e.Msg;
  52. RezulData.Rows[RezulData.RowCount - 1].Cells[4].Value = 3;
  53. RezulData.Rows[RezulData.RowCount - 1].Cells[5].Value = e.Beg.ToString();
  54. RezulData.Rows[RezulData.RowCount - 1].Cells[6].Value = e.End.ToString();
  55. RezulData.Rows.Add();
  56. RezulData.Rows[RezulData.RowCount - 1].Cells[0].Value = Resources.Msg2;
  57. RezulData.Rows[RezulData.RowCount-1].Cells[1].Value = "Количество возвращённых ISyntaxElement = " + CQL_Interpreter.Instance.Rezult.Count;
  58. RezulData.Rows[RezulData.RowCount - 1].Cells[4].Value = 2;
  59. int i = 1;
  60. foreach(/*ISyntaxTreeElement*/Object elt in CQL_Interpreter.Instance.Rezult)
  61. {
  62. RezulData.Rows.Add();
  63. RezulData.Rows[RezulData.RowCount - 1].Cells[0].Value = Resources.Msg1;
  64. RezulData.Rows[RezulData.RowCount - 1].Cells[1].Value = elt.ToString();
  65. //RezulData.Rows[RezulData.RowCount - 1].Cells[2].Value = elt.StartInfo.Line.ToString();
  66. //RezulData.Rows[RezulData.RowCount - 1].Cells[3].Value = elt.StartInfo.File;
  67. RezulData.Rows[RezulData.RowCount - 1].Cells[4].Value = 1;
  68. i++;
  69. }
  70. CQL_Interpreter.Instance.Rezult.Clear();
  71. }
  72. private void УдалитьПодстрокуИз_RichTextBox(RichTextBox box, String substr, bool b)
  73. {
  74. int count = 0;
  75. String[] Lines = box.Lines;
  76. String text = "";
  77. int SelectionStart = box.SelectionStart;
  78. for (int i = 0; i < Lines.Length; ++i)
  79. {
  80. if ((SelectionStart >= count) && (SelectionStart <= count + Lines[i].Length))
  81. {
  82. int l = 0;
  83. if(Lines[i].Length<substr.Length) return;
  84. if (!Lines[i].StartsWith(substr))
  85. {
  86. if (b)
  87. {
  88. while ((Lines[i][l]==substr[0]) && (l < Lines[i].Length)) l++;
  89. }
  90. else return;
  91. }
  92. else l = substr.Length;
  93. String str = Lines[i].Substring(l);
  94. text = text + str + '\n';
  95. if (i != Lines.Length - 1)
  96. {
  97. for (int j = i + 1; j < Lines.Length; ++j)
  98. {
  99. text = text = text + Lines[j];
  100. if (j < Lines.Length - 1)
  101. {
  102. text = text + '\n';
  103. }
  104. }
  105. }
  106. box.Text = text;
  107. box.SelectionStart = SelectionStart - l;
  108. return;
  109. }
  110. text = text + Lines[i] + '\n';
  111. count += Lines[i].Length;
  112. }
  113. }
  114. private void ДобавитьПодстрокуВ_RichTextBox(RichTextBox box, String substr)
  115. {
  116. int count = 0;
  117. String[] Lines = box.Lines;
  118. String text = "";
  119. int SelectionStart = box.SelectionStart;
  120. for (int i = 0; i < Lines.Length; ++i)
  121. {
  122. if ((SelectionStart >= count) && (SelectionStart <= count + Lines[i].Length))
  123. {
  124. String str = substr + Lines[i];
  125. text = text + str + '\n';
  126. if (i != Lines.Length - 1)
  127. {
  128. for (int j = i + 1; j < Lines.Length; ++j)
  129. {
  130. text = text = text + Lines[j];
  131. if (j < Lines.Length - 1)
  132. {
  133. text = text + '\n';
  134. }
  135. }
  136. }
  137. box.Text = text;
  138. box.SelectionStart = SelectionStart + substr.Length;
  139. return;
  140. }
  141. text = text + Lines[i] + '\n';
  142. count += Lines[i].Length;
  143. }
  144. }
  145. private void toolStripButton8_Click(object sender, EventArgs e)
  146. {
  147. splitContainer1.Panel2Collapsed = true;
  148. }
  149. private void toolStripButton7_Click(object sender, EventArgs e)
  150. {
  151. splitContainer1.Panel2Collapsed = false;
  152. }
  153. private void toolStripButton9_Click(object sender, EventArgs e)
  154. {
  155. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  156. {
  157. SourceFiles.Items.Add(openFileDialog1.FileName);
  158. }
  159. }
  160. private void toolStripButton2_Click_1(object sender, EventArgs e)
  161. {
  162. if (openFileDialog2.ShowDialog() == DialogResult.OK)
  163. {
  164. ТекстЗапроса.LoadFile(openFileDialog2.FileName, RichTextBoxStreamType.PlainText);
  165. }
  166. }
  167. private void toolStripButton10_Click(object sender, EventArgs e)
  168. {
  169. SourceFiles.Items.RemoveAt(SourceFiles.SelectedIndex);
  170. }
  171. private void toolStripButton1_Click_1(object sender, EventArgs e)
  172. {
  173. splitContainer1.Panel2Collapsed = true;
  174. if (ТекстЗапроса.Text == "")
  175. {
  176. MessageBox.Show("Нет ни одного CQL-запроса!");
  177. return;
  178. }
  179. //РезультатЗапроса.Items.Clear();
  180. RezulData.Rows.Clear();
  181. String Query = ТекстЗапроса.Text;
  182. List<String> fn = new List<string>();
  183. if (SourceFiles.Items.Count == 0)
  184. {
  185. MessageBox.Show("Отсутствуют файлы для статического анализа!");
  186. return;
  187. }
  188. for (int i = 0; i < SourceFiles.Items.Count;++i )
  189. {
  190. fn.Add(SourceFiles.Items[i].ToString());
  191. }
  192. if (CQL_Interpreter.Instance.ExecuteQuery(Query, fn))
  193. {
  194. //РезультатЗапроса.Items.Add("Количество возвращённых ISyntaxElement = " + CQL_Interpreter.Instance.Rezult.Count);
  195. //int i = 1;
  196. //foreach (ISyntaxTreeElement elt in CQL_Interpreter.Instance.Rezult)
  197. //{
  198. // РезультатЗапроса.Items.Add("[" + i.ToString() + "] " + elt.ToString());
  199. // i++;
  200. //}
  201. }
  202. }
  203. private void toolStripButton5_Click_1(object sender, EventArgs e)
  204. {
  205. ДобавитьПодстрокуВ_RichTextBox(ТекстЗапроса, "--");
  206. }
  207. private void toolStripButton6_Click_1(object sender, EventArgs e)
  208. {
  209. УдалитьПодстрокуИз_RichTextBox(ТекстЗапроса, "--", false);
  210. }
  211. private void toolStripButton3_Click_1(object sender, EventArgs e)
  212. {
  213. ДобавитьПодстрокуВ_RichTextBox(ТекстЗапроса, " ");
  214. }
  215. private void toolStripButton4_Click_1(object sender, EventArgs e)
  216. {
  217. УдалитьПодстрокуИз_RichTextBox(ТекстЗапроса, " ", true);
  218. }
  219. private void toolStripButton11_Click(object sender, EventArgs e)
  220. {
  221. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  222. {
  223. ТекстЗапроса.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
  224. }
  225. }
  226. private void CQLForm_Shown(object sender, EventArgs e)
  227. {
  228. ТекстЗапроса.Focus();
  229. }
  230. private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  231. {
  232. }
  233. private void dataGridView1_DoubleClick(object sender, EventArgs e)
  234. {
  235. if (RezulData.RowCount == 0) return;
  236. int ind = RezulData.SelectedRows[0].Index;
  237. switch (Convert.ToInt32(RezulData.Rows[ind].Cells[4].Value))
  238. {
  239. case 1:
  240. FormController.Instance.Editor.ShowLoad(Convert.ToString(RezulData.Rows[ind].Cells[3].Value),
  241. Convert.ToInt32(RezulData.Rows[ind].Cells[2].Value), "test", false);
  242. break;
  243. case 3:
  244. //break;
  245. case 4:
  246. int beg = Convert.ToInt32(RezulData.Rows[ind].Cells[5].Value);
  247. int end = Convert.ToInt32(RezulData.Rows[ind].Cells[6].Value);
  248. if (beg == -1) return;
  249. int len = end - beg;
  250. if (len == 0) len = 1;
  251. ТекстЗапроса.Select(beg-1, len);
  252. break;
  253. }
  254. }
  255. private void dataGridView1_Paint(object sender, PaintEventArgs e)
  256. {
  257. }
  258. private void CQLForm_FormClosed(object sender, FormClosedEventArgs e)
  259. {
  260. CQL_LA.Instance.ErrorOccuredEvent -= new EventHandler<ErrorArgs>(ErrorOccered);
  261. CQL_SA.Instance.ErrorOccuredEvent -= new EventHandler<ErrorArgs>(ErrorOccered);
  262. CQL_Semantic.Instance.ErrorOccuredEvent -= new EventHandler<ErrorArgs>(ErrorOccered);
  263. CQL_Interpreter.Instance.ErrorOccuredEvent -= new EventHandler<ErrorArgs>(ErrorOccered);
  264. CQL_Interpreter.Instance.MsgEvent -= new EventHandler<MsgArg>(GetMsg);
  265. }
  266. }
  267. }