private void AssignLevel(TreeNode node, MethodCallVisitor visitor) { ArrayList levels = (ArrayList)visitor.GetData(_LevelAssignData); ((ArrayList)levels[node.Level]).Add(node); }
private void FindMaxLevel(TreeNode node, MethodCallVisitor visitor) { visitor.SetData(_MaxLevelData, Math.Max(node.Level, (int)visitor.GetData(_MaxLevelData))); }
/// <summary> /// Build the levels of the tree. Each level contain all the nodes from that level. /// </summary> protected void BuildLevels() { int levels; MethodCallVisitor visitor; // Find the number of levels in the tree, i.e. tree depth. visitor = new MethodCallVisitor(new VisitOperation(this.FindMaxLevel)); visitor.SetData(_MaxLevelData, 0); WalkTree(_root, visitor); levels = (int)visitor.GetData(_MaxLevelData) + 1; _treeLevels = new ArrayList(); for(int i = 0; i < levels; i++) _treeLevels.Add(new ArrayList()); // Assign each node to its appropriate level. visitor = new MethodCallVisitor(new VisitOperation(this.AssignLevel)); visitor.SetData(_LevelAssignData, _treeLevels); WalkTree(_root, visitor); }
public bool Arrange(INode rootNode, TreeLayoutInfo info, LayoutProgress progress) { // Preserve the root position. RectangleF rcRoot = rootNode.Bounds; _tree = new Tree(); if (!_tree.Build(rootNode)) { return(false); } // Place the root at (0, 0) TreeNode root = _tree.Root; root.Center = new PointF(0, 0); // Set root's bisectors double deg = 0; switch (info.Direction) { case TreeLayoutDirection.TopToBottom: deg = 270; break; case TreeLayoutDirection.BottomToTop: deg = 90; break; case TreeLayoutDirection.LeftToRight: deg = 180; break; case TreeLayoutDirection.RightToLeft: deg = 0; break; } root.SetData(_LeftBisectorData, deg); root.SetData(_RightBisectorData, deg + 360); // Update progress int total = _tree.TreeLevels.Count; int current = 0; if (progress != null) { progress(current++, total); } // Iterate through all levels and arrange them for (int l = 0; l < _tree.TreeLevels.Count - 1; l++) { // Update progress if (progress != null) { progress(current++, total); } ArrangeLevel(l, info); } // Offset the whole tree structure float xOff = 0; float yOff = 0; MethodCallVisitor visitor; if (info.KeepRootPosition) { xOff = rcRoot.Left - root.Bounds.Left; yOff = rcRoot.Top - root.Bounds.Top; } else { // Calculate the bounding rect of the entire tree's visitor = new MethodCallVisitor(new VisitOperation(this.CalcBounds)); _tree.WalkTree(root, visitor); RectangleF rc = (RectangleF)visitor.GetData(_CalcBoundsData); xOff = -rc.Left + info.XGap; yOff = -rc.Top + info.YGap; } visitor = new MethodCallVisitor(new VisitOperation(this.Offset)); visitor.SetData(_XOffsetData, xOff); visitor.SetData(_YOffsetData, yOff); _tree.WalkTree(root, visitor); // Apply the stretch factor visitor = new MethodCallVisitor(new VisitOperation(this.Stretch)); visitor.SetData(_StretchFactor, info.StretchFactor); if (info.KeepRootPosition) { visitor.SetData(_StretchCenter, _tree.Root.Center); } else { visitor.SetData(_StretchCenter, new PointF(info.XGap, info.YGap)); } _tree.WalkTree(root, visitor); // Apply the arrangement visitor = new MethodCallVisitor(new VisitOperation(this.Apply)); _tree.WalkTree(_tree.Root, visitor); // Update progress if (progress != null) { progress(total, total); } return(true); }