protected string GetText(Tree tree) { string s = TreeTextProvider.Text(tree); s = Utils.EscapeWhitespace(s, true); return(s); }
public TreeViewer(List <string> ruleNames, Tree tree) { SetRuleNames(ruleNames); if (tree != null) { SetTree(tree); } }
public void SetTree(Tree root) { if (root != null) { treeLayout = new TreeLayout <Tree>(new TreeLayoutAdaptor(root), new VariableExtentProvide(this), new DefaultConfiguration <Tree>(gapBetweenLevels, gapBetweenNodes)); // Let the UI display this new AST. UpdatePreferredSize(); } else { treeLayout = null; Invalidate(); } }
protected void GenerateBox(TextWriter writer, Tree parent) { // draw the box in the background var box = BoundsOfNode(parent); writer.Write(Rect("" + box.X, "" + box.Y, "" + box.Width, "" + box.Height, "fill:orange; stroke:rgb(0,0,0);", "rx=\"1\"")); // draw the text on top of the box (possibly multiple lines) string line = TreeTextProvider.Text(parent).Replace("<", "<").Replace(">", ">"); int fontBoxSize = 10; int x = (int)box.X + 2; int y = (int)box.Y + fontBoxSize - 1; string style = string.Format("font-family:sans-serif;font-size:{0}px;", fontBoxSize); writer.Write(Text("" + x, "" + y, style, line)); }
protected void GenerateEdges(TextWriter writer, Tree parent) { if (!Tree.IsLeaf(parent)) { var b1 = BoundsOfNode(parent); float x1 = b1.Center.X; float y1 = b1.Center.Y; foreach (Tree child in Tree.Children(parent)) { var childbounds = BoundsOfNode(child); float x2 = childbounds.Center.X; float y2 = childbounds.BottomLeft.Y; writer.WriteLine(Line("" + x1, "" + y1, "" + x2, "" + y2, "stroke:black; stroke-width:1px;")); GenerateEdges(writer, child); } } }
protected void PaintBox(Graphics g, Tree tree) { var box = BoundsOfNode(tree); // draw the box in the background bool ruleFailedAndMatchedNothing = false; if (tree is ParserRuleContext ctx) { ruleFailedAndMatchedNothing = ctx.exception != null && ctx.Stop != null && ctx.Stop.TokenIndex < ctx.Start.TokenIndex; } if (IsHighlighted(tree) || tree is IErrorNode || ruleFailedAndMatchedNothing) { var color = boxColor; if (IsHighlighted(tree)) { color = highlightedBoxColor; } if (tree is IErrorNode || ruleFailedAndMatchedNothing) { color = LIGHT_RED; } g.FillRectangle(color, box.Center.X, box.BottomLeft.Y, box.Width - 1, box.Height - 1); } //g.DrawRectangle(borderColor, box.X, box.Y, box.Width - 1, box.Height - 1); // draw the text on top of the box (possibly multiple lines) string s = TreeTextProvider.Text(tree); string[] lines = s.Split('\n'); float x = box.X + arcSize / 2 + nodeWidthPadding; float y = box.BottomLeft.Y + font.Ascent + font.Leading + 1 + nodeHeightPadding; for (int i = 0; i < lines.Length; i++) { Text(g, lines[i], new PointF(x, y)); y += font.LineHeight; } }
protected void PaintEdges(Graphics g, Tree parent) { if (!Tree.IsLeaf(parent)) { Pen stroke = new Pen(Colors.Black, 1.0f) { LineCap = PenLineCap.Round, LineJoin = PenLineJoin.Round }; var parentBounds = BoundsOfNode(parent); float x1 = parentBounds.Center.X; float y1 = parentBounds.TopLeft.Y; foreach (Tree child in Tree.Children(parent)) { var childBounds = BoundsOfNode(child); float x2 = childBounds.Center.X; float y2 = childBounds.TopLeft.Y; //if (UseCurvedEdges) //{ // CubicCurve2D c = new CubicCurve2D.Double(); // float ctrlx1 = x1; // float ctrly1 = (y1 + y2) / 2; // float ctrlx2 = x2; // float ctrly2 = y1; // c.setCurve(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2); // g.DrawArc(stroke,); //} //else //{ System.Console.WriteLine($"{x1},{y1},{x2},{y2}"); g.DrawLine(stroke, x1, y1, x2, y2); //} PaintEdges(g, child); } } }
///** Get an adaptor for root that indicates how to walk ANTLR trees. // * Override to change the adapter from the default of {@link TreeLayoutAdaptor} */ public ITreeForTreeLayout <Tree> GetTreeLayoutAdaptor(Tree root) => new TreeLayoutAdaptor(root);
protected int HighlightedNodeIndex(Tree node) => highlightedNodes == null ? -1 : highlightedNodes.IndexOf(node);
protected bool IsHighlighted(Tree node) => HighlightedNodeIndex(node) >= 0;
//// --------------------------------------------------- protected RectangleF BoundsOfNode(Tree node) => treeLayout.NodeBounds[node];