Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/BatchRunView.cs @ 4434

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

Fixed exception which was thrown when hiding views while the execution time is updated (#1148)

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