Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5124 was 5077, checked in by epitzer, 14 years ago

disable context menu "Show Value" if value is null or not an IContent (#47)

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