Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

  • implemented reviewers' comments
File size: 6.0 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 HeuristicLab.Common;
24using HeuristicLab.MainForm;
25
26namespace HeuristicLab.Core.Views {
27  /// <summary>
28  /// Base class for editors of engines.
29  /// </summary>
30  [View("Engine View")]
31  [Content(typeof(Engine), true)]
32  [Content(typeof(IEngine), false)]
33  public partial class EngineView : ItemView {
34    private int executionTimeCounter;
35
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>
40    public new IEngine Content {
41      get { return (IEngine)base.Content; }
42      set { base.Content = value; }
43    }
44
45    /// <summary>
46    /// Initializes a new instance of <see cref="EngineBaseEditor"/>.
47    /// </summary>
48    public EngineView() {
49      InitializeComponent();
50    }
51    public EngineView(IEngine content)
52      : this() {
53      Content = content;
54    }
55
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>
60    protected override void DeregisterContentEvents() {
61      Content.Prepared -= new EventHandler(Content_Prepared);
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();
67    }
68
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>
73    protected override void RegisterContentEvents() {
74      base.RegisterContentEvents();
75      Content.Prepared += new EventHandler(Content_Prepared);
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);
80    }
81
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>
86    protected override void OnContentChanged() {
87      base.OnContentChanged();
88      logTextBox.Clear();
89      if (Content == null) {
90        logTextBox.Enabled = false;
91        executionTimeTextBox.Enabled = false;
92      } else {
93        logTextBox.Enabled = true;
94        UpdateExecutionTimeTextBox();
95        executionTimeTextBox.Enabled = true;
96      }
97    }
98
99    #region Content Events
100    protected virtual void Content_Prepared(object sender, EventArgs e) {
101      if (InvokeRequired)
102        Invoke(new EventHandler(Content_Prepared), sender, e);
103      else {
104        UpdateExecutionTimeTextBox();
105        logTextBox.Clear();
106        Log("Engine prepared");
107      }
108    }
109    protected virtual void Content_Started(object sender, EventArgs e) {
110      executionTimeCounter = 0;
111      if (InvokeRequired)
112        Invoke(new EventHandler(Content_Started), sender, e);
113      else {
114        UpdateExecutionTimeTextBox();
115        Log("Engine started");
116      }
117    }
118    protected virtual void Content_Stopped(object sender, EventArgs e) {
119      if (InvokeRequired)
120        Invoke(new EventHandler(Content_Stopped), sender, e);
121      else {
122        UpdateExecutionTimeTextBox();
123        if (Content.Finished) Log("Engine finished");
124        else Log("Engine stopped");
125      }
126    }
127    protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
128      executionTimeCounter++;
129      if ((executionTimeCounter == 100) || !Content.Running) {
130        executionTimeCounter = 0;
131        UpdateExecutionTimeTextBox();
132      }
133    }
134    protected virtual void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
135      if (InvokeRequired)
136        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
137      else
138        Log(Auxiliary.BuildErrorMessage(e.Value));
139    }
140    #endregion
141
142    #region Helpers
143    protected virtual void UpdateExecutionTimeTextBox() {
144      if (InvokeRequired)
145        Invoke(new Action(UpdateExecutionTimeTextBox));
146      else
147        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
148    }
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    }
161    #endregion
162  }
163}
Note: See TracBrowser for help on using the repository browser.