Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6040 was 5837, checked in by swagner, 14 years ago

Implemented review comments of mkommend (#1112)

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