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
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.PluginInfrastructure;
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() {
52      Content.AlgorithmChanged -= new EventHandler(Content_AlgorithmChanged);
53      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
54      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
55      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
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);
60      Content.RepetitionsChanged -= new EventHandler(Content_RepetitionsChanged);
61      base.DeregisterContentEvents();
62    }
63    protected override void RegisterContentEvents() {
64      base.RegisterContentEvents();
65      Content.AlgorithmChanged += new EventHandler(Content_AlgorithmChanged);
66      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
67      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
68      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
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);
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;
81        runsView.Content = null;
82        executionTimeTextBox.Text = "-";
83      } else {
84        repetitionsNumericUpDown.Value = Content.Repetitions;
85        algorithmViewHost.Content = Content.Algorithm;
86        runsView.Content = Content.Runs;
87        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
88      }
89    }
90    protected override void SetEnabledStateOfControls() {
91      base.SetEnabledStateOfControls();
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();
99    }
100
101    protected override void OnClosed(FormClosedEventArgs e) {
102      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
103      base.OnClosed(e);
104    }
105
106    #region Content Events
107    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
108      if (InvokeRequired)
109        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
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);
116      else {
117        ReadOnly = Locked = false;
118        SetEnabledStateOfExecutableButtons();
119      }
120    }
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    }
145    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
146      if (InvokeRequired)
147        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
148      else
149        executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
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
155        ErrorHandling.ShowErrorDialog(this, e.Value);
156    }
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    }
164    private void Content_RepetitionsChanged(object sender, EventArgs e) {
165      if (InvokeRequired)
166        Invoke(new EventHandler(Content_RepetitionsChanged), sender, e);
167      else
168        repetitionsNumericUpDown.Value = Content.Repetitions;
169    }
170    #endregion
171
172    #region Control events
173    private void repetitionsNumericUpDown_Validated(object sender, System.EventArgs e) {
174      if (repetitionsNumericUpDown.Text == string.Empty)
175        repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
176    }
177    private void repetitionsNumericUpDown_ValueChanged(object sender, EventArgs e) {
178      if (Content != null)
179        Content.Repetitions = (int)repetitionsNumericUpDown.Value;
180    }
181    private void newAlgorithmButton_Click(object sender, EventArgs e) {
182      if (algorithmTypeSelectorDialog == null) {
183        algorithmTypeSelectorDialog = new TypeSelectorDialog();
184        algorithmTypeSelectorDialog.Caption = "Select Algorithm";
185        algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithms";
186        algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, true);
187      }
188      if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
189        try {
190          Content.Algorithm = (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
191        }
192        catch (Exception ex) {
193          ErrorHandling.ShowErrorDialog(this, ex);
194        }
195      }
196    }
197    private void openAlgorithmButton_Click(object sender, EventArgs e) {
198      openFileDialog.Title = "Open Algorithm";
199      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
200        newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = false;
201        algorithmViewHost.Enabled = false;
202
203        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
204          try {
205            if (error != null) throw error;
206            IAlgorithm algorithm = content as IAlgorithm;
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;
211          }
212          catch (Exception ex) {
213            ErrorHandling.ShowErrorDialog(this, ex);
214          }
215          finally {
216            Invoke(new Action(delegate() {
217              algorithmViewHost.Enabled = true;
218              newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = true;
219            }));
220          }
221        });
222      }
223    }
224    private void startButton_Click(object sender, EventArgs e) {
225      Content.Start();
226    }
227    private void pauseButton_Click(object sender, EventArgs e) {
228      Content.Pause();
229    }
230    private void stopButton_Click(object sender, EventArgs e) {
231      Content.Stop();
232    }
233    private void resetButton_Click(object sender, EventArgs e) {
234      Content.Prepare(false);
235    }
236    private void algorithmViewHost_DragEnterOver(object sender, DragEventArgs e) {
237      e.Effect = DragDropEffects.None;
238      if (ReadOnly)
239        return;
240      Type type = e.Data.GetData("Type") as Type;
241      if ((type != null) && (typeof(IAlgorithm).IsAssignableFrom(type))) {
242        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
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;
246        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
247      }
248    }
249    private void algorithmViewHost_DragDrop(object sender, DragEventArgs e) {
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    }
256    #endregion
257
258    #region Helpers
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      }
268    }
269    #endregion
270  }
271}
Note: See TracBrowser for help on using the repository browser.