using System.Windows.Forms; namespace HeuristicLab.Problems.ProgramSynthesis.Push.Views { using System.Collections.Generic; using System.Linq; using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; public partial class PushProgramTreeView : TreeView { private const string PushProgramStringFormat = "List {0}@{1}"; private const string EmptyList = "Empty List"; public PushProgramTreeView() { InitializeComponent(); ShowNodeToolTips = true; } public void LoadExpressions(IEnumerable expressions) { Nodes.Clear(); AddExpressions(expressions); } public void AddExpressions(IEnumerable expressions) { var nodes = expressions.Select(ToTreeNode).ToArray(); Nodes.AddRange(nodes); } private TreeNode ToTreeNode(Expression expression) { var node = new TreeNode { Text = GetNodeText(expression), Tag = expression, }; if (expression.IsProgram) { var program = (PushProgram)expression; var subNodes = program.Expressions.Reverse().Select(ToTreeNode).ToArray(); node.Nodes.AddRange(subNodes); } else { var type = expression.GetType(); var attribute = ExpressionTable.TypeToAttributeTable[type]; node.ToolTipText = attribute.Description; } return node; } private string GetNodeText(Expression expression) { if (expression.IsProgram) { var program = (PushProgram)expression; if (program.IsEmpty) return EmptyList; return string.Format(PushProgramStringFormat, program.TotalInstructionCount, program.Depth); } return expression.StringRepresentation; } } }