Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5114 was 5114, checked in by swagner, 13 years ago

Integrated DebugEngine into trunk (#47).

File size: 9.9 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        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) {
66      param = (IParameter)param.Clone();
67      param.ExecutionContext = context;
68      object value = null;
69      try {
70        value = param.ActualValue;
71      } catch (Exception x) {
72        value = x.Message;
73      }
74      return value;
75    }
76
77    private void UpdateScopeTree() {
78      scopeTreeView.BeginUpdate();
79      scopeTreeView.Nodes.Clear();
80      scopeTreeView.ImageList.Images.Clear();
81      scopeTreeView.ImageList.Images.Add(VS2008ImageLibrary.Namespace);
82      scopeTreeView.ImageList.Images.Add(VS2008ImageLibrary.Field);
83      if (Content.IsContext) {
84        var scope = Content.ExecutionContext.Scope;
85        while (scope != null && scope.Parent != null)
86          scope = scope.Parent;
87        if (scope != null)
88          AddScope(scopeTreeView.Nodes, scope);
89      }
90      scopeTreeView.EndUpdate();
91    }
92
93    private void AddScope(TreeNodeCollection nodes, IScope scope) {
94      TreeNode node = nodes.Add(string.Format("{0} ({1}+{2})",
95        scope.Name, scope.Variables.Count, scope.SubScopes.Count));
96      node.Tag = scope;
97      node.ImageIndex = 0;
98      node.SelectedImageIndex = 0;
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;
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;
107      }
108      foreach (var subScope in scope.SubScopes)
109        AddScope(node.Nodes, subScope);
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;
116        node.ToolTipText = "Current scope of active operation";
117      }
118    }
119
120
121    private void UpdateExecutionContext() {
122      executionContextTreeView.BeginUpdate();
123      executionContextTreeView.Nodes.Clear();
124      executionContextTreeView.ImageList.Images.Clear();
125      executionContextTreeView.ImageList.Images.Add(VS2008ImageLibrary.Namespace);
126      if (Content.IsContext) {
127        AddExecutionContext(Content.ExecutionContext, executionContextTreeView.Nodes);
128      }
129      executionContextTreeView.ExpandAll();
130      if (executionContextTreeView.Nodes.Count > 0)
131        executionContextTreeView.TopNode = executionContextTreeView.Nodes[0];
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)
139        name.Append(context.Operator.Name);
140      else
141        name.Append("<Context>");
142      name.Append("@").Append(executionContext.Scope.Name);
143      TreeNode node = nodes.Add(name.ToString());
144      node.Tag = executionContext;
145      node.ImageIndex = 0;
146      node.SelectedImageIndex = 0;
147      foreach (var param in executionContext.Parameters) {
148        TreeNode paramNode = node.Nodes.Add(string.Format("{0}={1}",
149          param.Name, GetParameterValue(param, executionContext)));
150        paramNode.Tag = param;
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));
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
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
180    private TreeNode selectedScopeNode = null;
181    private void executionContextTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
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      }
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) {
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);
206            }
207            selectedScopeNode = scopeNode;
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
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
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
240    #endregion
241
242    private void executionContextConextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
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;
248    }
249
250
251
252  }
253}
Note: See TracBrowser for help on using the repository browser.