Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed bugs when quickly stopping, resetting and restarting algorithms (#1027)

File size: 11.8 KB
RevLine 
[2851]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;
[2916]23using System.Collections.Generic;
[2851]24using System.Windows.Forms;
25using HeuristicLab.Common;
[3262]26using HeuristicLab.Core;
[2851]27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
[3758]29using HeuristicLab.PluginInfrastructure;
[2851]30
[2852]31namespace HeuristicLab.Optimization.Views {
[2851]32  /// <summary>
33  /// The base class for visual representations of items.
34  /// </summary>
[2917]35  [View("Algorithm View")]
[2851]36  [Content(typeof(Algorithm), true)]
37  [Content(typeof(IAlgorithm), false)]
38  public partial class AlgorithmView : NamedItemView {
39    private TypeSelectorDialog problemTypeSelectorDialog;
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
[2916]53    protected override void OnInitialized(EventArgs e) {
54      // Set order of tab pages according to z order.
55      // NOTE: This is required due to a bug in the VS designer.
56      List<Control> tabPages = new List<Control>();
57      for (int i = 0; i < tabControl.Controls.Count; i++) {
58        tabPages.Add(tabControl.Controls[i]);
59      }
60      tabControl.Controls.Clear();
61      foreach (Control control in tabPages)
62        tabControl.Controls.Add(control);
63
64      base.OnInitialized(e);
65    }
66
[2851]67    protected override void DeregisterContentEvents() {
68      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
[3262]69      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
[2851]70      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
71      Content.Prepared -= new EventHandler(Content_Prepared);
[4070]72      Content.Started -= new EventHandler(Content_Started);
73      Content.Paused -= new EventHandler(Content_Paused);
74      Content.Stopped -= new EventHandler(Content_Stopped);
[2851]75      Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
76      base.DeregisterContentEvents();
77    }
78    protected override void RegisterContentEvents() {
79      base.RegisterContentEvents();
80      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
[3262]81      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
[2851]82      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
83      Content.Prepared += new EventHandler(Content_Prepared);
[4070]84      Content.Started += new EventHandler(Content_Started);
85      Content.Paused += new EventHandler(Content_Paused);
86      Content.Stopped += new EventHandler(Content_Stopped);
[2851]87      Content.ProblemChanged += new EventHandler(Content_ProblemChanged);
88    }
89
90    protected override void OnContentChanged() {
91      base.OnContentChanged();
92      if (Content == null) {
93        parameterCollectionView.Content = null;
94        problemViewHost.Content = null;
[2882]95        resultsView.Content = null;
[3275]96        runsView.Content = null;
[2851]97        executionTimeTextBox.Text = "-";
98      } else {
99        parameterCollectionView.Content = Content.Parameters;
[2949]100        problemViewHost.ViewType = null;
[2851]101        problemViewHost.Content = Content.Problem;
[3226]102        resultsView.Content = Content.Results.AsReadOnly();
[3275]103        runsView.Content = Content.Runs;
[3262]104        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
[2851]105      }
106    }
107
[3904]108    protected override void SetEnabledStateOfControls() {
109      base.SetEnabledStateOfControls();
[3454]110      parameterCollectionView.Enabled = Content != null;
111      newProblemButton.Enabled = Content != null && !ReadOnly;
112      openProblemButton.Enabled = Content != null && !ReadOnly;
113      problemViewHost.Enabled = Content != null;
114      resultsView.Enabled = Content != null;
115      runsView.Enabled = Content != null;
116      executionTimeTextBox.Enabled = Content != null;
117      SetEnabledStateOfExecutableButtons();
[3367]118    }
119
[2955]120    protected override void OnClosed(FormClosedEventArgs e) {
[3262]121      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
[2955]122      base.OnClosed(e);
123    }
124
[2851]125    #region Content Events
[2998]126    protected virtual void Content_ProblemChanged(object sender, EventArgs e) {
[2851]127      if (InvokeRequired)
128        Invoke(new EventHandler(Content_ProblemChanged), sender, e);
129      else {
[2949]130        problemViewHost.ViewType = null;
[2851]131        problemViewHost.Content = Content.Problem;
132      }
133    }
[4070]134    protected virtual void Content_ExecutionStateChanged(object sender, EventArgs e) {
135      if (InvokeRequired)
136        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
137      else
138        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
139    }
[3262]140    protected virtual void Content_Prepared(object sender, EventArgs e) {
[2851]141      if (InvokeRequired)
[3262]142        Invoke(new EventHandler(Content_Prepared), sender, e);
[4070]143      else {
[3262]144        resultsView.Content = Content.Results.AsReadOnly();
[4070]145        ReadOnly = Locked = false;
146        SetEnabledStateOfExecutableButtons();
147      }
[3262]148    }
[4070]149    protected virtual void Content_Started(object sender, EventArgs e) {
[3262]150      if (InvokeRequired)
[4070]151        Invoke(new EventHandler(Content_Started), sender, e);
[2851]152      else {
[4070]153        ReadOnly = Locked = true;
[3454]154        SetEnabledStateOfExecutableButtons();
[2851]155      }
156    }
[4070]157    protected virtual void Content_Paused(object sender, EventArgs e) {
158      if (InvokeRequired)
159        Invoke(new EventHandler(Content_Paused), sender, e);
160      else {
161        ReadOnly = Locked = false;
162        SetEnabledStateOfExecutableButtons();
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        ReadOnly = Locked = false;
170        SetEnabledStateOfExecutableButtons();
171      }
172    }
[2851]173    protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
[3262]174      if (InvokeRequired)
175        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
176      else
177        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
[2851]178    }
179    protected virtual void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
180      if (InvokeRequired)
181        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
182      else
[3758]183        ErrorHandling.ShowErrorDialog(this, e.Value);
[2851]184    }
185    #endregion
186
[3299]187    #region Control Events
[2998]188    protected virtual void newProblemButton_Click(object sender, EventArgs e) {
[2851]189      if (problemTypeSelectorDialog == null) {
190        problemTypeSelectorDialog = new TypeSelectorDialog();
191        problemTypeSelectorDialog.Caption = "Select Problem";
[3407]192        problemTypeSelectorDialog.TypeSelector.Caption = "Available Problems";
[3588]193        problemTypeSelectorDialog.TypeSelector.Configure(Content.ProblemType, false, true);
[2851]194      }
195      if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
[3407]196        try {
197          Content.Problem = (IProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
198        }
199        catch (Exception ex) {
[3758]200          ErrorHandling.ShowErrorDialog(this, ex);
[3407]201        }
[2851]202      }
203    }
[2998]204    protected virtual void openProblemButton_Click(object sender, EventArgs e) {
[2851]205      openFileDialog.Title = "Open Problem";
206      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
[3454]207        newProblemButton.Enabled = openProblemButton.Enabled = false;
[3177]208        problemViewHost.Enabled = false;
[2954]209
[3500]210        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
[2954]211          try {
[3500]212            if (error != null) throw error;
213            IProblem problem = content as IProblem;
[2954]214            if (problem == null)
[4068]215              Invoke(new Action(() =>
[3548]216                MessageBox.Show(this, "The selected file does not contain a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error)));
[2954]217            else if (!Content.ProblemType.IsInstanceOfType(problem))
[3548]218              Invoke(new Action(() =>
219                MessageBox.Show(this, "The selected file contains a problem type which is not supported by this algorithm.", "Invalid Problem Type", MessageBoxButtons.OK, MessageBoxIcon.Error)));
[2954]220            else
221              Content.Problem = problem;
[3500]222          }
223          catch (Exception ex) {
[3758]224            Invoke(new Action(() => ErrorHandling.ShowErrorDialog(this, ex)));
[3500]225          }
226          finally {
227            Invoke(new Action(delegate() {
228              problemViewHost.Enabled = true;
229              newProblemButton.Enabled = openProblemButton.Enabled = true;
230            }));
231          }
232        });
[2851]233      }
234    }
235    protected virtual void startButton_Click(object sender, EventArgs e) {
236      Content.Start();
237    }
[3262]238    protected virtual void pauseButton_Click(object sender, EventArgs e) {
239      Content.Pause();
240    }
[2851]241    protected virtual void stopButton_Click(object sender, EventArgs e) {
242      Content.Stop();
243    }
244    protected virtual void resetButton_Click(object sender, EventArgs e) {
[3716]245      Content.Prepare(false);
[2851]246    }
[4011]247    protected virtual void problemViewHost_DragEnterOver(object sender, DragEventArgs e) {
[3299]248      e.Effect = DragDropEffects.None;
249      Type type = e.Data.GetData("Type") as Type;
250      if ((type != null) && (Content.ProblemType.IsAssignableFrom(type))) {
[3694]251        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[3299]252        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
253        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
254        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
[3526]255        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
[3299]256      }
257    }
[4011]258    protected virtual void problemViewHost_DragDrop(object sender, DragEventArgs e) {
[3299]259      if (e.Effect != DragDropEffects.None) {
260        IProblem problem = e.Data.GetData("Value") as IProblem;
261        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) problem = (IProblem)problem.Clone();
262        Content.Problem = problem;
263      }
264    }
[2851]265    #endregion
266
267    #region Helpers
[3454]268    private void SetEnabledStateOfExecutableButtons() {
269      if (Content == null) {
270        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
271      } else {
272        startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
273        pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
274        stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
275        resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
276      }
[2851]277    }
278    #endregion
279  }
280}
Note: See TracBrowser for help on using the repository browser.