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