Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4935 was 4931, checked in by epitzer, 14 years ago

Add tool tip for current scope (#47)

File size: 9.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Text;
5using System.Windows.Forms;
6using HeuristicLab.Common;
7using HeuristicLab.Common.Resources;
8using HeuristicLab.Core;
9using HeuristicLab.MainForm;
10using HeuristicLab.MainForm.WindowsForms;
11
12namespace HeuristicLab.DebugEngine {
13
14  [View("Operation Content View")]
15  [Content(typeof(OperationContent), IsDefaultView = true)]
16  public sealed partial class OperationContentView : AsynchronousContentView {
17    public new OperationContent Content {
18      get { return (OperationContent)base.Content; }
19      set { base.Content = value; }
20    }
21
22    public OperationContentView() {
23      InitializeComponent();
24    }
25
26    protected override void OnContentChanged() {
27      base.OnContentChanged();
28      if (Content == null) {
29        nameTextBox.Text = "";
30        contextLabel.ForeColor = SystemColors.ControlDark;
31        atomicLabel.ForeColor = SystemColors.ControlDark;
32        collectionLabel.ForeColor = SystemColors.ControlDark;
33        parameterListView.Items.Clear();
34        scopeTreeView.Nodes.Clear();
35        executionContextTreeView.Nodes.Clear();
36      } else {
37        contextLabel.ForeColor = Content.IsContext ? Color.Black : SystemColors.ControlDark;
38        atomicLabel.ForeColor = Content.IsAtomic ? Color.Black : SystemColors.ControlDark;
39        collectionLabel.ForeColor = Content.IsCollection ? Color.Black : SystemColors.ControlDark;
40        nameTextBox.Text = Content.Name;
41        UpdateParameters();
42        UpdateScopeTree();
43        UpdateExecutionContext();
44      }
45    }
46
47    private void UpdateParameters() {
48      parameterListView.BeginUpdate();
49      parameterListView.Items.Clear();
50      parameterListView.SmallImageList.Images.Clear();
51      if (Content.IsAtomic) {
52        foreach (var param in Content.AtomicOperation.Operator.Parameters) {
53          ListViewItem item = parameterListView.Items.Add(
54            string.Format("{0} = {1}", param.Name, GetParameterValue(param, Content.ExecutionContext)));
55          item.Tag = param;
56          parameterListView.SmallImageList.Images.Add(param.ItemImage ?? VS2008ImageLibrary.Nothing);
57          item.ImageIndex = parameterListView.SmallImageList.Images.Count - 1;
58          item.ToolTipText = string.Format("{0}{1}{1}{2}",
59            Utils.TypeName(param), Environment.NewLine,
60            Utils.Wrap(param.Description ?? param.ItemDescription, 60));
61        }
62      }
63      if (parameterListView.Items.Count > 0) {
64        for (int i = 0; i < parameterListView.Columns.Count; i++)
65          parameterListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
66      }
67      parameterListView.EndUpdate();
68    }
69
70    private object GetParameterValue(IParameter param, IExecutionContext context) {
71      param = (IParameter)param.Clone();
72      param.ExecutionContext = context;
73      object value = null;
74      try {
75        value = param.ActualValue;
76      } catch (Exception x) {
77        value = x.Message;
78      }
79      return value;
80    }
81
82    private void UpdateScopeTree() {
83      scopeTreeView.BeginUpdate();
84      scopeTreeView.Nodes.Clear();
85      scopeTreeView.ImageList.Images.Clear();
86      scopeTreeView.ImageList.Images.Add(VS2008ImageLibrary.Namespace);
87      scopeTreeView.ImageList.Images.Add(VS2008ImageLibrary.Field);
88      if (Content.IsContext) {
89        var scope = Content.ExecutionContext.Scope;
90        while (scope != null && scope.Parent != null)
91          scope = scope.Parent;
92        if (scope != null) {
93          AddScope(scopeTreeView.Nodes, scope);
94
95        }
96      }
97      scopeTreeView.EndUpdate();
98    }
99
100    private void AddScope(TreeNodeCollection nodes, IScope scope) {
101      TreeNode node = nodes.Add(string.Format("{0} ({1}+{2})",
102        scope.Name, scope.Variables.Count, scope.SubScopes.Count));
103      node.Tag = scope;
104      node.ImageIndex = 0;
105      node.SelectedImageIndex = 0;
106      foreach (var var in scope.Variables) {
107        TreeNode varNode = node.Nodes.Add(string.Format("{0}={1}", var.Name, var.Value));
108        varNode.Tag = var.Value;
109        varNode.ToolTipText = string.Format("{0}{1}{1}{2}",
110          Utils.TypeName(var.Value), Environment.NewLine,
111          Utils.Wrap(var.Description ?? var.ItemDescription, 60));
112        varNode.ImageIndex = 1;
113        varNode.SelectedImageIndex = 1;
114      }
115      foreach (var subScope in scope.SubScopes)
116        AddScope(node.Nodes, subScope);
117      if (Content.IsAtomic && Content.AtomicOperation.Scope == scope) {
118        node.NodeFont = new Font(DefaultFont, FontStyle.Bold);
119        node.ForeColor = Color.White;
120        node.BackColor = Color.Crimson;
121        node.Expand();
122        scopeTreeView.TopNode = node;
123        node.ToolTipText = "Current scope of active operation";
124      }
125    }
126
127
128    private void UpdateExecutionContext() {
129      executionContextTreeView.BeginUpdate();
130      executionContextTreeView.Nodes.Clear();
131      executionContextTreeView.ImageList.Images.Clear();
132      executionContextTreeView.ImageList.Images.Add(VS2008ImageLibrary.Namespace);
133      if (Content.IsContext) {
134        AddExecutionContext(Content.ExecutionContext, executionContextTreeView.Nodes);
135      }
136      executionContextTreeView.ExpandAll();
137      if (executionContextTreeView.Nodes.Count > 0)
138        executionContextTreeView.TopNode = executionContextTreeView.Nodes[0];
139      executionContextTreeView.EndUpdate();
140    }
141
142    private void AddExecutionContext(IExecutionContext executionContext, TreeNodeCollection nodes) {
143      ExecutionContext context = executionContext as ExecutionContext;
144      StringBuilder name = new StringBuilder();
145      if (context != null && context.Operator != null)
146        name.Append(context.Operator.Name);
147      else
148        name.Append("<Context>");
149      name.Append("@").Append(executionContext.Scope.Name);
150      TreeNode node = nodes.Add(name.ToString());
151      node.Tag = executionContext;
152      node.ImageIndex = 0;
153      node.SelectedImageIndex = 0;
154      foreach (var param in executionContext.Parameters) {
155        TreeNode paramNode = node.Nodes.Add(string.Format("{0}={1}",
156          param.Name, GetParameterValue(param, executionContext)));
157        paramNode.Tag = param;
158        executionContextTreeView.ImageList.Images.Add(param.ItemImage ?? VS2008ImageLibrary.Nothing);
159        paramNode.ImageIndex = executionContextTreeView.ImageList.Images.Count - 1;
160        paramNode.SelectedImageIndex = paramNode.ImageIndex;
161        paramNode.ToolTipText = string.Format("{0}{1}{1}{2}",
162          Utils.TypeName(param), Environment.NewLine,
163          Utils.Wrap(param.Description ?? param.ItemDescription, 60));
164      }
165      if (executionContext.Parent != null)
166        AddExecutionContext(executionContext.Parent, node.Nodes);
167    }
168
169    #region Event Handlers (child controls)
170
171    private void parameterListView_ItemActivate(object sender, EventArgs e) {
172      if (parameterListView.SelectedItems.Count > 0) {
173        IParameter param = parameterListView.SelectedItems[0].Tag as IParameter;
174        if (param != null) {
175          MainFormManager.MainForm.ShowContent((IContent)GetParameterValue(param, Content.ExecutionContext));
176        }
177      }
178    }
179
180    private void executionContextTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) {
181      if (e.Node != null) {
182        IParameter param = e.Node.Tag as IParameter;
183        if (param != null)
184          MainFormManager.MainForm.ShowContent(param);
185      }
186    }
187
188    private void scopeTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) {
189      if (e.Node != null) {
190        IItem item = e.Node.Tag as IItem;
191        if (item != null)
192          MainFormManager.MainForm.ShowContent(item);
193      }
194    }
195
196    private void executionContextTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
197      if (e.Node != null) {
198        IExecutionContext context = e.Node.Tag as IExecutionContext;
199        if (context != null && context.Scope != null) {
200          TreeNode scopeNode = FindScopeNode(context.Scope, scopeTreeView.Nodes);
201          if (scopeNode != null) {
202            if (timer.Enabled) {
203              timer_Tick(this, EventArgs.Empty);
204            }
205            timer.Tag = new KeyValuePair<TreeNode, TreeNode>(scopeNode, new TreeNode() {
206              BackColor = scopeNode.BackColor,
207              ForeColor = scopeNode.ForeColor,
208              NodeFont = scopeNode.NodeFont,
209            });
210            scopeNode.BackColor = Color.Blue;
211            scopeNode.ForeColor = Color.White;
212            scopeNode.NodeFont = new Font(DefaultFont, FontStyle.Bold);
213            scopeTreeView.TopNode = scopeNode;
214            timer.Start();
215          }
216        }
217      }
218    }
219
220    private void ResetNode(TreeNode node, TreeNode template) {
221      if (InvokeRequired)
222        Invoke(new Action<TreeNode, TreeNode>(ResetNode), node, template);
223      else {
224        node.ForeColor = template.ForeColor;
225        node.BackColor = template.BackColor;
226        node.NodeFont = template.NodeFont;
227      }
228    }
229
230    private TreeNode FindScopeNode(IScope scope, TreeNodeCollection nodes) {
231      foreach (TreeNode node in nodes) {
232        if (node.Tag == scope) {
233          return node;
234        } else {
235          TreeNode childNode = FindScopeNode(scope, node.Nodes);
236          if (childNode != null)
237            return childNode;
238        }
239      }
240      return null;
241    }
242
243    private void timer_Tick(object sender, EventArgs e) {
244      KeyValuePair<TreeNode, TreeNode> kvp = (KeyValuePair<TreeNode, TreeNode>)timer.Tag;
245      ResetNode(kvp.Key, kvp.Value);
246      timer.Stop();
247    }
248
249    private void nameTextBox_DoubleClick(object sender, EventArgs e) {
250      if (Content != null && Content.IsAtomic && Content.AtomicOperation.Operator != null)
251        MainFormManager.MainForm.ShowContent(Content.AtomicOperation.Operator);
252    }
253
254    #endregion
255  }
256}
Note: See TracBrowser for help on using the repository browser.