Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4997 was 4993, checked in by epitzer, 14 years ago

Create own class for OperatorTrace, remove unnecessary event handlers, prevent flickering while stepping, permanently highlight execution context's scope (#47)

File size: 9.9 KB
Line 
1using System;
2using System.Drawing;
3using System.Text;
4using System.Windows.Forms;
5using HeuristicLab.Common;
6using HeuristicLab.Common.Resources;
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      parameterListView.SmallImageList.Images.Clear();
50      if (Content.IsAtomic) {
51        foreach (var param in Content.AtomicOperation.Operator.Parameters) {
52          ListViewItem item = parameterListView.Items.Add(
53            string.Format("{0} = {1}", param.Name, GetParameterValue(param, Content.ExecutionContext)));
54          item.Tag = param;
55          parameterListView.SmallImageList.Images.Add(param.ItemImage ?? VS2008ImageLibrary.Nothing);
56          item.ImageIndex = parameterListView.SmallImageList.Images.Count - 1;
57          item.ToolTipText = string.Format("{0}{1}{1}{2}",
58            Utils.TypeName(param), Environment.NewLine,
59            Utils.Wrap(param.Description ?? param.ItemDescription, 60));
60        }
61      }
62      if (parameterListView.Items.Count > 0) {
63        for (int i = 0; i < parameterListView.Columns.Count; i++)
64          parameterListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
65      }
66      parameterListView.EndUpdate();
67    }
68
69    private object GetParameterValue(IParameter param, IExecutionContext context) {
70      param = (IParameter)param.Clone();
71      param.ExecutionContext = context;
72      object value = null;
73      try {
74        value = param.ActualValue;
75      } catch (Exception x) {
76        value = x.Message;
77      }
78      return value;
79    }
80
81    private void UpdateScopeTree() {
82      scopeTreeView.BeginUpdate();
83      scopeTreeView.Nodes.Clear();
84      scopeTreeView.ImageList.Images.Clear();
85      scopeTreeView.ImageList.Images.Add(VS2008ImageLibrary.Namespace);
86      scopeTreeView.ImageList.Images.Add(VS2008ImageLibrary.Field);
87      if (Content.IsContext) {
88        var scope = Content.ExecutionContext.Scope;
89        while (scope != null && scope.Parent != null)
90          scope = scope.Parent;
91        if (scope != null)
92          AddScope(scopeTreeView.Nodes, scope);
93      }
94      scopeTreeView.EndUpdate();
95    }
96
97    private void AddScope(TreeNodeCollection nodes, IScope scope) {
98      TreeNode node = nodes.Add(string.Format("{0} ({1}+{2})",
99        scope.Name, scope.Variables.Count, scope.SubScopes.Count));
100      node.Tag = scope;
101      node.ImageIndex = 0;
102      node.SelectedImageIndex = 0;
103      foreach (var var in scope.Variables) {
104        TreeNode varNode = node.Nodes.Add(string.Format("{0}={1}", var.Name, var.Value));
105        varNode.Tag = var.Value;
106        varNode.ToolTipText = string.Format("{0}{1}{1}{2}",
107          Utils.TypeName(var.Value), Environment.NewLine,
108          Utils.Wrap(var.Description ?? var.ItemDescription, 60));
109        varNode.ImageIndex = 1;
110        varNode.SelectedImageIndex = 1;
111      }
112      foreach (var subScope in scope.SubScopes)
113        AddScope(node.Nodes, subScope);
114      if (Content.IsAtomic && Content.AtomicOperation.Scope == scope) {
115        node.NodeFont = new Font(DefaultFont, FontStyle.Bold);
116        node.ForeColor = Color.White;
117        node.BackColor = Color.Crimson;
118        node.Expand();
119        scopeTreeView.TopNode = node;
120        node.ToolTipText = "Current scope of active operation";
121      }
122    }
123
124
125    private void UpdateExecutionContext() {
126      executionContextTreeView.BeginUpdate();
127      executionContextTreeView.Nodes.Clear();
128      executionContextTreeView.ImageList.Images.Clear();
129      executionContextTreeView.ImageList.Images.Add(VS2008ImageLibrary.Namespace);
130      if (Content.IsContext) {
131        AddExecutionContext(Content.ExecutionContext, executionContextTreeView.Nodes);
132      }
133      executionContextTreeView.ExpandAll();
134      if (executionContextTreeView.Nodes.Count > 0)
135        executionContextTreeView.TopNode = executionContextTreeView.Nodes[0];
136      executionContextTreeView.EndUpdate();
137    }
138
139    private void AddExecutionContext(IExecutionContext executionContext, TreeNodeCollection nodes) {
140      ExecutionContext context = executionContext as ExecutionContext;
141      StringBuilder name = new StringBuilder();
142      if (context != null && context.Operator != null)
143        name.Append(context.Operator.Name);
144      else
145        name.Append("<Context>");
146      name.Append("@").Append(executionContext.Scope.Name);
147      TreeNode node = nodes.Add(name.ToString());
148      node.Tag = executionContext;
149      node.ImageIndex = 0;
150      node.SelectedImageIndex = 0;
151      foreach (var param in executionContext.Parameters) {
152        TreeNode paramNode = node.Nodes.Add(string.Format("{0}={1}",
153          param.Name, GetParameterValue(param, executionContext)));
154        paramNode.Tag = param;
155        executionContextTreeView.ImageList.Images.Add(param.ItemImage ?? VS2008ImageLibrary.Nothing);
156        paramNode.ImageIndex = executionContextTreeView.ImageList.Images.Count - 1;
157        paramNode.SelectedImageIndex = paramNode.ImageIndex;
158        paramNode.ToolTipText = string.Format("{0}{1}{1}{2}",
159          Utils.TypeName(param), Environment.NewLine,
160          Utils.Wrap(param.Description ?? param.ItemDescription, 60));
161      }
162      if (executionContext.Parent != null)
163        AddExecutionContext(executionContext.Parent, node.Nodes);
164    }
165
166    #region Event Handlers (child controls)
167
168    private void parameterListView_ItemActivate(object sender, EventArgs e) {
169      if (parameterListView.SelectedItems.Count > 0) {
170        IParameter param = parameterListView.SelectedItems[0].Tag as IParameter;
171        if (param != null) {
172          MainFormManager.MainForm.ShowContent((IContent)GetParameterValue(param, Content.ExecutionContext));
173        }
174      }
175    }
176
177    private void executionContextTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) {
178      if (e.Node != null) {
179        IParameter param = e.Node.Tag as IParameter;
180        if (param != null)
181          MainFormManager.MainForm.ShowContent(param);
182      }
183    }
184
185    private void scopeTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) {
186      if (e.Node != null) {
187        IItem item = e.Node.Tag as IItem;
188        if (item != null)
189          MainFormManager.MainForm.ShowContent(item);
190      }
191    }
192
193    private TreeNode selectedScopeNode = null;
194    private void executionContextTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
195      if (selectedScopeNode != null) {
196        if (Content.IsAtomic && Content.AtomicOperation.Scope == selectedScopeNode.Tag) {
197          selectedScopeNode.BackColor = Color.Crimson;
198          selectedScopeNode.ForeColor = Color.White;
199          selectedScopeNode.NodeFont = new Font(DefaultFont, FontStyle.Bold);
200        } else {
201          selectedScopeNode.BackColor = Color.White;
202          selectedScopeNode.ForeColor = Color.Black;
203          selectedScopeNode.NodeFont = DefaultFont;
204        }
205      }
206      if (e.Node != null) {
207        IExecutionContext context = e.Node.Tag as IExecutionContext;
208        if (context != null && context.Scope != null) {
209          TreeNode scopeNode = FindScopeNode(context.Scope, scopeTreeView.Nodes);
210          if (scopeNode != null) {
211            if (Content.IsAtomic && Content.AtomicOperation.Scope == scopeNode.Tag) {
212              scopeNode.BackColor = Color.DarkViolet;
213              scopeNode.ForeColor = Color.White;
214              scopeNode.NodeFont = new Font(DefaultFont, FontStyle.Bold);
215            } else {
216              scopeNode.BackColor = Color.Blue;
217              scopeNode.ForeColor = Color.White;
218              scopeNode.NodeFont = new Font(DefaultFont, FontStyle.Bold);
219            }
220            selectedScopeNode = scopeNode;
221            scopeTreeView.TopNode = scopeNode;
222          }
223        }
224      }
225    }
226
227    private TreeNode FindScopeNode(IScope scope, TreeNodeCollection nodes) {
228      foreach (TreeNode node in nodes) {
229        if (node.Tag == scope) {
230          return node;
231        } else {
232          TreeNode childNode = FindScopeNode(scope, node.Nodes);
233          if (childNode != null)
234            return childNode;
235        }
236      }
237      return null;
238    }
239
240    private void nameTextBox_DoubleClick(object sender, EventArgs e) {
241      if (Content != null && Content.IsAtomic && Content.AtomicOperation.Operator != null)
242        MainFormManager.MainForm.ShowContent(Content.AtomicOperation.Operator);
243    }
244
245    #endregion
246  }
247}
Note: See TracBrowser for help on using the repository browser.