using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using HeuristicLab.Collections; using HeuristicLab.Core; using HeuristicLab.MainForm.WindowsForms; using HeuristicLab.MainForm; namespace HeuristicLab.DebugEngine { [View("Execution Stack View")] [Content(typeof(ExecutionStack), IsDefaultView = true)] public partial class ExecutionStackView : AsynchronousContentView { public new ExecutionStack Content { get { return (ExecutionStack)base.Content; } set { base.Content = value; } } public ExecutionStackView() { InitializeComponent(); } protected override void DeregisterContentEvents() { Content.CollectionReset -= Content_Changed; Content.ItemsAdded -= Content_Changed; Content.ItemsRemoved -= Content_Changed; Content.ItemsMoved -= Content_Changed; Content.ItemsReplaced -= Content_Changed; base.DeregisterContentEvents(); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.CollectionReset += Content_Changed; Content.ItemsAdded += Content_Changed; Content.ItemsRemoved += Content_Changed; Content.ItemsMoved += Content_Changed; Content.ItemsReplaced += Content_Changed; } #region Event Handlers (Content) void Content_Changed(object sender, CollectionItemsChangedEventArgs> e) { if (InvokeRequired) Invoke(new EventHandler>>(Content_Changed), sender, e); else UpdateExecutionStack(); } private void UpdateExecutionStack() { if (!suspended) { treeView.BeginUpdate(); treeView.Nodes.Clear(); int totalNodes = AddStackOperations(treeView.Nodes, ((IEnumerable)Content).Reverse()); if (treeView.Nodes.Count > 0) treeView.TopNode = treeView.Nodes[0]; treeView.EndUpdate(); groupBox.Text = string.Format("Execution Stack ({0})", totalNodes); } else { groupBox.Text = string.Format("Execution Stack ({0})", CountOperations(Content)); } } private int CountOperations(IEnumerable operations) { int count = 0; foreach (IOperation op in operations) { count++; OperationCollection ops = op as OperationCollection; if (ops != null) count += CountOperations(ops); } return count; } private int AddStackOperations(TreeNodeCollection nodes, IEnumerable operations) { int count = 0; foreach (IOperation op in operations) { if (op is IAtomicOperation) { IAtomicOperation atom = op as IAtomicOperation; TreeNode node = nodes.Add(atom.Operator.Name); node.Tag = atom; node.ToolTipText = Utils.TypeName(atom.Operator); node.ImageKey = "AtomicOperation"; if (atom.Operator.Breakpoint) node.ForeColor = Color.Red; count++; } else if (op is OperationCollection) { OperationCollection ops = op as OperationCollection; TreeNode node = treeView.Nodes.Add( string.Format("{0} Operation{1}", ops.Count, ops.Count == 1 ? string.Empty : "s")); node.Tag = op; node.ToolTipText = Utils.TypeName(ops); node.ImageKey = "OperationCollection"; count += AddStackOperations(node.Nodes, ops); } } return count; } #endregion protected override void OnContentChanged() { base.OnContentChanged(); if (Content == null) { treeView.Nodes.Clear(); } else { UpdateExecutionStack(); } } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); } #region Event Handlers (child controls) private void treeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) { if (e.Node != null) { IAtomicOperation op = e.Node.Tag as IAtomicOperation; if (op != null) MainFormManager.MainForm.ShowContent(op.Operator); } } #endregion #region Suspension private bool suspended = false; public virtual void SuspendUpdate() { suspended = true; treeView.Nodes.Clear(); } public virtual void ResumeUpdate() { suspended = false; UpdateExecutionStack(); } #endregion } }