Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

  • worked on operators, engines, and optimization
File size: 9.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Windows.Forms;
24using HeuristicLab.Common;
25using HeuristicLab.MainForm;
26using HeuristicLab.Persistence.Default.Xml;
27
28namespace HeuristicLab.Core.Views {
29  /// <summary>
30  /// Base class for editors of engines.
31  /// </summary>
32  [Content(typeof(Engine), true)]
33  [Content(typeof(IEngine), false)]
34  public partial class EngineView : ItemView {
35    protected TypeSelectorDialog typeSelectorDialog;
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 new IEngine Content {
43      get { return (IEngine)base.Content; }
44      set { base.Content = value; }
45    }
46
47    /// <summary>
48    /// Initializes a new instance of <see cref="EngineBaseEditor"/>.
49    /// </summary>
50    public EngineView() {
51      InitializeComponent();
52    }
53    public EngineView(IEngine content)
54      : this() {
55      Content = content;
56    }
57
58    /// <summary>
59    /// Removes the event handlers from the underlying <see cref="IEngine"/>.
60    /// </summary>
61    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
62    protected override void DeregisterContentEvents() {
63      Content.OperatorGraphChanged -= new EventHandler(Content_OperatorGraphChanged);
64      Content.Prepared -= new EventHandler(Content_Prepared);
65      Content.Started -= new EventHandler(Content_Started);
66      Content.Stopped -= new EventHandler(Content_Stopped);
67      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
68      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
69      base.DeregisterContentEvents();
70    }
71
72    /// <summary>
73    /// Adds event handlers to the underlying <see cref="IEngine"/>.
74    /// </summary>
75    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
76    protected override void RegisterContentEvents() {
77      base.RegisterContentEvents();
78      Content.OperatorGraphChanged += new EventHandler(Content_OperatorGraphChanged);
79      Content.Prepared += new EventHandler(Content_Prepared);
80      Content.Started += new EventHandler(Content_Started);
81      Content.Stopped += new EventHandler(Content_Stopped);
82      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
83      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
84    }
85
86    /// <summary>
87    /// Updates all controls with the latest data of the model.
88    /// </summary>
89    /// <remarks>Calls <see cref="EditorBase.UpdateControls"/> of base class <see cref="EditorBase"/>.</remarks>
90    protected override void OnContentChanged() {
91      base.OnContentChanged();
92      stopButton.Enabled = false;
93      if (Content == null) {
94        newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = false;
95        operatorGraphViewHost.Enabled = false;
96        scopeView.Enabled = false;
97        startButton.Enabled = resetButton.Enabled = false;
98        executionTimeTextBox.Enabled = false;
99      } else {
100        newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = true;
101        operatorGraphViewHost.Content = Content.OperatorGraph;
102        operatorGraphViewHost.Enabled = true;
103        scopeView.Content = Content.GlobalScope;
104        scopeView.Enabled = true;
105        startButton.Enabled = !Content.Finished;
106        resetButton.Enabled = true;
107        UpdateExecutionTimeTextBox();
108        executionTimeTextBox.Enabled = true;
109      }
110    }
111
112    #region Content Events
113    protected virtual void Content_OperatorGraphChanged(object sender, EventArgs e) {
114      if (InvokeRequired)
115        Invoke(new EventHandler(Content_OperatorGraphChanged), sender, e);
116      else
117        operatorGraphViewHost.Content = Content.OperatorGraph;
118    }
119    protected virtual void Content_Prepared(object sender, EventArgs e) {
120      if (InvokeRequired)
121        Invoke(new EventHandler(Content_Prepared), sender, e);
122      else {
123        newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = true;
124        operatorGraphViewHost.Enabled = true;
125        scopeView.Enabled = true;
126        startButton.Enabled = !Content.Finished;
127        stopButton.Enabled = false;
128        resetButton.Enabled = true;
129        UpdateExecutionTimeTextBox();
130      }
131    }
132    protected virtual void Content_Started(object sender, EventArgs e) {
133      executionTimeCounter = 0;
134      if (InvokeRequired)
135        Invoke(new EventHandler(Content_Started), sender, e);
136      else {
137        newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = false;
138        operatorGraphViewHost.Enabled = false;
139        scopeView.Enabled = false;
140        startButton.Enabled = false;
141        stopButton.Enabled = true;
142        resetButton.Enabled = false;
143        UpdateExecutionTimeTextBox();
144      }
145    }
146    protected virtual void Content_Stopped(object sender, EventArgs e) {
147      if (InvokeRequired)
148        Invoke(new EventHandler(Content_Stopped), sender, e);
149      else {
150        newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = true;
151        operatorGraphViewHost.Enabled = true;
152        scopeView.Enabled = true;
153        startButton.Enabled = !Content.Finished;
154        stopButton.Enabled = false;
155        resetButton.Enabled = true;
156        UpdateExecutionTimeTextBox();
157      }
158    }
159    protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
160      executionTimeCounter++;
161      if ((executionTimeCounter == 100) || !Content.Running) {
162        executionTimeCounter = 0;
163        UpdateExecutionTimeTextBox();
164      }
165    }
166    protected virtual void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
167      if (InvokeRequired)
168        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
169      else
170        Auxiliary.ShowErrorMessageBox(e.Value);
171    }
172    #endregion
173
174    #region Button events
175    protected void newOperatorGraphButton_Click(object sender, EventArgs e) {
176      Content.OperatorGraph = new OperatorGraph();
177    }
178    protected void openOperatorGraphButton_Click(object sender, EventArgs e) {
179      openFileDialog.Title = "Open Operator Graph";
180      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
181        OperatorGraph operatorGraph = null;
182        try {
183          operatorGraph = XmlParser.Deserialize(openFileDialog.FileName) as OperatorGraph;
184        }
185        catch (Exception ex) {
186          Auxiliary.ShowErrorMessageBox(ex);
187        }
188        if (operatorGraph == null)
189          MessageBox.Show(this, "Selected file does not contain an operator graph.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
190        else
191          Content.OperatorGraph = operatorGraph;
192      }
193    }
194    protected void saveOperatorGraphButton_Click(object sender, EventArgs e) {
195      saveFileDialog.Title = "Save Operator Graph";
196      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
197        try {
198          if (saveFileDialog.FilterIndex == 1)
199            XmlGenerator.Serialize(Content.OperatorGraph, saveFileDialog.FileName, 0);
200          else
201            XmlGenerator.Serialize(Content.OperatorGraph, saveFileDialog.FileName, 9);
202        }
203        catch (Exception ex) {
204          Auxiliary.ShowErrorMessageBox(ex);
205        }
206      }
207    }
208    protected virtual void startButton_Click(object sender, EventArgs e) {
209      Content.Start();
210    }
211    protected virtual void stopButton_Click(object sender, EventArgs e) {
212      Content.Stop();
213    }
214    protected virtual void resetButton_Click(object sender, EventArgs e) {
215      Content.Prepare();
216    }
217    #endregion
218
219    #region Helpers
220    protected virtual void UpdateExecutionTimeTextBox() {
221      if (InvokeRequired)
222        Invoke(new Action(UpdateExecutionTimeTextBox));
223      else
224        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
225    }
226    #endregion
227  }
228}
Note: See TracBrowser for help on using the repository browser.