Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented ErrorDialog and OperatorExecutionException (#1007)

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