Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/ExperimentView.cs @ 3832

Last change on this file since 3832 was 3758, checked in by swagner, 14 years ago

Implemented ErrorDialog and OperatorExecutionException (#1007)

File size: 5.6 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("Experiment View")]
35  [Content(typeof(Experiment), true)]
36  public sealed partial class ExperimentView : NamedItemView {
37    public new Experiment Content {
38      get { return (Experiment)base.Content; }
39      set { base.Content = value; }
40    }
41
42    /// <summary>
43    /// Initializes a new instance of <see cref="ItemBaseView"/>.
44    /// </summary>
45    public ExperimentView() {
46      InitializeComponent();
47    }
48
49    protected override void DeregisterContentEvents() {
50      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
51      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
52      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
53      base.DeregisterContentEvents();
54    }
55    protected override void RegisterContentEvents() {
56      base.RegisterContentEvents();
57      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
58      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
59      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
60    }
61
62    protected override void OnContentChanged() {
63      base.OnContentChanged();
64      if (Content == null) {
65        optimizerListView.Content = null;
66        runsViewHost.Content = null;
67        executionTimeTextBox.Text = "-";
68      } else {
69        optimizerListView.Content = Content.Optimizers;
70        runsViewHost.Content = Content.Runs;
71        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
72      }
73      SetEnableStateOfControls();
74    }
75
76    protected override void OnReadOnlyChanged() {
77      base.OnReadOnlyChanged();
78      SetEnableStateOfControls();
79    }
80    private void SetEnableStateOfControls() {
81      optimizerListView.Enabled = Content != null;
82      runsViewHost.Enabled = Content != null;
83      executionTimeTextBox.Enabled = Content != null;
84      SetEnabledStateOfExecutableButtons();
85    }
86
87    protected override void OnClosed(FormClosedEventArgs e) {
88      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
89      base.OnClosed(e);
90    }
91
92    #region Content Events
93    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
94      if (InvokeRequired)
95        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
96      else {
97        nameTextBox.Enabled = Content.ExecutionState != ExecutionState.Started;
98        descriptionTextBox.Enabled = Content.ExecutionState != ExecutionState.Started;
99        Locked = Content.ExecutionState == ExecutionState.Started;
100        SetEnabledStateOfExecutableButtons();
101      }
102    }
103    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
104      if (InvokeRequired)
105        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
106      else
107        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
108    }
109    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
110      if (InvokeRequired)
111        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
112      else
113        ErrorHandling.ShowErrorDialog(this, e.Value);
114    }
115    #endregion
116
117    #region Control events
118    private void startButton_Click(object sender, EventArgs e) {
119      Content.Start();
120    }
121    private void pauseButton_Click(object sender, EventArgs e) {
122      Content.Pause();
123    }
124    private void stopButton_Click(object sender, EventArgs e) {
125      Content.Stop();
126    }
127    private void resetButton_Click(object sender, EventArgs e) {
128      Content.Prepare(false);
129    }
130    #endregion
131
132    #region Helpers
133    private void SetEnabledStateOfExecutableButtons() {
134      if (Content == null) {
135        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
136      } else {
137        startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
138        pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
139        stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
140        resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
141      }
142    }
143    #endregion
144  }
145}
Note: See TracBrowser for help on using the repository browser.