void CallCollectionChanged(NotifyDocumentTreeViewCollectionChangedEventArgs eventArgs) => CollectionChanged?.Invoke(this, eventArgs);
void DocumentService_CollectionChanged(object sender, NotifyDocumentCollectionChangedEventArgs e) { switch (e.Type) { case NotifyDocumentCollectionType.Add: IDsDocumentNode newNode; var addDocumentInfo = e.Data as AddDocumentInfo; int index; if (addDocumentInfo != null) { newNode = addDocumentInfo.DsDocumentNode; index = addDocumentInfo.Index; if (newNode.TreeNode == null) { TreeView.Create(newNode); } } else { newNode = CreateNode(null, e.Documents[0]); TreeView.Create(newNode); index = TreeView.Root.Children.Count; } if ((uint)index >= (uint)TreeView.Root.Children.Count) { index = TreeView.Root.Children.Count; } TreeView.Root.Children.Insert(index, newNode.TreeNode); CallCollectionChanged(NotifyDocumentTreeViewCollectionChangedEventArgs.CreateAdd(newNode)); break; case NotifyDocumentCollectionType.Remove: var dict = new Dictionary <IDsDocumentNode, int>(); var dict2 = new Dictionary <IDsDocument, IDsDocumentNode>(); int i = 0; foreach (var n in TopNodes) { dict[n] = i++; dict2[n.Document] = n; } var list = new List <Tuple <IDsDocumentNode, int> >(e.Documents.Select(a => { IDsDocumentNode node; bool b = dict2.TryGetValue(a, out node); Debug.Assert(b); int j = -1; b = b && dict.TryGetValue(node, out j); Debug.Assert(b); return(Tuple.Create(node, b ? j : -1)); })); list.Sort((a, b) => b.Item2.CompareTo(a.Item2)); var removed = new List <IDsDocumentNode>(); foreach (var t in list) { if (t.Item2 < 0) { continue; } Debug.Assert((uint)t.Item2 < (uint)TreeView.Root.Children.Count); Debug.Assert(TreeView.Root.Children[t.Item2].Data == t.Item1); TreeView.Root.Children.RemoveAt(t.Item2); removed.Add(t.Item1); } DisableMemoryMappedIO(list.Select(a => a.Item1).ToArray()); CallCollectionChanged(NotifyDocumentTreeViewCollectionChangedEventArgs.CreateRemove(removed.ToArray())); break; case NotifyDocumentCollectionType.Clear: var oldNodes = TreeView.Root.Children.Select(a => (IDsDocumentNode)a.Data).ToArray(); TreeView.Root.Children.Clear(); DisableMemoryMappedIO(oldNodes); CallCollectionChanged(NotifyDocumentTreeViewCollectionChangedEventArgs.CreateClear(oldNodes)); break; default: Debug.Fail(string.Format("Unknown event type: {0}", e.Type)); break; } }