Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/AlgorithmView.cs @ 2955

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

Stopped algorithm when its view is closed (#208)

File size: 10.9 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.Collections.Generic;
24using System.Windows.Forms;
25using HeuristicLab.Common;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28using HeuristicLab.Persistence.Default.Xml;
29
30namespace HeuristicLab.Optimization.Views {
31  /// <summary>
32  /// The base class for visual representations of items.
33  /// </summary>
34  [View("Algorithm View")]
35  [Content(typeof(Algorithm), true)]
36  [Content(typeof(IAlgorithm), false)]
37  public partial class AlgorithmView : NamedItemView {
38    private TypeSelectorDialog problemTypeSelectorDialog;
39    private int executionTimeCounter;
40
41    public new IAlgorithm Content {
42      get { return (IAlgorithm)base.Content; }
43      set { base.Content = value; }
44    }
45
46    /// <summary>
47    /// Initializes a new instance of <see cref="ItemBaseView"/>.
48    /// </summary>
49    public AlgorithmView() {
50      InitializeComponent();
51    }
52    /// <summary>
53    /// Intializes a new instance of <see cref="ItemBaseView"/> with the given <paramref name="item"/>.
54    /// </summary>
55    /// <param name="item">The item that should be displayed.</param>
56    public AlgorithmView(IAlgorithm content)
57      : this() {
58      Content = content;
59    }
60
61    protected override void OnInitialized(EventArgs e) {
62      // Set order of tab pages according to z order.
63      // NOTE: This is required due to a bug in the VS designer.
64      List<Control> tabPages = new List<Control>();
65      for (int i = 0; i < tabControl.Controls.Count; i++) {
66        tabPages.Add(tabControl.Controls[i]);
67      }
68      tabControl.Controls.Clear();
69      foreach (Control control in tabPages)
70        tabControl.Controls.Add(control);
71
72      base.OnInitialized(e);
73    }
74
75    protected override void DeregisterContentEvents() {
76      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
77      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
78      Content.Prepared -= new EventHandler(Content_Prepared);
79      Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
80      Content.Started -= new EventHandler(Content_Started);
81      Content.Stopped -= new EventHandler(Content_Stopped);
82      base.DeregisterContentEvents();
83    }
84    protected override void RegisterContentEvents() {
85      base.RegisterContentEvents();
86      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
87      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
88      Content.Prepared += new EventHandler(Content_Prepared);
89      Content.ProblemChanged += new EventHandler(Content_ProblemChanged);
90      Content.Started += new EventHandler(Content_Started);
91      Content.Stopped += new EventHandler(Content_Stopped);
92    }
93
94    protected override void OnContentChanged() {
95      base.OnContentChanged();
96      stopButton.Enabled = false;
97      if (Content == null) {
98        parameterCollectionView.Content = null;
99        problemViewHost.Content = null;
100        resultsView.Content = null;
101        tabControl.Enabled = false;
102        startButton.Enabled = resetButton.Enabled = false;
103        executionTimeTextBox.Text = "-";
104        executionTimeTextBox.Enabled = false;
105      } else {
106        parameterCollectionView.Content = Content.Parameters;
107        saveProblemButton.Enabled = Content.Problem != null;
108        problemViewHost.ViewType = null;
109        problemViewHost.Content = Content.Problem;
110        resultsView.Content = Content.Results;
111        tabControl.Enabled = true;
112        startButton.Enabled = !Content.Finished;
113        resetButton.Enabled = true;
114        UpdateExecutionTimeTextBox();
115        executionTimeTextBox.Enabled = true;
116      }
117    }
118
119    protected override void OnClosed(FormClosedEventArgs e) {
120      Content.Stop();
121      base.OnClosed(e);
122    }
123
124    #region Content Events
125    protected virtual void Content_Prepared(object sender, EventArgs e) {
126      if (InvokeRequired)
127        Invoke(new EventHandler(Content_Prepared), sender, e);
128      else {
129        parameterCollectionView.Enabled = true;
130        newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = true;
131        problemViewHost.Enabled = true;
132        resultsView.Content = Content.Results;
133        resultsView.Enabled = true;
134        startButton.Enabled = !Content.Finished;
135        stopButton.Enabled = false;
136        resetButton.Enabled = true;
137        UpdateExecutionTimeTextBox();
138      }
139    }
140    protected void Content_ProblemChanged(object sender, EventArgs e) {
141      if (InvokeRequired)
142        Invoke(new EventHandler(Content_ProblemChanged), sender, e);
143      else {
144        problemViewHost.ViewType = null;
145        problemViewHost.Content = Content.Problem;
146        saveProblemButton.Enabled = Content.Problem != null;
147      }
148    }
149    protected virtual void Content_Started(object sender, EventArgs e) {
150      executionTimeCounter = 0;
151      if (InvokeRequired)
152        Invoke(new EventHandler(Content_Started), sender, e);
153      else {
154        parameterCollectionView.Enabled = false;
155        newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = false;
156        problemViewHost.Enabled = false;
157        resultsView.Enabled = false;
158        startButton.Enabled = false;
159        stopButton.Enabled = true;
160        resetButton.Enabled = false;
161        UpdateExecutionTimeTextBox();
162      }
163    }
164    protected virtual void Content_Stopped(object sender, EventArgs e) {
165      if (InvokeRequired)
166        Invoke(new EventHandler(Content_Stopped), sender, e);
167      else {
168        parameterCollectionView.Enabled = true;
169        newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = true;
170        problemViewHost.Enabled = true;
171        resultsView.Enabled = true;
172        startButton.Enabled = !Content.Finished;
173        stopButton.Enabled = false;
174        resetButton.Enabled = true;
175        UpdateExecutionTimeTextBox();
176      }
177    }
178    protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
179      executionTimeCounter++;
180      if ((executionTimeCounter == 100) || !Content.Running) {
181        executionTimeCounter = 0;
182        UpdateExecutionTimeTextBox();
183      }
184    }
185    protected virtual void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
186      if (InvokeRequired)
187        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
188      else
189        Auxiliary.ShowErrorMessageBox(e.Value);
190    }
191    #endregion
192
193    #region Button events
194    protected void newProblemButton_Click(object sender, EventArgs e) {
195      if (problemTypeSelectorDialog == null) {
196        problemTypeSelectorDialog = new TypeSelectorDialog();
197        problemTypeSelectorDialog.Caption = "Select Problem";
198        problemTypeSelectorDialog.TypeSelector.Configure(Content.ProblemType, false, false);
199      }
200      if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
201        Content.Problem = (IProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
202      }
203    }
204    protected void openProblemButton_Click(object sender, EventArgs e) {
205      openFileDialog.Title = "Open Problem";
206      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
207        this.Cursor = Cursors.AppStarting;
208        newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = false;
209
210        var call = new Func<string, object>(XmlParser.Deserialize);
211        call.BeginInvoke(openFileDialog.FileName, delegate(IAsyncResult a) {
212          IProblem problem = null;
213          try {
214            problem = call.EndInvoke(a) as IProblem;
215          } catch (Exception ex) {
216            Auxiliary.ShowErrorMessageBox(ex);
217          }
218          Invoke(new Action(delegate() {
219            if (problem == null)
220              MessageBox.Show(this, "The selected file does not contain a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
221            else if (!Content.ProblemType.IsInstanceOfType(problem))
222              MessageBox.Show(this, "The selected file contains a problem type which is not supported by this algorithm.", "Invalid Problem Type", MessageBoxButtons.OK, MessageBoxIcon.Error);
223            else
224              Content.Problem = problem;
225            newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = true;
226            this.Cursor = Cursors.Default;
227          }));
228        }, null);
229      }
230    }
231    protected void saveProblemButton_Click(object sender, EventArgs e) {
232      saveFileDialog.Title = "Save Problem";
233      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
234        this.Cursor = Cursors.AppStarting;
235        newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = false;
236
237        var call = new Action<IProblem, string, int>(XmlGenerator.Serialize);
238        int compression = 9;
239        if (saveFileDialog.FilterIndex == 1) compression = 0;
240        call.BeginInvoke(Content.Problem, saveFileDialog.FileName, compression, delegate(IAsyncResult a) {
241          try {
242            call.EndInvoke(a);
243          }
244          catch (Exception ex) {
245            Auxiliary.ShowErrorMessageBox(ex);
246          }
247          Invoke(new Action(delegate() {
248            newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = true;
249            this.Cursor = Cursors.Default;
250          }));
251        }, null);
252      }
253    }
254    protected virtual void startButton_Click(object sender, EventArgs e) {
255      Content.Start();
256    }
257    protected virtual void stopButton_Click(object sender, EventArgs e) {
258      Content.Stop();
259    }
260    protected virtual void resetButton_Click(object sender, EventArgs e) {
261      Content.Prepare();
262    }
263    #endregion
264
265    #region Helpers
266    protected virtual void UpdateExecutionTimeTextBox() {
267      if (InvokeRequired)
268        Invoke(new Action(UpdateExecutionTimeTextBox));
269      else
270        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
271    }
272    #endregion
273  }
274}
Note: See TracBrowser for help on using the repository browser.