Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DebugEngine/3.3/OperationContentView.cs @ 5187

Last change on this file since 5187 was 5146, checked in by epitzer, 14 years ago

(#47)

  • Show a different icon for breakpoints
  • Show an icon for the active operation
File size: 10.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Drawing;
24using System.Text;
25using System.Windows.Forms;
26using HeuristicLab.Common;
27using HeuristicLab.Common.Resources;
28using HeuristicLab.Core;
29using HeuristicLab.MainForm;
30using HeuristicLab.MainForm.WindowsForms;
31
32namespace HeuristicLab.DebugEngine {
33
34  [View("Operation Content View")]
35  [Content(typeof(OperationContent), IsDefaultView = true)]
36  public partial class OperationContentView : AsynchronousContentView {
37    public new OperationContent Content {
38      get { return (OperationContent)base.Content; }
39      set { base.Content = value; }
40    }
41
42    public OperationContentView() {
43      InitializeComponent();
44    }
45
46    protected override void OnContentChanged() {
47      base.OnContentChanged();
48      if (Content == null) {
49        nameTextBox.Text = "";
50        scopeTreeView.Nodes.Clear();
51        executionContextTreeView.Nodes.Clear();
52        iconBox.Image = null;
53      } else {
54        nameTextBox.Text = Content.Name;
55        UpdateScopeTree();
56        UpdateExecutionContext();
57        iconBox.Image = Content.Icon;
58      }
59    }
60
61    private object GetParameterValue(IParameter param, IExecutionContext context, out string actualName) {
62      param = (IParameter)param.Clone();
63      param.ExecutionContext = context;
64      object value = null;
65      try {
66        value = param.ActualValue;
67      } catch (Exception x) {
68        value = x.Message;
69      }
70      ILookupParameter lookupParam = param as ILookupParameter;
71      if (lookupParam != null)
72        actualName = lookupParam.ActualName;
73      else
74        actualName = null;
75      return value;
76    }
77
78    private void UpdateScopeTree() {
79      scopeTreeView.BeginUpdate();
80      scopeTreeView.Nodes.Clear();
81      scopeTreeView.ImageList.Images.Clear();
82      scopeTreeView.ImageList.Images.Add(VS2008ImageLibrary.Namespace);
83      scopeTreeView.ImageList.Images.Add(VS2008ImageLibrary.Field);
84      if (Content.IsContext) {
85        var scope = Content.ExecutionContext.Scope;
86        while (scope != null && scope.Parent != null)
87          scope = scope.Parent;
88        if (scope != null)
89          AddScope(scopeTreeView.Nodes, scope);
90      }
91      scopeTreeView.EndUpdate();
92    }
93
94    private void AddScope(TreeNodeCollection nodes, IScope scope) {
95      TreeNode node = nodes.Add(string.Format("{0} ({1}+{2})",
96        scope.Name, scope.Variables.Count, scope.SubScopes.Count));
97      node.Tag = scope;
98      node.ImageIndex = 0;
99      node.SelectedImageIndex = 0;
100      foreach (var var in scope.Variables) {
101        TreeNode varNode = node.Nodes.Add(string.Format("{0}={1}", var.Name, var.Value));
102        varNode.Tag = var.Value;
103        varNode.ToolTipText = string.Format("{0}{1}{1}{2}",
104          Utils.TypeName(var.Value), Environment.NewLine,
105          Utils.Wrap(var.Description ?? var.ItemDescription, 60));
106        varNode.ImageIndex = 1;
107        varNode.SelectedImageIndex = 1;
108      }
109      foreach (var subScope in scope.SubScopes)
110        AddScope(node.Nodes, subScope);
111      if (Content.IsAtomic && Content.AtomicOperation.Scope == scope) {
112        node.NodeFont = new Font(DefaultFont, FontStyle.Bold);
113        node.ForeColor = Color.White;
114        node.BackColor = Color.Crimson;
115        node.Expand();
116        scopeTreeView.TopNode = node;
117        node.ToolTipText = "Current scope of active operation";
118      }
119    }
120
121
122    private void UpdateExecutionContext() {
123      executionContextTreeView.BeginUpdate();
124      executionContextTreeView.Nodes.Clear();
125      executionContextTreeView.ImageList.Images.Clear();
126      executionContextTreeView.ImageList.Images.Add(VS2008ImageLibrary.Namespace);
127      if (Content.IsContext) {
128        AddExecutionContext(Content.ExecutionContext, executionContextTreeView.Nodes);
129      }
130      executionContextTreeView.ExpandAll();
131      if (executionContextTreeView.Nodes.Count > 0)
132        executionContextTreeView.TopNode = executionContextTreeView.Nodes[0];
133      executionContextTreeView.EndUpdate();
134    }
135
136    private void AddExecutionContext(IExecutionContext executionContext, TreeNodeCollection nodes) {
137      ExecutionContext context = executionContext as ExecutionContext;
138      StringBuilder name = new StringBuilder();
139      if (context != null && context.Operator != null)
140        name.Append(context.Operator.Name);
141      else
142        name.Append("<Context>");
143      name.Append("@").Append(executionContext.Scope.Name);
144      TreeNode node = nodes.Add(name.ToString());
145      node.Tag = executionContext;
146      node.ImageIndex = 0;
147      node.SelectedImageIndex = 0;
148      foreach (var param in executionContext.Parameters) {
149        string actualName = null;
150        object value = GetParameterValue(param, executionContext, out actualName);
151        string label = actualName != null && actualName != param.Name ?
152          string.Format("{0}({1})={2}", param.Name, actualName, value) :
153          string.Format("{0}={1}", param.Name, value);
154        TreeNode paramNode = node.Nodes.Add(label);
155        paramNode.Tag = param;
156        executionContextTreeView.ImageList.Images.Add(param.ItemImage ?? VS2008ImageLibrary.Nothing);
157        paramNode.ImageIndex = executionContextTreeView.ImageList.Images.Count - 1;
158        paramNode.SelectedImageIndex = paramNode.ImageIndex;
159        paramNode.ToolTipText = string.Format("{0}{1}{1}{2}",
160          Utils.TypeName(param), Environment.NewLine,
161          Utils.Wrap(param.Description ?? param.ItemDescription, 60));
162      }
163      if (executionContext.Parent != null)
164        AddExecutionContext(executionContext.Parent, node.Nodes);
165    }
166
167    #region Event Handlers (child controls)
168
169    private void executionContextTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) {
170      if (e.Node != null) {
171        IParameter param = e.Node.Tag as IParameter;
172        if (param != null)
173          MainFormManager.MainForm.ShowContent(param);
174      }
175    }
176
177    private void scopeTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) {
178      if (e.Node != null) {
179        IItem item = e.Node.Tag as IItem;
180        if (item != null)
181          MainFormManager.MainForm.ShowContent(item);
182      }
183    }
184
185    private TreeNode selectedScopeNode = null;
186    private void executionContextTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
187      scopeTreeView.BeginUpdate();
188      if (selectedScopeNode != null) {
189        if (Content.IsAtomic && Content.AtomicOperation.Scope == selectedScopeNode.Tag) {
190          selectedScopeNode.BackColor = Color.Crimson;
191          selectedScopeNode.ForeColor = Color.White;
192          selectedScopeNode.NodeFont = new Font(DefaultFont, FontStyle.Bold);
193        } else {
194          selectedScopeNode.BackColor = Color.White;
195          selectedScopeNode.ForeColor = Color.Black;
196          selectedScopeNode.NodeFont = DefaultFont;
197        }
198      }
199      if (e.Node != null) {
200        IExecutionContext context = e.Node.Tag as IExecutionContext;
201        if (context != null && context.Scope != null) {
202          TreeNode scopeNode = FindScopeNode(context.Scope, scopeTreeView.Nodes);
203          if (scopeNode != null) {
204            if (Content.IsAtomic && Content.AtomicOperation.Scope == scopeNode.Tag) {
205              scopeNode.BackColor = Color.DarkViolet;
206              scopeNode.ForeColor = Color.White;
207              scopeNode.NodeFont = new Font(DefaultFont, FontStyle.Bold);
208            } else {
209              scopeNode.BackColor = Color.Blue;
210              scopeNode.ForeColor = Color.White;
211              scopeNode.NodeFont = new Font(DefaultFont, FontStyle.Bold);
212            }
213            selectedScopeNode = scopeNode;
214            scopeTreeView.TopNode = scopeNode;
215          }
216        }
217      }
218      scopeTreeView.EndUpdate();
219    }
220
221    private TreeNode FindScopeNode(IScope scope, TreeNodeCollection nodes) {
222      foreach (TreeNode node in nodes) {
223        if (node.Tag == scope) {
224          return node;
225        } else {
226          TreeNode childNode = FindScopeNode(scope, node.Nodes);
227          if (childNode != null)
228            return childNode;
229        }
230      }
231      return null;
232    }
233
234    private void nameTextBox_DoubleClick(object sender, EventArgs e) {
235      if (Content != null && Content.IsAtomic && Content.AtomicOperation.Operator != null)
236        MainFormManager.MainForm.ShowContent(Content.AtomicOperation.Operator);
237    }
238
239    private void ShowValue_Click(object sender, EventArgs e) {
240      if (executionContextTreeView.SelectedNode == null)
241        return;
242      IParameter param = executionContextTreeView.SelectedNode.Tag as IParameter;
243      if (param != null) {
244        string actualName = null;
245        IExecutionContext context = executionContextTreeView.SelectedNode.Parent.Tag as IExecutionContext ?? Content.ExecutionContext;
246        MainFormManager.MainForm.ShowContent(GetParameterValue(param, context, out actualName) as IContent);
247      }
248    }
249
250    private void executionContextConextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
251      IParameter param = executionContextTreeView.SelectedNode.Tag as IParameter;
252      if (param != null) {
253        string actualName = null;
254        IExecutionContext context = executionContextTreeView.SelectedNode.Parent.Tag as IExecutionContext ?? Content.ExecutionContext;
255        showValueToolStripMenuItem.Enabled = GetParameterValue(param, context, out actualName) is IContent;
256      } else
257        e.Cancel = true;
258    }
259
260    private void executionContextTreeView_MouseDown(object sender, MouseEventArgs e) {
261      if (e.Button == System.Windows.Forms.MouseButtons.Right)
262        executionContextTreeView.SelectedNode = executionContextTreeView.GetNodeAt(e.Location);
263    }
264
265    #endregion
266
267
268  }
269}
Note: See TracBrowser for help on using the repository browser.