// The viewport has changed -- create new nodes and links that should now be visible public void UpdateViewport(Rect oldview, Rect newview) { this.ViewportBounds = newview; Diagram diagram = this.Diagram; IDiagramModel model = diagram.Model; if (!this.OffscreenQueued) { // don't immediately remove nodes and links that have scrolled out of the viewport this.OffscreenQueued = true; diagram.Dispatcher.BeginInvoke((Action)RemoveOffscreen); } // maybe create Nodes foreach (Object data in model.NodesSource) { AddNodeForData(data, model); } ILinksModel lmodel = model as ILinksModel; if (lmodel != null) { // maybe create Links foreach (Object data in lmodel.LinksSource) { AddLinkForData(data, lmodel); } } // You might want to delete this statement, for efficiency: if (this.Status != null) { this.Status(); } }
// a replacement for TreeNetwork.AddNodesAndLinks using model data instead of Nodes or Links public VirtualizingTreeNetwork AddData(ILinksModel model) { var nodes = model.NodesSource as IEnumerable <NodeData>; var links = model.LinksSource as IEnumerable <LinkData>; var NodeDataMap = new Dictionary <NodeData, VirtualizingTreeVertex>(); var LinkDataMap = new Dictionary <LinkData, VirtualizingTreeEdge>(); foreach (NodeData d in nodes) { if (NodeDataMap.ContainsKey(d)) { continue; } // create and add VirtualizingTreeVertex var v = (VirtualizingTreeVertex)CreateVertex(); v.Data = d; NodeDataMap.Add(d, v); AddVertex(v); } foreach (LinkData d in links) { if (LinkDataMap.ContainsKey(d)) { continue; } // find connected node data var from = (NodeData)model.FindNodeByKey(d.From); var to = (NodeData)model.FindNodeByKey(d.To); if (from == null || to == null || from == to) { continue; // skip } // now find corresponding vertexes VirtualizingTreeVertex f; NodeDataMap.TryGetValue(from, out f); VirtualizingTreeVertex t; NodeDataMap.TryGetValue(to, out t); if (f == null || t == null) { continue; // skip } // create and add VirtualizingTreeEdge var e = (VirtualizingTreeEdge)CreateEdge(); e.Data = d; e.FromVertex = f; e.ToVertex = t; AddEdge(e); } return(this); }
// Override DeleteParts with the only change being a custom RemoveNode public override void DeleteParts(IEnumerable <Part> coll) { VerifyAccess(); Diagram diagram = this.Diagram; if (diagram == null || diagram.IsReadOnly) { return; } foreach (Part p in coll.ToList()) // work on copy of collection { if (!p.CanDelete()) { continue; // not removable? } IDiagramModel dmodel = p.Model; if (dmodel == null) { return; } Node n = p as Node; if (n != null) { RemoveNode(n); } else { Link l = p as Link; if (l != null) { ILinksModel lmodel = dmodel as ILinksModel; if (lmodel != null) { lmodel.RemoveLink(p.Data); } else { dmodel.RemoveLink(l.FromData, l.FromPortId, l.ToData, l.ToPortId); } } } } }