Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2958 was 2958, checked in by swagner, 15 years ago

Implemented enabling and disabling of save buttons and menu items to prevent saving of running algorithms (#685)

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