Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/BatchRunView.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.1 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.Windows.Forms;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28using HeuristicLab.Persistence.Default.Xml;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Optimization.Views {
32  /// <summary>
33  /// The base class for visual representations of items.
34  /// </summary>
35  [View("BatchRun View")]
36  [Content(typeof(BatchRun), true)]
37  public sealed partial class BatchRunView : NamedItemView {
38    private TypeSelectorDialog algorithmTypeSelectorDialog;
39
40    public new BatchRun Content {
41      get { return (BatchRun)base.Content; }
42      set { base.Content = value; }
43    }
44
45    /// <summary>
46    /// Initializes a new instance of <see cref="ItemBaseView"/>.
47    /// </summary>
48    public BatchRunView() {
49      InitializeComponent();
50    }
51
52    protected override void DeregisterContentEvents() {
53      Content.AlgorithmChanged -= new EventHandler(Content_AlgorithmChanged);
54      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
55      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
56      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
57      Content.RepetitionsChanged -= new EventHandler(Content_RepetitionsChanged);
58      base.DeregisterContentEvents();
59    }
60    protected override void RegisterContentEvents() {
61      base.RegisterContentEvents();
62      Content.AlgorithmChanged += new EventHandler(Content_AlgorithmChanged);
63      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
64      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
65      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
66      Content.RepetitionsChanged += new EventHandler(Content_RepetitionsChanged);
67    }
68
69    protected override void OnContentChanged() {
70      base.OnContentChanged();
71      if (Content == null) {
72        repetitionsNumericUpDown.Value = 1;
73        algorithmViewHost.Content = null;
74        runsView.Content = null;
75        executionTimeTextBox.Text = "-";
76      } else {
77        repetitionsNumericUpDown.Value = Content.Repetitions;
78        algorithmViewHost.Content = Content.Algorithm;
79        runsView.Content = Content.Runs;
80        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
81      }
82      SetEnableStateOfControls();
83    }
84    protected override void OnReadOnlyChanged() {
85      base.OnReadOnlyChanged();
86      SetEnableStateOfControls();
87    }
88    private void SetEnableStateOfControls() {
89      repetitionsNumericUpDown.Enabled = Content != null && !ReadOnly;
90      newAlgorithmButton.Enabled = Content != null && !ReadOnly;
91      openAlgorithmButton.Enabled = Content != null && !ReadOnly;
92      algorithmViewHost.Enabled = Content != null;
93      runsView.Enabled = Content != null;
94      executionTimeTextBox.Enabled = Content != null;
95      SetEnabledStateOfExecutableButtons();
96    }
97
98    protected override void OnClosed(FormClosedEventArgs e) {
99      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
100      base.OnClosed(e);
101    }
102
103    #region Content Events
104    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
105      if (InvokeRequired)
106        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
107      else {
108        this.ReadOnly = Content.ExecutionState == ExecutionState.Started;
109        Locked = Content.ExecutionState == ExecutionState.Started;
110        SetEnabledStateOfExecutableButtons();
111      }
112    }
113    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
114      if (InvokeRequired)
115        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
116      else
117        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
118    }
119    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
120      if (InvokeRequired)
121        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
122      else
123        ErrorHandling.ShowErrorDialog(this, e.Value);
124    }
125    private void Content_AlgorithmChanged(object sender, EventArgs e) {
126      if (InvokeRequired)
127        Invoke(new EventHandler(Content_AlgorithmChanged), sender, e);
128      else {
129        algorithmViewHost.Content = Content.Algorithm;
130      }
131    }
132    private void Content_RepetitionsChanged(object sender, EventArgs e) {
133      if (InvokeRequired)
134        Invoke(new EventHandler(Content_RepetitionsChanged), sender, e);
135      else
136        repetitionsNumericUpDown.Value = Content.Repetitions;
137    }
138    #endregion
139
140    #region Control events
141    private void repetitionsNumericUpDown_Validated(object sender, System.EventArgs e) {
142      if (repetitionsNumericUpDown.Text == string.Empty)
143        repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
144    }
145    private void repetitionsNumericUpDown_ValueChanged(object sender, EventArgs e) {
146      Content.Repetitions = (int)repetitionsNumericUpDown.Value;
147    }
148    private void newAlgorithmButton_Click(object sender, EventArgs e) {
149      if (algorithmTypeSelectorDialog == null) {
150        algorithmTypeSelectorDialog = new TypeSelectorDialog();
151        algorithmTypeSelectorDialog.Caption = "Select Algorithm";
152        algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithms";
153        algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, true);
154      }
155      if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
156        try {
157          Content.Algorithm = (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
158        }
159        catch (Exception ex) {
160          ErrorHandling.ShowErrorDialog(this, ex);
161        }
162      }
163    }
164    private void openAlgorithmButton_Click(object sender, EventArgs e) {
165      openFileDialog.Title = "Open Algorithm";
166      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
167        newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = false;
168        algorithmViewHost.Enabled = false;
169
170        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
171          try {
172            if (error != null) throw error;
173            IAlgorithm algorithm = content as IAlgorithm;
174            if (algorithm == null)
175              MessageBox.Show(this, "The selected file does not contain an algorithm.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
176            else
177              Content.Algorithm = algorithm;
178          }
179          catch (Exception ex) {
180            ErrorHandling.ShowErrorDialog(this, ex);
181          }
182          finally {
183            Invoke(new Action(delegate() {
184              algorithmViewHost.Enabled = true;
185              newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = true;
186            }));
187          }
188        });
189      }
190    }
191    private void startButton_Click(object sender, EventArgs e) {
192      Content.Start();
193    }
194    private void pauseButton_Click(object sender, EventArgs e) {
195      Content.Pause();
196    }
197    private void stopButton_Click(object sender, EventArgs e) {
198      Content.Stop();
199    }
200    private void resetButton_Click(object sender, EventArgs e) {
201      Content.Prepare(false);
202    }
203    private void algorithmPanel_DragEnterOver(object sender, DragEventArgs e) {
204      e.Effect = DragDropEffects.None;
205      if (ReadOnly)
206        return;
207      Type type = e.Data.GetData("Type") as Type;
208      if ((type != null) && (typeof(IAlgorithm).IsAssignableFrom(type))) {
209        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
210        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
211        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
212        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
213        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
214      }
215    }
216    private void algorithmPanel_DragDrop(object sender, DragEventArgs e) {
217      if (e.Effect != DragDropEffects.None) {
218        IAlgorithm algorithm = e.Data.GetData("Value") as IAlgorithm;
219        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) algorithm = (IAlgorithm)algorithm.Clone();
220        Content.Algorithm = algorithm;
221      }
222    }
223    #endregion
224
225    #region Helpers
226    private void SetEnabledStateOfExecutableButtons() {
227      if (Content == null) {
228        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
229      } else {
230        startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
231        pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
232        stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
233        resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
234      }
235    }
236    #endregion
237  }
238}
Note: See TracBrowser for help on using the repository browser.