Free cookie consent management tool by TermsFeed Policy Generator

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

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

Continued work on Optimizer and on adapting all views to the new MainForm concept (#770)

File size: 6.4 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 IEngine Engine {
46      get { return (IEngine)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
57    /// <summary>
58    /// Removes the event handlers from the underlying <see cref="IEngine"/>.
59    /// </summary>
60    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
61    protected override void RemoveItemEvents() {
62      Engine.Initialized -= new EventHandler(Engine_Initialized);
63      Engine.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
64      Engine.ExecutionTimeChanged -= new EventHandler(Engine_ExecutionTimeChanged);
65      Engine.Finished -= new EventHandler(Engine_Finished);
66      operatorGraphView.OperatorGraph = null;
67      scopeView.Scope = null;
68      base.RemoveItemEvents();
69    }
70    /// <summary>
71    /// Adds event handlers to the underlying <see cref="IEngine"/>.
72    /// </summary>
73    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
74    protected override void AddItemEvents() {
75      base.AddItemEvents();
76      Engine.Initialized += new EventHandler(Engine_Initialized);
77      Engine.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
78      Engine.ExecutionTimeChanged += new EventHandler(Engine_ExecutionTimeChanged);
79      Engine.Finished += new EventHandler(Engine_Finished);
80      operatorGraphView.OperatorGraph = Engine.OperatorGraph;
81      scopeView.Scope = Engine.GlobalScope;
82    }
83
84    /// <summary>
85    /// Updates all controls with the latest data of the model.
86    /// </summary>
87    /// <remarks>Calls <see cref="EditorBase.UpdateControls"/> of base class <see cref="EditorBase"/>.</remarks>
88    protected override void UpdateControls() {
89      base.UpdateControls();
90      abortButton.Enabled = false;
91      if (Engine == null) {
92        executeButton.Enabled = false;
93        resetButton.Enabled = false;
94        executionTimeTextBox.Enabled = false;
95      } else {
96        executeButton.Enabled = true;
97        resetButton.Enabled = true;
98        executionTimeCounter = 0;
99        executionTimeTextBox.Text = Engine.ExecutionTime.ToString();
100        executionTimeTextBox.Enabled = true;
101      }
102    }
103
104    #region Engine Events
105    private void Engine_Initialized(object sender, EventArgs e) {
106      Refresh();
107    }
108    private delegate void OnExceptionEventDelegate(object sender, EventArgs<Exception> e);
109    private void Engine_ExceptionOccurred(object sender, EventArgs<Exception> e) {
110      if (InvokeRequired)
111        Invoke(new OnExceptionEventDelegate(Engine_ExceptionOccurred), sender, e);
112      else
113        Auxiliary.ShowErrorMessageBox(e.Value);
114    }
115    private void Engine_ExecutionTimeChanged(object sender, EventArgs e) {
116      executionTimeCounter++;
117      if (executionTimeCounter == 1000)
118        UpdateExecutionTimeTextBox();
119    }
120    private void Engine_Finished(object sender, EventArgs e) {
121      UpdateExecutionTimeTextBox();
122      if (globalScopeGroupBox.Controls.Count > 0) {
123        ScopeView scopeEditor = (ScopeView)globalScopeGroupBox.Controls[0];
124        if (!scopeEditor.AutomaticUpdating)
125          scopeEditor.Refresh();
126      }
127      EnableDisableControls();
128    }
129    private void UpdateExecutionTimeTextBox() {
130      if (InvokeRequired)
131        Invoke(new MethodInvoker(UpdateExecutionTimeTextBox));
132      else {
133        executionTimeCounter = 0;
134        executionTimeTextBox.Text = Engine.ExecutionTime.ToString();
135      }
136    }
137    private void EnableDisableControls() {
138      if (InvokeRequired)
139        Invoke(new MethodInvoker(EnableDisableControls));
140      else {
141        executeButton.Enabled = true;
142        abortButton.Enabled = false;
143        resetButton.Enabled = true;
144        operatorGraphGroupBox.Enabled = true;
145        globalScopeGroupBox.Enabled = true;
146      }
147    }
148    #endregion
149
150    #region Button events
151    private void executeButton_Click(object sender, EventArgs e) {
152      executeButton.Enabled = false;
153      abortButton.Enabled = true;
154      resetButton.Enabled = false;
155      operatorGraphGroupBox.Enabled = false;
156      globalScopeGroupBox.Enabled = false;
157      Engine.Execute();
158    }
159    private void abortButton_Click(object sender, EventArgs e) {
160      Engine.Abort();
161    }
162    private void resetButton_Click(object sender, EventArgs e) {
163      Engine.Reset();
164      UpdateExecutionTimeTextBox();
165      if (globalScopeGroupBox.Controls.Count > 0) {
166        ScopeView scopeEditor = (ScopeView)globalScopeGroupBox.Controls[0];
167        if (!scopeEditor.AutomaticUpdating)
168          scopeEditor.Refresh();
169      }
170    }
171    #endregion
172  }
173}
Note: See TracBrowser for help on using the repository browser.