Free cookie consent management tool by TermsFeed Policy Generator

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

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

Enabled batch runs to deal with optimizers (#1378)

File size: 11.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.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 optimizerTypeSelectorDialog;
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 Dispose(bool disposing) {
52      if (disposing) {
53        if (optimizerTypeSelectorDialog != null) optimizerTypeSelectorDialog.Dispose();
54        if (components != null) components.Dispose();
55      }
56      base.Dispose(disposing);
57    }
58
59    protected override void DeregisterContentEvents() {
60      Content.OptimizerChanged -= new EventHandler(Content_OptimizerChanged);
61      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
62      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
63      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
64      Content.Prepared -= new EventHandler(Content_Prepared);
65      Content.Started -= new EventHandler(Content_Started);
66      Content.Paused -= new EventHandler(Content_Paused);
67      Content.Stopped -= new EventHandler(Content_Stopped);
68      Content.RepetitionsChanged -= new EventHandler(Content_RepetitionsChanged);
69      base.DeregisterContentEvents();
70    }
71    protected override void RegisterContentEvents() {
72      base.RegisterContentEvents();
73      Content.OptimizerChanged += new EventHandler(Content_OptimizerChanged);
74      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
75      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
76      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
77      Content.Prepared += new EventHandler(Content_Prepared);
78      Content.Started += new EventHandler(Content_Started);
79      Content.Paused += new EventHandler(Content_Paused);
80      Content.Stopped += new EventHandler(Content_Stopped);
81      Content.RepetitionsChanged += new EventHandler(Content_RepetitionsChanged);
82    }
83
84    protected override void OnContentChanged() {
85      base.OnContentChanged();
86      if (Content == null) {
87        repetitionsNumericUpDown.Value = 1;
88        optimizerViewHost.Content = null;
89        runsView.Content = null;
90        executionTimeTextBox.Text = "-";
91      } else {
92        Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started;
93        repetitionsNumericUpDown.Value = Content.Repetitions;
94        optimizerViewHost.Content = Content.Optimizer;
95        runsView.Content = Content.Runs;
96        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
97      }
98    }
99    protected override void SetEnabledStateOfControls() {
100      base.SetEnabledStateOfControls();
101      repetitionsNumericUpDown.Enabled = Content != null && !ReadOnly;
102      newOptimizerButton.Enabled = Content != null && !ReadOnly;
103      openOptimizerButton.Enabled = Content != null && !ReadOnly;
104      optimizerViewHost.Enabled = Content != null;
105      runsView.Enabled = Content != null;
106      executionTimeTextBox.Enabled = Content != null;
107      SetEnabledStateOfExecutableButtons();
108    }
109
110    protected override void OnClosed(FormClosedEventArgs e) {
111      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
112      base.OnClosed(e);
113    }
114
115    #region Content Events
116    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
117      if (InvokeRequired)
118        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
119      else
120        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
121    }
122    private void Content_Prepared(object sender, EventArgs e) {
123      if (InvokeRequired)
124        Invoke(new EventHandler(Content_Prepared), sender, e);
125      else {
126        ReadOnly = Locked = false;
127        SetEnabledStateOfExecutableButtons();
128      }
129    }
130    private void Content_Started(object sender, EventArgs e) {
131      if (InvokeRequired)
132        Invoke(new EventHandler(Content_Started), sender, e);
133      else {
134        ReadOnly = Locked = true;
135        SetEnabledStateOfExecutableButtons();
136      }
137    }
138    private void Content_Paused(object sender, EventArgs e) {
139      if (InvokeRequired)
140        Invoke(new EventHandler(Content_Paused), sender, e);
141      else {
142        ReadOnly = Locked = false;
143        SetEnabledStateOfExecutableButtons();
144      }
145    }
146    private void Content_Stopped(object sender, EventArgs e) {
147      if (InvokeRequired)
148        Invoke(new EventHandler(Content_Stopped), sender, e);
149      else {
150        ReadOnly = Locked = false;
151        SetEnabledStateOfExecutableButtons();
152      }
153    }
154    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
155      if (InvokeRequired)
156        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
157      else
158        executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
159    }
160    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
161      if (InvokeRequired)
162        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
163      else
164        ErrorHandling.ShowErrorDialog(this, e.Value);
165    }
166    private void Content_OptimizerChanged(object sender, EventArgs e) {
167      if (InvokeRequired)
168        Invoke(new EventHandler(Content_OptimizerChanged), sender, e);
169      else {
170        optimizerViewHost.Content = Content.Optimizer;
171      }
172    }
173    private void Content_RepetitionsChanged(object sender, EventArgs e) {
174      if (InvokeRequired)
175        Invoke(new EventHandler(Content_RepetitionsChanged), sender, e);
176      else
177        repetitionsNumericUpDown.Value = Content.Repetitions;
178    }
179    #endregion
180
181    #region Control events
182    private void repetitionsNumericUpDown_Validated(object sender, System.EventArgs e) {
183      if (repetitionsNumericUpDown.Text == string.Empty)
184        repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
185    }
186    private void repetitionsNumericUpDown_ValueChanged(object sender, EventArgs e) {
187      if (Content != null)
188        Content.Repetitions = (int)repetitionsNumericUpDown.Value;
189    }
190    private void newOptimizerButton_Click(object sender, EventArgs e) {
191      if (optimizerTypeSelectorDialog == null) {
192        optimizerTypeSelectorDialog = new TypeSelectorDialog();
193        optimizerTypeSelectorDialog.Caption = "Select Optimizer";
194        optimizerTypeSelectorDialog.TypeSelector.Caption = "Available Optimizers";
195        optimizerTypeSelectorDialog.TypeSelector.Configure(typeof(IOptimizer), false, true);
196      }
197      if (optimizerTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
198        try {
199          Content.Optimizer = (IOptimizer)optimizerTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
200        }
201        catch (Exception ex) {
202          ErrorHandling.ShowErrorDialog(this, ex);
203        }
204      }
205    }
206    private void openOptimizerButton_Click(object sender, EventArgs e) {
207      openFileDialog.Title = "Open Optimizer";
208      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
209        newOptimizerButton.Enabled = openOptimizerButton.Enabled = false;
210        optimizerViewHost.Enabled = false;
211
212        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
213          try {
214            if (error != null) throw error;
215            IOptimizer optimizer = content as IOptimizer;
216            if (optimizer == null)
217              MessageBox.Show(this, "The selected file does not contain an optimizer.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
218            else
219              Content.Optimizer = optimizer;
220          }
221          catch (Exception ex) {
222            ErrorHandling.ShowErrorDialog(this, ex);
223          }
224          finally {
225            Invoke(new Action(delegate() {
226              optimizerViewHost.Enabled = true;
227              newOptimizerButton.Enabled = openOptimizerButton.Enabled = true;
228            }));
229          }
230        });
231      }
232    }
233    private void startButton_Click(object sender, EventArgs e) {
234      Content.Start();
235    }
236    private void pauseButton_Click(object sender, EventArgs e) {
237      Content.Pause();
238    }
239    private void stopButton_Click(object sender, EventArgs e) {
240      Content.Stop();
241    }
242    private void resetButton_Click(object sender, EventArgs e) {
243      Content.Prepare(false);
244    }
245    private void optimizerTabPage_DragEnterOver(object sender, DragEventArgs e) {
246      e.Effect = DragDropEffects.None;
247      if (ReadOnly)
248        return;
249      Type type = e.Data.GetData("Type") as Type;
250      if ((type != null) && (typeof(IOptimizer).IsAssignableFrom(type))) {
251        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
252        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
253        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
254        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
255        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
256      }
257    }
258    private void optimizerTabPage_DragDrop(object sender, DragEventArgs e) {
259      if (e.Effect != DragDropEffects.None) {
260        IOptimizer optimizer = e.Data.GetData("Value") as IOptimizer;
261        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) optimizer = (IOptimizer)optimizer.Clone();
262        Content.Optimizer = optimizer;
263      }
264    }
265    #endregion
266
267    #region Helpers
268    private void SetEnabledStateOfExecutableButtons() {
269      if (Content == null) {
270        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
271      } else {
272        startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
273        pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
274        stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
275        resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
276      }
277    }
278    #endregion
279  }
280}
Note: See TracBrowser for help on using the repository browser.