public Dictionary <string, TreeNode> CodeCollectionToForm(string find, TreeView treeview) { List <TreeNode> _expandNodes = new List <TreeNode>(); treeview.BeginUpdate(); List <CodeSnippet> items = new List <CodeSnippet>(); if (string.IsNullOrWhiteSpace(find)) { items = CodeLib.Instance.CodeSnippets.OrderBy(p => p.Order).OrderBy(p => Utils.SplitPath(p.GetPath(), '\\').Length).ToList(); } else { items = FindNodes(find).OrderBy(p => p.Order).OrderBy(p => Utils.SplitPath(p.GetPath(), '\\').Length).ToList(); } Dictionary <string, TreeNode> _foundNodes = new Dictionary <string, TreeNode>(); treeview.Nodes.Clear(); foreach (CodeSnippet snippet in items) { if (string.IsNullOrEmpty(snippet.Id)) { snippet.Id = Guid.NewGuid().ToString(); } if (snippet.Id == Constants.CLIPBOARDMONITOR) { continue; } TreeNodeCollection parentCollection = treeview.Nodes; string parentPath = Utils.ParentPath(snippet.GetPath(), '\\'); string name = Utils.PathName(snippet.GetPath(), '\\'); TreeNode parent = LocalUtils.GetNodeByParentPath(treeview.Nodes, parentPath); if (parent != null) { parentCollection = parent.Nodes; } int imageIndex = LocalUtils.GetImageIndex(snippet); TreeNode node = new TreeNode(name, imageIndex, imageIndex) { Name = snippet.Id }; _foundNodes.Add(snippet.Id, node); parentCollection.Add(node); } if (!string.IsNullOrWhiteSpace(find)) { treeview.ExpandAll(); } treeview.EndUpdate(); return(_foundNodes); }
// #TODO GetPath will be expensive !!!! public List <CodeSnippet> FindNodes(string find) { DictionaryList <CodeSnippet, string> _items = CodeLib.Instance.CodeSnippets.Where(p => LocalUtils.LastPart(p.GetPath()).ToLower().Contains(find.ToLower())).ToDictionaryList(p => p.Id); _items.RegisterLookup("PATH", p => p.GetPath()); DictionaryList <CodeSnippet, string> _paths = new DictionaryList <CodeSnippet, string>(p => p.GetPath()); foreach (CodeSnippet item in _items) { List <CodeSnippet> _parents = GetParents(item.GetPath()); foreach (CodeSnippet parent in _parents) { if (!_paths.ContainsKey(parent.GetPath()) && (_items.Lookup("PATH", parent.GetPath()).FirstOrDefault() == null)) { _paths.Add(parent); } } } _items.AddRange(_paths); return(_items.ToList()); }
public Dictionary <string, TreeNode> CodeCollectionToForm(string find) { _Find = find; List <TreeNode> _expandNodes = new List <TreeNode>(); TreeHelper.BeginUpdate(); List <CodeSnippet> items = new List <CodeSnippet>(); if (string.IsNullOrWhiteSpace(find)) { items = CodeLib.Instance.CodeSnippets.OrderBy(p => p.Order).OrderBy(p => Utils.SplitPath(p.GetPath(), '\\').Length).ToList(); } else { items = FindNodes(find).OrderBy(p => p.Order).OrderBy(p => Utils.SplitPath(p.GetPath(), '\\').Length).ToList(); } Dictionary <string, TreeNode> _foundNodes = new Dictionary <string, TreeNode>(); _treeViewLibrary.Nodes.Clear(); foreach (CodeSnippet snippet in items) { if (string.IsNullOrEmpty(snippet.Id)) { snippet.Id = Guid.NewGuid().ToString(); } TreeNodeCollection parentCollection = _treeViewLibrary.Nodes; string parentPath = Utils.ParentPath(snippet.GetPath(), '\\'); string name = Utils.PathName(snippet.GetPath(), '\\'); if (snippet.CodeType == CodeType.ReferenceLink) { var _refSnippet = CodeLib.Instance.CodeSnippets.Get(snippet.ReferenceLinkId); name = Utils.PathName(_refSnippet.GetPath(), '\\'); } TreeNode parent = LocalUtils.GetNodeByParentPath(_treeViewLibrary.Nodes, parentPath); if (parent != null) { parentCollection = parent.Nodes; } int imageIndex = LocalUtils.GetImageIndex(snippet); TreeNode node = new TreeNode(name, imageIndex, imageIndex) { Name = snippet.Id }; _foundNodes.Add(snippet.Id, node); parentCollection.Add(node); if (snippet.Id == Constants.TRASHCAN) { TrashcanNode = node; } if (snippet.Important) { _treeViewLibrary.SelectedNode = node; } if (snippet.Expanded) { _expandNodes.Add(node); } } foreach (TreeNode node in _expandNodes) { node.Expand(); } CodeLib.Instance.TreeNodes.Add(_treeViewLibrary); if (!string.IsNullOrWhiteSpace(find)) { _treeViewLibrary.ExpandAll(); } TreeHelper.EndUpdate(); return(_foundNodes); }