Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed disabling of controls in algorithm views when the algorithm is executed (#905)

File size: 10.8 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      if (Content != null) 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        resultsView.Content = Content.Results;
130        startButton.Enabled = !Content.Finished;
131        UpdateExecutionTimeTextBox();
132      }
133    }
134    protected virtual void Content_ProblemChanged(object sender, EventArgs e) {
135      if (InvokeRequired)
136        Invoke(new EventHandler(Content_ProblemChanged), sender, e);
137      else {
138        problemViewHost.ViewType = null;
139        problemViewHost.Content = Content.Problem;
140        saveProblemButton.Enabled = Content.Problem != null;
141      }
142    }
143    protected virtual void Content_Started(object sender, EventArgs e) {
144      executionTimeCounter = 0;
145      if (InvokeRequired)
146        Invoke(new EventHandler(Content_Started), sender, e);
147      else {
148        SaveEnabled = false;
149        parameterCollectionView.Enabled = false;
150        newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = false;
151        problemViewHost.Enabled = false;
152        resultsView.Enabled = false;
153        startButton.Enabled = false;
154        stopButton.Enabled = true;
155        resetButton.Enabled = false;
156        UpdateExecutionTimeTextBox();
157      }
158    }
159    protected virtual void Content_Stopped(object sender, EventArgs e) {
160      if (InvokeRequired)
161        Invoke(new EventHandler(Content_Stopped), sender, e);
162      else {
163        SaveEnabled = true;
164        parameterCollectionView.Enabled = true;
165        newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = true;
166        problemViewHost.Enabled = true;
167        resultsView.Enabled = true;
168        startButton.Enabled = !Content.Finished;
169        stopButton.Enabled = false;
170        resetButton.Enabled = true;
171        UpdateExecutionTimeTextBox();
172      }
173    }
174    protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
175      executionTimeCounter++;
176      if ((executionTimeCounter == 100) || !Content.Running) {
177        executionTimeCounter = 0;
178        UpdateExecutionTimeTextBox();
179      }
180    }
181    protected virtual void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
182      if (InvokeRequired)
183        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
184      else
185        Auxiliary.ShowErrorMessageBox(e.Value);
186    }
187    #endregion
188
189    #region Button events
190    protected virtual void newProblemButton_Click(object sender, EventArgs e) {
191      if (problemTypeSelectorDialog == null) {
192        problemTypeSelectorDialog = new TypeSelectorDialog();
193        problemTypeSelectorDialog.Caption = "Select Problem";
194        problemTypeSelectorDialog.TypeSelector.Configure(Content.ProblemType, false, false);
195      }
196      if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
197        Content.Problem = (IProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
198      }
199    }
200    protected virtual void openProblemButton_Click(object sender, EventArgs e) {
201      openFileDialog.Title = "Open Problem";
202      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
203        this.Cursor = Cursors.AppStarting;
204        newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = false;
205
206        var call = new Func<string, object>(XmlParser.Deserialize);
207        call.BeginInvoke(openFileDialog.FileName, delegate(IAsyncResult a) {
208          IProblem problem = null;
209          try {
210            problem = call.EndInvoke(a) as IProblem;
211          } catch (Exception ex) {
212            Auxiliary.ShowErrorMessageBox(ex);
213          }
214          Invoke(new Action(delegate() {
215            if (problem == null)
216              MessageBox.Show(this, "The selected file does not contain a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
217            else if (!Content.ProblemType.IsInstanceOfType(problem))
218              MessageBox.Show(this, "The selected file contains a problem type which is not supported by this algorithm.", "Invalid Problem Type", MessageBoxButtons.OK, MessageBoxIcon.Error);
219            else
220              Content.Problem = problem;
221            newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = true;
222            this.Cursor = Cursors.Default;
223          }));
224        }, null);
225      }
226    }
227    protected virtual void saveProblemButton_Click(object sender, EventArgs e) {
228      saveFileDialog.Title = "Save Problem";
229      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
230        this.Cursor = Cursors.AppStarting;
231        newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = false;
232
233        var call = new Action<IProblem, string, int>(XmlGenerator.Serialize);
234        int compression = 9;
235        if (saveFileDialog.FilterIndex == 1) compression = 0;
236        call.BeginInvoke(Content.Problem, saveFileDialog.FileName, compression, delegate(IAsyncResult a) {
237          try {
238            call.EndInvoke(a);
239          }
240          catch (Exception ex) {
241            Auxiliary.ShowErrorMessageBox(ex);
242          }
243          Invoke(new Action(delegate() {
244            newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = true;
245            this.Cursor = Cursors.Default;
246          }));
247        }, null);
248      }
249    }
250    protected virtual void startButton_Click(object sender, EventArgs e) {
251      Content.Start();
252    }
253    protected virtual void stopButton_Click(object sender, EventArgs e) {
254      Content.Stop();
255    }
256    protected virtual void resetButton_Click(object sender, EventArgs e) {
257      Content.Prepare();
258    }
259    #endregion
260
261    #region Helpers
262    protected virtual void UpdateExecutionTimeTextBox() {
263      if (InvokeRequired)
264        Invoke(new Action(UpdateExecutionTimeTextBox));
265      else
266        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
267    }
268    #endregion
269  }
270}
Note: See TracBrowser for help on using the repository browser.