Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DebugEngine/OperationContentView.cs @ 4903

Last change on this file since 4903 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: 7.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Text;
5using System.Windows.Forms;
6using HeuristicLab.Common;
7using HeuristicLab.Core;
8using HeuristicLab.MainForm;
9using HeuristicLab.MainForm.WindowsForms;
10
11namespace HeuristicLab.DebugEngine {
12
13  [View("Operation Content View")]
14  [Content(typeof(OperationContent), IsDefaultView = true)]
15  public sealed partial class OperationContentView : AsynchronousContentView {
16    public new OperationContent Content {
17      get { return (OperationContent)base.Content; }
18      set { base.Content = value; }
19    }
20
21    public OperationContentView() {
22      InitializeComponent();
23    }
24
25    protected override void OnContentChanged() {
26      base.OnContentChanged();
27      if (Content == null) {
28        nameTextBox.Text = "";
29        contextLabel.ForeColor = SystemColors.ControlDark;
30        atomicLabel.ForeColor = SystemColors.ControlDark;
31        collectionLabel.ForeColor = SystemColors.ControlDark;
32        parameterListView.Items.Clear();
33        scopeTreeView.Nodes.Clear();
34        executionContextTreeView.Nodes.Clear();
35      } else {
36        contextLabel.ForeColor = Content.IsContext ? Color.Black : SystemColors.ControlDark;
37        atomicLabel.ForeColor = Content.IsAtomic ? Color.Black : SystemColors.ControlDark;
38        collectionLabel.ForeColor = Content.IsCollection ? Color.Black : SystemColors.ControlDark;
39        nameTextBox.Text = Content.Name;
40        UpdateParameters();
41        UpdateScopeTree();
42        UpdateExecutionContext();
43      }
44    }
45
46    private void UpdateParameters() {
47      parameterListView.BeginUpdate();
48      parameterListView.Items.Clear();
49      if (Content.IsAtomic) {
50        foreach (var param in Content.AtomicOperation.Operator.Parameters) {
51          ListViewItem item = parameterListView.Items.Add(
52            string.Format("{0} = {1}", param.Name, GetParameterValue(param, Content.ExecutionContext)));
53          item.Tag = param;
54        }
55      }
56      parameterListView.EndUpdate();
57    }
58
59    private object GetParameterValue(IParameter param, IExecutionContext context) {
60      param = (IParameter)param.Clone();
61      param.ExecutionContext = context;
62      object value = null;
63      try {
64        value = param.ActualValue;
65      } catch (Exception x) {
66        value = x.Message;
67      }
68      return value;
69    }
70
71    private void UpdateScopeTree() {
72      scopeTreeView.BeginUpdate();
73      scopeTreeView.Nodes.Clear();
74      if (Content.IsContext) {
75        var scope = Content.ExecutionContext.Scope;
76        while (scope != null && scope.Parent != null)
77          scope = scope.Parent;
78        if (scope != null)
79          AddScope(scopeTreeView.Nodes, scope);
80        if (scopeTreeView.Nodes.Count > 0)
81          scopeTreeView.TopNode = scopeTreeView.Nodes[0];
82      }
83      scopeTreeView.ExpandAll();
84      if (scopeTreeView.Nodes.Count > 0)
85        scopeTreeView.TopNode = scopeTreeView.Nodes[0];
86      scopeTreeView.EndUpdate();
87    }
88
89    private void AddScope(TreeNodeCollection nodes, IScope scope) {
90      TreeNode node = nodes.Add(string.Format("{0} ({1}+{2})", scope.Name, scope.Variables.Count, scope.SubScopes.Count));
91      node.Tag = scope;
92      if (Content.IsAtomic && Content.AtomicOperation.Scope == scope) {
93        node.ForeColor = Color.Red;
94        node.BackColor = Color.LightGray;
95      }
96      foreach (var var in scope.Variables) {
97        TreeNode varNode = node.Nodes.Add(string.Format("{0}={1}", var.Name, var.Value));
98        varNode.Tag = var.Value;
99        varNode.ToolTipText = Utils.TypeName(var.Value);
100      }
101      foreach (var subScope in scope.SubScopes) {
102        AddScope(node.Nodes, subScope);
103      }
104    }
105
106    private void UpdateExecutionContext() {
107      executionContextTreeView.BeginUpdate();
108      executionContextTreeView.Nodes.Clear();
109      if (Content.IsContext) {
110        AddExecutionContext(Content.ExecutionContext, executionContextTreeView.Nodes);
111      }
112      executionContextTreeView.ExpandAll();
113      if (executionContextTreeView.Nodes.Count > 0)
114        executionContextTreeView.TopNode = executionContextTreeView.Nodes[0];
115      executionContextTreeView.EndUpdate();
116    }
117
118    private void AddExecutionContext(IExecutionContext executionContext, TreeNodeCollection nodes) {
119      ExecutionContext context = executionContext as ExecutionContext;
120      StringBuilder name = new StringBuilder();
121      if (context != null && context.Operator != null)
122        name.Append("Op ").Append(context.Operator.Name);
123      else
124        name.Append("<Context>");
125      name.Append("@").Append(executionContext.Scope.Name);
126      TreeNode node = nodes.Add(name.ToString());
127      node.Tag = executionContext;
128      foreach (var param in executionContext.Parameters) {
129        TreeNode paramNode = node.Nodes.Add(string.Format("Param {0}={1}", param.Name, GetParameterValue(param, executionContext)));
130        paramNode.Tag = param;
131      }
132      if (executionContext.Parent != null)
133        AddExecutionContext(executionContext.Parent, node.Nodes);
134    }
135
136    #region Event Handlers (child controls)
137
138    private void parameterListView_ItemActivate(object sender, EventArgs e) {
139      if (parameterListView.SelectedItems.Count > 0) {
140        IParameter param = parameterListView.SelectedItems[0].Tag as IParameter;
141        if (param != null) {
142          MainFormManager.MainForm.ShowContent((IContent)GetParameterValue(param, Content.ExecutionContext));
143        }
144      }
145    }
146
147    private void executionContextTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) {
148      if (e.Node != null) {
149        IParameter param = e.Node.Tag as IParameter;
150        if (param != null)
151          MainFormManager.MainForm.ShowContent(param);
152      }
153    }
154
155    private void executionContextTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
156      if (e.Node != null) {
157        IExecutionContext context = e.Node.Tag as IExecutionContext;
158        if (context != null && context.Scope != null) {
159          TreeNode scopeNode = FindScopeNode(context.Scope, scopeTreeView.Nodes);
160          if (scopeNode != null) {
161            if (timer.Enabled) {
162              timer_Tick(this, EventArgs.Empty);
163            }
164            timer.Tag = new KeyValuePair<TreeNode, Color>(scopeNode, scopeNode.BackColor);
165            scopeNode.BackColor = Color.Blue;
166            scopeTreeView.TopNode = scopeNode;
167            timer.Start();
168          }
169        }
170      }
171    }
172
173    private void ResetNode(TreeNode node, Color color) {
174      if (InvokeRequired)
175        Invoke(new Action<TreeNode, Color>(ResetNode), node, color);
176      else
177        node.BackColor = color;
178    }
179
180    private TreeNode FindScopeNode(IScope scope, TreeNodeCollection nodes) {
181      foreach (TreeNode node in nodes) {
182        if (node.Tag == scope) {
183          return node;
184        } else {
185          TreeNode childNode = FindScopeNode(scope, node.Nodes);
186          if (childNode != null)
187            return childNode;
188        }
189      }
190      return null;
191    }
192
193    private void timer_Tick(object sender, EventArgs e) {
194      KeyValuePair<TreeNode, Color> kvp = (KeyValuePair<TreeNode, Color>)timer.Tag;
195      ResetNode(kvp.Key, kvp.Value);
196      timer.Stop();
197    }
198
199    #endregion
200
201    private void nameTextBox_DoubleClick(object sender, EventArgs e) {
202      if (Content != null && Content.IsAtomic && Content.AtomicOperation.Operator != null)
203        MainFormManager.MainForm.ShowContent(Content.AtomicOperation.Operator);
204    }
205
206  }
207}
Note: See TracBrowser for help on using the repository browser.