123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- using FastReport.Data;
- using FastReport.Dialog;
- using FastReport.Utils;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Reflection;
- using System.Windows.Forms;
- namespace FastReport.Controls
- {
- internal static class DataTreeHelper
- {
- public static void AddColumns(TreeNodeCollection root, ColumnCollection columns, bool enabledOnly, bool showColumns)
- {
- foreach (Column column in columns)
- {
- if (!enabledOnly || column.Enabled)
- {
- TreeNode node = new TreeNode(column.Alias);
- node.Tag = column;
- node.Checked = column.Enabled;
- int imageIndex = column.GetImageIndex();
- node.ImageIndex = imageIndex;
- node.SelectedImageIndex = imageIndex;
- AddColumns(node.Nodes, column.Columns, enabledOnly, showColumns);
- bool isDataSource = column is DataSourceBase;
- bool addNode = showColumns || isDataSource || node.Nodes.Count > 0;
- if (addNode)
- root.Add(node);
- }
- }
- }
- public static void AddDataSource(Dictionary dictionary, DataSourceBase data, TreeNodeCollection root,
- bool enabledOnly, bool showRelations, bool showColumns)
- {
- AddDataSource(dictionary, data, root, null, new ArrayList(), enabledOnly, showRelations, showColumns, false);
- }
- private static void AddDataSource(Dictionary dictionary, DataSourceBase data, TreeNodeCollection root,
- Relation rel, ArrayList processed, bool enabledOnly, bool showRelations, bool showColumns,
- bool useRelationName)
- {
- if (data == null)
- return;
- TreeNode dataNode = root.Add(rel != null && useRelationName ? rel.Alias : data.Alias);
- dataNode.Tag = rel != null ? (object)rel : (object)data;
- dataNode.Checked = data.Enabled;
- dataNode.ImageIndex = rel != null ? 58 : data is ProcedureDataSource ? 260 : 222;
- dataNode.SelectedImageIndex = dataNode.ImageIndex;
- bool alreadyProcessed = processed.Contains(data);
- processed.Add(data);
- // process relations
- if (showRelations && !alreadyProcessed)
- {
- foreach (Relation relation in dictionary.Relations)
- {
- if ((!enabledOnly || relation.Enabled) && relation.ChildDataSource == data)
- {
- useRelationName = GetNumberOfRelations(dictionary, relation.ParentDataSource, relation.ChildDataSource) > 1;
- AddDataSource(dictionary, relation.ParentDataSource, dataNode.Nodes, relation, processed,
- enabledOnly, true, showColumns, useRelationName);
- }
- }
- }
- // add columns and/or nested datasources
- AddColumns(dataNode.Nodes, data.Columns, enabledOnly, showColumns);
- processed.Remove(data);
- }
- private static void AddParameter(Parameter par, TreeNodeCollection root)
- {
- TreeNode parNode = root.Add(par.Name);
- parNode.Tag = par;
- parNode.ImageIndex = par.Parameters.Count == 0 ? GetTypeImageIndex(par.DataType) : 234;
- parNode.SelectedImageIndex = parNode.ImageIndex;
- foreach (Parameter p in par.Parameters)
- {
- AddParameter(p, parNode.Nodes);
- }
- }
- private static void AddVariable(Parameter par, TreeNodeCollection root)
- {
- TreeNode parNode = root.Add(par.Name);
- parNode.Tag = par;
- parNode.ImageIndex = GetTypeImageIndex(par.DataType);
- parNode.SelectedImageIndex = parNode.ImageIndex;
- foreach (Parameter p in par.Parameters)
- {
- AddParameter(p, parNode.Nodes);
- }
- }
- public static void AddCubeSource(Dictionary dictionary, CubeSourceBase cube, TreeNodeCollection root,
- bool enabledOnly)
- {
- AddCubeSource(dictionary, cube, root, new ArrayList(), enabledOnly);
- }
- private static void AddCubeSource(Dictionary dictionary, CubeSourceBase cube, TreeNodeCollection root,
- ArrayList processed, bool enabledOnly)
- {
- if (cube == null)
- return;
- TreeNode dataNode = root.Add(cube.Alias);
- dataNode.Tag = (object)cube;
- dataNode.Checked = cube.Enabled;
- dataNode.ImageIndex = 248;
- dataNode.SelectedImageIndex = dataNode.ImageIndex;
- }
- private static void CreateFunctionsTree(Report report, FunctionInfo rootItem, TreeNodeCollection rootNode)
- {
- foreach (FunctionInfo item in rootItem.Items)
- {
- string text;
- int imageIndex = 66;
- MethodInfo func = item.Function;
- if (func != null)
- {
- text = func.Name;
- // if this is an overridden function, show its parameters
- if (rootItem.Name == text)
- text = report.CodeHelper.GetMethodSignature(func, false);
- imageIndex = GetTypeImageIndex(func.ReturnType);
- }
- else
- {
- // it's a category
- text = Res.TryGet(item.Text);
- }
- TreeNode node = rootNode.Add(text);
- node.Tag = func;
- node.ImageIndex = imageIndex;
- node.SelectedImageIndex = imageIndex;
- if (item.Items.Count > 0)
- CreateFunctionsTree(report, item, node.Nodes);
- }
- }
- public static void CreateDataTree(Dictionary dictionary, TreeNodeCollection root, bool enabledOnly,
- bool relations, bool dataSources, bool columns)
- {
- if (dataSources)
- {
- // create connection node and enumerate connection tables
- foreach (DataConnectionBase connection in dictionary.Connections)
- {
- TreeNode connNode = root.Add(connection.Name);
- connNode.Tag = connection;
- connNode.Checked = true;
- connNode.ImageIndex = 53;
- connNode.SelectedImageIndex = connNode.ImageIndex;
- foreach (TableDataSource data in connection.Tables)
- {
- if (!enabledOnly || data.Enabled)
- AddDataSource(dictionary, data, connNode.Nodes, enabledOnly, relations, columns);
- }
- }
- // process regular tables
- foreach (DataSourceBase data in dictionary.DataSources)
- {
- if (!enabledOnly || data.Enabled)
- AddDataSource(dictionary, data, root, enabledOnly, relations, columns);
- }
- }
- else if (relations)
- {
- // display relations only (used by the Relation type editor)
- foreach (Relation relation in dictionary.Relations)
- {
- if (relation.ParentDataSource == null || relation.ChildDataSource == null)
- continue;
- TreeNode relNode = root.Add(relation.Alias + " (" +
- relation.ParentDataSource.Alias + "->" + relation.ChildDataSource.Alias + ")");
- relNode.Tag = relation;
- relNode.Checked = true;
- relNode.ImageIndex = 58;
- relNode.SelectedImageIndex = relNode.ImageIndex;
- }
- }
- }
- public static void CreateDataTree(Dictionary dictionary, DataConnectionBase connection, TreeNodeCollection root, bool tables, bool queries, bool procedures)
- {
- SortedList<string, TableDataSource> list = new SortedList<string, TableDataSource>();
- // sort the tables first
- foreach (TableDataSource table in connection.Tables)
- {
- bool isProcedure = table is ProcedureDataSource;
- bool isQuery = !isProcedure && !string.IsNullOrEmpty(table.SelectCommand);
- bool isTable = !isProcedure && !isQuery;
- if (isTable && !tables)
- continue;
- if (isQuery && !queries)
- continue;
- if (isProcedure && !procedures)
- continue;
- string tableName = table.Alias;
- // fix the node text if table is not a query: display the TableName instead of Alias
- if (isTable)
- tableName = table.TableName.Replace("\"", "");
- list.Add(tableName, table);
- }
- foreach (KeyValuePair<string, TableDataSource> keyValue in list)
- {
- var data = keyValue.Value;
- AddDataSource(dictionary, data, root, false, false, true);
- var node = root[root.Count - 1];
- node.Text = data is ProcedureDataSource proc ? proc.DisplayNameWithParams : keyValue.Key;
- // add dummy child node for the table which has no schema, to allow expand the node and get schema
- if (data.Columns.Count == 0)
- node.Nodes.Add("dummy");
- }
- }
- public static void CreateParametersTree(ParameterCollection parameters, TreeNodeCollection root)
- {
- foreach (Parameter p in parameters)
- {
- AddParameter(p, root);
- }
- }
- public static void CreateVariablesTree(ParameterCollection parameters, TreeNodeCollection root)
- {
- foreach (Parameter p in parameters)
- {
- AddVariable(p, root);
- }
- }
- public static void CreateTotalsTree(TotalCollection totals, TreeNodeCollection root)
- {
- foreach (Total s in totals)
- {
- TreeNode sumNode = root.Add(s.Name);
- sumNode.Tag = s;
- sumNode.ImageIndex = 132;
- sumNode.SelectedImageIndex = sumNode.ImageIndex;
- }
- }
- public static void CreateCustomItemsTree(Report report, TreeNodeCollection root)
- {
- TreeNode dialogNode = null;
- foreach (Base c in report.AllObjects)
- {
- if (c is DialogControl)
- {
- PropertyInfo bindableProp = (c as DialogControl).BindableProperty;
- if (bindableProp != null)
- {
- if (dialogNode == null)
- {
- dialogNode = root.Add(Res.Get("Designer,ToolWindow,Dictionary,DialogControls"));
- dialogNode.ImageIndex = 136;
- dialogNode.SelectedImageIndex = dialogNode.ImageIndex;
- }
- TreeNode node = dialogNode.Nodes.Add(c.Name + "." + bindableProp.Name);
- node.ImageIndex = DataTreeHelper.GetTypeImageIndex(bindableProp.PropertyType);
- node.SelectedImageIndex = node.ImageIndex;
- }
- }
- }
- }
- public static void CreateFunctionsTree(Report report, TreeNodeCollection root)
- {
- CreateFunctionsTree(report, RegisteredObjects.FindFunctionsRoot(), root);
- }
- public static void CreateCubeTree(Dictionary dictionary, TreeNodeCollection root, bool enabledOnly)
- {
- foreach (CubeSourceBase cube in dictionary.CubeSources)
- {
- if (!enabledOnly || cube.Enabled)
- AddCubeSource(dictionary, cube, root, enabledOnly);
- }
- }
- private static int GetNumberOfRelations(Dictionary dictionary, DataSourceBase parent, DataSourceBase child)
- {
- int result = 0;
- foreach (Relation rel in dictionary.Relations)
- {
- if (rel.ParentDataSource == parent && rel.ChildDataSource == child)
- result++;
- }
- return result;
- }
- public static int GetTypeImageIndex(Type dataType)
- {
- int index = 224;
- if (dataType == typeof(string) || dataType == typeof(char))
- index = 223;
- else if (dataType == typeof(float) || dataType == typeof(double))
- index = 225;
- else if (dataType == typeof(decimal))
- index = 226;
- else if (dataType == typeof(DateTime) || dataType == typeof(TimeSpan))
- index = 227;
- else if (dataType == typeof(bool))
- index = 228;
- else if (dataType == typeof(byte[]) || dataType == typeof(Image) || dataType == typeof(Bitmap))
- index = 229;
- return index;
- }
- }
- }
|