Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Core/3.3/EngineBaseEditor.cs @ 2760

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