Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5001 was 4998, checked in by epitzer, 14 years ago

remove resources, add license headers, update plug-in dependencies (#47)

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