Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2668 was 2664, checked in by swagner, 15 years ago

Abandoned policy that the names of all abstract base classes have to end in "Base" (#95)

File size: 6.9 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;
31using HeuristicLab.MainForm;
32
33namespace HeuristicLab.Core.Views {
34  /// <summary>
35  /// Base class for editors of engines.
36  /// </summary>
37  [Content(typeof(Engine), true)]
38  public partial class EngineView : ItemView {
39    private int executionTimeCounter;
40
41    /// <summary>
42    /// Gets or sets the current engine.
43    /// </summary>
44    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="EditorBase"/>.</remarks>
45    public Engine Engine {
46      get { return (Engine)Item; }
47      set { base.Item = value; }
48    }
49
50    /// <summary>
51    /// Initializes a new instance of <see cref="EngineBaseEditor"/>.
52    /// </summary>
53    public EngineView() {
54      InitializeComponent();
55    }
56    public EngineView(Engine engine)
57      : this() {
58      Engine = engine;
59    }
60
61    /// <summary>
62    /// Removes the event handlers from the underlying <see cref="IEngine"/>.
63    /// </summary>
64    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
65    protected override void DeregisterObjectEvents() {
66      Engine.OperatorGraphChanged -= new EventHandler(Engine_OperatorGraphChanged);
67      Engine.Initialized -= new EventHandler(Engine_Initialized);
68      Engine.Started -= new EventHandler(Engine_Started);
69      Engine.Stopped -= new EventHandler(Engine_Stopped);
70      Engine.ExecutionTimeChanged -= new EventHandler(Engine_ExecutionTimeChanged);
71      Engine.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
72      base.DeregisterObjectEvents();
73    }
74
75    /// <summary>
76    /// Adds event handlers to the underlying <see cref="IEngine"/>.
77    /// </summary>
78    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
79    protected override void RegisterObjectEvents() {
80      base.RegisterObjectEvents();
81      Engine.OperatorGraphChanged += new EventHandler(Engine_OperatorGraphChanged);
82      Engine.Initialized += new EventHandler(Engine_Initialized);
83      Engine.Started += new EventHandler(Engine_Started);
84      Engine.Stopped += new EventHandler(Engine_Stopped);
85      Engine.ExecutionTimeChanged += new EventHandler(Engine_ExecutionTimeChanged);
86      Engine.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
87    }
88
89    /// <summary>
90    /// Updates all controls with the latest data of the model.
91    /// </summary>
92    /// <remarks>Calls <see cref="EditorBase.UpdateControls"/> of base class <see cref="EditorBase"/>.</remarks>
93    protected override void OnObjectChanged() {
94      base.OnObjectChanged();
95      stopButton.Enabled = false;
96      if (Engine == null) {
97        operatorGraphView.Enabled = false;
98        scopeView.Enabled = false;
99        startButton.Enabled = false;
100        resetButton.Enabled = false;
101        executionTimeTextBox.Enabled = false;
102      } else {
103        operatorGraphView.OperatorGraph = Engine.OperatorGraph;
104        scopeView.Scope = Engine.GlobalScope;
105        startButton.Enabled = !Engine.Finished;
106        resetButton.Enabled = true;
107        UpdateExecutionTimeTextBox();
108        executionTimeTextBox.Enabled = true;
109      }
110    }
111
112    #region Engine Events
113    private void Engine_OperatorGraphChanged(object sender, EventArgs e) {
114      if (InvokeRequired)
115        Invoke(new EventHandler(Engine_OperatorGraphChanged), sender, e);
116      else
117        operatorGraphView.OperatorGraph = Engine.OperatorGraph;
118    }
119    private void Engine_Initialized(object sender, EventArgs e) {
120      if (InvokeRequired)
121        Invoke(new EventHandler(Engine_Initialized), sender, e);
122      else {
123        operatorGraphView.Enabled = true;
124        scopeView.Enabled = true;
125        startButton.Enabled = !Engine.Finished;
126        stopButton.Enabled = false;
127        resetButton.Enabled = true;
128        UpdateExecutionTimeTextBox();
129      }
130    }
131    private void Engine_Started(object sender, EventArgs e) {
132      executionTimeCounter = 0;
133      if (InvokeRequired)
134        Invoke(new EventHandler(Engine_Started), sender, e);
135      else {
136        operatorGraphView.Enabled = false;
137        scopeView.Enabled = false;
138        startButton.Enabled = false;
139        stopButton.Enabled = true;
140        resetButton.Enabled = false;
141        UpdateExecutionTimeTextBox();
142      }
143    }
144    private void Engine_Stopped(object sender, EventArgs e) {
145      if (InvokeRequired)
146        Invoke(new EventHandler(Engine_Stopped), sender, e);
147      else {
148        operatorGraphView.Enabled = true;
149        scopeView.Enabled = true;
150        startButton.Enabled = !Engine.Finished;
151        stopButton.Enabled = false;
152        resetButton.Enabled = true;
153        UpdateExecutionTimeTextBox();
154      }
155    }
156    private void Engine_ExecutionTimeChanged(object sender, EventArgs e) {
157      executionTimeCounter++;
158      if ((executionTimeCounter == 100) || !Engine.Running) {
159        executionTimeCounter = 0;
160        UpdateExecutionTimeTextBox();
161      }
162    }
163    private void Engine_ExceptionOccurred(object sender, EventArgs<Exception> e) {
164      if (InvokeRequired)
165        Invoke(new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred), sender, e);
166      else
167        Auxiliary.ShowErrorMessageBox(e.Value);
168    }
169    #endregion
170
171    #region Button events
172    private void startButton_Click(object sender, EventArgs e) {
173      Engine.Start();
174    }
175    private void stopButton_Click(object sender, EventArgs e) {
176      Engine.Stop();
177    }
178    private void resetButton_Click(object sender, EventArgs e) {
179      Engine.Initialize();
180    }
181    #endregion
182
183    #region Helpers
184    private void UpdateExecutionTimeTextBox() {
185      if (InvokeRequired)
186        Invoke(new Action(UpdateExecutionTimeTextBox));
187      else
188        executionTimeTextBox.Text = Engine.ExecutionTime.ToString();
189    }
190    #endregion
191  }
192}
Note: See TracBrowser for help on using the repository browser.