Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5928 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 10.6 KB
RevLine 
[4998]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4998]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;
[4876]23using System.Drawing;
24using System.Text;
25using System.Windows.Forms;
[4903]26using HeuristicLab.Common;
[4909]27using HeuristicLab.Common.Resources;
[4903]28using HeuristicLab.Core;
29using HeuristicLab.MainForm;
[4876]30using HeuristicLab.MainForm.WindowsForms;
31
32namespace HeuristicLab.DebugEngine {
33
34  [View("Operation Content View")]
35  [Content(typeof(OperationContent), IsDefaultView = true)]
[5003]36  public partial class OperationContentView : AsynchronousContentView {
[4876]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();
[5146]52        iconBox.Image = null;
[4876]53      } else {
54        nameTextBox.Text = Content.Name;
55        UpdateScopeTree();
56        UpdateExecutionContext();
[5146]57        iconBox.Image = Content.Icon;
[4876]58      }
59    }
60
[5116]61    private object GetParameterValue(IParameter param, IExecutionContext context, out string actualName) {
[4903]62      param = (IParameter)param.Clone();
[4876]63      param.ExecutionContext = context;
64      object value = null;
65      try {
[4903]66        value = param.ActualValue;
67      } catch (Exception x) {
68        value = x.Message;
[4876]69      }
[5116]70      ILookupParameter lookupParam = param as ILookupParameter;
71      if (lookupParam != null)
72        actualName = lookupParam.ActualName;
73      else
74        actualName = null;
[4876]75      return value;
76    }
77
78    private void UpdateScopeTree() {
79      scopeTreeView.BeginUpdate();
80      scopeTreeView.Nodes.Clear();
[4909]81      scopeTreeView.ImageList.Images.Clear();
[5287]82      scopeTreeView.ImageList.Images.Add(VSImageLibrary.Namespace);
83      scopeTreeView.ImageList.Images.Add(VSImageLibrary.Field);
[4876]84      if (Content.IsContext) {
85        var scope = Content.ExecutionContext.Scope;
86        while (scope != null && scope.Parent != null)
87          scope = scope.Parent;
[4993]88        if (scope != null)
[4903]89          AddScope(scopeTreeView.Nodes, scope);
[4876]90      }
91      scopeTreeView.EndUpdate();
92    }
93
94    private void AddScope(TreeNodeCollection nodes, IScope scope) {
[4909]95      TreeNode node = nodes.Add(string.Format("{0} ({1}+{2})",
96        scope.Name, scope.Variables.Count, scope.SubScopes.Count));
[4876]97      node.Tag = scope;
[4909]98      node.ImageIndex = 0;
99      node.SelectedImageIndex = 0;
[4876]100      foreach (var var in scope.Variables) {
[5220]101        TreeNode varNode = node.Nodes.Add(string.Format("{0} = {1}", var.Name, var.Value));
[4876]102        varNode.Tag = var.Value;
[4909]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;
[4876]108      }
[4930]109      foreach (var subScope in scope.SubScopes)
[4876]110        AddScope(node.Nodes, subScope);
[4930]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;
[4931]117        node.ToolTipText = "Current scope of active operation";
[4876]118      }
119    }
120
[4930]121
[4876]122    private void UpdateExecutionContext() {
123      executionContextTreeView.BeginUpdate();
124      executionContextTreeView.Nodes.Clear();
[4909]125      executionContextTreeView.ImageList.Images.Clear();
[5287]126      executionContextTreeView.ImageList.Images.Add(VSImageLibrary.Namespace);
[4876]127      if (Content.IsContext) {
128        AddExecutionContext(Content.ExecutionContext, executionContextTreeView.Nodes);
129      }
[4903]130      executionContextTreeView.ExpandAll();
131      if (executionContextTreeView.Nodes.Count > 0)
132        executionContextTreeView.TopNode = executionContextTreeView.Nodes[0];
[4876]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)
[4909]140        name.Append(context.Operator.Name);
[4876]141      else
142        name.Append("<Context>");
143      name.Append("@").Append(executionContext.Scope.Name);
144      TreeNode node = nodes.Add(name.ToString());
145      node.Tag = executionContext;
[4909]146      node.ImageIndex = 0;
147      node.SelectedImageIndex = 0;
[4876]148      foreach (var param in executionContext.Parameters) {
[5116]149        string actualName = null;
150        object value = GetParameterValue(param, executionContext, out actualName);
[5220]151        if (value == null)
152          value = "null";
[5116]153        string label = actualName != null && actualName != param.Name ?
[5220]154          string.Format("{0} ({1}) = {2}", param.Name, actualName, value) :
155          string.Format("{0} = {1}", param.Name, value);
[5116]156        TreeNode paramNode = node.Nodes.Add(label);
[4876]157        paramNode.Tag = param;
[5287]158        executionContextTreeView.ImageList.Images.Add(param.ItemImage ?? VSImageLibrary.Nothing);
[4909]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));
[4876]164      }
165      if (executionContext.Parent != null)
166        AddExecutionContext(executionContext.Parent, node.Nodes);
167    }
168
169    #region Event Handlers (child controls)
170
171    private void executionContextTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) {
172      if (e.Node != null) {
173        IParameter param = e.Node.Tag as IParameter;
174        if (param != null)
175          MainFormManager.MainForm.ShowContent(param);
176      }
177    }
178
[4909]179    private void scopeTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) {
180      if (e.Node != null) {
181        IItem item = e.Node.Tag as IItem;
182        if (item != null)
183          MainFormManager.MainForm.ShowContent(item);
184      }
185    }
186
[4993]187    private TreeNode selectedScopeNode = null;
[4876]188    private void executionContextTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
[5116]189      scopeTreeView.BeginUpdate();
[4993]190      if (selectedScopeNode != null) {
191        if (Content.IsAtomic && Content.AtomicOperation.Scope == selectedScopeNode.Tag) {
192          selectedScopeNode.BackColor = Color.Crimson;
193          selectedScopeNode.ForeColor = Color.White;
194          selectedScopeNode.NodeFont = new Font(DefaultFont, FontStyle.Bold);
195        } else {
196          selectedScopeNode.BackColor = Color.White;
197          selectedScopeNode.ForeColor = Color.Black;
198          selectedScopeNode.NodeFont = DefaultFont;
199        }
200      }
[4876]201      if (e.Node != null) {
202        IExecutionContext context = e.Node.Tag as IExecutionContext;
203        if (context != null && context.Scope != null) {
204          TreeNode scopeNode = FindScopeNode(context.Scope, scopeTreeView.Nodes);
205          if (scopeNode != null) {
[4993]206            if (Content.IsAtomic && Content.AtomicOperation.Scope == scopeNode.Tag) {
207              scopeNode.BackColor = Color.DarkViolet;
208              scopeNode.ForeColor = Color.White;
209              scopeNode.NodeFont = new Font(DefaultFont, FontStyle.Bold);
210            } else {
211              scopeNode.BackColor = Color.Blue;
212              scopeNode.ForeColor = Color.White;
213              scopeNode.NodeFont = new Font(DefaultFont, FontStyle.Bold);
[4876]214            }
[4993]215            selectedScopeNode = scopeNode;
[4876]216            scopeTreeView.TopNode = scopeNode;
217          }
218        }
219      }
[5116]220      scopeTreeView.EndUpdate();
[4876]221    }
222
223    private TreeNode FindScopeNode(IScope scope, TreeNodeCollection nodes) {
224      foreach (TreeNode node in nodes) {
225        if (node.Tag == scope) {
226          return node;
227        } else {
228          TreeNode childNode = FindScopeNode(scope, node.Nodes);
229          if (childNode != null)
230            return childNode;
231        }
232      }
233      return null;
234    }
235
[4903]236    private void nameTextBox_DoubleClick(object sender, EventArgs e) {
237      if (Content != null && Content.IsAtomic && Content.AtomicOperation.Operator != null)
238        MainFormManager.MainForm.ShowContent(Content.AtomicOperation.Operator);
239    }
240
[5003]241    private void ShowValue_Click(object sender, EventArgs e) {
242      if (executionContextTreeView.SelectedNode == null)
243        return;
244      IParameter param = executionContextTreeView.SelectedNode.Tag as IParameter;
[5122]245      if (param != null) {
246        string actualName = null;
[5124]247        IExecutionContext context = executionContextTreeView.SelectedNode.Parent.Tag as IExecutionContext ?? Content.ExecutionContext;
[5122]248        MainFormManager.MainForm.ShowContent(GetParameterValue(param, context, out actualName) as IContent);
249      }
[5003]250    }
251
252    private void executionContextConextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
[5077]253      IParameter param = executionContextTreeView.SelectedNode.Tag as IParameter;
[5122]254      if (param != null) {
255        string actualName = null;
256        IExecutionContext context = executionContextTreeView.SelectedNode.Parent.Tag as IExecutionContext ?? Content.ExecutionContext;
257        showValueToolStripMenuItem.Enabled = GetParameterValue(param, context, out actualName) is IContent;
258      } else
[5077]259        e.Cancel = true;
[5003]260    }
261
[5116]262    private void executionContextTreeView_MouseDown(object sender, MouseEventArgs e) {
263      if (e.Button == System.Windows.Forms.MouseButtons.Right)
264        executionContextTreeView.SelectedNode = executionContextTreeView.GetNodeAt(e.Location);
265    }
[5003]266
[5116]267    #endregion
[5003]268
[4876]269  }
270}
Note: See TracBrowser for help on using the repository browser.