#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using HeuristicLab.Core;
using HeuristicLab.Core.Views;
using HeuristicLab.MainForm;
namespace HeuristicLab.DebugEngine {
///
/// Base class for editors of engines.
///
[View("DebugEngine View")]
[Content(typeof(DebugEngine), true)]
public partial class DebugEngineView : ItemView {
///
/// Gets or sets the current engine.
///
/// Uses property of base class .
public new DebugEngine Content {
get { return (DebugEngine)base.Content; }
set { base.Content = value; }
}
///
/// Initializes a new instance of .
///
public DebugEngineView() {
InitializeComponent();
}
///
/// Removes the event handlers from the underlying .
///
/// Calls of base class .
protected override void DeregisterContentEvents() {
Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
base.DeregisterContentEvents();
}
///
/// Adds event handlers to the underlying .
///
/// Calls of base class .
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
}
///
/// Updates all controls with the latest data of the model.
///
/// Calls of base class .
protected override void OnContentChanged() {
base.OnContentChanged();
if (Content == null) {
logView.Content = null;
executionTimeTextBox.Text = "-";
} else {
logView.Content = Content.Log;
executionTimeTextBox.Text = Content.ExecutionTime.ToString();
}
}
protected override void SetEnabledStateOfControls() {
base.SetEnabledStateOfControls();
if (Content == null) {
logView.Enabled = false;
executionTimeTextBox.Enabled = false;
} else {
logView.Enabled = true;
executionTimeTextBox.Enabled = true;
}
}
protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
if (InvokeRequired)
Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
else
executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
}
void Content_ExecutionStateChanged(object sender, EventArgs e) {
switch (Content.ExecutionState) {
case ExecutionState.Paused:
case ExecutionState.Stopped:
case ExecutionState.Prepared:
UpdateView(); break;
}
}
protected virtual void UpdateView() {
if (InvokeRequired) {
Invoke(new Action(UpdateView));
} else {
executionStackTreeView.Nodes.Clear();
AddOperations(executionStackTreeView.Nodes, Content.ExecutionStack.ToArray());
executionStackTreeView.ExpandAll();
scopeTreeView.Nodes.Clear();
if (Content.CurrentOperation != null) {
AddScope(scopeTreeView.Nodes, Content.CurrentOperation.Scope);
}
scopeTreeView.ExpandAll();
}
}
private void AddOperations(TreeNodeCollection nodes, IEnumerable operations) {
foreach (IOperation op in operations) {
if (op is IAtomicOperation) {
IAtomicOperation atom = op as IAtomicOperation;
TreeNode node = nodes.Add(atom.Operator.Name);
node.Tag = atom;
if (atom.Operator.Breakpoint)
node.ForeColor = Color.Red;
foreach (var param in atom.Operator.Parameters) {
IItem value = null;
try {
value = param.ActualValue;
} catch (Exception) { }
TreeNode paramNode = node.Nodes.Add(string.Format("Param {0}={1}", param.Name, value));
paramNode.Tag = param;
}
} else if (op is OperationCollection) {
OperationCollection ops = op as OperationCollection;
TreeNode node = executionStackTreeView.Nodes.Add(string.Format("{0} Operations", ops.Count));
node.Tag = op;
AddOperations(node.Nodes, ops);
}
}
}
private void AddScope(TreeNodeCollection nodes, IScope scope) {
TreeNode node = nodes.Add(scope.Name);
foreach (var var in scope.Variables) {
TreeNode varNode = node.Nodes.Add(string.Format("{0}={1}", var.Name, var.Value.ToString()));
varNode.Tag = var.Value;
}
foreach (var subScope in scope.SubScopes) {
AddScope(node.Nodes, subScope);
}
}
private void stepButton_Click(object sender, EventArgs e) {
Content.Step();
UpdateView();
}
private void executionStackTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) {
if (e.Node != null) {
IAtomicOperation op = e.Node.Tag as IAtomicOperation;
if (op != null) {
op.Operator.Breakpoint = !op.Operator.Breakpoint;
if (op.Operator.Breakpoint) {
e.Node.ForeColor = Color.Red;
} else {
e.Node.ForeColor = Color.Black;
}
executionStackTreeView.SelectedNode = null;
}
IParameter param = e.Node.Tag as IParameter;
if (param != null)
MainFormManager.MainForm.ShowContent(param);
}
}
private void updateButton_Click(object sender, EventArgs e) {
UpdateView();
}
private void scopeTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) {
if (e.Node.Tag != null)
MainFormManager.MainForm.ShowContent((IItem)e.Node.Tag);
}
}
}