private void RenderNodes(KeyCatalog catalog, NodeCollection nodes, int restoreLevel)
        {
            restoreLevel--;
            foreach (KeyCatalog sub in catalog.Subcatalogs.SortedValues)
            {
                KeyNode n = new KeyNode(sub.ToString())
                {
                    Catalog = sub
                };
                nodes.Add(n);

                if (restoreLevel < 0)
                {
                    if (SelectionNodeName == sub.Name)
                    {
                        ATree.SelectedNode = n;
                    }
                }

                if (!sub.IsLeaf)
                {
                    RenderNodes(sub, n.Nodes, restoreLevel);
                }
            }
        }
        protected override void GenerateTreeStruct(KeyCatalog root)
        {
            string cmd = "select uid,\"group\" from $dsaserver where uid in(@PrimaryKeys) order by \"group\"";

            StringBuilder primarykeys = new StringBuilder();

            primarykeys.AppendFormat("{0}", "-1"); //如果沒有資料也不會爆掉。
            foreach (string key in Source)
            {
                primarykeys.AppendFormat(",{0}", key);
            }

            cmd = cmd.Replace("@PrimaryKeys", primarykeys.ToString());

            DataTable result = Backend.Select(cmd);

            root["未分類"].Tag = int.MaxValue;

            foreach (DataRow row in result.Rows)
            {
                string id    = row["uid"].ToString();
                string group = row["group"].ToString();

                if (string.IsNullOrWhiteSpace(group))
                {
                    group = "未分類";
                }

                root[group].AddKey(id);
            }
        }
        public KeyCatalog this[string name]
        {
            get
            {
                if (!_catalogs.ContainsKey(name))
                {
                    KeyCatalog kc = null;

                    kc = Factory.Create(name);

                    _catalogs.Add(name, kc);
                }
                return(_catalogs[name]);
            }
        }
        protected override int KeyCatalogComparer(KeyCatalog x, KeyCatalog y)
        {
            int X, Y;

            if (!int.TryParse(x.Tag + "", out X))
            {
                X = int.MinValue;
            }

            if (!int.TryParse(y.Tag + "", out Y))
            {
                Y = int.MinValue;
            }

            return(X.CompareTo(Y));
        }
        /// <summary>
        /// 產生資料到畫面上,請透過 GenerateDataModel 產生資料結構。
        /// </summary>
        protected void RenderTreeView(bool reserveSelection)
        {
            if (reserveSelection)
            {
                ReserveTreeSelection();
            }

            KeyCatalog userroot = new KeyCatalog(RootCaption, KCFactory);
            Task       task     = Task.Factory.StartNew((x) =>
            {
                GenerateTreeStruct(x as KeyCatalog);
            }, userroot);

            task.ContinueWith(x =>
            {
                if (x.Exception != null)
                {
                    ErrorBox.Show(x.Exception.Message, x.Exception);
                }

                ATree.Nodes.Clear();

                KeyCatalog rkc = x.AsyncState as KeyCatalog;

                if (ShowRoot)
                {
                    KeyCatalog root = new KeyCatalog("", KCFactory);
                    root.Subcatalogs.Add(rkc);
                    RenderNodes(root, ATree.Nodes, RestoreLevel);
                }
                else
                {
                    RenderNodes(rkc, ATree.Nodes, RestoreLevel);
                }

                foreach (Node n in ATree.Nodes)
                {
                    n.Expand();
                }
            }, UISyncContext);
        }
 /// <summary>
 /// 排序 KeyCatalog,如果不改寫則使用 Name 屬性排序。
 /// </summary>
 /// <param name="x"></param>
 /// <param name="y"></param>
 /// <returns></returns>
 protected virtual int KeyCatalogComparer(KeyCatalog x, KeyCatalog y)
 {
     return(NameComparer.Compare(x.Name, y.Name));
 }
 /// <summary>
 /// 產生資料模型,將資料產生在 Root 屬性上。
 /// </summary>
 protected virtual void GenerateTreeStruct(KeyCatalog root)
 {
     throw new NotImplementedException("您應該實作此方法。");
 }
 /// <summary>
 /// 提供 KeyCatalog 標題的格式化方法。
 /// </summary>
 /// <param name="catalog"></param>
 /// <returns></returns>
 protected virtual string KeyCatalogTitleFormat(KeyCatalog catalog)
 {
     return(string.Format("{0}({1})", catalog.Name, catalog.TotalKeyCount));
 }
 public void Add(KeyCatalog catalog)
 {
     _catalogs.Add(catalog.Name, catalog);
 }