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
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;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29using HeuristicLab.Persistence.Default.Xml;
30using HeuristicLab.PluginInfrastructure;
31
32namespace HeuristicLab.Optimization.Views {
33  /// <summary>
34  /// The base class for visual representations of items.
35  /// </summary>
36  [View("Algorithm View")]
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
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
68    protected override void DeregisterContentEvents() {
69      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
70      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
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);
79      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
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;
90        resultsView.Content = null;
91        runsView.Content = null;
92        executionTimeTextBox.Text = "-";
93      } else {
94        parameterCollectionView.Content = Content.Parameters;
95        problemViewHost.ViewType = null;
96        problemViewHost.Content = Content.Problem;
97        resultsView.Content = Content.Results.AsReadOnly();
98        runsView.Content = Content.Runs;
99        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
100      }
101      SetEnableStateOfControls();
102    }
103
104    protected override void OnReadOnlyChanged() {
105      base.OnReadOnlyChanged();
106      SetEnableStateOfControls();
107    }
108    private void SetEnableStateOfControls() {
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();
117    }
118
119    protected override void OnClosed(FormClosedEventArgs e) {
120      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
121      base.OnClosed(e);
122    }
123
124    #region Content Events
125    protected virtual void Content_ProblemChanged(object sender, EventArgs e) {
126      if (InvokeRequired)
127        Invoke(new EventHandler(Content_ProblemChanged), sender, e);
128      else {
129        problemViewHost.ViewType = null;
130        problemViewHost.Content = Content.Problem;
131      }
132    }
133    protected virtual void Content_Prepared(object sender, EventArgs e) {
134      if (InvokeRequired)
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);
142      else {
143        ReadOnly = Content.ExecutionState == ExecutionState.Started;
144        Locked = Content.ExecutionState == ExecutionState.Started;
145        SetEnabledStateOfExecutableButtons();
146      }
147    }
148    protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
149      if (InvokeRequired)
150        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
151      else
152        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
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
158        ErrorHandling.ShowErrorDialog(this, e.Value);
159    }
160    #endregion
161
162    #region Control Events
163    protected virtual void newProblemButton_Click(object sender, EventArgs e) {
164      if (problemTypeSelectorDialog == null) {
165        problemTypeSelectorDialog = new TypeSelectorDialog();
166        problemTypeSelectorDialog.Caption = "Select Problem";
167        problemTypeSelectorDialog.TypeSelector.Caption = "Available Problems";
168        problemTypeSelectorDialog.TypeSelector.Configure(Content.ProblemType, false, true);
169      }
170      if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
171        try {
172          Content.Problem = (IProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
173        }
174        catch (Exception ex) {
175          ErrorHandling.ShowErrorDialog(this, ex);
176        }
177      }
178    }
179    protected virtual void openProblemButton_Click(object sender, EventArgs e) {
180      openFileDialog.Title = "Open Problem";
181      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
182        newProblemButton.Enabled = openProblemButton.Enabled = false;
183        problemViewHost.Enabled = false;
184
185        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
186          try {
187            if (error != null) throw error;
188            IProblem problem = content as IProblem;
189            if (problem == null)
190              Invoke(new Action(() =>
191                MessageBox.Show(this, "The selected file does not contain a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error)));
192            else if (!Content.ProblemType.IsInstanceOfType(problem))
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)));
195            else
196              Content.Problem = problem;
197          }
198          catch (Exception ex) {
199            Invoke(new Action(() => ErrorHandling.ShowErrorDialog(this, ex)));
200          }
201          finally {
202            Invoke(new Action(delegate() {
203              problemViewHost.Enabled = true;
204              newProblemButton.Enabled = openProblemButton.Enabled = true;
205            }));
206          }
207        });
208      }
209    }
210    protected virtual void startButton_Click(object sender, EventArgs e) {
211      Content.Start();
212    }
213    protected virtual void pauseButton_Click(object sender, EventArgs e) {
214      Content.Pause();
215    }
216    protected virtual void stopButton_Click(object sender, EventArgs e) {
217      Content.Stop();
218    }
219    protected virtual void resetButton_Click(object sender, EventArgs e) {
220      Content.Prepare(false);
221    }
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))) {
226        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
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;
230        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
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    }
240    #endregion
241
242    #region Helpers
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      }
252    }
253    #endregion
254  }
255}
Note: See TracBrowser for help on using the repository browser.