Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DebugEngine/ExecutionStackView.cs @ 4904

Last change on this file since 4904 was 4903, checked in by epitzer, 14 years ago

Many small improvements to DebugEngine (#47)

  • suppress logging during execution
  • add refresh button
  • optionally skip over execution stack operators
  • expand all tree views and scroll to top node
  • show operator on click on atomic operation
  • add build.cmd
File size: 4.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Linq;
5using System.Windows.Forms;
6using HeuristicLab.Collections;
7using HeuristicLab.Core;
8using HeuristicLab.MainForm;
9using HeuristicLab.MainForm.WindowsForms;
10
11namespace HeuristicLab.DebugEngine {
12
13  [View("Execution Stack View")]
14  [Content(typeof(ExecutionStack), IsDefaultView = true)]
15  public partial class ExecutionStackView : AsynchronousContentView {
16    public new ExecutionStack Content {
17      get { return (ExecutionStack)base.Content; }
18      set { base.Content = value; }
19    }
20
21    public ExecutionStackView() {
22      InitializeComponent();
23    }
24
25    protected override void DeregisterContentEvents() {
26      Content.CollectionReset -= Content_Changed;
27      Content.ItemsAdded -= Content_Changed;
28      Content.ItemsRemoved -= Content_Changed;
29      Content.ItemsMoved -= Content_Changed;
30      Content.ItemsReplaced -= Content_Changed;
31      base.DeregisterContentEvents();
32    }
33
34    protected override void RegisterContentEvents() {
35      base.RegisterContentEvents();
36      Content.CollectionReset += Content_Changed;
37      Content.ItemsAdded += Content_Changed;
38      Content.ItemsRemoved += Content_Changed;
39      Content.ItemsMoved += Content_Changed;
40      Content.ItemsReplaced += Content_Changed;
41    }
42
43    #region Event Handlers (Content)
44    void Content_Changed(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperation>> e) {
45      if (InvokeRequired)
46        Invoke(new EventHandler<CollectionItemsChangedEventArgs<IndexedItem<IOperation>>>(Content_Changed), sender, e);
47      else
48        UpdateExecutionStack();
49    }
50
51
52    private void UpdateExecutionStack() {
53      if (!suspended) {
54        treeView.BeginUpdate();
55        treeView.Nodes.Clear();
56        int totalNodes = AddStackOperations(treeView.Nodes, ((IEnumerable<IOperation>)Content).Reverse());
57        if (treeView.Nodes.Count > 0)
58          treeView.TopNode = treeView.Nodes[0];
59        treeView.ExpandAll();
60        treeView.EndUpdate();
61        groupBox.Text = string.Format("Execution Stack ({0})", totalNodes);
62      } else {
63        groupBox.Text = string.Format("Execution Stack ({0})", CountOperations(Content));
64      }
65    }
66
67    private int CountOperations(IEnumerable<IOperation> operations) {
68      int count = 0;
69      foreach (IOperation op in operations) {
70        count++;
71        OperationCollection ops = op as OperationCollection;
72        if (ops != null)
73          count += CountOperations(ops);
74      }
75      return count;
76    }
77
78    private int AddStackOperations(TreeNodeCollection nodes, IEnumerable<IOperation> operations) {
79      int count = 0;
80      foreach (IOperation op in operations) {
81        if (op is IAtomicOperation) {
82          IAtomicOperation atom = op as IAtomicOperation;
83          TreeNode node = nodes.Add(atom.Operator.Name);
84          node.Tag = atom;
85          node.ToolTipText = Utils.TypeName(atom.Operator);
86          node.ImageKey = "AtomicOperation";
87          if (atom.Operator.Breakpoint)
88            node.ForeColor = Color.Red;
89          count++;
90        } else if (op is OperationCollection) {
91          OperationCollection ops = op as OperationCollection;
92          TreeNode node = treeView.Nodes.Add(
93            string.Format("{0} Operation{1}", ops.Count, ops.Count == 1 ? string.Empty : "s"));
94          node.Tag = op;
95          node.ToolTipText = Utils.TypeName(ops);
96          node.ImageKey = "OperationCollection";
97          count += AddStackOperations(node.Nodes, ops);
98        }
99      }
100      return count;
101    }
102    #endregion
103
104    protected override void OnContentChanged() {
105      base.OnContentChanged();
106      if (Content == null) {
107        treeView.Nodes.Clear();
108      } else {
109        UpdateExecutionStack();
110      }
111    }
112
113
114    protected override void SetEnabledStateOfControls() {
115      base.SetEnabledStateOfControls();
116    }
117
118
119
120    #region Event Handlers (child controls)
121    private void treeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) {
122      if (e.Node != null) {
123        IAtomicOperation op = e.Node.Tag as IAtomicOperation;
124        if (op != null)
125          MainFormManager.MainForm.ShowContent(op.Operator);
126      }
127    }
128    #endregion
129
130    #region Suspension
131    private bool suspended = false;
132    public virtual void SuspendUpdate() {
133      suspended = true;
134      treeView.Nodes.Clear();
135    }
136
137    public virtual void ResumeUpdate() {
138      suspended = false;
139      UpdateExecutionStack();
140    }
141    #endregion
142  }
143}
Note: See TracBrowser for help on using the repository browser.