Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/EngineBaseView.cs @ 2655

Last change on this file since 2655 was 2655, checked in by swagner, 14 years ago

Committing first results of the refactoring of HeuristicLab.Core and related plugins (#95)

File size: 7.0 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(EngineBase), true)]
38  public partial class EngineBaseView : ItemViewBase {
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 EngineBase Engine {
46      get { return (EngineBase)Item; }
47      set { base.Item = value; }
48    }
49
50    /// <summary>
51    /// Initializes a new instance of <see cref="EngineBaseEditor"/>.
52    /// </summary>
53    public EngineBaseView() {
54      InitializeComponent();
55    }
56    public EngineBaseView(EngineBase 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        executionTimeTextBox.Text = Engine.ExecutionTime.ToString();
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        executionTimeTextBox.Text = Engine.ExecutionTime.ToString();
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        executionTimeTextBox.Text = Engine.ExecutionTime.ToString();
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        executionTimeTextBox.Text = Engine.ExecutionTime.ToString();
154      }
155    }
156    private void Engine_ExecutionTimeChanged(object sender, EventArgs e) {
157      executionTimeCounter++;
158      if ((executionTimeCounter == 1000) || !Engine.Running) {
159        executionTimeCounter = 0;
160        if (InvokeRequired)
161          Invoke(new EventHandler(Engine_ExecutionTimeChanged), sender, e);
162        else
163          executionTimeTextBox.Text = Engine.ExecutionTime.ToString();
164      }
165    }
166    private void Engine_ExceptionOccurred(object sender, EventArgs<Exception> e) {
167      if (InvokeRequired)
168        Invoke(new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred), sender, e);
169      else
170        Auxiliary.ShowErrorMessageBox(e.Value);
171    }
172    #endregion
173
174    #region Button events
175    private void startButton_Click(object sender, EventArgs e) {
176      Engine.Start();
177    }
178    private void stopButton_Click(object sender, EventArgs e) {
179      Engine.Stop();
180    }
181    private void resetButton_Click(object sender, EventArgs e) {
182      Engine.Initialize();
183    }
184    #endregion
185  }
186}
Note: See TracBrowser for help on using the repository browser.