Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4871 was 4871, checked in by epitzer, 13 years ago

Refactoring and modularization of DebugEngine (#47)

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