using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using HeuristicLab.MainForm.WindowsForms; using HeuristicLab.MainForm; using HeuristicLab.Core; using HeuristicLab.Common; namespace HeuristicLab.DebugEngine { [View("Operation Content View")] [Content(typeof(OperationContent), IsDefaultView = true)] public sealed partial class OperationContentView : AsynchronousContentView { public new OperationContent Content { get { return (OperationContent)base.Content; } set { base.Content = value; } } public OperationContentView() { InitializeComponent(); } protected override void OnContentChanged() { base.OnContentChanged(); if (Content == null) { nameTextBox.Text = ""; contextLabel.ForeColor = SystemColors.ControlDark; atomicLabel.ForeColor = SystemColors.ControlDark; collectionLabel.ForeColor = SystemColors.ControlDark; parameterListView.Items.Clear(); scopeTreeView.Nodes.Clear(); executionContextTreeView.Nodes.Clear(); } else { contextLabel.ForeColor = Content.IsContext ? Color.Black : SystemColors.ControlDark; atomicLabel.ForeColor = Content.IsAtomic ? Color.Black : SystemColors.ControlDark; collectionLabel.ForeColor = Content.IsCollection ? Color.Black : SystemColors.ControlDark; nameTextBox.Text = Content.Name; UpdateParameters(); UpdateScopeTree(); UpdateExecutionContext(); } } private void UpdateParameters() { parameterListView.BeginUpdate(); parameterListView.Items.Clear(); if (Content.IsAtomic) { foreach (var param in Content.AtomicOperation.Operator.Parameters) { ListViewItem item = parameterListView.Items.Add( string.Format("{0} = {1}", param.Name, GetParameterValue(param, Content.ExecutionContext))); item.Tag = param; } } parameterListView.EndUpdate(); } private object GetParameterValue(IParameter param, IExecutionContext context) { var originalContext = param.ExecutionContext; param.ExecutionContext = context; object value = null; try { try { value = param.ActualValue; } catch (Exception x) { value = x; } } finally { param.ExecutionContext = originalContext; } return value; } private void UpdateScopeTree() { scopeTreeView.BeginUpdate(); scopeTreeView.Nodes.Clear(); if (Content.IsContext) { var scope = Content.ExecutionContext.Scope; while (scope != null && scope.Parent != null) scope = scope.Parent; UpdateScope(scope); } scopeTreeView.EndUpdate(); } private void UpdateScope(IScope scope) { if (scope != null) { AddScope(scopeTreeView.Nodes, scope); } scopeTreeView.ExpandAll(); if (scopeTreeView.Nodes.Count > 0) scopeTreeView.TopNode = scopeTreeView.Nodes[0]; scopeTreeView.EndUpdate(); } private void AddScope(TreeNodeCollection nodes, IScope scope) { TreeNode node = nodes.Add(scope.Name); node.Tag = scope; if (Content.IsAtomic && Content.AtomicOperation.Scope == scope) { node.ForeColor = Color.Red; node.BackColor = Color.LightGray; } foreach (var var in scope.Variables) { TreeNode varNode = node.Nodes.Add(string.Format("{0}={1}", var.Name, var.Value)); varNode.Tag = var.Value; varNode.ToolTipText = Utils.TypeName(var.Value); } foreach (var subScope in scope.SubScopes) { AddScope(node.Nodes, subScope); } } private void UpdateExecutionContext() { executionContextTreeView.BeginUpdate(); executionContextTreeView.Nodes.Clear(); if (Content.IsContext) { AddExecutionContext(Content.ExecutionContext, executionContextTreeView.Nodes); } executionContextTreeView.EndUpdate(); } private void AddExecutionContext(IExecutionContext executionContext, TreeNodeCollection nodes) { ExecutionContext context = executionContext as ExecutionContext; StringBuilder name = new StringBuilder(); if (context != null && context.Operator != null) name.Append("Op ").Append(context.Operator.Name); else name.Append(""); name.Append("@").Append(executionContext.Scope.Name); TreeNode node = nodes.Add(name.ToString()); node.Tag = executionContext; foreach (var param in executionContext.Parameters) { TreeNode paramNode = node.Nodes.Add(string.Format("Param {0}={1}", param.Name, GetParameterValue(param, executionContext))); paramNode.Tag = param; } if (executionContext.Parent != null) AddExecutionContext(executionContext.Parent, node.Nodes); } #region Event Handlers (child controls) private void parameterListView_ItemActivate(object sender, EventArgs e) { if (parameterListView.SelectedItems.Count > 0) { IParameter param = parameterListView.SelectedItems[0].Tag as IParameter; if (param != null) { MainFormManager.MainForm.ShowContent((IContent)GetParameterValue(param, Content.ExecutionContext)); } } } private void executionContextTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) { if (e.Node != null) { IParameter param = e.Node.Tag as IParameter; if (param != null) MainFormManager.MainForm.ShowContent(param); } } private void executionContextTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { if (e.Node != null) { IExecutionContext context = e.Node.Tag as IExecutionContext; if (context != null && context.Scope != null) { TreeNode scopeNode = FindScopeNode(context.Scope, scopeTreeView.Nodes); if (scopeNode != null) { if (timer.Enabled) { timer_Tick(this, EventArgs.Empty); } timer.Tag = new KeyValuePair(scopeNode, scopeNode.BackColor); scopeNode.BackColor = Color.Blue; scopeTreeView.TopNode = scopeNode; timer.Start(); } } } } private void ResetNode(TreeNode node, Color color) { if (InvokeRequired) Invoke(new Action(ResetNode), node, color); else node.BackColor = color; } private TreeNode FindScopeNode(IScope scope, TreeNodeCollection nodes) { foreach (TreeNode node in nodes) { if (node.Tag == scope) { return node; } else { TreeNode childNode = FindScopeNode(scope, node.Nodes); if (childNode != null) return childNode; } } return null; } private void timer_Tick(object sender, EventArgs e) { KeyValuePair kvp = (KeyValuePair)timer.Tag; ResetNode(kvp.Key, kvp.Value); timer.Stop(); } #endregion } }