Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.2/EngineBaseEditor.cs @ 3351

Last change on this file since 3351 was 2474, checked in by swagner, 15 years ago

Implemented generic EventArgs (#796)

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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 System.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.Common;
30
31namespace HeuristicLab.Core {
32  /// <summary>
33  /// Base class for editors of engines.
34  /// </summary>
35  public partial class EngineBaseEditor : EditorBase {
36    private int executionTimeCounter;
37
38    /// <summary>
39    /// Gets or sets the current engine.
40    /// </summary>
41    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="EditorBase"/>.</remarks>
42    public IEngine Engine {
43      get { return (IEngine)Item; }
44      set { base.Item = value; }
45    }
46
47    /// <summary>
48    /// Initializes a new instance of <see cref="EngineBaseEditor"/>.
49    /// </summary>
50    public EngineBaseEditor() {
51      InitializeComponent();
52    }
53
54    /// <summary>
55    /// Removes the event handlers from the underlying <see cref="IEngine"/>.
56    /// </summary>
57    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
58    protected override void RemoveItemEvents() {
59      Engine.Initialized -= new EventHandler(Engine_Initialized);
60      Engine.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
61      Engine.ExecutionTimeChanged -= new EventHandler(Engine_ExecutionTimeChanged);
62      Engine.Finished -= new EventHandler(Engine_Finished);
63      operatorGraphView.OperatorGraph = null;
64      scopeView.Scope = null;
65      base.RemoveItemEvents();
66    }
67    /// <summary>
68    /// Adds event handlers to the underlying <see cref="IEngine"/>.
69    /// </summary>
70    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
71    protected override void AddItemEvents() {
72      base.AddItemEvents();
73      Engine.Initialized += new EventHandler(Engine_Initialized);
74      Engine.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
75      Engine.ExecutionTimeChanged += new EventHandler(Engine_ExecutionTimeChanged);
76      Engine.Finished += new EventHandler(Engine_Finished);
77      operatorGraphView.OperatorGraph = Engine.OperatorGraph;
78      scopeView.Scope = Engine.GlobalScope;
79    }
80
81    /// <summary>
82    /// Updates all controls with the latest data of the model.
83    /// </summary>
84    /// <remarks>Calls <see cref="EditorBase.UpdateControls"/> of base class <see cref="EditorBase"/>.</remarks>
85    protected override void UpdateControls() {
86      base.UpdateControls();
87      abortButton.Enabled = false;
88      if (Engine == null) {
89        executeButton.Enabled = false;
90        resetButton.Enabled = false;
91        executionTimeTextBox.Enabled = false;
92      } else {
93        executeButton.Enabled = true;
94        resetButton.Enabled = true;
95        executionTimeCounter = 0;
96        executionTimeTextBox.Text = Engine.ExecutionTime.ToString();
97        executionTimeTextBox.Enabled = true;
98      }
99    }
100
101    #region Engine Events
102    private void Engine_Initialized(object sender, EventArgs e) {
103      Refresh();
104    }
105    private delegate void OnExceptionEventDelegate(object sender, EventArgs<Exception> e);
106    private void Engine_ExceptionOccurred(object sender, EventArgs<Exception> e) {
107      if (InvokeRequired)
108        Invoke(new OnExceptionEventDelegate(Engine_ExceptionOccurred), sender, e);
109      else
110        Auxiliary.ShowErrorMessageBox(e.Value);
111    }
112    private void Engine_ExecutionTimeChanged(object sender, EventArgs e) {
113      executionTimeCounter++;
114      if (executionTimeCounter == 1000)
115        UpdateExecutionTimeTextBox();
116    }
117    private void Engine_Finished(object sender, EventArgs e) {
118      UpdateExecutionTimeTextBox();
119      if (globalScopeGroupBox.Controls.Count > 0) {
120        ScopeView scopeEditor = (ScopeView)globalScopeGroupBox.Controls[0];
121        if (!scopeEditor.AutomaticUpdating)
122          scopeEditor.Refresh();
123      }
124      EnableDisableControls();
125    }
126    private void UpdateExecutionTimeTextBox() {
127      if (InvokeRequired)
128        Invoke(new MethodInvoker(UpdateExecutionTimeTextBox));
129      else {
130        executionTimeCounter = 0;
131        executionTimeTextBox.Text = Engine.ExecutionTime.ToString();
132      }
133    }
134    private void EnableDisableControls() {
135      if (InvokeRequired)
136        Invoke(new MethodInvoker(EnableDisableControls));
137      else {
138        executeButton.Enabled = true;
139        abortButton.Enabled = false;
140        resetButton.Enabled = true;
141        operatorGraphGroupBox.Enabled = true;
142        globalScopeGroupBox.Enabled = true;
143      }
144    }
145    #endregion
146
147    #region Button events
148    private void executeButton_Click(object sender, EventArgs e) {
149      executeButton.Enabled = false;
150      abortButton.Enabled = true;
151      resetButton.Enabled = false;
152      operatorGraphGroupBox.Enabled = false;
153      globalScopeGroupBox.Enabled = false;
154      Engine.Execute();
155    }
156    private void abortButton_Click(object sender, EventArgs e) {
157      Engine.Abort();
158    }
159    private void resetButton_Click(object sender, EventArgs e) {
160      Engine.Reset();
161      UpdateExecutionTimeTextBox();
162      if (globalScopeGroupBox.Controls.Count > 0) {
163        ScopeView scopeEditor = (ScopeView)globalScopeGroupBox.Controls[0];
164        if (!scopeEditor.AutomaticUpdating)
165          scopeEditor.Refresh();
166      }
167    }
168    #endregion
169  }
170}
Note: See TracBrowser for help on using the repository browser.