DataTreeHelper.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. using FastReport.Data;
  2. using FastReport.Dialog;
  3. using FastReport.Utils;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.Reflection;
  9. using System.Windows.Forms;
  10. namespace FastReport.Controls
  11. {
  12. internal static class DataTreeHelper
  13. {
  14. public static void AddColumns(TreeNodeCollection root, ColumnCollection columns, bool enabledOnly, bool showColumns)
  15. {
  16. foreach (Column column in columns)
  17. {
  18. if (!enabledOnly || column.Enabled)
  19. {
  20. TreeNode node = new TreeNode(column.Alias);
  21. node.Tag = column;
  22. node.Checked = column.Enabled;
  23. int imageIndex = column.GetImageIndex();
  24. node.ImageIndex = imageIndex;
  25. node.SelectedImageIndex = imageIndex;
  26. AddColumns(node.Nodes, column.Columns, enabledOnly, showColumns);
  27. bool isDataSource = column is DataSourceBase;
  28. bool addNode = showColumns || isDataSource || node.Nodes.Count > 0;
  29. if (addNode)
  30. root.Add(node);
  31. }
  32. }
  33. }
  34. public static void AddDataSource(Dictionary dictionary, DataSourceBase data, TreeNodeCollection root,
  35. bool enabledOnly, bool showRelations, bool showColumns)
  36. {
  37. AddDataSource(dictionary, data, root, null, new ArrayList(), enabledOnly, showRelations, showColumns, false);
  38. }
  39. private static void AddDataSource(Dictionary dictionary, DataSourceBase data, TreeNodeCollection root,
  40. Relation rel, ArrayList processed, bool enabledOnly, bool showRelations, bool showColumns,
  41. bool useRelationName)
  42. {
  43. if (data == null)
  44. return;
  45. TreeNode dataNode = root.Add(rel != null && useRelationName ? rel.Alias : data.Alias);
  46. dataNode.Tag = rel != null ? (object)rel : (object)data;
  47. dataNode.Checked = data.Enabled;
  48. dataNode.ImageIndex = rel != null ? 58 : data is ProcedureDataSource ? 260 : 222;
  49. dataNode.SelectedImageIndex = dataNode.ImageIndex;
  50. bool alreadyProcessed = processed.Contains(data);
  51. processed.Add(data);
  52. // process relations
  53. if (showRelations && !alreadyProcessed)
  54. {
  55. foreach (Relation relation in dictionary.Relations)
  56. {
  57. if ((!enabledOnly || relation.Enabled) && relation.ChildDataSource == data)
  58. {
  59. useRelationName = GetNumberOfRelations(dictionary, relation.ParentDataSource, relation.ChildDataSource) > 1;
  60. AddDataSource(dictionary, relation.ParentDataSource, dataNode.Nodes, relation, processed,
  61. enabledOnly, true, showColumns, useRelationName);
  62. }
  63. }
  64. }
  65. // add columns and/or nested datasources
  66. AddColumns(dataNode.Nodes, data.Columns, enabledOnly, showColumns);
  67. processed.Remove(data);
  68. }
  69. private static void AddParameter(Parameter par, TreeNodeCollection root)
  70. {
  71. TreeNode parNode = root.Add(par.Name);
  72. parNode.Tag = par;
  73. parNode.ImageIndex = par.Parameters.Count == 0 ? GetTypeImageIndex(par.DataType) : 234;
  74. parNode.SelectedImageIndex = parNode.ImageIndex;
  75. foreach (Parameter p in par.Parameters)
  76. {
  77. AddParameter(p, parNode.Nodes);
  78. }
  79. }
  80. private static void AddVariable(Parameter par, TreeNodeCollection root)
  81. {
  82. TreeNode parNode = root.Add(par.Name);
  83. parNode.Tag = par;
  84. parNode.ImageIndex = GetTypeImageIndex(par.DataType);
  85. parNode.SelectedImageIndex = parNode.ImageIndex;
  86. foreach (Parameter p in par.Parameters)
  87. {
  88. AddParameter(p, parNode.Nodes);
  89. }
  90. }
  91. public static void AddCubeSource(Dictionary dictionary, CubeSourceBase cube, TreeNodeCollection root,
  92. bool enabledOnly)
  93. {
  94. AddCubeSource(dictionary, cube, root, new ArrayList(), enabledOnly);
  95. }
  96. private static void AddCubeSource(Dictionary dictionary, CubeSourceBase cube, TreeNodeCollection root,
  97. ArrayList processed, bool enabledOnly)
  98. {
  99. if (cube == null)
  100. return;
  101. TreeNode dataNode = root.Add(cube.Alias);
  102. dataNode.Tag = (object)cube;
  103. dataNode.Checked = cube.Enabled;
  104. dataNode.ImageIndex = 248;
  105. dataNode.SelectedImageIndex = dataNode.ImageIndex;
  106. }
  107. private static void CreateFunctionsTree(Report report, FunctionInfo rootItem, TreeNodeCollection rootNode)
  108. {
  109. foreach (FunctionInfo item in rootItem.Items)
  110. {
  111. string text;
  112. int imageIndex = 66;
  113. MethodInfo func = item.Function;
  114. if (func != null)
  115. {
  116. text = func.Name;
  117. // if this is an overridden function, show its parameters
  118. if (rootItem.Name == text)
  119. text = report.CodeHelper.GetMethodSignature(func, false);
  120. imageIndex = GetTypeImageIndex(func.ReturnType);
  121. }
  122. else
  123. {
  124. // it's a category
  125. text = Res.TryGet(item.Text);
  126. }
  127. TreeNode node = rootNode.Add(text);
  128. node.Tag = func;
  129. node.ImageIndex = imageIndex;
  130. node.SelectedImageIndex = imageIndex;
  131. if (item.Items.Count > 0)
  132. CreateFunctionsTree(report, item, node.Nodes);
  133. }
  134. }
  135. public static void CreateDataTree(Dictionary dictionary, TreeNodeCollection root, bool enabledOnly,
  136. bool relations, bool dataSources, bool columns)
  137. {
  138. if (dataSources)
  139. {
  140. // create connection node and enumerate connection tables
  141. foreach (DataConnectionBase connection in dictionary.Connections)
  142. {
  143. TreeNode connNode = root.Add(connection.Name);
  144. connNode.Tag = connection;
  145. connNode.Checked = true;
  146. connNode.ImageIndex = 53;
  147. connNode.SelectedImageIndex = connNode.ImageIndex;
  148. foreach (TableDataSource data in connection.Tables)
  149. {
  150. if (!enabledOnly || data.Enabled)
  151. AddDataSource(dictionary, data, connNode.Nodes, enabledOnly, relations, columns);
  152. }
  153. }
  154. // process regular tables
  155. foreach (DataSourceBase data in dictionary.DataSources)
  156. {
  157. if (!enabledOnly || data.Enabled)
  158. AddDataSource(dictionary, data, root, enabledOnly, relations, columns);
  159. }
  160. }
  161. else if (relations)
  162. {
  163. // display relations only (used by the Relation type editor)
  164. foreach (Relation relation in dictionary.Relations)
  165. {
  166. if (relation.ParentDataSource == null || relation.ChildDataSource == null)
  167. continue;
  168. TreeNode relNode = root.Add(relation.Alias + " (" +
  169. relation.ParentDataSource.Alias + "->" + relation.ChildDataSource.Alias + ")");
  170. relNode.Tag = relation;
  171. relNode.Checked = true;
  172. relNode.ImageIndex = 58;
  173. relNode.SelectedImageIndex = relNode.ImageIndex;
  174. }
  175. }
  176. }
  177. public static void CreateDataTree(Dictionary dictionary, DataConnectionBase connection, TreeNodeCollection root, bool tables, bool queries, bool procedures)
  178. {
  179. SortedList<string, TableDataSource> list = new SortedList<string, TableDataSource>();
  180. // sort the tables first
  181. foreach (TableDataSource table in connection.Tables)
  182. {
  183. bool isProcedure = table is ProcedureDataSource;
  184. bool isQuery = !isProcedure && !string.IsNullOrEmpty(table.SelectCommand);
  185. bool isTable = !isProcedure && !isQuery;
  186. if (isTable && !tables)
  187. continue;
  188. if (isQuery && !queries)
  189. continue;
  190. if (isProcedure && !procedures)
  191. continue;
  192. string tableName = table.Alias;
  193. // fix the node text if table is not a query: display the TableName instead of Alias
  194. if (isTable)
  195. tableName = table.TableName.Replace("\"", "");
  196. list.Add(tableName, table);
  197. }
  198. foreach (KeyValuePair<string, TableDataSource> keyValue in list)
  199. {
  200. var data = keyValue.Value;
  201. AddDataSource(dictionary, data, root, false, false, true);
  202. var node = root[root.Count - 1];
  203. node.Text = data is ProcedureDataSource proc ? proc.DisplayNameWithParams : keyValue.Key;
  204. // add dummy child node for the table which has no schema, to allow expand the node and get schema
  205. if (data.Columns.Count == 0)
  206. node.Nodes.Add("dummy");
  207. }
  208. }
  209. public static void CreateParametersTree(ParameterCollection parameters, TreeNodeCollection root)
  210. {
  211. foreach (Parameter p in parameters)
  212. {
  213. AddParameter(p, root);
  214. }
  215. }
  216. public static void CreateVariablesTree(ParameterCollection parameters, TreeNodeCollection root)
  217. {
  218. foreach (Parameter p in parameters)
  219. {
  220. AddVariable(p, root);
  221. }
  222. }
  223. public static void CreateTotalsTree(TotalCollection totals, TreeNodeCollection root)
  224. {
  225. foreach (Total s in totals)
  226. {
  227. TreeNode sumNode = root.Add(s.Name);
  228. sumNode.Tag = s;
  229. sumNode.ImageIndex = 132;
  230. sumNode.SelectedImageIndex = sumNode.ImageIndex;
  231. }
  232. }
  233. public static void CreateCustomItemsTree(Report report, TreeNodeCollection root)
  234. {
  235. TreeNode dialogNode = null;
  236. foreach (Base c in report.AllObjects)
  237. {
  238. if (c is DialogControl)
  239. {
  240. PropertyInfo bindableProp = (c as DialogControl).BindableProperty;
  241. if (bindableProp != null)
  242. {
  243. if (dialogNode == null)
  244. {
  245. dialogNode = root.Add(Res.Get("Designer,ToolWindow,Dictionary,DialogControls"));
  246. dialogNode.ImageIndex = 136;
  247. dialogNode.SelectedImageIndex = dialogNode.ImageIndex;
  248. }
  249. TreeNode node = dialogNode.Nodes.Add(c.Name + "." + bindableProp.Name);
  250. node.ImageIndex = DataTreeHelper.GetTypeImageIndex(bindableProp.PropertyType);
  251. node.SelectedImageIndex = node.ImageIndex;
  252. }
  253. }
  254. }
  255. }
  256. public static void CreateFunctionsTree(Report report, TreeNodeCollection root)
  257. {
  258. CreateFunctionsTree(report, RegisteredObjects.FindFunctionsRoot(), root);
  259. }
  260. public static void CreateCubeTree(Dictionary dictionary, TreeNodeCollection root, bool enabledOnly)
  261. {
  262. foreach (CubeSourceBase cube in dictionary.CubeSources)
  263. {
  264. if (!enabledOnly || cube.Enabled)
  265. AddCubeSource(dictionary, cube, root, enabledOnly);
  266. }
  267. }
  268. private static int GetNumberOfRelations(Dictionary dictionary, DataSourceBase parent, DataSourceBase child)
  269. {
  270. int result = 0;
  271. foreach (Relation rel in dictionary.Relations)
  272. {
  273. if (rel.ParentDataSource == parent && rel.ChildDataSource == child)
  274. result++;
  275. }
  276. return result;
  277. }
  278. public static int GetTypeImageIndex(Type dataType)
  279. {
  280. int index = 224;
  281. if (dataType == typeof(string) || dataType == typeof(char))
  282. index = 223;
  283. else if (dataType == typeof(float) || dataType == typeof(double))
  284. index = 225;
  285. else if (dataType == typeof(decimal))
  286. index = 226;
  287. else if (dataType == typeof(DateTime) || dataType == typeof(TimeSpan))
  288. index = 227;
  289. else if (dataType == typeof(bool))
  290. index = 228;
  291. else if (dataType == typeof(byte[]) || dataType == typeof(Image) || dataType == typeof(Bitmap))
  292. index = 229;
  293. return index;
  294. }
  295. }
  296. }