コード例 #1
0
    private void BindObject(object obj, TreeNode root, bool isPriorityTree)
    {
        int index = 0;

        try
        {
            // DataRow source, bind column names
            var dr = obj as DataRow;
            if (dr != null)
            {
                // Create tree structure
                foreach (DataColumn col in dr.Table.Columns)
                {
                    // Stop on max nodes
                    if (index++ >= MacroStaticSettings.MaxMacroNodes)
                    {
                        AppendMore(root);
                        break;
                    }

                    // Add the column
                    object childObj = dr[col.ColumnName];
                    AppendChild(root, col.ColumnName, childObj, false, isPriorityTree);
                }
            }
            else
            {
                // DataRowView source, bind column names
                var drv = obj as DataRowView;
                if (drv != null)
                {
                    // Create tree structure
                    foreach (DataColumn col in drv.DataView.Table.Columns)
                    {
                        // Stop on max nodes
                        if (index++ >= MacroStaticSettings.MaxMacroNodes)
                        {
                            AppendMore(root);
                            break;
                        }

                        // Add the column
                        object childObj = drv[col.ColumnName];
                        AppendChild(root, col.ColumnName, childObj, false, isPriorityTree);
                    }
                }
                else
                {
                    // Hierarchical object source
                    var hc = obj as IHierarchicalObject;
                    if (hc != null)
                    {
                        BaseInfo info = obj as BaseInfo;

                        var props = new List <string>(hc.Properties);

                        var fields = MacroFieldContainer.GetFieldsForObject(hc);
                        if (fields != null)
                        {
                            props.AddRange(fields.Select(x => x.Name));
                        }
                        //props.Sort();

                        if ((info != null) && (info.TypeInfo.SensitiveColumns != null))
                        {
                            props = props.Except(info.TypeInfo.SensitiveColumns).ToList();
                        }

                        // Create tree structure
                        foreach (string col in props)
                        {
                            // Stop on max nodes
                            if (index++ >= MacroStaticSettings.MaxMacroNodes)
                            {
                                AppendMore(root);
                                break;
                            }

                            // Add the property
                            AddProperty(root, isPriorityTree, hc, col);
                        }
                    }
                    else
                    {
                        // Data container source
                        var dc = obj as IDataContainer;
                        if (dc != null)
                        {
                            // Create tree structure
                            foreach (string col in dc.ColumnNames)
                            {
                                // Stop on max nodes
                                if (index++ >= MacroStaticSettings.MaxMacroNodes)
                                {
                                    AppendMore(root);
                                    break;
                                }

                                // Add the column
                                object childObj;
                                dc.TryGetValue(col, out childObj);

                                AppendChild(root, col, childObj, false, isPriorityTree);
                            }
                        }
                    }
                }
            }

            // Enumerable objects
            if ((obj is IEnumerable) && !(obj is string))
            {
                IEnumerable collection = (IEnumerable)obj;
                IEnumerator enumerator = null;

                bool indexByName = false;

                INamedEnumerable namedCol = null;
                if (obj is INamedEnumerable)
                {
                    // Collection with name enumerator
                    namedCol = (INamedEnumerable)collection;
                    if (namedCol.ItemsHaveNames)
                    {
                        enumerator  = namedCol.GetNamedEnumerator <object>();
                        indexByName = true;
                    }
                }

                if (!indexByName)
                {
                    // Standard collection
                    enumerator = collection.GetEnumerator();
                }

                int i = 0;

                List <string> addedItems = new List <string>();
                addedItems.Add("trees");
                addedItems.Add("documents");

                while (enumerator.MoveNext())
                {
                    // Stop on max nodes
                    if (index++ >= MacroStaticSettings.MaxMacroNodes)
                    {
                        AppendMore(root);
                        break;
                    }

                    // Add the item
                    object item = MacroResolver.EncapsulateObject(enumerator.Current);
                    if (indexByName)
                    {
                        // Convert the name with dot to indexer
                        string name = namedCol.GetObjectName(item);
                        if (!ValidationHelper.IsIdentifier(name))
                        {
                            name = "[\"" + name + "\"]";
                        }

                        string nameToLower = name.ToLowerCSafe();
                        if (!addedItems.Contains(nameToLower))
                        {
                            addedItems.Add(nameToLower);
                            AppendChild(root, name, item, false, isPriorityTree);
                        }
                    }
                    else
                    {
                        // Indexed item
                        AppendChild(root, i.ToString(), item, true, isPriorityTree);
                    }

                    i++;
                }
            }
        }
        catch (Exception ex)
        {
            EventLogProvider.LogException("MacroTree", "EXPAND", ex);

            throw;
        }
    }