Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/EngineView.cs @ 3149

Last change on this file since 3149 was 2917, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • implemented reviewers' comments
File size: 6.0 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]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;
[2474]23using HeuristicLab.Common;
[2520]24using HeuristicLab.MainForm;
[2]25
[2520]26namespace HeuristicLab.Core.Views {
[776]27  /// <summary>
28  /// Base class for editors of engines.
29  /// </summary>
[2917]30  [View("Engine View")]
[2664]31  [Content(typeof(Engine), true)]
[2727]32  [Content(typeof(IEngine), false)]
[2664]33  public partial class EngineView : ItemView {
[2]34    private int executionTimeCounter;
35
[776]36    /// <summary>
37    /// Gets or sets the current engine.
38    /// </summary>
39    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="EditorBase"/>.</remarks>
[2727]40    public new IEngine Content {
41      get { return (IEngine)base.Content; }
[2713]42      set { base.Content = value; }
[2]43    }
44
[776]45    /// <summary>
46    /// Initializes a new instance of <see cref="EngineBaseEditor"/>.
47    /// </summary>
[2664]48    public EngineView() {
[2]49      InitializeComponent();
50    }
[2727]51    public EngineView(IEngine content)
[2655]52      : this() {
[2727]53      Content = content;
[2655]54    }
[2]55
[776]56    /// <summary>
57    /// Removes the event handlers from the underlying <see cref="IEngine"/>.
58    /// </summary>
59    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
[2713]60    protected override void DeregisterContentEvents() {
[2790]61      Content.Prepared -= new EventHandler(Content_Prepared);
[2713]62      Content.Started -= new EventHandler(Content_Started);
63      Content.Stopped -= new EventHandler(Content_Stopped);
64      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
65      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
66      base.DeregisterContentEvents();
[2]67    }
[2655]68
[776]69    /// <summary>
70    /// Adds event handlers to the underlying <see cref="IEngine"/>.
71    /// </summary>
72    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
[2713]73    protected override void RegisterContentEvents() {
74      base.RegisterContentEvents();
[2790]75      Content.Prepared += new EventHandler(Content_Prepared);
[2713]76      Content.Started += new EventHandler(Content_Started);
77      Content.Stopped += new EventHandler(Content_Stopped);
78      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
79      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
[2]80    }
81
[776]82    /// <summary>
83    /// Updates all controls with the latest data of the model.
84    /// </summary>
85    /// <remarks>Calls <see cref="EditorBase.UpdateControls"/> of base class <see cref="EditorBase"/>.</remarks>
[2713]86    protected override void OnContentChanged() {
87      base.OnContentChanged();
[2916]88      logTextBox.Clear();
[2713]89      if (Content == null) {
[2916]90        logTextBox.Enabled = false;
[2]91        executionTimeTextBox.Enabled = false;
92      } else {
[2916]93        logTextBox.Enabled = true;
[2662]94        UpdateExecutionTimeTextBox();
[2]95        executionTimeTextBox.Enabled = true;
96      }
97    }
98
[2713]99    #region Content Events
[2790]100    protected virtual void Content_Prepared(object sender, EventArgs e) {
[2655]101      if (InvokeRequired)
[2790]102        Invoke(new EventHandler(Content_Prepared), sender, e);
[2655]103      else {
[2662]104        UpdateExecutionTimeTextBox();
[2916]105        logTextBox.Clear();
106        Log("Engine prepared");
[2]107      }
108    }
[2713]109    protected virtual void Content_Started(object sender, EventArgs e) {
[2655]110      executionTimeCounter = 0;
[2]111      if (InvokeRequired)
[2713]112        Invoke(new EventHandler(Content_Started), sender, e);
[2]113      else {
[2662]114        UpdateExecutionTimeTextBox();
[2916]115        Log("Engine started");
[2]116      }
117    }
[2713]118    protected virtual void Content_Stopped(object sender, EventArgs e) {
[2]119      if (InvokeRequired)
[2713]120        Invoke(new EventHandler(Content_Stopped), sender, e);
[2]121      else {
[2662]122        UpdateExecutionTimeTextBox();
[2916]123        if (Content.Finished) Log("Engine finished");
124        else Log("Engine stopped");
[2]125      }
126    }
[2713]127    protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
[2655]128      executionTimeCounter++;
[2713]129      if ((executionTimeCounter == 100) || !Content.Running) {
[2655]130        executionTimeCounter = 0;
[2662]131        UpdateExecutionTimeTextBox();
[2655]132      }
133    }
[2713]134    protected virtual void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
[2655]135      if (InvokeRequired)
[2713]136        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
[2655]137      else
[2916]138        Log(Auxiliary.BuildErrorMessage(e.Value));
[2655]139    }
[2]140    #endregion
141
[2662]142    #region Helpers
[2676]143    protected virtual void UpdateExecutionTimeTextBox() {
[2662]144      if (InvokeRequired)
145        Invoke(new Action(UpdateExecutionTimeTextBox));
146      else
[2713]147        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
[2662]148    }
[2916]149    protected virtual void Log(string message) {
150      if (InvokeRequired)
151        Invoke(new Action<string>(Log), message);
152      else {
153        message = DateTime.Now.ToString() + "\t" + message;
154        string[] newLines = message.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
155        string[] lines = new string[logTextBox.Lines.Length + newLines.Length];
156        logTextBox.Lines.CopyTo(lines, 0);
157        newLines.CopyTo(lines, logTextBox.Lines.Length);
158        logTextBox.Lines = lines;
159      }
160    }
[2662]161    #endregion
[2]162  }
163}
Note: See TracBrowser for help on using the repository browser.