Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3975 was 3904, checked in by mkommend, 14 years ago

Added SetEnabledStateOfControls as protected virtual method in !View. Therefore the overloading of OnReadOnlyChanged and OnLockedChanged got obsolete in most views, because the method got called in the !View respectively ContentView. (ticket #1021)

File size: 10.0 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;
28using HeuristicLab.Persistence.Default.Xml;
[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 {
38    private TypeSelectorDialog algorithmTypeSelectorDialog;
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
52    protected override void DeregisterContentEvents() {
[3265]53      Content.AlgorithmChanged -= new EventHandler(Content_AlgorithmChanged);
[3226]54      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
[3265]55      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
[3226]56      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
57      Content.RepetitionsChanged -= new EventHandler(Content_RepetitionsChanged);
58      base.DeregisterContentEvents();
59    }
60    protected override void RegisterContentEvents() {
61      base.RegisterContentEvents();
[3265]62      Content.AlgorithmChanged += new EventHandler(Content_AlgorithmChanged);
[3226]63      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
[3265]64      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
[3226]65      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
66      Content.RepetitionsChanged += new EventHandler(Content_RepetitionsChanged);
67    }
68
69    protected override void OnContentChanged() {
70      base.OnContentChanged();
71      if (Content == null) {
72        repetitionsNumericUpDown.Value = 1;
73        algorithmViewHost.Content = null;
[3260]74        runsView.Content = null;
[3226]75        executionTimeTextBox.Text = "-";
76      } else {
77        repetitionsNumericUpDown.Value = Content.Repetitions;
78        algorithmViewHost.Content = Content.Algorithm;
[3260]79        runsView.Content = Content.Runs;
[3265]80        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
[3226]81      }
82    }
[3904]83    protected override void SetEnabledStateOfControls() {
84      base.SetEnabledStateOfControls();
[3454]85      repetitionsNumericUpDown.Enabled = Content != null && !ReadOnly;
86      newAlgorithmButton.Enabled = Content != null && !ReadOnly;
87      openAlgorithmButton.Enabled = Content != null && !ReadOnly;
88      algorithmViewHost.Enabled = Content != null;
89      runsView.Enabled = Content != null;
90      executionTimeTextBox.Enabled = Content != null;
91      SetEnabledStateOfExecutableButtons();
[3367]92    }
[3226]93
94    protected override void OnClosed(FormClosedEventArgs e) {
[3265]95      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
[3226]96      base.OnClosed(e);
97    }
98
99    #region Content Events
[3265]100    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
[3226]101      if (InvokeRequired)
[3265]102        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
[3226]103      else {
[3367]104        this.ReadOnly = Content.ExecutionState == ExecutionState.Started;
[3416]105        Locked = Content.ExecutionState == ExecutionState.Started;
[3454]106        SetEnabledStateOfExecutableButtons();
[3226]107      }
108    }
[3265]109    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
[3226]110      if (InvokeRequired)
[3265]111        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
112      else
113        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
[3226]114    }
115    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
116      if (InvokeRequired)
117        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
118      else
[3758]119        ErrorHandling.ShowErrorDialog(this, e.Value);
[3226]120    }
[3265]121    private void Content_AlgorithmChanged(object sender, EventArgs e) {
122      if (InvokeRequired)
123        Invoke(new EventHandler(Content_AlgorithmChanged), sender, e);
124      else {
125        algorithmViewHost.Content = Content.Algorithm;
126      }
127    }
[3226]128    private void Content_RepetitionsChanged(object sender, EventArgs e) {
129      if (InvokeRequired)
130        Invoke(new EventHandler(Content_RepetitionsChanged), sender, e);
[3265]131      else
[3226]132        repetitionsNumericUpDown.Value = Content.Repetitions;
133    }
134    #endregion
135
136    #region Control events
[3265]137    private void repetitionsNumericUpDown_Validated(object sender, System.EventArgs e) {
138      if (repetitionsNumericUpDown.Text == string.Empty)
139        repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
140    }
[3226]141    private void repetitionsNumericUpDown_ValueChanged(object sender, EventArgs e) {
142      Content.Repetitions = (int)repetitionsNumericUpDown.Value;
143    }
144    private void newAlgorithmButton_Click(object sender, EventArgs e) {
145      if (algorithmTypeSelectorDialog == null) {
146        algorithmTypeSelectorDialog = new TypeSelectorDialog();
147        algorithmTypeSelectorDialog.Caption = "Select Algorithm";
[3407]148        algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithms";
[3588]149        algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, true);
[3226]150      }
151      if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
[3407]152        try {
153          Content.Algorithm = (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
154        }
155        catch (Exception ex) {
[3758]156          ErrorHandling.ShowErrorDialog(this, ex);
[3407]157        }
[3226]158      }
159    }
160    private void openAlgorithmButton_Click(object sender, EventArgs e) {
161      openFileDialog.Title = "Open Algorithm";
162      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
[3454]163        newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = false;
[3226]164        algorithmViewHost.Enabled = false;
165
[3500]166        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
[3226]167          try {
[3500]168            if (error != null) throw error;
169            IAlgorithm algorithm = content as IAlgorithm;
[3226]170            if (algorithm == null)
171              MessageBox.Show(this, "The selected file does not contain an algorithm.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
172            else
173              Content.Algorithm = algorithm;
[3500]174          }
175          catch (Exception ex) {
[3758]176            ErrorHandling.ShowErrorDialog(this, ex);
[3500]177          }
178          finally {
179            Invoke(new Action(delegate() {
180              algorithmViewHost.Enabled = true;
181              newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = true;
182            }));
183          }
184        });
[3226]185      }
186    }
187    private void startButton_Click(object sender, EventArgs e) {
188      Content.Start();
189    }
[3265]190    private void pauseButton_Click(object sender, EventArgs e) {
191      Content.Pause();
192    }
[3226]193    private void stopButton_Click(object sender, EventArgs e) {
194      Content.Stop();
195    }
196    private void resetButton_Click(object sender, EventArgs e) {
[3716]197      Content.Prepare(false);
[3226]198    }
[3299]199    private void algorithmPanel_DragEnterOver(object sender, DragEventArgs e) {
200      e.Effect = DragDropEffects.None;
[3367]201      if (ReadOnly)
202        return;
[3299]203      Type type = e.Data.GetData("Type") as Type;
204      if ((type != null) && (typeof(IAlgorithm).IsAssignableFrom(type))) {
[3694]205        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[3299]206        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
207        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
208        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
[3526]209        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
[3299]210      }
211    }
212    private void algorithmPanel_DragDrop(object sender, DragEventArgs e) {
213      if (e.Effect != DragDropEffects.None) {
214        IAlgorithm algorithm = e.Data.GetData("Value") as IAlgorithm;
215        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) algorithm = (IAlgorithm)algorithm.Clone();
216        Content.Algorithm = algorithm;
217      }
218    }
[3226]219    #endregion
220
221    #region Helpers
[3454]222    private void SetEnabledStateOfExecutableButtons() {
223      if (Content == null) {
224        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
225      } else {
226        startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
227        pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
228        stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
229        resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
230      }
[3226]231    }
232    #endregion
233  }
234}
Note: See TracBrowser for help on using the repository browser.