Free cookie consent management tool by TermsFeed Policy Generator

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

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

Enabled batch runs to deal with optimizers (#1378)

File size: 11.8 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 {
[5300]37    private TypeSelectorDialog optimizerTypeSelectorDialog;
[3226]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
[5237]51    protected override void Dispose(bool disposing) {
52      if (disposing) {
[5300]53        if (optimizerTypeSelectorDialog != null) optimizerTypeSelectorDialog.Dispose();
[5237]54        if (components != null) components.Dispose();
55      }
56      base.Dispose(disposing);
57    }
58
[3226]59    protected override void DeregisterContentEvents() {
[5300]60      Content.OptimizerChanged -= new EventHandler(Content_OptimizerChanged);
[3226]61      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
[3265]62      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
[3226]63      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
[4070]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);
[3226]68      Content.RepetitionsChanged -= new EventHandler(Content_RepetitionsChanged);
69      base.DeregisterContentEvents();
70    }
71    protected override void RegisterContentEvents() {
72      base.RegisterContentEvents();
[5300]73      Content.OptimizerChanged += new EventHandler(Content_OptimizerChanged);
[3226]74      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
[3265]75      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
[3226]76      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
[4070]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);
[3226]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;
[5300]88        optimizerViewHost.Content = null;
[3260]89        runsView.Content = null;
[3226]90        executionTimeTextBox.Text = "-";
91      } else {
[4540]92        Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started;
[3226]93        repetitionsNumericUpDown.Value = Content.Repetitions;
[5300]94        optimizerViewHost.Content = Content.Optimizer;
[3260]95        runsView.Content = Content.Runs;
[3265]96        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
[3226]97      }
98    }
[3904]99    protected override void SetEnabledStateOfControls() {
100      base.SetEnabledStateOfControls();
[3454]101      repetitionsNumericUpDown.Enabled = Content != null && !ReadOnly;
[5300]102      newOptimizerButton.Enabled = Content != null && !ReadOnly;
103      openOptimizerButton.Enabled = Content != null && !ReadOnly;
104      optimizerViewHost.Enabled = Content != null;
[3454]105      runsView.Enabled = Content != null;
106      executionTimeTextBox.Enabled = Content != null;
107      SetEnabledStateOfExecutableButtons();
[3367]108    }
[3226]109
110    protected override void OnClosed(FormClosedEventArgs e) {
[3265]111      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
[3226]112      base.OnClosed(e);
113    }
114
115    #region Content Events
[3265]116    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
[3226]117      if (InvokeRequired)
[3265]118        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
[4070]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);
[3226]125      else {
[4070]126        ReadOnly = Locked = false;
[3454]127        SetEnabledStateOfExecutableButtons();
[3226]128      }
129    }
[4070]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    }
[3265]154    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
[3226]155      if (InvokeRequired)
[3265]156        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
157      else
[4216]158        executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
[3226]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
[3758]164        ErrorHandling.ShowErrorDialog(this, e.Value);
[3226]165    }
[5300]166    private void Content_OptimizerChanged(object sender, EventArgs e) {
[3265]167      if (InvokeRequired)
[5300]168        Invoke(new EventHandler(Content_OptimizerChanged), sender, e);
[3265]169      else {
[5300]170        optimizerViewHost.Content = Content.Optimizer;
[3265]171      }
172    }
[3226]173    private void Content_RepetitionsChanged(object sender, EventArgs e) {
174      if (InvokeRequired)
175        Invoke(new EventHandler(Content_RepetitionsChanged), sender, e);
[3265]176      else
[3226]177        repetitionsNumericUpDown.Value = Content.Repetitions;
178    }
179    #endregion
180
181    #region Control events
[3265]182    private void repetitionsNumericUpDown_Validated(object sender, System.EventArgs e) {
183      if (repetitionsNumericUpDown.Text == string.Empty)
184        repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
185    }
[3226]186    private void repetitionsNumericUpDown_ValueChanged(object sender, EventArgs e) {
[4011]187      if (Content != null)
188        Content.Repetitions = (int)repetitionsNumericUpDown.Value;
[3226]189    }
[5300]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);
[3226]196      }
[5300]197      if (optimizerTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
[3407]198        try {
[5300]199          Content.Optimizer = (IOptimizer)optimizerTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
[3407]200        }
201        catch (Exception ex) {
[3758]202          ErrorHandling.ShowErrorDialog(this, ex);
[3407]203        }
[3226]204      }
205    }
[5300]206    private void openOptimizerButton_Click(object sender, EventArgs e) {
207      openFileDialog.Title = "Open Optimizer";
[3226]208      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
[5300]209        newOptimizerButton.Enabled = openOptimizerButton.Enabled = false;
210        optimizerViewHost.Enabled = false;
[3226]211
[3500]212        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
[3226]213          try {
[3500]214            if (error != null) throw error;
[5300]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);
[3226]218            else
[5300]219              Content.Optimizer = optimizer;
[3500]220          }
221          catch (Exception ex) {
[3758]222            ErrorHandling.ShowErrorDialog(this, ex);
[3500]223          }
224          finally {
225            Invoke(new Action(delegate() {
[5300]226              optimizerViewHost.Enabled = true;
227              newOptimizerButton.Enabled = openOptimizerButton.Enabled = true;
[3500]228            }));
229          }
230        });
[3226]231      }
232    }
233    private void startButton_Click(object sender, EventArgs e) {
234      Content.Start();
235    }
[3265]236    private void pauseButton_Click(object sender, EventArgs e) {
237      Content.Pause();
238    }
[3226]239    private void stopButton_Click(object sender, EventArgs e) {
240      Content.Stop();
241    }
242    private void resetButton_Click(object sender, EventArgs e) {
[3716]243      Content.Prepare(false);
[3226]244    }
[5300]245    private void optimizerTabPage_DragEnterOver(object sender, DragEventArgs e) {
[3299]246      e.Effect = DragDropEffects.None;
[3367]247      if (ReadOnly)
248        return;
[3299]249      Type type = e.Data.GetData("Type") as Type;
[5300]250      if ((type != null) && (typeof(IOptimizer).IsAssignableFrom(type))) {
[3694]251        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[3299]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;
[3526]255        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
[3299]256      }
257    }
[5300]258    private void optimizerTabPage_DragDrop(object sender, DragEventArgs e) {
[3299]259      if (e.Effect != DragDropEffects.None) {
[5300]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;
[3299]263      }
264    }
[3226]265    #endregion
266
267    #region Helpers
[3454]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      }
[3226]277    }
278    #endregion
279  }
280}
Note: See TracBrowser for help on using the repository browser.