PageRenderTime 13ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/DynamicTemplate/Plugin/TemplateEditorForm.cs

#
C# | 306 lines | 271 code | 32 blank | 3 comment | 36 complexity | 6dc4f23fbfc0ac3681c1f1a6004e721b 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 System.Collections;
  9. using System.Xml.Serialization;
  10. using System.IO;
  11. using DynamicTemplate.Compiler;
  12. using System.CodeDom.Compiler;
  13. namespace DynamicTemplate.Plugin
  14. {
  15. public partial class TemplateEditorForm : Form
  16. {
  17. private static Dictionary<string, ArgumentType> s_argStrToEnum = new Dictionary<string, ArgumentType>();
  18. private static Dictionary<ArgumentType, string> s_argEnumToStr = new Dictionary<ArgumentType, string>();
  19. private LanguageProvider _provider = new CSharpLanguageProvider();
  20. public TemplateEditorForm()
  21. {
  22. InitializeComponent();
  23. }
  24. protected override void OnLoad(EventArgs e)
  25. {
  26. base.OnLoad(e);
  27. // Do this late because otherwise exceptions occasionally occur while loading
  28. gridArgs.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
  29. }
  30. public Template Template
  31. {
  32. get
  33. {
  34. string body = txtBody.Text;
  35. List<ArgumentDescription> args = new List<ArgumentDescription>();
  36. foreach (DataGridViewRow row in gridArgs.Rows)
  37. {
  38. if (row.IsNewRow)
  39. continue;
  40. string identifier = row.Cells[0].Value as string;
  41. string rawType = row.Cells[1].Value as string;
  42. string label = row.Cells[2].Value as string;
  43. ArgumentType type = s_argStrToEnum[rawType];
  44. args.Add(new ArgumentDescription(type, identifier, label));
  45. }
  46. Template template = new Template();
  47. template.Arguments = args.ToArray();
  48. template.Body = body;
  49. return template;
  50. }
  51. set
  52. {
  53. txtBody.Text = value.Body;
  54. ArgumentDescription[] args = value.Arguments;
  55. gridArgs.Rows.Clear();
  56. if (args.Length > 0)
  57. {
  58. gridArgs.Rows.Add(args.Length);
  59. for (int i = 0; i < args.Length; i++)
  60. {
  61. DataGridViewRow row = gridArgs.Rows[i];
  62. row.Cells[0].Value = args[i].Identifier;
  63. row.Cells[1].Value = s_argEnumToStr[args[i].Type];
  64. row.Cells[2].Value = args[i].Label;
  65. }
  66. }
  67. }
  68. }
  69. public void LoadFile(string filename)
  70. {
  71. Template = Template.Load(filename);
  72. }
  73. public void SaveFile(string filename)
  74. {
  75. Template.Save(filename);
  76. }
  77. static TemplateEditorForm()
  78. {
  79. s_argStrToEnum["Text"] = ArgumentType.TextString;
  80. s_argStrToEnum["Text (Multi-line)"] = ArgumentType.TextStringExtended;
  81. s_argStrToEnum["HTML"] = ArgumentType.HtmlString;
  82. s_argStrToEnum["HTML (Multi-line)"] = ArgumentType.HtmlStringExtended;
  83. s_argStrToEnum["Integer"] = ArgumentType.Integer;
  84. s_argStrToEnum["Double"] = ArgumentType.Double;
  85. s_argStrToEnum["Boolean"] = ArgumentType.Boolean;
  86. s_argStrToEnum["Date/Time"] = ArgumentType.DateTime;
  87. foreach (KeyValuePair<string, ArgumentType> pair in s_argStrToEnum)
  88. s_argEnumToStr[pair.Value] = pair.Key;
  89. }
  90. private void btnDeleteRow_Click(object sender, EventArgs e)
  91. {
  92. if (gridArgs.SelectedRows.Count == 1)
  93. {
  94. DataGridViewRow row = gridArgs.SelectedRows[0];
  95. if (!row.IsNewRow)
  96. gridArgs.Rows.Remove(row);
  97. }
  98. }
  99. private void btnRowUp_Click(object sender, EventArgs e)
  100. {
  101. if (gridArgs.SelectedRows.Count == 1)
  102. {
  103. DataGridViewRow row = gridArgs.SelectedRows[0];
  104. if (!row.IsNewRow)
  105. {
  106. int rowIndex = gridArgs.Rows.IndexOf(row);
  107. if (rowIndex > 0)
  108. {
  109. DataGridViewRow otherRow = gridArgs.Rows[rowIndex - 1];
  110. gridArgs.Rows.RemoveAt(rowIndex - 1);
  111. gridArgs.Rows.Insert(rowIndex, otherRow);
  112. }
  113. }
  114. }
  115. }
  116. private void SwapRows(int rowIndex1, int rowIndex2)
  117. {
  118. if (rowIndex1 == rowIndex2)
  119. return;
  120. if (rowIndex1 > rowIndex2)
  121. {
  122. int tmp = rowIndex2;
  123. rowIndex2 = rowIndex1;
  124. rowIndex1 = tmp;
  125. }
  126. DataGridViewRow row1 = gridArgs.Rows[rowIndex1];
  127. DataGridViewRow row2 = gridArgs.Rows[rowIndex2];
  128. gridArgs.Rows.RemoveAt(rowIndex2);
  129. gridArgs.Rows.RemoveAt(rowIndex1);
  130. gridArgs.Rows.Insert(rowIndex1, row2);
  131. gridArgs.Rows.Insert(rowIndex2, row1);
  132. }
  133. private void btnRowDown_Click(object sender, EventArgs e)
  134. {
  135. if (gridArgs.SelectedRows.Count == 1)
  136. {
  137. DataGridViewRow row = gridArgs.SelectedRows[0];
  138. if (!row.IsNewRow)
  139. {
  140. int rowIndex = gridArgs.Rows.IndexOf(row);
  141. if (rowIndex < gridArgs.Rows.Count - 1)
  142. {
  143. DataGridViewRow otherRow = gridArgs.Rows[rowIndex + 1];
  144. gridArgs.Rows.RemoveAt(rowIndex + 1);
  145. gridArgs.Rows.Insert(rowIndex, otherRow);
  146. }
  147. }
  148. }
  149. }
  150. private void gridArgs_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
  151. {
  152. e.Row.Cells[1].Value = s_argEnumToStr[ArgumentType.TextString];
  153. }
  154. private void btnOK_Click(object sender, EventArgs e)
  155. {
  156. if (ValidateArgs() && Compile())
  157. DialogResult = DialogResult.OK;
  158. }
  159. private bool Compile()
  160. {
  161. UseWaitCursor = true;
  162. try
  163. {
  164. new BodyParser().Parse(Template.Body, Template.Arguments);
  165. return true;
  166. }
  167. catch (TemplateCompilationException ex)
  168. {
  169. int sourceLine = ex.Position.Line;
  170. int sourceCol = ex.Position.Column;
  171. if (sourceLine >= 0 && sourceCol >= 0)
  172. {
  173. int idx = IndexToPosition.ReverseFind(txtBody.Text, sourceLine, sourceCol);
  174. if (idx >= 0)
  175. {
  176. txtBody.Select(idx, 0);
  177. //tooltipPos = TooltipHelper.GetPointFromIndex(txtBody, idx);
  178. //tooltipPos.Offset(txtBody.Location);
  179. txtBody.Select();
  180. }
  181. }
  182. string errorTitle = "Compile Error";
  183. string errorMessage = ex.Message;
  184. ShowError(errorTitle, errorMessage, OffsetToOrigin(txtBody, new Point(0, txtBody.Height)));
  185. return false;
  186. }
  187. finally
  188. {
  189. UseWaitCursor = false;
  190. }
  191. }
  192. private void ShowError(string errorTitle, string errorMessage, Point p)
  193. {
  194. toolTip.ToolTipTitle = errorTitle;
  195. toolTip.ToolTipIcon = ToolTipIcon.Warning;
  196. toolTip.Show(errorMessage, pnlOrigin, p, 10000);
  197. }
  198. private bool ValidateArgs()
  199. {
  200. Dictionary<string, DataGridViewRow> usedIdentifiers = new Dictionary<string, DataGridViewRow>();
  201. foreach (DataGridViewRow row in gridArgs.Rows)
  202. {
  203. if (row.IsNewRow)
  204. continue;
  205. DataGridViewCell cell = row.Cells[0];
  206. if (!ValidateCell(cell, (string)cell.FormattedValue))
  207. {
  208. if (!cell.Displayed)
  209. {
  210. gridArgs.FirstDisplayedCell = cell;
  211. }
  212. Rectangle rect = gridArgs.GetCellDisplayRectangle(cell.ColumnIndex, cell.RowIndex, true);
  213. ShowError("Error", cell.ErrorText, OffsetToOrigin(gridArgs, new Point(rect.Left, rect.Bottom)));
  214. return false;
  215. }
  216. string identifier = (string)cell.FormattedValue;
  217. string normalizedIdentifier = _provider.NormalizeIdentifier(identifier);
  218. if (normalizedIdentifier == "_selection")
  219. {
  220. foreach (DataGridViewRow row2 in gridArgs.Rows)
  221. row2.Selected = row2 == row;
  222. ShowError("Error", "_selection is a reserved variable name and cannot be used", OffsetToOrigin(gridArgs, new Point(0, gridArgs.Height)));
  223. return false;
  224. }
  225. if (usedIdentifiers.ContainsKey(normalizedIdentifier))
  226. {
  227. DataGridViewRow prevRow = usedIdentifiers[normalizedIdentifier];
  228. foreach (DataGridViewRow row2 in gridArgs.Rows)
  229. {
  230. row2.Selected = (row2 == row || row2 == prevRow);
  231. }
  232. gridArgs.FirstDisplayedScrollingRowIndex = prevRow.Index;
  233. ShowError("Error", "Variable names must be unique", OffsetToOrigin(gridArgs, new Point(0, gridArgs.Height)));
  234. return false;
  235. }
  236. usedIdentifiers.Add(normalizedIdentifier, row);
  237. }
  238. return true;
  239. }
  240. private void gridArgs_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
  241. {
  242. if (e.ColumnIndex == 0)
  243. {
  244. DataGridViewCell cell = gridArgs.Rows[e.RowIndex].Cells[0];
  245. ValidateCell(cell, (string)e.FormattedValue);
  246. }
  247. }
  248. private void gridArgs_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
  249. {
  250. DataGridViewCell cell = gridArgs.Rows[e.RowIndex].Cells[0];
  251. ValidateCell(cell, (string)cell.FormattedValue);
  252. }
  253. private bool ValidateCell(DataGridViewCell cell, string identifier)
  254. {
  255. if (cell.OwningRow.IsNewRow)
  256. return true;
  257. string errorMessage;
  258. if (!_provider.IsValidIdentifier(identifier, out errorMessage))
  259. {
  260. cell.ErrorText = errorMessage;
  261. return false;
  262. }
  263. else
  264. {
  265. cell.ErrorText = "";
  266. }
  267. return true;
  268. }
  269. private Point OffsetToOrigin(Control reference, Point p)
  270. {
  271. return pnlOrigin.PointToClient(reference.PointToScreen(p));
  272. }
  273. }
  274. }