Free cookie consent management tool by TermsFeed Policy Generator

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

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

Correctly select parent.tag instead of just parent when trying to show the actual value (#47)

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