Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4893 was 4876, checked in by epitzer, 14 years ago

Complete overhaul of DebugEngine (#47)

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