Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VNS/HeuristicLab.DebugEngine/3.3/OperationContentView.cs @ 5594

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

Updated year of copyrights (#1406)

File size: 10.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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(VSImageLibrary.Namespace);
83      scopeTreeView.ImageList.Images.Add(VSImageLibrary.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(VSImageLibrary.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        if (value == null)
152          value = "null";
153        string label = actualName != null && actualName != param.Name ?
154          string.Format("{0} ({1}) = {2}", param.Name, actualName, value) :
155          string.Format("{0} = {1}", param.Name, value);
156        TreeNode paramNode = node.Nodes.Add(label);
157        paramNode.Tag = param;
158        executionContextTreeView.ImageList.Images.Add(param.ItemImage ?? VSImageLibrary.Nothing);
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));
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
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
187    private TreeNode selectedScopeNode = null;
188    private void executionContextTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
189      scopeTreeView.BeginUpdate();
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      }
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) {
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);
214            }
215            selectedScopeNode = scopeNode;
216            scopeTreeView.TopNode = scopeNode;
217          }
218        }
219      }
220      scopeTreeView.EndUpdate();
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
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
241    private void ShowValue_Click(object sender, EventArgs e) {
242      if (executionContextTreeView.SelectedNode == null)
243        return;
244      IParameter param = executionContextTreeView.SelectedNode.Tag as IParameter;
245      if (param != null) {
246        string actualName = null;
247        IExecutionContext context = executionContextTreeView.SelectedNode.Parent.Tag as IExecutionContext ?? Content.ExecutionContext;
248        MainFormManager.MainForm.ShowContent(GetParameterValue(param, context, out actualName) as IContent);
249      }
250    }
251
252    private void executionContextConextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
253      IParameter param = executionContextTreeView.SelectedNode.Tag as IParameter;
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
259        e.Cancel = true;
260    }
261
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    }
266
267    #endregion
268
269  }
270}
Note: See TracBrowser for help on using the repository browser.