DataTreeHelper.cs 10 KB

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