#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.Windows.Forms; using HeuristicLab.Common; using HeuristicLab.MainForm; using HeuristicLab.Persistence.Default.Xml; namespace HeuristicLab.Core.Views { /// /// Base class for editors of engines. /// [Content(typeof(Engine), true)] [Content(typeof(IEngine), false)] public partial class EngineView : ItemView { protected TypeSelectorDialog typeSelectorDialog; private int executionTimeCounter; /// /// Gets or sets the current engine. /// /// Uses property of base class . public new IEngine Content { get { return (IEngine)base.Content; } set { base.Content = value; } } /// /// Initializes a new instance of . /// public EngineView() { InitializeComponent(); } public EngineView(IEngine content) : this() { Content = content; } /// /// Removes the event handlers from the underlying . /// /// Calls of base class . protected override void DeregisterContentEvents() { Content.OperatorGraphChanged -= new EventHandler(Content_OperatorGraphChanged); Content.Prepared -= new EventHandler(Content_Prepared); Content.Started -= new EventHandler(Content_Started); Content.Stopped -= new EventHandler(Content_Stopped); Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged); Content.ExceptionOccurred -= new EventHandler>(Content_ExceptionOccurred); base.DeregisterContentEvents(); } /// /// Adds event handlers to the underlying . /// /// Calls of base class . protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.OperatorGraphChanged += new EventHandler(Content_OperatorGraphChanged); Content.Prepared += new EventHandler(Content_Prepared); Content.Started += new EventHandler(Content_Started); Content.Stopped += new EventHandler(Content_Stopped); Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged); Content.ExceptionOccurred += new EventHandler>(Content_ExceptionOccurred); } /// /// Updates all controls with the latest data of the model. /// /// Calls of base class . protected override void OnContentChanged() { base.OnContentChanged(); stopButton.Enabled = false; if (Content == null) { newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = false; operatorGraphViewHost.Enabled = false; scopeView.Enabled = false; startButton.Enabled = resetButton.Enabled = false; executionTimeTextBox.Enabled = false; } else { newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = true; operatorGraphViewHost.Content = Content.OperatorGraph; operatorGraphViewHost.Enabled = true; scopeView.Content = Content.GlobalScope; scopeView.Enabled = true; startButton.Enabled = !Content.Finished; resetButton.Enabled = true; UpdateExecutionTimeTextBox(); executionTimeTextBox.Enabled = true; } } #region Content Events protected virtual void Content_OperatorGraphChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_OperatorGraphChanged), sender, e); else operatorGraphViewHost.Content = Content.OperatorGraph; } protected virtual void Content_Prepared(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_Prepared), sender, e); else { newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = true; operatorGraphViewHost.Enabled = true; scopeView.Enabled = true; startButton.Enabled = !Content.Finished; stopButton.Enabled = false; resetButton.Enabled = true; UpdateExecutionTimeTextBox(); } } protected virtual void Content_Started(object sender, EventArgs e) { executionTimeCounter = 0; if (InvokeRequired) Invoke(new EventHandler(Content_Started), sender, e); else { newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = false; operatorGraphViewHost.Enabled = false; scopeView.Enabled = false; startButton.Enabled = false; stopButton.Enabled = true; resetButton.Enabled = false; UpdateExecutionTimeTextBox(); } } protected virtual void Content_Stopped(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_Stopped), sender, e); else { newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = true; operatorGraphViewHost.Enabled = true; scopeView.Enabled = true; startButton.Enabled = !Content.Finished; stopButton.Enabled = false; resetButton.Enabled = true; UpdateExecutionTimeTextBox(); } } protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) { executionTimeCounter++; if ((executionTimeCounter == 100) || !Content.Running) { executionTimeCounter = 0; UpdateExecutionTimeTextBox(); } } protected virtual void Content_ExceptionOccurred(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler>(Content_ExceptionOccurred), sender, e); else Auxiliary.ShowErrorMessageBox(e.Value); } #endregion #region Button events protected void newOperatorGraphButton_Click(object sender, EventArgs e) { Content.OperatorGraph = new OperatorGraph(); } protected void openOperatorGraphButton_Click(object sender, EventArgs e) { openFileDialog.Title = "Open Operator Graph"; if (openFileDialog.ShowDialog(this) == DialogResult.OK) { OperatorGraph operatorGraph = null; try { operatorGraph = XmlParser.Deserialize(openFileDialog.FileName) as OperatorGraph; } catch (Exception ex) { Auxiliary.ShowErrorMessageBox(ex); } if (operatorGraph == null) MessageBox.Show(this, "Selected file does not contain an operator graph.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error); else Content.OperatorGraph = operatorGraph; } } protected void saveOperatorGraphButton_Click(object sender, EventArgs e) { saveFileDialog.Title = "Save Operator Graph"; if (saveFileDialog.ShowDialog(this) == DialogResult.OK) { try { if (saveFileDialog.FilterIndex == 1) XmlGenerator.Serialize(Content.OperatorGraph, saveFileDialog.FileName, 0); else XmlGenerator.Serialize(Content.OperatorGraph, saveFileDialog.FileName, 9); } catch (Exception ex) { Auxiliary.ShowErrorMessageBox(ex); } } } protected virtual void startButton_Click(object sender, EventArgs e) { Content.Start(); } protected virtual void stopButton_Click(object sender, EventArgs e) { Content.Stop(); } protected virtual void resetButton_Click(object sender, EventArgs e) { Content.Prepare(); } #endregion #region Helpers protected virtual void UpdateExecutionTimeTextBox() { if (InvokeRequired) Invoke(new Action(UpdateExecutionTimeTextBox)); else executionTimeTextBox.Text = Content.ExecutionTime.ToString(); } #endregion } }